Skip to content

Commit 28cdc39

Browse files
authored
Merge pull request #2 from hasenradball/develop
added SANITY check methods for SystemCode or ButtonCode
2 parents e3db7da + af8078c commit 28cdc39

2 files changed

Lines changed: 71 additions & 5 deletions

File tree

cRcSocketSwitch/cRcSocketSwitch.py

Lines changed: 69 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,11 @@ class for switching remote socket devices as:
1111
I calculated the corresponding send code in decimal value
1212
and uses the library rpi-rf to send the command via 433 MHz send device
1313
'''
14-
14+
15+
_button_list = ['a', 'A', 'b', 'B', 'c', 'C', 'd', 'D', 'e', 'E']
16+
_button_mapping = {'A':16, 'B':8, 'C':4, 'D':2, 'E':1}
17+
18+
1519
def __init__(self, gpio_pin = 17):
1620
'''
1721
Constructor for GPIO Pin and Configuration
@@ -20,17 +24,76 @@ def __init__(self, gpio_pin = 17):
2024
self.config = {'code': None, 'tx_proto': 1, 'tx_pulselength': 320, 'tx_length': 24}
2125
logging.info("Brennenstuhl RCS1000 N object created with GPIO pin {}".format(self.gpio))
2226

27+
28+
def sanity_check_Systemcode(self, SystemCode_raw):
29+
'''
30+
check is SystemCode has the intented format of str (e.g: '10000')
31+
'''
32+
# check if SystemCode ist of type str
33+
if isinstance(SystemCode_raw, str):
34+
# check now the length of the SystemCode
35+
if len(SystemCode_raw) == 5:
36+
if set(SystemCode_raw) <= {'0','1'}:
37+
# check if SystemCode contains only '0' or '1'
38+
# SystemCode ok
39+
logging.info("SystemCode is ok: {} - {}\n".format(SystemCode_raw, type(SystemCode_raw)))
40+
return True
41+
else:
42+
logging.error("SystemCode is type of str BUT contains NOT only '0' or '1': {} - {}\n".format(SystemCode_raw, type(SystemCode_raw)))
43+
return False
44+
else:
45+
# SystemCode not ok
46+
logging.error("SystemCode length is NOT 5: {} - {}\n".format(SystemCode_raw, type(SystemCode_raw)))
47+
return False
48+
else:
49+
logging.error("SystemCode is not a type of str: {} - {}\n".format(SystemCode_raw, type(SystemCode_raw)))
50+
return False
51+
52+
53+
def sanity_check_Buttoncode(self, ButtonCode_raw):
54+
'''
55+
check the intented format of ButtonCode
56+
The ButtonCode can have the following formats:
57+
- type str like 'a' or 'A'
58+
- type str like '10000'
59+
- type int like 1, 2, 4, ...etc
60+
'''
61+
# check if ButtonCode is of type str
62+
if ButtonCode_raw in self._button_list:
63+
# ok fine ButtonCode is type of str like 'A', 'B, etc...
64+
logging.info("ButtonCode is ok: {} - {}\n".format(ButtonCode_raw, type(ButtonCode_raw)))
65+
return True
66+
elif isinstance(ButtonCode_raw, str):
67+
if len(ButtonCode_raw) == 5:
68+
# check if ButtonCode is like '10000'
69+
if set(ButtonCode_raw) <= {'0','1'}:
70+
# check if Buttoncode contains only '0' or '1'
71+
logging.info("ButtonCode is ok: {} - {}\n".format(ButtonCode_raw, type(ButtonCode_raw)))
72+
return True
73+
else:
74+
logging.error("ButtonCode is type of str BUT contains NOT only '0' or '1': {} - {}\n".format(ButtonCode_raw, type(ButtonCode_raw)))
75+
return False
76+
else:
77+
logging.error("ButtonCode is type of str BUT len i NOT 5: {} - {}\n".format(ButtonCode_raw, type(ButtonCode_raw)))
78+
return False
79+
elif isinstance(ButtonCode_raw, int) and ButtonCode_raw < 32:
80+
# ButtonCode is of type int and has valid value
81+
logging.info("ButtonCode is ok: {} - {}\n".format(ButtonCode_raw, type(ButtonCode_raw)))
82+
return True
83+
else:
84+
logging.error("ButtonCode is type of int BUT has wrong value: {} - {}\n".format(ButtonCode_raw, type(ButtonCode_raw)))
85+
return False
86+
87+
2388
def prepareCodes(self, SystemCode_raw, ButtonCode_raw, status):
2489
'''
2590
this method prepares the codes and checks the imput format in case
2691
of different usecases
2792
'''
28-
button_list = ['a', 'A', 'b', 'B', 'c', 'C', 'd', 'D', 'e', 'E']
29-
button_mapping = {'A':16, 'B':8, 'C':4, 'D':2, 'E':1}
3093
# check if the input for the ButtonCode is in the Case 'A', 'B', etc...
31-
if ButtonCode_raw in button_list:
94+
if ButtonCode_raw in self._button_list:
3295
ButtonCode_raw = ButtonCode_raw.upper()
33-
ButtonCode = button_mapping[ButtonCode_raw]
96+
ButtonCode = self._button_mapping[ButtonCode_raw]
3497
ButtonCode = '{:05b}'.format(ButtonCode)
3598
logging.info("Buttoncode: {}\n".format(ButtonCode))
3699

@@ -139,6 +202,7 @@ def calc_DecimalCode_python_style(self, SystemCode, ButtonCode, status):
139202
logging.info("binary string: {}\n".format(binstr))
140203
return int(binstr, 2)
141204

205+
142206
def send(self, systemCode, btn_code, status):
143207
'''
144208
Method to prepare the codes and send it to the actuator

switchSocket.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@
3838
try:
3939
# create Brennenstuhl RCS1000N object
4040
obj = cRcSocketSwitch.RCS1000N(gpio_pin)
41+
#obj.sanity_check_Systemcode('10000')
42+
#obj.sanity_check_Buttoncode(32)
4143
# prepare and send values
4244
obj.send(*values1)
4345
finally:

0 commit comments

Comments
 (0)