-
Notifications
You must be signed in to change notification settings - Fork 0
/
pump_state_set_time.py
445 lines (386 loc) · 12.5 KB
/
pump_state_set_time.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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
import transitions
from transitions import *
from transitions.extensions import MachineFactory
#import picologging as logging
import logging
import calendar
import pexpect
from pump_screen import *
from pump_variables import PV
#from pump_util import ThreadSafeSingleton
from pump_btn import PumpButtons, buttons
import pump_util as util
logger = logging.getLogger(util.MAIN_LOGGER_NAME)
#class SetTimeStateMachine(metaclass=ThreadSafeSingleton):
class SetTimeStateMachine():
states = [
{ 'name':'ENTER_TIME_SETTING', 'on_enter':['enter_time_setting'] }
, { 'name':'YEAR10', 'on_enter':['year10'] }
, { 'name':'YEAR01', 'on_enter':['year01'] }
, { 'name':'MONTH10', 'on_enter':['month10'] }
, { 'name':'MONTH01', 'on_enter':['month01'] }
, { 'name':'DAY10', 'on_enter':['day10'] }
, { 'name':'DAY01', 'on_enter':['day01'] }
, { 'name':'HOUR10', 'on_enter':['hour10'] }
, { 'name':'HOUR01', 'on_enter':['hour01'] }
, { 'name':'MIN10', 'on_enter':['min10'] }
, { 'name':'MIN01', 'on_enter':['min01'] }
, { 'name':'SAVE_OR_NOT', 'on_enter':['save_or_not'] }
, { 'name':'SAVE_TIME', 'on_enter':['save_time'] }
, { 'name':'DONT_SAVE_TIME', 'on_enter':['dont_save_time'] }
, { 'name':'LEAVE_TIME_SETTING', 'on_enter':['leave_time_setting'] }
]
transitions = [
{ 'source':'*', 'trigger':'enter', 'dest':'ENTER_TIME_SETTING'}
, { 'source':'ENTER_TIME_SETTING', 'trigger':'init', 'dest':'YEAR10'}
, { 'source':'YEAR10', 'trigger':'btn1_s', 'dest':'YEAR01'}
#, { 'source':'YEAR01', 'trigger':'btn1_s', 'dest':'MONTH10'}
, { 'source':'YEAR01', 'trigger':'btn1_s', 'dest':'MONTH01', 'before':'check_leap'}
, { 'source':'MONTH10', 'trigger':'btn1_s', 'dest':'MONTH01'}
, { 'source':'MONTH01', 'trigger':'btn1_s', 'dest':'DAY10', 'before':'check_date'}
, { 'source':'DAY10', 'trigger':'btn1_s', 'dest':'DAY01'}
, { 'source':'DAY01', 'trigger':'btn1_s', 'dest':'HOUR10'}
, { 'source':'HOUR10', 'trigger':'btn1_s', 'dest':'HOUR01'}
, { 'source':'HOUR01', 'trigger':'btn1_s', 'dest':'MIN10'}
, { 'source':'MIN10', 'trigger':'btn1_s', 'dest':'MIN01'}
, { 'source':'MIN01', 'trigger':'btn1_s', 'dest':'YEAR10'}
, { 'source':'YEAR10', 'trigger':'btn2_s', 'dest':'YEAR10', 'before':'y10_inc'}
, { 'source':'YEAR10', 'trigger':'btn3_s', 'dest':'YEAR10', 'before':'y10_dec'}
, { 'source':'YEAR01', 'trigger':'btn2_s', 'dest':'YEAR01', 'before':'y01_inc'}
, { 'source':'YEAR01', 'trigger':'btn3_s', 'dest':'YEAR01', 'before':'y01_dec'}
, { 'source':'MONTH10', 'trigger':'btn2_s', 'dest':'MONTH10', 'before':'m10_inc'}
, { 'source':'MONTH10', 'trigger':'btn3_s', 'dest':'MONTH10', 'before':'m10_dec'}
, { 'source':'MONTH01', 'trigger':'btn2_s', 'dest':'MONTH01', 'before':'m01_inc'}
, { 'source':'MONTH01', 'trigger':'btn3_s', 'dest':'MONTH01', 'before':'m01_dec'}
, { 'source':'DAY10', 'trigger':'btn2_s', 'dest':'DAY10', 'before':'d10_inc'}
, { 'source':'DAY10', 'trigger':'btn3_s', 'dest':'DAY10', 'before':'d10_dec'}
, { 'source':'DAY01', 'trigger':'btn2_s', 'dest':'DAY01', 'before':'d01_inc'}
, { 'source':'DAY01', 'trigger':'btn3_s', 'dest':'DAY01', 'before':'d01_dec'}
, { 'source':'HOUR10', 'trigger':'btn2_s', 'dest':'HOUR10', 'before':'h10_inc'}
, { 'source':'HOUR10', 'trigger':'btn3_s', 'dest':'HOUR10', 'before':'h10_dec'}
, { 'source':'HOUR01', 'trigger':'btn2_s', 'dest':'HOUR01', 'before':'h01_inc'}
, { 'source':'HOUR01', 'trigger':'btn3_s', 'dest':'HOUR01', 'before':'h01_dec'}
, { 'source':'MIN10', 'trigger':'btn2_s', 'dest':'MIN10', 'before':'min10_inc'}
, { 'source':'MIN10', 'trigger':'btn3_s', 'dest':'MIN10', 'before':'min10_dec'}
, { 'source':'MIN01', 'trigger':'btn2_s', 'dest':'MIN01', 'before':'min01_inc'}
, { 'source':'MIN01', 'trigger':'btn3_s', 'dest':'MIN01', 'before':'min01_dec'}
, { 'source':'*', 'trigger':'btn1_l', 'dest':'SAVE_OR_NOT'}
, { 'source':'SAVE_OR_NOT', 'trigger':'btn2_s', 'dest':'SAVE_TIME'}
, { 'source':'SAVE_OR_NOT', 'trigger':'btn3_s', 'dest':'DONT_SAVE_TIME'}
, { 'source':'*', 'trigger':'leave', 'dest':'LEAVE_TIME_SETTING'}
]
def __init__(self, *args, **kwargs):
self.name = kwargs.get('name')
self.pv = kwargs.get('pv')
self.sm_locked_cls = MachineFactory.get_predefined(locked=True)
self.machine = self.sm_locked_cls(model=self,
#self.machine = Machine(model=self,
states=SetTimeStateMachine.states,
transitions=SetTimeStateMachine.transitions,
initial='ENTER_TIME_SETTING',
ignore_invalid_triggers=True)
self.y_old = 0
self.m_old = 0
self.d_old = 0
self.h_old = 0
self.min_old = 0
self.y = 0
self.m = 0
self.d = 0
self.h = 0
self.min = 0
def enter_time_setting(self):
logger.debug("enter_time_setting:{}".format(self))
#buttons().set_statemachine(self)
now = datetime.datetime.now()
#ct = f"{now.year}/{now.month}/{now.day} {now.hour}:{now.minute:02d}"
self.y_old = now.year
self.m_old = now.month
self.d_old = now.day
self.h_old = now.hour
self.min_old = now.minute
self.y = now.year
self.m = now.month
self.d = now.day
self.h = now.hour
self.min = now.minute
scr_enter_time_setting(self.pv)
self.init()
def save_or_not(self):
if self.y_old!=self.y or self.m_old!=self.m or self.d_old!=self.d or self.h_old!=self.h or self.min_old!=self.min:
scr_setting_save_or_not(self.pv)
else:
self.leave()
def save_time(self):
ts = f"{self.m}/{self.d}/{self.y} {self.h}:{self.min}"
logger.debug(f"callback: save_time:{ts}, {self}")
c = pexpect.spawn(f"sudo /usr/bin/date --set='{ts}'")
try: # 현재 user id에 따라서 sudo password prompt가 안나올 수도 있음
c.expect('password')
c.sendline(self.pv.password)
logger.debug(f"save_time: sudo prompt: {c.read()}")
except:
logger.debug(f"save_time: No sudo prompt!")
pass
self.leave()
def dont_save_time(self):
self.leave()
def leave_time_setting(self):
#buttons().restore_statemachine()
sm=buttons().next_state()
sm.enter()
def check_leap(self):
if self.m==2 and self.d>28:
self.d=29
if calendar.isleap(self.y)==False:
self.d=28
scr_settime_d01(self.pv)
def check_date(self):
if self.m==2:
self.check_leap()
elif self.d==31 and self.m in [4,6,9,11]:
self.d=30
scr_settime_d01(self.pv)
def year10(self):
scr_settime_y10(self.pv)
def year01(self):
scr_settime_y01(self.pv)
def month10(self):
scr_settime_m10(self.pv)
def month01(self):
scr_settime_m01(self.pv)
def day10(self):
scr_settime_d10(self.pv)
def day01(self):
scr_settime_d01(self.pv)
def hour10(self):
scr_settime_h10(self.pv)
def hour01(self):
scr_settime_h01(self.pv)
def min10(self):
scr_settime_min10(self.pv)
def min01(self):
scr_settime_min01(self.pv)
def y10_inc(self):
logger.debug("before inc: year10->year10")
temp = (self.y//10)%100
temp += 1
if temp==10:
temp=2
self.y = ((((self.y//100)*10)+temp)*10)+(self.y%10)
def y10_dec(self):
logger.debug("before dec: year10->year10")
temp = (self.y//10)%100
temp -= 1
if temp==1:
temp=9
self.y = ((((self.y//100)*10)+temp)*10)+(self.y%10)
def y01_inc(self):
logger.debug("before inc: year01->year01")
temp = self.y%10
temp += 1
if self.y<2030:
if temp==10:
temp=2
else:
if temp==10:
temp=0
self.y = ((self.y//10)*10)+temp
def y01_dec(self):
logger.debug("before dec: year01->year01")
temp = self.y%10
temp -= 1
if self.y<2030:
if temp==1:
temp=9
else:
if temp==-1:
temp=9
self.y = ((self.y//10)*10)+temp
def m10_inc(self):
logger.debug("before inc: month10->month10")
temp = self.m//10
temp += 1
if temp>1:
temp=0
self.m = temp*10+self.m%10
def m10_dec(self):
logger.debug("before dec: year10->year10")
temp = (self.y//10)%100
temp -= 1
if temp==1:
temp=9
self.y = ((((self.y//100)*10)+temp)*10)+(self.y%10)
def m01_inc(self):
logger.debug("before inc: month01->month01")
if self.m>11:
self.m = 1
else:
self.m += 1
def m01_dec(self):
logger.debug("before dec: month01->month01")
if self.m<2:
self.m = 12
else:
self.m -= 1
def d10_inc(self):
logger.debug("before inc: day10->day10")
if self.m==2:
if self.d==19:
if calendar.isleap(self.y)==False:
self.d += 9
else:
self.d += 10
elif self.d==20:
self.d = 1
elif self.d<19:
self.d += 10
else:
self.d -= 20
elif self.m in [4,6,9,11]:
if self.d>29:
self.d = 1
elif self.d>20:
self.d -= 20
else:
self.d += 10
else:
if self.d>30:
self.d = 1
elif self.d>21:
self.d -= 20
else:
self.d += 10
def d10_dec(self):
logger.debug("before dec: day10->day10")
if self.m==2:
if self.d==9:
if calendar.isleap(self.y)==False:
self.d += 19 # 9 -> 28
else:
self.d += 20 # 9 -> 29
elif self.d==10:
self.d = 1 # 10 -> 01
elif self.d<10:
self.d += 20 # 8 -> 28
else:
self.d -= 10 # 20 -> 10
elif self.m in [4,6,9,11]:
if self.d==10:
self.d = 1
elif self.d<10:
self.d += 20 # 9 -> 29
else:
self.d -= 10 # 25 -> 15
else:
if self.d==10:
self.d = 1
elif self.d==1:
self.d = 31
elif self.d<10:
self.d += 20 # 9 -> 29
else:
self.d -= 10 # 25 -> 15
def d01_inc(self):
logger.debug("before inc: day01->day01")
if self.m==2:
if self.d==28:
if calendar.isleap(self.y)==False:
self.d = 20
else:
self.d = 29
elif self.d==29:
self.d = 20
else:
self.d = ((self.d//10)*10)+((self.d%10)+1)%10
elif self.m in [4,6,9,11]:
if self.d==30:
self.d = 30
else:
self.d = ((self.d//10)*10)+((self.d%10)+1)%10
else:
if self.d==30:
self.d = 30
else:
self.d = ((self.d//10)*10)+((self.d%10)+1)%10
def d01_dec(self):
logger.debug("before dec: day01->day01")
if self.m==2:
if self.d==20:
if calendar.isleap(self.y)==False:
self.d = 28
else:
self.d = 29
elif (self.d%10)==0: # 10
self.d = self.d+9 # 10 -> 19
elif self.d==1:
self.d = 9
else:
self.d = ((self.d//10)*10)+(self.d%10)-1
elif self.m in [4,6,9,11]:
if self.d==1:
self.d = 9
elif self.d==30:
self.d = 29
elif (self.d%10)==0:
self.d = (self.d//10)*10 + 9
else:
self.d = ((self.d//10)*10)+(self.d%10)-1
else:
if self.d==1:
self.d = 9
elif self.d==31:
self.d = 30
elif self.d==30:
self.d = 29
elif (self.d%10)==0:
self.d = (self.d//10)*10 + 9
else:
self.d = ((self.d//10)*10)+(self.d%10)-1
def h10_inc(self):
logger.debug("before inc: hour10->hour10")
modulo = 3
temp = (((self.h//10)+1)%modulo)*10 + self.h%10
if temp>23:
temp = 23
self.h = temp
def h10_dec(self):
logger.debug("before dec: hour10->hour10")
if self.h//10 == 0:
self.h += 20 # 1 -> 21
else:
self.h = (((self.h//10)-1)%3)*10 + self.h%10
def h01_inc(self):
logger.debug("before inc: hour01->hour01")
temp = (self.h//10)*10 + ((self.h%10)+1)%10
if temp > 23:
temp = 23
self.h = temp
def h01_dec(self):
logger.debug("before dec: hour10->hour10")
if self.h%10 == 0:
temp = (self.h//10)*10 + 9
if temp>23:
temp=23
self.h = temp
else:
self.h = (self.h//10)*10 + (self.h%10)-1
def min10_inc(self):
logger.debug("before inc: min10->min10")
modulo = 6
self.min = (((self.min//10)+1)%modulo)*10 + self.min%10
def min10_dec(self):
logger.debug("before dec: min10->min10")
if self.min//10 == 0:
self.min += 50 # 01 -> 51
else:
self.min = (((self.min//10)-1)%6)*10 + self.min%10
def min01_inc(self):
logger.debug("before inc: min01->min01")
self.min = ((self.min//10)*10) + ((self.min%10)+1)%10
def min01_dec(self):
logger.debug("before dec: min01->min01")
if self.min%10 == 0:
self.min = (self.min//10)*10 + 9
else:
self.min = (self.min//10)*10 + (self.min%10)-1