-
Notifications
You must be signed in to change notification settings - Fork 0
/
updatetime_aux.py
280 lines (227 loc) · 8.79 KB
/
updatetime_aux.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
#!/usr/bin/env python3
"""This script updates the repeater's time and date"""
import sys
from typing import Any, Union, Tuple, Callable, TypeVar, Generic, Sequence, Mapping, List, Dict, Set, Deque, Iterable
#from typing import Any, Union, Tuple, Callable, TypeVar, Generic, Sequence, Mapping, List, Dict, Set, Deque, Iterable
import argparse
import time
import logging
import logging.handlers
import userinput
import knowncontrollers
import controllerspecific
HOURLIM: int = 23
MINLIM: int = 50
O_DOW: List[str] = ['monday', 'tuesday', 'wednesday', 'thursday',
'friday', 'saturday', 'sunday', 'foolsday']
R_DOW: List[int] = [7, 6, 0, 1, 2, 3, 4, 5, ]
M_DOW: List[str] = ['2', '3', '4', '5', '6', '7', '1', ]
TWO_CHAR: str = '{num:02d}'
SET_ATTEMPT_MAX: int = 15
INST = controllerspecific.INST
PAT = controllerspecific.PAT
REPL_FMT = controllerspecific.REPL_FMT
_PARSER = argparse.ArgumentParser(description="Sets a controller's date and time if \
needed: a blank argument assumes dlx2 and one available com port",
epilog="Useful for running in a cron job"
)
_PARSER.add_argument('Controller', default=None,
help='Controller type, one of [dlx2, rlc1+] required unless no arguments'
)
_PARSER.add_argument('Port', nargs='?', default=None,
help='Port id if required, if only one open port, that one will be used'
)
_PARSER.add_argument('-dbg', '--cldebug',
help='turns off setting the new time',
action="store_true")
_PARSER.add_argument('-v', '--verbose',
help='display detailed messages',
action="store_true")
_PARSER.add_argument('-li', '--loginfo',
help='enable INFO logging',
action="store_true")
_PARSER.add_argument('-ld', '--logdebug',
help='enable DEBUG logging',
action="store_true")
def _no_op(ignore):
# pylint: disable=W0613
pass
CLOSER: Dict[bool, Any] = {
False: lambda a: a.close(), True: lambda a: _no_op(a)}
#cmdl_debug = False
#verbose = False
LOGGER: str = logging.getLogger(__name__)
LOG_DIR: str = '../logs'
LOG_FILE: str = '/updatetime'
def _delay(debug: bool = False, testtime=None) -> Any:
"""_delay()
if the time is >23:50, waits until 2 seconds after the date changes before continuing
after the delay, returns a tuple with the then current time and a debug message. if
debugging is false the debugging message is None
"""
while True:
msg = None
os_time = time.localtime(time.time())
if testtime:
os_time = testtime
if os_time.tm_hour < HOURLIM or os_time.tm_min < MINLIM:
break
delymin = 60 - os_time.tm_min # - MINLIM
LOGGER.debug("debug wait for %s min and 2 seconds", str(delymin))
if debug:
msg = 'debug wait for {} min and 2 seconds' .format(delymin)
break
else:
time.sleep(delymin * 60 + 2)
return (time.localtime(time.time()), msg)
def check_date(_res: Dict[str, Any], _sdtpl, _os_time):
"""check_date(_res,_sdtpl,_os_time)
_res is the dict set by gdtpl[2] that has keys A, m, d, Y
with the same meaning as https://docs.python.org/3.0/library/time.html
_sdtpl is the set date tuple
_os_time is the current computer time
return None if the repeater date and the computer date match
otherwise returns the command to set the repeater date.
"""
# pylint: disable=C0103
cmd = None
dow = _res['A'].lower()
_m = _res['m']
_d = _res['d']
_Y = _res['Y']
_oY: str = str(_os_time.tm_year)
# f'{_os_time.tm_mon:02d}'
_om: str = TWO_CHAR.format(num=_os_time.tm_mon)
# f'{_os_time.tm_mday:02d}'
_od: str = TWO_CHAR.format(num=_os_time.tm_mday)
_owd = _os_time.tm_wday
textdow = O_DOW[_owd]
#aa = M_DOW[_owd]
if not (_Y == _oY and _d == _od and _m == _om and textdow == dow):
arg = (_sdtpl[0], _om, _od, _oY[2:4], M_DOW[_owd], )
cmd = _sdtpl[3](arg)
LOGGER.debug("returned %s", cmd)
return cmd
def check_time(_res: Dict[str, Any], _sttpl, _os_time) -> str:
"""check_time(_res,_sttpl,_os_time)
_res is the dict set by gttpl[2] that has keys I, M, p
with the same meaning as https://docs.python.org/3.0/library/time.html
_sttpl is the set time tuple
_os_time is a time.struct_time with the current computer time
return None if the repeater time and the computer time match
otherwise returns the command to set the repeater time.
"""
# pylint: disable=C0103
cmd = None
_I = _res['I']
_M = _res['M']
_p = _res['p']
_pm = '0'
if _p[0:1] == 'P':
_pm = '1'
_seconds = int(_M) * 60
if _pm == '0':
if int(_I) != 12:
_seconds += int(_I) * 3600
else:
j = int(_I)
if j != 12:
j += 12
_seconds += j * 3600
_H = time.strftime("%H", time.gmtime(_seconds))
_sysI = time.strftime("%I", _os_time)
# f'{_os_time.tm_hour:02d}'
_sysH = TWO_CHAR.format(num=_os_time.tm_hour)
_sysM = TWO_CHAR.format(num=_os_time.tm_min) # f'{_os_time.tm_min:02d}'
_sysPM = '0'
if int(_sysH) > 11:
_sysPM = '1'
if _H == _sysH and _M == _sysM and _pm == _sysPM:
LOGGER.debug("returned %s", cmd)
return cmd
# now need to adjust
arg = (_sttpl[0], _sysI, _sysM, _sysPM)
cmd = _sttpl[3](arg)
LOGGER.debug("returned %s", cmd)
return cmd
def proc1(tempargs, _available_ports):
"""tbd"""
if not ('-h' in tempargs or '--help' in tempargs):
if _available_ports.isEmpty():
msg = 'no available communication port: aborting'
raise SystemExit(msg)
if '-h' in tempargs or '--help' in tempargs:
_PARSER.print_help()
raise SystemExit()
def proc2(args):
"""tbd"""
if args.logdebug:
LOGGER.setLevel(logging.DEBUG)
elif args.loginfo:
LOGGER.setLevel(logging.INFO)
possible_port = args.Port
if args.Controller:
possible_ctrl = args.Controller
else:
possible_ctrl = 'dlxii'
return (possible_port, possible_ctrl, )
def process_cmdline(_available_ports: List[str], _testcmdline: List[str] = None) -> Tuple[Any, Any, Any]:
"""process_cmdline(_available_ports, _testcmdline="")
_available_ports is a list? of port names
_testcmdline can be list of strings that can be used to fake a command line.
If None, the default
command line processing is performed, otherwise,
the list of strings in the argument is used.
processes the command line arguments
returns a tuple of (ui, verbose, cmdl_debug) if arguments are ok, Otherwise raises an exception
"""
_tcl = []
if _testcmdline:
_tcl = _testcmdline
tempargs = sys.argv[1:] + _tcl
proc1(tempargs, _available_ports)
# if not ('-h' in tempargs or '--help' in tempargs):
# if _available_ports.isEmpty():
#msg = 'no available communication port: aborting'
#raise SystemExit(msg)
# if '-h' in tempargs or '--help' in tempargs:
# _PARSER.print_help()
#raise SystemExit()
if _testcmdline:
args = _PARSER.parse_args(_testcmdline)
else:
args = _PARSER.parse_args()
_aa = proc2(args, )
possible_port = _aa[0]
possible_ctrl = _aa[1]
# if args.logdebug:
# LOGGER.setLevel(logging.DEBUG)
# elif args.loginfo:
# LOGGER.setLevel(logging.INFO)
#possible_port = args.Port
# if args.Controller:
#possible_ctrl = args.Controller
# else:
#possible_ctrl = 'dlxii'
if len(_available_ports) != 1:
if not args.Port.upper() in _available_ports:
msg = 'Port {} not available: available ports are: {}, aborting' \
.format(args.Port, _available_ports)
raise Exception(msg)
else:
possible_port = _available_ports[0]
ctrl = knowncontrollers.select_controller(possible_ctrl)
if not ctrl:
msg = 'Controller {} not available: available controlers are: {}, aborting' \
.format(args.Controller, knowncontrollers.get_controller_ids())
raise Exception(msg)
verbose = args.verbose
cmdl_debug = args.cldebug
_ui = userinput.UserInput(ctrl[1])
_ui.comm_port = possible_port
if verbose:
msg = '[verbose] ctrl:{}, port:{}, dbg:{}, verbose: True'\
.format(ctrl, _ui.comm_port, cmdl_debug)
return (_ui, verbose, cmdl_debug)
if __name__ == '__main__':
pass