-
Notifications
You must be signed in to change notification settings - Fork 0
/
siadmin.py
executable file
·333 lines (291 loc) · 11.2 KB
/
siadmin.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
#!.venv/bin/python
#
################################################
# Package for sportident admin operations
#
# Author: Martin Horak
# Version: 1.1
# Date: 12. 1. 2019
#
################################################
import sportident as si
import sys, time, datetime, logging
class SiAdmin(si.Si):
'''SI Administration tasks'''
def __init__(self, tty):
super().__init__(tty)
def setremote(self):
'''Set communication to remote (controlled station).'''
self.handshake(si.C_SETMSMODE, (si.MODE_REMOTE,))
def setlocal(self):
'''Set communication to local (master station itself).'''
self.handshake(si.C_SETMSMODE, (si.MODE_LOCAL,))
def off(self):
'''Turn station off.'''
self.handshake(si.C_OFF)
def getime(self):
'''Read station time.'''
self.handshake(si.C_GETTIME)
td = self.rdata[7]
is_pm = td & 0x01
secs = (self.rdata[8] << 8) + self.rdata[9] + is_pm * 43200;
subsec = int(self.rdata[10] * 1000000/256)
if self.rdata[5] == 0: self.rdata[5] = 1 # Repair not set date
if self.rdata[6] == 0: self.rdata[6] = 1 # Repair not set date
return datetime.datetime(2000+self.rdata[4], self.rdata[5], self.rdata[6], secs // 3600, (secs % 3600) // 60, secs % 60, subsec)
def getmodecn(self):
'''Read station mode and number.'''
self.handshake(si.C_GETDATA, (si.O_MODE, 2))
return self.rdata[5:7]
def getfwversion(self):
'''Read firmware version (string).'''
self.handshake(si.C_GETDATA, (si.O_FWVER, 3))
return bytes(self.rdata[5:8])
def getbatstate(self):
'''Read battery status. Returns: Remaining percent.'''
self.handshake(si.C_GETDATA, (si.O_BATCONS, 4))
consumed = (self.rdata[5] << 24) + (self.rdata[6] << 16) + (self.rdata[7] << 8) + self.rdata[8]
logging.debug("Battery consumed: {:.2f} mAh.".format(consumed/3600))
self.handshake(si.C_GETDATA, (si.O_BATCAP, 4))
capacity = (self.rdata[5] << 24) + (self.rdata[6] << 16) + (self.rdata[7] << 8) + self.rdata[8]
logging.debug("Battery capacity: {:.2f} mAh.".format(capacity/3600))
return round(100 * (1 - consumed/capacity))
def getbatvoltage(self):
'''Read battery voltage.'''
self.handshake(si.C_GETDATA, (si.O_BATVOLT, 2))
voltage = (self.rdata[5] << 8) + self.rdata[6]
if voltage > 13100: voltage /= 131
return voltage / 100
def getbatemp(self):
'''Read battery temperature.'''
self.handshake(si.C_GETDATA, (si.O_BATTEMP, 2))
temper = (self.rdata[5] << 8) + self.rdata[6]
if temper >= 25800:
return (temper - 25800) / 92
else:
return temper / 10
def getbatdate(self):
'''Read battery change date.'''
self.handshake(si.C_GETDATA, (si.O_BATDATE, 3))
if self.rdata[5] < 80: year = 2000 + self.rdata[5]
else: year = 1900 + self.rdata[5]
return datetime.date(year, self.rdata[6], self.rdata[7])
def getbatall(self):
'''Read all battery data at once.
Returns: date, percent, voltage, temperature.'''
self.handshake(si.C_GETDATA, (si.O_BATALL, 63))
# Date
if self.rdata[5] < 80: year = 2000 + self.rdata[5]
else: year = 1900 + self.rdata[5]
changedate = datetime.date(year, self.rdata[6], self.rdata[7])
# Capacity
capacity = (self.rdata[3+5] << 24) + (self.rdata[3+6] << 16) + (self.rdata[3+7] << 8) + self.rdata[3+8]
logging.debug("Battery capacity: {:.2f} mAh.".format(capacity/3600))
# Consumed
consumed = (self.rdata[31+5] << 24) + (self.rdata[31+6] << 16) + (self.rdata[31+7] << 8) + self.rdata[31+8]
logging.debug("Battery consumed: {:.2f} mAh.".format(consumed/3600))
# Voltage
voltage = (self.rdata[59+5] << 8) + self.rdata[59+6]
if voltage > 13100: voltage /= 131
voltage /= 100
temper = (self.rdata[61+5] << 8) + self.rdata[61+6]
if temper >= 25800: temper = (temper - 25800) / 92
else: temper = temper / 10
return changedate, round(100 * (1 - consumed/capacity)), voltage, temper
def beep(self, times=1):
'''Beep several times.'''
self.handshake(si.C_BEEP, (times,))
return times
def getprot(self):
'''Read communication protocol info.'''
# Already read during init and saved in class attributes
return {'cpc': self.cpc,
'extprot': self.extprot,
'autosend': self.autosend,
'handshk': self.handshk,
'password': self.password,
'punchread': self.punchread}
def setprot(self, prot={}):
'''Set communication protocol parameters.'''
cpc = self.cpc
if 'extprot' in prot:
cpc = si.set_bit(cpc, 0, prot['extprot'])
if 'autosend' in prot:
cpc = si.set_bit(cpc, 1, prot['autosend'])
if 'handshk' in prot:
cpc = si.set_bit(cpc, 2, prot['handshk'])
if 'password' in prot:
cpc = si.set_bit(cpc, 4, prot['password'])
if 'punchread' in prot:
cpc = si.set_bit(cpc, 7, prot['punchread'])
self.handshake(si.C_SETDATA, (si.O_PROT, cpc))
def setcnmode(self, cn, mode='Control'):
'''Set control number and mode.'''
cn = int(cn)
if not 1 <= cn <= 255:
raise si.SiException('Control number not in range 1-255.')
if mode not in si.MODES:
raise si.SiException('Unknown station mode.')
self.handshake(si.C_SETDATA, (si.O_MODE, si.MODES.index(mode), cn))
def setbatdate(self, date):
'''Set battery change date.'''
self.handshake(si.C_SETDATA, (si.O_BATDATE, date.year % 100, date.month, date.day))
## End of class SiAdmin ## -----------------
## Constants ## ----------------------------
############### ----------------------------
LOCAL = 0
REMOTE = 1
## Functions ## ----------------------------
############### ----------------------------
## Usage ## --------------------------------
def Usage():
'Usage help'
usage = """
Usage:
{script_name} [-h] [-tvql] -s <tty> command [params]
Setup SI station
Parameters:
-h ... help - this help
-v ... more verbose
-q ... more quiet = less verbose
-l ... setup local (master) SI station
-r ... setup remote SI station [default]
-f <file> ... log messages to <file>
-s <tty> ... serial port to use [first autodetected]
Commands:
off ... turn off
beep [n] ... make beep [n times]
rbat ... read battery status
rcn ... read control mode and number
rfw ... read firmware version
rprot ... read protocol info
rtime ... read time
wbatdate <dd.mm.yyyy> ... write battery change date
wcn <number>,[mode] ... write control number and mode [Control]
modes = {{Control, Start, Finish, Readout, Clear, Check}}
wprot <ep>,<as> ... write protocol info
ep = extended protocol {{0,1}}
as = autosend {{0,1}}
wtime ... write computer localtime to the station
EOF
"""
print(usage.format(script_name = sys.argv[0]))
return
## Usage end ## ----------------------------
## Main ## ---------------------------
######################################
def main():
'''CLI utility for SI stations setup'''
## Variables ## ============================
loglevel = logging.WARNING # 30
target = REMOTE
logfile = None
port = None
## Getparam ## -----------------------------
argn = []
args = sys.argv
i = 1
try:
while(i < len(args)):
if(args[i][0] == '-'):
for j in args[i][1:]:
if j == 'h':
Usage()
return
elif j == 'v':
if loglevel > 10: loglevel -= 10
elif j == 'q':
if loglevel < 50: loglevel += 10
elif j == 'l':
target = LOCAL
elif j == 'r':
target = REMOTE
elif j == 'f':
i += 1
logfile = args[i]
elif j == 's':
i += 1
port = args[i]
else:
argn.append(args[i])
i += 1
except IndexError:
print("Parameter read error.")
Usage()
return
## Getparam end ## -------------------------
logcfg = {'format': '%(levelname)s: %(message)s', 'level': loglevel}
if logfile:
logcfg['filename'] = logfile
logging.basicConfig(**logcfg)
if not port:
ports = si.station_detect()
if len(ports) == 0:
logging.error("No master station detected.")
return 1
else:
port = ports[0]
logging.debug("Detected master station at: {}".format(port))
# Only first detected SI station is used
siadm = SiAdmin(port)
if target == REMOTE:
siadm.setremote()
# Local is set during initialization
while len(argn) > 0:
cmd = argn.pop(0)
if cmd == 'off':
siadm.off()
elif cmd == 'beep':
if len(argn) > 0 and argn[0].isdigit():
times = int(argn.pop(0))
else:
times = 1
siadm.beep(times)
elif cmd == 'rtime':
t = siadm.getime()
print('Station datetime: ', t.strftime('%d.%m.%Y %H:%M:%S'))
elif cmd == 'wtime':
siadm.setime()
elif cmd == 'rprot':
print("""Station protocol CPC: 0x{:02x}
Extended protocol: {}
Autosend: {}
Handshake: {}
Password: {}
Read after punch: {}""".format(siadm.cpc, siadm.extprot, siadm.autosend, siadm.handshk, siadm.password, siadm.punchread))
elif cmd == 'rcn':
mode, cn = siadm.getmodecn()
try:
modestr = si.MODES[mode]
except IndexError:
modestr = 'Undef'
if modestr == 'Undef':
modestr += " ({})".format(mode)
print("Station number: {}".format(cn))
print("Station mode: {}".format(modestr))
elif cmd == 'rbat':
bdate, bperc, bvolt, btemp = siadm.getbatall()
print("""Battery state:
Charge: {} %
Voltage: {:2.1f} V
Temperature: {:2.1f} °C
Change date: {}""".format(bperc, bvolt, btemp, bdate.strftime('%d.%m.%Y')))
elif cmd == 'wprot':
ep,au = argn.pop(0).split(',', 1)
siadm.setprot({'extprot': bool(int(ep)), 'autosend': bool(int(au))})
elif cmd == 'wcn':
numode = argn.pop(0).split(',', 1)
siadm.setcnmode(*numode)
elif cmd == 'rfw':
fw = siadm.getfwversion()
print("Firmware version: {}".format(fw.decode()))
elif cmd == 'wbatdate':
y,m,d = argn(pop(0).split(',', 1))
bd = datetime.date(int(y), int(m), int (d))
siadm.setbatdate(bd)
if len(argn) > 0: time.sleep(0.5)
###
## Main run ## -----------------------
######################################
if __name__ == '__main__':
main()