-
Notifications
You must be signed in to change notification settings - Fork 4
/
nextion_rtc.py
211 lines (175 loc) · 7.05 KB
/
nextion_rtc.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
#!/usr/bin/env python3
# -*- coding: UTF-8 -*-
"""
NexRtc
Functions to interact with Nextion RTC
"""
# system packages
from time import sleep
# custom packages
from .common import Common
from .typing import List, Optional, Union
class NexRtcError(Exception):
"""Base class for exceptions in this module."""
pass
class NexRtc(Common):
"""docstring for NexRtc"""
def __init__(self, nh) -> None:
"""
Init RTC
:param nh: The Nextion hardware interface object
:type nh: NexHardware
"""
super().__init__(nh, pid=-1, cid=-1, name="rtc")
self._time_types = ["year", "mon", "day", "hour", "min", "sec", "week"]
@property
def time_types(self) -> List[str]:
"""
Get available time types
:returns: Requestable time types from RTC
:rtype: List[str]
"""
return self._time_types
def write_rtc_time(self, *args, **kwargs) -> bool:
"""
Write time to RTC
:param time: The time to set the RTC
:type time: Union[str, List[int]]
:param time_type: The type of time to change
:type time_type: str
:returns: True on success, False otherwise
:rtype: bool
"""
if (len(args) + len(kwargs) == 1):
# only a time is given
if len(args):
time = args[0]
else:
time = kwargs["time"]
if isinstance(time, str) and len(time) >= 19:
# "2016,11,25,12,34,50"
year = time[0:4]
month = time[5:7]
day = time[8:10]
hour = time[11:13]
minute = time[14:16]
second = time[17:19]
elif (isinstance(time, list) and
all(isinstance(x, int) for x in time)):
# [2016, 11, 25, 12, 34, 50]
year = time[0]
month = time[1]
day = time[2]
hour = time[3]
minute = time[4]
second = time[5]
else:
raise NexRtcError("Time can either be given as '{}' or '{}'".
format("2016,11,25,12,34,50",
[2016, 11, 25, 12, 34, 50]))
self._logger.debug("Timestamp (ISO8601): {}-{}-{}T{}:{}:{}".
format(year, month, day, hour, minute, second))
cmd = "rtc0={}".format(year)
self._nh.sendCommand(cmd)
self._nh.recvRetCommandFinished()
cmd = "rtc1={}".format(month)
self._nh.sendCommand(cmd)
self._nh.recvRetCommandFinished()
cmd = "rtc2={}".format(day)
self._nh.sendCommand(cmd)
self._nh.recvRetCommandFinished()
cmd = "rtc3={}".format(hour)
self._nh.sendCommand(cmd)
self._nh.recvRetCommandFinished()
cmd = "rtc4={}".format(minute)
self._nh.sendCommand(cmd)
self._nh.recvRetCommandFinished()
cmd = "rtc5={}".format(second)
self._nh.sendCommand(cmd)
return self._nh.recvRetCommandFinished()
elif (len(args) + len(kwargs) == 2):
# time_type is given as well
if len(kwargs) == 2:
time_type = kwargs['time_type']
time = kwargs['time']
else:
if len(args) == 2:
time_type = args[0]
time = args[1]
else:
raise NexRtcError("Either use keyword or positional args")
self._logger.debug("Set '{}' to '{}'".format(time_type, time))
rtc_index = self.time_types.index(time_type.lower())
cmd = "rtc{}={}".format(rtc_index, time)
self._nh.sendCommand(cmd)
return self._nh.recvRetCommandFinished()
else:
raise NexRtcError("Only two args are allowed")
def read_rtc_time(self,
return_type: str,
length: Optional[int] = 22) -> Union[str, List[int]]:
"""
Read RTC time
:param return_type: The return type, choose "int", "str" or from
["year", "mon", "day", "hour", "min", "sec",
"week"]
:type return_type: str
:param length: The length
:type length: Optional[int]
:returns: RTC time
:rtype: Union[str, List[int]]
"""
return_type = return_type.lower()
if return_type in ["str", "int"]:
cmd = "get rtc0"
self._nh.sendCommand(cmd)
sleep(0.1) # necessary, data might not be available otherwise
year = self._nh.recvRetNumber()
cmd = "get rtc1"
self._nh.sendCommand(cmd)
sleep(0.1) # necessary, data might not be available otherwise
month = self._nh.recvRetNumber()
cmd = "get rtc2"
self._nh.sendCommand(cmd)
sleep(0.1) # necessary, data might not be available otherwise
day = self._nh.recvRetNumber()
cmd = "get rtc3"
self._nh.sendCommand(cmd)
sleep(0.1) # necessary, data might not be available otherwise
hour = self._nh.recvRetNumber()
cmd = "get rtc4"
self._nh.sendCommand(cmd)
sleep(0.1) # necessary, data might not be available otherwise
minute = self._nh.recvRetNumber()
cmd = "get rtc5"
self._nh.sendCommand(cmd)
sleep(0.1) # necessary, data might not be available otherwise
second = self._nh.recvRetNumber()
cmd = "get rtc6"
self._nh.sendCommand(cmd)
sleep(0.1) # necessary, data might not be available otherwise
week = self._nh.recvRetNumber()
time_buf = [year, month, day, hour, minute, second, week]
if return_type == "str":
time_string = ("{}/{}/{} {}:{}:{} {}".
format(year, month, day, hour, minute, second,
week))
if length >= 22:
return time_string
else:
return time_string[0:length]
elif return_type == "int":
if length >= 7:
return time_buf
else:
return time_buf[0:length]
elif return_type in self.time_types:
rtc_index = self.time_types.index(return_type)
cmd = "get rtc{}".format(rtc_index)
self._nh.sendCommand(cmd)
sleep(0.1) # necessary, data might not be available otherwise
return self._nh.recvRetNumber()
else:
raise NexRtcError("RTC time can only be returned as '{}' or '{}'"
"or chosen from {}".
format("str", "int", self.time_types))