-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbar.py
74 lines (52 loc) · 1.66 KB
/
bar.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#!/usr/bin/env python3
from __future__ import print_function
import time
from vbar import VirtualPowerBar, VirtualPowerSocket
VIRTUAL = False
if not VIRTUAL:
try:
from serial import Serial
except ImportError as e:
print('Could not import serial. Trying to continue', e)
else:
print('WARNING: Virtual mode activated')
class Serial(object):
def __init__(self, **kwargs):
pass
def __getattr__(self, attr):
return lambda self, **kwargs: None
def write_read(s, m):
s.write(m)
s.timeout = 2
l = len(m) + len('RPC-28A>\r')
t1 = time.time()
f = s.read(l)
return f
class BayTechPowerBar(VirtualPowerBar):
def __init__(self, path, name='Default PowerBar name'):
VirtualPowerBar.__init__(self, name)
s = Serial(port=path, baudrate=9600)
# Just in case
s.setDsrDtr(False)
s.setRtsCts(False)
s.setXonXoff(False)
self.s = s
def make_socket(self, name, ident):
return BayTechPowerSocket(self, name, ident)
def reset_bar(self, timeout=2):
self.s.timeout = timeout
self.s.write('\r\n')
m = self.s.read(1000)
print('Reset read:', m)
return True
class BayTechPowerSocket(VirtualPowerSocket):
def __init__(self, bar, name, ident):
VirtualPowerSocket.__init__(self, bar, name, ident)
def set_state(self, state):
s = 'On' if state else 'Off'
self.state = state
#print('Setting', self.name, 'to', s)
m = '%s %d\r\n' % (s, self.ident)
#print('Writing:', repr(m))
r = write_read(self.bar.s, m)
#print('Read:', repr(r))