-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathhttpbar.py
36 lines (23 loc) · 1003 Bytes
/
httpbar.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#!/usr/bin/env python3
from __future__ import print_function
import requests
from vbar import VirtualPowerSocket, VirtualPowerBar
TIMEOUT = 5. # Quite high; but this is a lot more stable.
class HTTPPowerBar(VirtualPowerBar):
def __init__(self, host='http://powerbar.ti', name='HTTPPowerBar',
timeout=TIMEOUT):
VirtualPowerBar.__init__(self, name)
self.host = host
self.timeout = timeout
def make_socket(self, name, ident):
return HTTPPowerSocket(self, name, ident)
class HTTPPowerSocket(VirtualPowerSocket):
def __init__(self, bar, name, ident):
VirtualPowerSocket.__init__(self, bar, name, ident)
def set_state(self, state):
s = 'On' if state else 'Off'
r = requests.post('%s/%s/%s' % (self.bar.host, self.bar.name,
self.ident),
data={'state': s}, timeout=self.bar.timeout)
if r.status_code == 200:
self.state = state