<s> ALL_SOCKETS = <NUM_LIT:0> <EOL> PREAMBLE = [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] <EOL> DEFAULT_ADDR = <NUM_LIT> <EOL> DEFAULT_ADDR_ENC = [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] <EOL> SW1_ON_ENC = [ <NUM_LIT> , <NUM_LIT> ] <EOL> SW1_OFF_ENC = [ <NUM_LIT> , <NUM_LIT> ] <EOL> def ashex ( payload ) : <EOL> line = "<STR_LIT>" <EOL> for b in payload : <EOL> line += str ( hex ( b ) ) + "<STR_LIT:U+0020>" <EOL> return line <EOL> def build_relay_msg ( relayState = False ) : <EOL> """<STR_LIT>""" <EOL> payload = PREAMBLE <EOL> if relayState : <EOL> payload += SW1_ON_ENC <EOL> else : <EOL> payload += SW1_OFF_ENC <EOL> return payload <EOL> def build_test_message ( pattern ) : <EOL> """<STR_LIT>""" <EOL> payload = PREAMBLE + DEFAULT_ADDR_ENC <EOL> pattern &= <NUM_LIT> <EOL> control = encode_bits ( pattern , <NUM_LIT:4> ) <EOL> payload += control <EOL> return payload <EOL> def build_switch_msg ( state , device_address = ALL_SOCKETS , house_address = None ) : <EOL> """<STR_LIT>""" <EOL> if house_address == None : <EOL> house_address = DEFAULT_ADDR <EOL> payload = [ ] + PREAMBLE <EOL> payload += encode_bits ( ( house_address & <NUM_LIT> ) >> <NUM_LIT:16> , <NUM_LIT:4> ) <EOL> payload += encode_bits ( ( house_address & <NUM_LIT> ) >> <NUM_LIT:8> , <NUM_LIT:8> ) <EOL> payload += encode_bits ( ( house_address & <NUM_LIT> ) , <NUM_LIT:8> ) <EOL> if not state : <EOL> bits = <NUM_LIT> <EOL> else : <EOL> bits = <NUM_LIT> <EOL> if device_address == ALL_SOCKETS : <EOL> bits |= <NUM_LIT> <EOL> elif device_address == <NUM_LIT:1> : <EOL> bits |= <NUM_LIT> <EOL> elif device_address == <NUM_LIT:2> : <EOL> bits |= <NUM_LIT> <EOL> elif device_address == <NUM_LIT:3> : <EOL> bits |= <NUM_LIT> <EOL> elif device_address == <NUM_LIT:4> : <EOL> bits |= <NUM_LIT> <EOL> payload += encode_bits ( bits , <NUM_LIT:4> ) <EOL> return payload <EOL> def encode_bytes ( data ) : <EOL> """<STR_LIT>""" <EOL> payload = [ ] <EOL> for b in data : <EOL> payload += encode_bits ( b , <NUM_LIT:8> ) <EOL> return payload <EOL> ENCODER = [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] <EOL> def encode_bits ( data , number ) : <EOL> """<STR_LIT>""" <EOL> shift = number - <NUM_LIT:2> <EOL> encoded = [ ] <EOL> for i in range ( number / <NUM_LIT:2> ) : <EOL> bits = ( data >> shift ) & <NUM_LIT> <EOL> encoded . append ( ENCODER [ bits ] ) <EOL> shift -= <NUM_LIT:2> <EOL> return encoded </s>
