1+ #!/usr/bin/python3
2+ # -*- coding: utf-8 -*-
3+
4+ from rpi_rf import RFDevice
5+ import logging
6+
7+ class RCS1000N :
8+ '''
9+ class for switching remote socket devices as:
10+ Brennenstuhl RCS 1000 N
11+ I calculated the corresponding send code in decimal value
12+ and uses the library rpi-rf to send the command via 433 MHz send device
13+ '''
14+
15+ def __init__ (self , gpio_pin = 17 ):
16+ '''
17+ Constructor for GPIO Pin and Configuration
18+ '''
19+ self .gpio = gpio_pin
20+ self .config = {'code' : None , 'tx_proto' : 1 , 'tx_pulselength' : 320 , 'tx_length' : 24 }
21+ logging .info ("Brennenstuhl RCS1000 N object created with GPIO pin {}" .format (self .gpio ))
22+
23+ def prepareCodes (self , SystemCode_raw , ButtonCode_raw , status ):
24+ '''
25+ this method prepares the codes and checks the imput format in case
26+ of different usecases
27+ '''
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 }
30+ # check if the input for the ButtonCode is in the Case 'A', 'B', etc...
31+ if ButtonCode_raw in button_list :
32+ ButtonCode_raw = ButtonCode_raw .upper ()
33+ ButtonCode = button_mapping [ButtonCode_raw ]
34+ ButtonCode = '{:05b}' .format (ButtonCode )
35+ logging .info ("Buttoncode: {}\n " .format (ButtonCode ))
36+
37+ # check if the ButtonCode is an integer like 1, 2, 3, etc...
38+ elif isinstance (ButtonCode_raw , int ):
39+ logging .info ("ButtonCode_raw is of type int" )
40+ ButtonCode = '{:05b}' .format (ButtonCode_raw )
41+ logging .info ("Buttoncode: {}\n " .format (ButtonCode ))
42+
43+ # assume the code is in the way '01000' check the length (5)
44+ elif isinstance (ButtonCode_raw , str ):
45+ logging .info ("ButtonCode_raw is of type str" )
46+ # check length of 5
47+ if len (ButtonCode_raw ) == 5 :
48+ ButtonCode = ButtonCode_raw
49+ logging .info ("Buttoncode: {} - {}\n " .format (ButtonCode , type (ButtonCode )))
50+ else :
51+ ButtonCode = None
52+ logging .error ("ERROR: wrong len of ButtonCode_raw!" )
53+
54+ # check now the lenght of the SystemCode
55+ if len (SystemCode_raw ) == 5 :
56+ SystemCode = SystemCode_raw
57+ logging .info ("SystemCode: {} - {}\n " .format (SystemCode , type (SystemCode )))
58+ else :
59+ SystemCode = None
60+ logging .error ("ERROR: wrong len of SystemCode_raw!" )
61+
62+ # check the status
63+ if isinstance (status , bool ):
64+ if status :
65+ status = 1
66+ else :
67+ status = 0
68+ return (SystemCode , ButtonCode , status )
69+
70+
71+ def calcTristateCode (self , SystemCode , ButtonCode , status ):
72+ '''
73+ calculate the corresponding Tristate Code in the same way as the library
74+ wiringPi does it in c-code
75+ return value is the TriState String Code
76+ '''
77+ code = ""
78+ for c in SystemCode :
79+ if c == '0' :
80+ code += 'F'
81+ else :
82+ code += '0'
83+
84+ for c in ButtonCode :
85+ if c == '0' :
86+ code += 'F'
87+ else :
88+ code += '0'
89+
90+ if status :
91+ code += '0F'
92+ else :
93+ code += 'F0'
94+ return code
95+
96+
97+ def calcBinaryCode (self , strCode ):
98+ '''
99+ calculate the Binary Code of the switch command in the same way as th library
100+ wiringPi does it in c-code
101+ return value is then a decimal value of the switch command
102+ '''
103+ code = 0
104+ len = 0
105+ for c in strCode :
106+ code <<= 2
107+ #print(c, type(c))
108+ #print(bin(code))
109+ if c == '0' :
110+ # bit pattern 00
111+ pass
112+ elif c == 'F' :
113+ # bit pattern 01
114+ code += 1
115+ elif c == '1' :
116+ # bit pattern 11
117+ code += 3
118+ len += 2
119+ logging .info ("Length of code: {}\n " .format (len ))
120+ logging .info ("code: {}\n " .format (int (code )))
121+ return code
122+
123+
124+ def calc_DecimalCode_python_style (self , SystemCode , ButtonCode , status ):
125+ '''
126+ calculate the decimal Code in a python style
127+ this combines the methods:
128+ calcTristateCode + calcBinaryCode in on step
129+ '''
130+ code = str (SystemCode + ButtonCode )
131+ help = code .replace ('0' , 'F' ).replace ('1' , '0' )
132+ if status :
133+ help += '0F'
134+ else :
135+ help += 'F0'
136+ logging .info ("Py - TriState code: {}\n " .format (help ))
137+ code = help .replace ('0' ,'00' ).replace ('F' , '01' )
138+ binstr = '0b' + code
139+ logging .info ("binary string: {}\n " .format (binstr ))
140+ return int (binstr , 2 )
141+
142+ def send (self , systemCode , btn_code , status ):
143+ '''
144+ Method to prepare the codes and send it to the actuator
145+ '''
146+ try :
147+ rfdevice = RFDevice (self .gpio )
148+ rfdevice .enable_tx ()
149+ rfdevice .tx_repeat = 10
150+ values = self .prepareCodes (systemCode , btn_code , status )
151+ send_code = self .calc_DecimalCode_python_style (* values )
152+ self .config ['code' ] = send_code
153+ rfdevice .tx_code (** self .config )
154+
155+ finally :
156+ rfdevice .cleanup ()
0 commit comments