def on_message(client, userdata, msg): # Process messages received from MQTT Broker # Place the messages received in the 'outboundMQTTqueue queue' # Format of message = DCB Function/Thermostat ID/DCB Value # Assumes that the DCB Function, Thermostat ID and Value are all correct if msg.topic.startswith(hmMQTTpath): queuemsg = msg.topic[len(hmMQTTpath) + 1:len(msg.topic)] + "/" + str( msg.payload) outboundMQTTqueue.put(queuemsg) logmessage('info', 'mqtt.py', 'Message received from MQTT Broker ' + queuemsg)
def connectMQTT(): global mqttclient while True: mqttclient = mqtt.Client() mqttclient.on_connect = on_connect mqttclient.on_disconnect = on_disconnect try: mqttclient.connect(MQTTBrokerIP, MQTTBrokerPort) mqttclient.loop_start() break except: logmessage('error', 'monitor.py', 'Error connecting to the MQTT Broker') time.sleep(30)
def sendtoSerial(sendMSG): # Send data to the serial interface for the thermostats receiveTimeout = 0.5 datal = [] recvMSG = '' try: s.send(sendMSG) except socket.error, msg: logmessage('error', 'heatmiser.py', 'Error connecting with the serial interface: ' + str(msg)) s.close() connectSerial() time.sleep(10) return datal
def on_connect(client, userdata, rc): # Do something when connected to MQTT Broker logmessage('info', 'mqtt.py', 'Connected to broker') # Go through all the entries in the hmDCBStructure array for loop in hmDCBStructure: # Find only those which have a status of 'RW' # Then subscribe to each element if hmDCBStructure[loop][5] == 'RW': hmMQTTSubscribePath = hmMQTTpath + "/" + hmDCBStructure[loop][ 1] + "/#" mqttclient.subscribe(hmMQTTSubscribePath) logmessage( 'info', 'mqtt.py', 'Subscribed to MQTT Broker using path ' + hmMQTTSubscribePath)
def connectSerial(): # Start a few things off before we get going # Connect to the Serial interface for the Heatmiser Thermostats global s while True: try: s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect((hmSerialIP, hmSerialPort)) s.settimeout(hmSerialTimeout) s.setblocking(0) time.sleep(0.5) logmessage('info', 'heatmiser.py', 'Connected to the serial interface') break except socket.error, msg: logmessage( 'error', 'heatmiser.py', 'Error connecting with the serial interface: ' + str(msg)) time.sleep(60)
def hmTimeUpdate(): # Update thermostat times dayofweek = datetime.datetime.now().isoweekday() hour = datetime.datetime.now().time().hour mins = datetime.datetime.now().time().minute secs = datetime.datetime.now().time().second payload = [dayofweek, hour, mins, secs] for sendIndex in hmStatList: sendMSG = bytearray(hmFormMsgCRC(sendIndex, FUNC_WRITE, 43, payload)) datal = sendtoSerial(sendMSG) if hmValidateResponse(datal) == 1: if datal[3] == sendIndex: if datal[4] == FUNC_WRITE: # Need to call the refresh of the DCB data to send to the MQTT broker logmessage( 'info', 'heatmiser.py', 'Time updated for Thermostat ID: ' + str(sendIndex)) else: logmessage( 'error', 'heatmiser.py', 'Failed to update time for Thermostat ID: ' + str(sendIndex)) else: logmessage('error', 'heatmiser.py', 'Incorrect ThermostatID response received')
def hmRecvMQTTmessage(): # Check the MQTT Receive queue to see if there are any messages to process # Receive queue is filled by mqtt.py # Receive Frame format [129, 7, 0, 4, 1, 69, 20] if outboundMQTTqueue.qsize() != 0: recvMSG = outboundMQTTqueue.get() # Loop through all the DCB settings for loop in hmDCBStructure: if hmDCBStructure[loop][5] == 'RW': # Is the message received from the broker equal to the message functions in the defined array if recvMSG.startswith(hmDCBStructure[loop][1]): destination = int( recvMSG[len(hmDCBStructure[loop][1]) + 1:recvMSG. find('/', len(hmDCBStructure[loop][1]) + 1)]) # Check to make sure the Thermostat ID is in the expect range in hmStatList if destination in hmStatList: dcbcommand = int(hmDCBStructure[loop][0]) # Check to make sure the value being sent is within allowable range dcbvalue = int( str(recvMSG[recvMSG. find('/', len(hmDCBStructure[loop][1]) + 1) + 1:len(recvMSG)]).split('.')[0]) if hmDCBStructure[loop][ 6] <= dcbvalue <= hmDCBStructure[loop][7]: # Work out the holiday time if hmDCBStructure[loop][1] == 'HolidayTime': hours = dcbvalue * 24 hours_lo = (hours & BYTEMASK) hours_hi = (hours >> 8) & BYTEMASK payload = [hours_lo, hours_hi] else: payload = [dcbvalue] # Send updates to the Thermostat sendMSG = bytearray( hmFormMsgCRC(destination, FUNC_WRITE, dcbcommand, payload)) datal = sendtoSerial(sendMSG) # Validate the response from the thermostat if hmValidateResponse(datal) == 1: # Does the receiving data source address match the destination send address if datal[3] == destination: # Does the response have a WRITE response in the frame if datal[4] == FUNC_WRITE: # Call the refresh of the DCB data for that thermostat sendMSG = bytearray( hmFormMsgCRC( destination, FUNC_READ, 00, 0)) datal = sendtoSerial(sendMSG) # Validate the response from the Thermostats to ensure that an appropriate message has been received for processing if hmValidateResponse(datal) == 1: # Check to work out what has been sent and forward to the MQTT broker hmForwardDCBValues(datal, 0) logmessage( 'info', 'heatmiser.py', 'Command sent to Thermostat ' + str(destination) + ': ' + str(hmDCBStructure[loop][1]) + ":" + str(dcbvalue)) else: logmessage( 'error', 'heatmiser.py', 'Failed to send command to Thermostat: ' + str(destination)) else: logmessage( 'error', 'heatmiser.py', 'Incorrect ThermostatID response received' )
def on_disconnect(client, userdata, rc): logmessage('info', 'heatmiser.py', 'Disconnected from MQTT broker')
def on_connect(client, userdata, rc): logmessage('info', 'heatmiser.py', 'Connected to MQTT broker')
def on_disconnect(client, userdata, rc): # Do something when connected to MQTT Broker logmessage('warning', 'mqtt.py', 'Disconnected from MQTT Broker')
# Place the messages received in the 'outboundMQTTqueue queue' # Format of message = DCB Function/Thermostat ID/DCB Value # Assumes that the DCB Function, Thermostat ID and Value are all correct if msg.topic.startswith(hmMQTTpath): queuemsg = msg.topic[len(hmMQTTpath) + 1:len(msg.topic)] + "/" + str( msg.payload) outboundMQTTqueue.put(queuemsg) logmessage('info', 'mqtt.py', 'Message received from MQTT Broker ' + queuemsg) # Start the MQTT Client mqttclient = mqtt.Client() mqttclient.on_connect = on_connect mqttclient.on_message = on_message mqttclient.on_disconnect = on_disconnect while True: mqttclient = mqtt.Client() mqttclient.on_connect = on_connect mqttclient.on_message = on_message mqttclient.on_disconnect = on_disconnect try: mqttclient.connect(MQTTBrokerIP, MQTTBrokerPort) break except: logmessage('error', 'mqtt.py', 'Error connecting to MQTT Broker') time.sleep(30) # Perform an infinate loop to receive messages from the MQTT Broker mqttclient.loop_start()
def on_disconnect(client, userdata, rc): logmessage('info', 'monitor.py', 'Disconnected from MQTT Broker')
def on_connect(client, userdata, rc): logmessage('info', 'monitor.py', 'Connected to MQTT Broker')
def syncrepodata(): logmessage('getting called ') return 'Hello World'