def get_data(self, monitor_mode= False, short_msg= True): '''Send discovery and monitor messages, and capture any responses. monitor_mode True: will loop continously False: will stop script once first succesful data packet is received (default) short_msg True: returns long detailed message False: returns short message (default)''' # Define the schedule of message polling radio.receiver() decoded = None message_not_received = True while message_not_received: # See if there is a payload, and if there is, process it if radio.isReceiveWaiting(): self.logger.info("receiving payload") payload = radio.receive() if monitor_mode == False: message_not_received = False try: decoded = OpenHEMS.decode(payload) except OpenHEMS.OpenHEMSException as e: self.logger.error("Can't decode payload:" + str(e)) message_not_received = True continue self.updateDirectory(decoded) if self.msg_join_ack['header']['sensorid'] == 0 or self.msg_switch['header']['sensorid'] == 0: self.msg_join_ack['header']['sensorid'] = decoded["header"]["sensorid"] self.msg_switch['header']['sensorid'] = decoded["header"]["sensorid"] #TODO: Should remember report time of each device, #and reschedule command messages to avoid their transmit slot #making it less likely to miss an incoming message due to #the radio being in transmit mode # assume only 1 rec in a join, for now if len(decoded["recs"])>0 and decoded["recs"][0]["paramid"] == OpenHEMS.PARAM_JOIN: #TODO: write OpenHEMS.getFromMessage("header_mfrid") response = OpenHEMS.alterMessage(self.msg_join_ack, header_mfrid=decoded["header"]["mfrid"], header_productid=decoded["header"]["productid"], header_sensorid=decoded["header"]["sensorid"]) p = OpenHEMS.encode(response) radio.transmitter() radio.transmit(p) radio.receiver() if short_msg: decoded = self.clean(decoded) return decoded
def send_data(self, switch_state): '''Send data to switch''' request = OpenHEMS.alterMessage(self.msg_switch, recs_0_value=switch_state) p = OpenHEMS.encode(request) radio.transmitter() radio.transmit(p) radio.receiver()
def test_Decode_MiHome_gateway_ACK(self): eTRVACK = [0xa, 0x4, 0x3, 0x2f, 0xd2, 0x0, 0x4, 0xae, 0x0, 0xe2, 0xb1] decoded = OpenHEMS.decode(eTRVACK, False) self.assertEqual(0x4ae, decoded['header']['sensorid'], "Unexpected sensor ID") self.assertEqual('OK', decoded['type'], "Unexpected type") self.assertEqual(0, decoded['recs'].__len__(), "Unexpected number of recs")
def test_Decode_eTRV_temperature_report(self): eTRVAnnounce = [0xe,0x4,0x3,0x46,0x13,0x0,0x4,0xae,0x74,0x92,0x14,0xb3,0x0,0xcb,0xc7] decoded = OpenHEMS.decode(eTRVAnnounce, False) self.assertEqual(0x4ae, decoded['header']['sensorid'], "Unexpected sensor ID") self.assertEqual('OK', decoded['type'], "Unexpected type") self.assertEqual(1, decoded['recs'].__len__(), "Unexpected number of recs") self.assertEqual('TEMPERATURE', decoded['recs'][0]['paramname'], "Unexpected number of recs") self.assertAlmostEqual(20.7, decoded['recs'][0]['value'], msg="Unexpected value", places=2)
def __init__(self, mfrid= Devices.MFRID_ENERGENIE, productid= Devices.PRODUCTID_R1_MONITOR_AND_CONTROL, sensorid= 0): self.logger = logging.getLogger('root') self.directory = {} self.msg_join_ack = MESSAGE_JOIN_ACK self.msg_join_ack['header']['mfrid'] = mfrid self.msg_join_ack['header']['productid'] = productid self.msg_join_ack['header']['sensorid'] = sensorid self.msg_switch = MESSAGE_SWITCH self.msg_switch['header']['sensorid'] = sensorid radio.init() OpenHEMS.init(Devices.CRYPT_PID)
def __init__(self, mfrid=Devices.MFRID_ENERGENIE, productid=Devices.PRODUCTID_R1_MONITOR_AND_CONTROL, sensorid=0): self.logger = logging.getLogger('root') self.directory = {} self.msg_join_ack = MESSAGE_JOIN_ACK self.msg_join_ack['header']['mfrid'] = mfrid self.msg_join_ack['header']['productid'] = productid self.msg_join_ack['header']['sensorid'] = sensorid self.msg_switch = MESSAGE_SWITCH self.msg_switch['header']['sensorid'] = sensorid radio.init() OpenHEMS.init(Devices.CRYPT_PID)
def monitor(): """Send discovery and monitor messages, and capture any responses""" # Define the schedule of message polling sendSwitchTimer = Timer(60, 1) # every n seconds offset by initial 1 switch_state = 0 # OFF radio.receiver() decoded = None while True: # See if there is a payload, and if there is, process it if radio.isReceiveWaiting(): trace("receiving payload") payload = radio.receive() try: decoded = OpenHEMS.decode(payload) except OpenHEMS.OpenHEMSException as e: print("Can't decode payload:" + str(e)) continue OpenHEMS.showMessage(decoded) updateDirectory(decoded) logMessage(decoded) #TODO: Should remember report time of each device, #and reschedule command messages to avoid their transmit slot #making it less likely to miss an incoming message due to #the radio being in transmit mode # assume only 1 rec in a join, for now if len(decoded["recs"]) > 0 and decoded["recs"][0][ "paramid"] == OpenHEMS.PARAM_JOIN: #TODO: write OpenHEMS.getFromMessage("header_mfrid") response = OpenHEMS.alterMessage( MESSAGE_JOIN_ACK, header_mfrid=decoded["header"]["mfrid"], header_productid=decoded["header"]["productid"], header_sensorid=decoded["header"]["sensorid"]) p = OpenHEMS.encode(response) radio.transmitter() radio.transmit(p) radio.receiver() if sendSwitchTimer.check( ) and decoded != None and decoded["header"]["productid"] in [ Devices.PRODUCTID_C1_MONITOR, Devices.PRODUCTID_R1_MONITOR_AND_CONTROL ]: request = OpenHEMS.alterMessage( MESSAGE_SWITCH, header_sensorid=decoded["header"]["sensorid"], recs_0_value=switch_state) p = OpenHEMS.encode(request) radio.transmitter() radio.transmit(p) radio.receiver() switch_state = (switch_state + 1) % 2 # toggle
def monitor(): """Send discovery and monitor messages, and capture any responses""" # Define the schedule of message polling sendSwitchTimer = Timer(5, 1) # every n seconds offset by initial 1 switch_state = 0 # OFF radio.receiver() decoded = None while True: # See if there is a payload, and if there is, process it if radio.isReceiveWaiting(): #trace("receiving payload") payload = radio.receive() try: decoded = OpenHEMS.decode(payload) except OpenHEMS.OpenHEMSException as e: warning("Can't decode payload:" + str(e)) continue OpenHEMS.showMessage(decoded) updateDirectory(decoded) logMessage(decoded) #TODO: Should remember report time of each device, #and reschedule command messages to avoid their transmit slot #making it less likely to miss an incoming message due to #the radio being in transmit mode # handle messages with zero recs in them silently #trace(decoded) if len(decoded["recs"]) == 0: print("Empty record:%s" % decoded) else: # assume only 1 rec in a join, for now if decoded["recs"][0]["paramid"] == OpenHEMS.PARAM_JOIN: #TODO: write OpenHEMS.getFromMessage("header_mfrid") # send back a JOIN ACK, so that join light stops flashing response = OpenHEMS.alterMessage(JOIN_ACK_MESSAGE, header_mfrid=decoded["header"]["mfrid"], header_productid=decoded["header"]["productid"], header_sensorid=decoded["header"]["sensorid"]) p = OpenHEMS.encode(response) radio.transmitter() radio.transmit(p) radio.receiver() if sendSwitchTimer.check() and decoded != None: request = OpenHEMS.alterMessage(SWITCH_MESSAGE, header_sensorid=decoded["header"]["sensorid"], recs_0_value=switch_state) p = OpenHEMS.encode(request) radio.transmitter() radio.transmit(p) radio.receiver() switch_state = (switch_state+1) % 2 # toggle
def monitor(): """Send discovery and monitor messages, and capture any responses""" # Define the schedule of message polling sendSwitchTimer = Timer(60, 1) # every n seconds offset by initial 1 switch_state = 0 # OFF radio.receiver() decoded = None while True: # See if there is a payload, and if there is, process it if radio.isReceiveWaiting(): trace("receiving payload") payload = radio.receive() try: decoded = OpenHEMS.decode(payload) except OpenHEMS.OpenHEMSException as e: print("Can't decode payload:" + str(e)) continue OpenHEMS.showMessage(decoded) updateDirectory(decoded) logMessage(decoded) #TODO: Should remember report time of each device, #and reschedule command messages to avoid their transmit slot #making it less likely to miss an incoming message due to #the radio being in transmit mode # assume only 1 rec in a join, for now if len(decoded["recs"])>0 and decoded["recs"][0]["paramid"] == OpenHEMS.PARAM_JOIN: #TODO: write OpenHEMS.getFromMessage("header_mfrid") response = OpenHEMS.alterMessage(MESSAGE_JOIN_ACK, header_mfrid=decoded["header"]["mfrid"], header_productid=decoded["header"]["productid"], header_sensorid=decoded["header"]["sensorid"]) p = OpenHEMS.encode(response) radio.transmitter() radio.transmit(p) radio.receiver() if sendSwitchTimer.check() and decoded != None and decoded["header"]["productid"] in [Devices.PRODUCTID_C1_MONITOR, Devices.PRODUCTID_R1_MONITOR_AND_CONTROL]: request = OpenHEMS.alterMessage(MESSAGE_SWITCH, header_sensorid=decoded["header"]["sensorid"], recs_0_value=switch_state) p = OpenHEMS.encode(request) radio.transmitter() radio.transmit(p) radio.receiver() switch_state = (switch_state+1) % 2 # toggle
def test_Decode_eTRV_temperature_report(self): eTRVAnnounce = [ 0xe, 0x4, 0x3, 0x46, 0x13, 0x0, 0x4, 0xae, 0x74, 0x92, 0x14, 0xb3, 0x0, 0xcb, 0xc7 ] decoded = OpenHEMS.decode(eTRVAnnounce, False) self.assertEqual(0x4ae, decoded['header']['sensorid'], "Unexpected sensor ID") self.assertEqual('OK', decoded['type'], "Unexpected type") self.assertEqual(1, decoded['recs'].__len__(), "Unexpected number of recs") self.assertEqual('TEMPERATURE', decoded['recs'][0]['paramname'], "Unexpected number of recs") self.assertAlmostEqual(20.7, decoded['recs'][0]['value'], msg="Unexpected value", places=2)
def get_data(self, monitor_mode=False, short_msg=True): '''Send discovery and monitor messages, and capture any responses. monitor_mode True: will loop continously False: will stop script once first succesful data packet is received (default) short_msg True: returns long detailed message False: returns short message (default)''' # Define the schedule of message polling radio.receiver() decoded = None message_not_received = True attempt_count = 2 while message_not_received: # See if there is a payload, and if there is, process it if radio.isReceiveWaiting(): self.logger.info("receiving payload") payload = radio.receive() if monitor_mode == False: message_not_received = False try: decoded = OpenHEMS.decode(payload) except OpenHEMS.OpenHEMSException as e: self.logger.error( "Attempt: {attempt} - Can't decode payload: {msg}". format(attempt=attempt_count, msg=str(e))) if receive_attempt > 0: message_not_received = True attempt_count = attempt_count - 1 continue self.updateDirectory(decoded) if self.msg_join_ack['header'][ 'sensorid'] == 0 or self.msg_switch['header'][ 'sensorid'] == 0: self.msg_join_ack['header']['sensorid'] = decoded[ "header"]["sensorid"] self.msg_switch['header']['sensorid'] = decoded["header"][ "sensorid"] #TODO: Should remember report time of each device, #and reschedule command messages to avoid their transmit slot #making it less likely to miss an incoming message due to #the radio being in transmit mode # assume only 1 rec in a join, for now if len(decoded["recs"]) > 0 and decoded["recs"][0][ "paramid"] == OpenHEMS.PARAM_JOIN: #TODO: write OpenHEMS.getFromMessage("header_mfrid") response = OpenHEMS.alterMessage( self.msg_join_ack, header_mfrid=decoded["header"]["mfrid"], header_productid=decoded["header"]["productid"], header_sensorid=decoded["header"]["sensorid"]) p = OpenHEMS.encode(response) radio.transmitter() radio.transmit(p) radio.receiver() if short_msg: decoded = self.clean(decoded) return decoded
def test_alterMessage(self): message = OpenHEMS.encode(OpenHEMS.alterMessage(MESSAGE_ETRV_SEND_BATTERY_LEVEL, header_sensorid=0x1234), False) self.assertEqual([0xc,0x4,0x3,0x1,0x0,0x0,0x12,0x34,0x76,0x0,0x0,0xcc,0x69], message);
def setUp(self): OpenHEMS.init(Devices.CRYPT_PID) pass
header_productid=decoded["header"]["productid"], header_sensorid=decoded["header"]["sensorid"]) p = OpenHEMS.encode(response) radio.transmitter() radio.transmit(p) radio.receiver() if sendSwitchTimer.check() and decoded != None: request = OpenHEMS.alterMessage(SWITCH_MESSAGE, header_sensorid=decoded["header"]["sensorid"], recs_0_value=switch_state) p = OpenHEMS.encode(request) radio.transmitter() radio.transmit(p) radio.receiver() switch_state = (switch_state+1) % 2 # toggle if __name__ == "__main__": radio.init() OpenHEMS.init(Devices.CRYPT_PID) try: monitor() finally: radio.finished() # END
radio.receiver() if sendSwitchTimer.check( ) and decoded != None and decoded["header"]["productid"] in [ Devices.PRODUCTID_C1_MONITOR, Devices.PRODUCTID_R1_MONITOR_AND_CONTROL ]: request = OpenHEMS.alterMessage( MESSAGE_SWITCH, header_sensorid=decoded["header"]["sensorid"], recs_0_value=switch_state) p = OpenHEMS.encode(request) radio.transmitter() radio.transmit(p) radio.receiver() switch_state = (switch_state + 1) % 2 # toggle if __name__ == "__main__": radio.init() OpenHEMS.init(Devices.CRYPT_PID) try: monitor() finally: radio.finished() # END
def test_Decode_MiHome_gateway_ACK(self): eTRVACK = [0xa,0x4,0x3,0x2f,0xd2,0x0,0x4,0xae,0x0,0xe2,0xb1] decoded = OpenHEMS.decode(eTRVACK, False) self.assertEqual(0x4ae, decoded['header']['sensorid'], "Unexpected sensor ID") self.assertEqual('OK', decoded['type'], "Unexpected type") self.assertEqual(0, decoded['recs'].__len__(), "Unexpected number of recs")