-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path__init__.py
320 lines (232 loc) · 11.2 KB
/
__init__.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
"""
Python C extension module to wrap the PiCode library.
The PiCode library provides an easy way to handle the wireless protocols of
weather stations and radio control switches using 433/315Mhz radio frequency
in ASK/OOK modulation, which have been implemented by the 'pilight' project.
See: https://github.com/latchdevel/pyPiCode
Copyright (c) 2022-2024 Jorge Rivera. All right reserved.
License GNU Lesser General Public License v3.0.
"""
__version__ = '0.1.0'
__title__ = 'pypicode'
__description__ = 'Python C extension module to wrap the PiCode library.'
__uri__ = 'https://github.com/latchdevel/pyPiCode'
__doc__ = __description__ + ' <' + __uri__ + '>'
__author__ = 'Jorge Rivera'
__license__ = 'LGPL-3.0'
__copyright__ = 'Copyright (c) 2022-2024 Jorge Rivera. All right reserved.'
from ast import literal_eval as _literal_eval
from copy import deepcopy as _deepcopy
from re import sub as _sub
# Import picode wrapper module as private module
from pypicode import picode_wrap as _picode_wraper
# Remove picode wrapper module from namespace
if 'picode_wrap' in dir():
del picode_wrap
# Redefine Python functions from picode wrapper module
# Added parameter checks and basic docstrings
def getPiCodeVersion():
"""Get version of PiCode library.
Returns the full version string of the PiCode library or None on failure.
"""
version = _picode_wraper.getPiCodeVersion()
if isinstance(version,str):
return version
else:
return None
def findProtocol(name:str):
"""Find a protocol among all those initialized by name.
Returns a Swig Object of type 'protocol_t *' or None on failure.
"""
if (not isinstance(name,str)):
raise TypeError("in method 'findProtocol', argument 1 'name' must be a string.")
result = _picode_wraper.findProtocol(name)
if type(result).__name__ == 'SwigPyObject':
return result
else:
return None
def pulseTrainToString(pulses_list:list, repeats:int=0):
"""Converts a list of pulses to a string in pilight format.
Returns a pilight string or None on failure.
"""
if (not isinstance(pulses_list,list)):
raise TypeError("in method 'pulseTrainToString', argument 1 'pulses_list' must be a list.")
if (not isinstance(repeats,int)):
raise TypeError("in method 'pulseTrainToString', argument 2 'repeats' must be an integer.")
if (repeats < 0 or repeats > 255):
raise TypeError("in method 'pulseTrainToString', argument 2 'repeats' must be in range from 0 to 255.")
# Define a C type array of unit32_t
pulses = _picode_wraper.uint32Array(len(pulses_list))
# Populate C type array from python list
for i in range(len(pulses_list)):
pulses[i]=abs(int(pulses_list[i]))
# Call wrapped C function to convert an array of pulses to pilight string format
result = _picode_wraper.pulseTrainToString(pulses.cast(), len(pulses_list), repeats)
# Free pulses C type array
pulses.__swig_destroy__(pulses)
if isinstance(result,str):
return result
else:
return None
def encodeToPulseTrain(protocol, json_data:dict):
"""Encodes a Swig Object of type 'protocol_t *' and a json data dict to a pulses list.
Returns a pulses list or None on failure.
"""
if not type(protocol).__name__ == 'SwigPyObject':
raise TypeError("in method 'encodeToPulseTrain', argument 1 'protocol' must be a Swig Object of type 'protocol_t *'.")
if (not isinstance(json_data,dict)):
raise TypeError("in method 'encodeToPulseTrain', argument 2 'json_data' must be a dict.")
# Define a C array type of unit32_t
pulses = _picode_wraper.uint32Array(_picode_wraper.protocol_maxrawlen())
# Make a deep copy of json_data dict to change it to support a dict out as dict in
json_data_copy = _deepcopy(json_data)
if len(json_data_copy) > 0:
if 'state' in json_data_copy:
if json_data_copy['state'] == 'on':
json_data_copy['on']=1
del json_data_copy['state']
elif json_data_copy['state'] == 'off':
json_data_copy['off']=1
del json_data_copy['state']
# Call wrapped C function to encode from protocol and json data to array of pulses
result_code = _picode_wraper.encodeToPulseTrain(pulses.cast(), _picode_wraper.protocol_maxrawlen(), protocol, str(json_data_copy))
# If success, populate a python list from C type array of pulses
if result_code > 0:
result = [ pulses[i] for i in range(result_code) ]
else:
result = None
# Free pulses C type array
pulses.__swig_destroy__(pulses)
return result
def encodeToPulseTrainByName(protocol_name:str, json_data:dict):
"""Encodes a protocol name string and a json data dict to a pulses list.
Returns a pulses list or None on failure.
"""
if (not isinstance(protocol_name,str)):
raise TypeError("in method 'encodeToPulseTrainByName', argument 1 'protocol_name' must be a string.")
if (not isinstance(json_data,dict)):
raise TypeError("in method 'encodeToPulseTrainByName', argument 2 'json_data' must be a dict.")
# Define a C array type of unit32_t
pulses = _picode_wraper.uint32Array(_picode_wraper.protocol_maxrawlen())
# Make a deep copy of json_data dict to change it to support a dict out as dict in
json_data_copy = _deepcopy(json_data)
if len(json_data_copy) > 0:
if 'state' in json_data_copy:
if json_data_copy['state'] == 'on':
json_data_copy['on']=1
del json_data_copy['state']
elif json_data_copy['state'] == 'off':
json_data_copy['off']=1
del json_data_copy['state']
# Call wrapped C function to encode from protocol name and json data to array of pulses
result_code = _picode_wraper.encodeToPulseTrainByName(pulses.cast(), _picode_wraper.protocol_maxrawlen(), protocol_name, str(json_data_copy))
# If success, populate a python list from C type array of pulses
if result_code > 0:
result = [ pulses[i] for i in range(result_code) ]
else:
result = None
# Free pulses C type array
pulses.__swig_destroy__(pulses)
return result
def stringToPulseTrain(pilight_string:str):
"""Converts a string in pilight format to a pulses lists.
Returns a pulses list or None on failure.
"""
if (not isinstance(pilight_string,str)):
raise TypeError("in method 'stringToPulseTrain', argument 1 'pilight_string' must be a string.")
# Define a C array type of unit32_t
pulses = _picode_wraper.uint32Array(_picode_wraper.protocol_maxrawlen())
# Call wrapped C function to convert from pilight string to array of pulses
result_code = _picode_wraper.stringToPulseTrain(pilight_string, pulses.cast(), _picode_wraper.protocol_maxrawlen())
# If success, populate a python list from C type array of pulses
if result_code > 0:
result = [ pulses[i] for i in range(result_code) ]
else:
result = None
# Free pulses C type array
pulses.__swig_destroy__(pulses)
return result
def decodePulseTrain(pulses_list:list):
"""Decodes a list of pulses to a results dict.
Returns a dict always, with a key "protocols" and a list of decoded protocols as its value,
which will be empty if none are decoded, like as { 'protocols': [ ] }
"""
if ( not isinstance(pulses_list,list) ):
raise TypeError("in method 'decodePulseTrain', argument 1 'pulses_list' must be a list.")
pulses = _picode_wraper.uint32Array(len(pulses_list))
for i in range(len(pulses_list)):
pulses[i]=pulses_list[i]
result = _literal_eval(_picode_wraper.decodePulseTrain(pulses.cast(), len(pulses_list), ""))
pulses.__swig_destroy__(pulses)
if isinstance(result,dict):
return result
else:
return dict()
def decodeString(pilight_string:str):
"""Decodes a string in pilight format to a results dict.
Returns a dict with a key "protocols" and a list of decoded protocols as its value,
or None on failure.
"""
if (not isinstance(pilight_string,str)):
raise TypeError("in method 'decodeString', argument 1 'pilight_string' must be a string.")
decoded_protocols = _picode_wraper.decodeString(pilight_string)
if (isinstance(decoded_protocols,str)):
decoded_protocols = _sub(r"\n\ *","",decoded_protocols)
result = _literal_eval(decoded_protocols)
if not isinstance(result,dict):
result = None
else:
result = None
return result
def encodeToString(protocol_name:str, json_data:dict, repeats:int=0):
"""Encodes a protocol name string and a json data dict to a string in pilight format.
Returns a pilight string or None on failure.
"""
if (not isinstance(protocol_name,str)):
raise TypeError("in method 'encodeToString', argument 1 'protocol_name' must be a string.")
if (not isinstance(json_data,dict)):
raise TypeError("in method 'encodeToString', argument 2 'json_data' must be a dict.")
if (not isinstance(repeats,int)):
raise TypeError("in method 'encodeToString', argument 3 'repeats' must be an integer.")
if (repeats < 0 or repeats > 255):
raise TypeError("in method 'encodeToString', argument 3 'repeats' must be in range from 0 to 255.")
# Make a deep copy of json_data dict to change it to support a dict out as dict in
json_data_copy = _deepcopy(json_data)
if len(json_data_copy) > 0:
if 'state' in json_data_copy:
if json_data_copy['state'] == 'on':
json_data_copy['on']=1
del json_data_copy['state']
elif json_data_copy['state'] == 'off':
json_data_copy['off']=1
del json_data_copy['state']
result = _picode_wraper.encodeToString(protocol_name, str(json_data_copy), repeats)
if isinstance(result,str):
return result
else:
return None
def encodeJson(json:dict, repeats:int=0):
"""Encodes a full json dict to a string in pilight format.
Returns a pilight string or None on failure.
"""
if (not isinstance(json,dict)):
raise TypeError("in method 'encodeJson', argument 1 'json' must be a dict.")
if (not isinstance(repeats,int)):
raise TypeError("in method 'encodeJson', argument 2 'repeats' must be an integer.")
if (repeats < 0 or repeats > 255):
raise TypeError("in method 'encodeJson', argument 2 'repeats' must be in range from 0 to 255.")
# Make a deep copy of json dict to change it to support a dict out as dict in
json_copy = _deepcopy(json)
if len(json_copy) > 0:
if 'state' in json_copy[list(json_copy.keys())[0]]:
if json_copy[list(json_copy.keys())[0]]['state'] == 'on':
json_copy[list(json_copy.keys())[0]]['on']=1
del json_copy[list(json_copy.keys())[0]]['state']
elif json_copy[list(json_copy.keys())[0]]['state'] == 'off':
json_copy[list(json_copy.keys())[0]]['off']=1
del json_copy[list(json_copy.keys())[0]]['state']
result = _picode_wraper.encodeJson(str(json_copy), repeats)
if isinstance(result,str):
return result
else:
return None