def filter_message(message): message_buffer = message.get_bytearray() size = len(message_buffer) message_str = message_buffer[:size].decode('utf-8') if not message_str: return None message_obj = json.loads(message_str) if message_obj["machine"]["temperature"] > TEMPERATURE_THRESHOLD: print(" Machine temperature : %d exceeds threshold %d" % (message_obj["machine"]["temperature"], TEMPERATURE_THRESHOLD)) filtered_message = IoTHubMessage(message_str) filtered_message.set_content_type_system_property( message.get_content_type_system_property() or "application/json") filtered_message.set_content_encoding_system_property( message.get_content_encoding_system_property() or "utf-8") prop_map = filtered_message.properties() origin_prop_key_value_pair = message.properties().get_internals() for key in origin_prop_key_value_pair: prop_map.add(key, origin_prop_key_value_pair[key]) prop_map.add("MessageType", "Alert") return filtered_message else: return None
def send_event(self, event, properties, send_context): if not isinstance(event, IoTHubMessage): event = IoTHubMessage(bytearray(event, 'utf8')) event.set_content_encoding_system_property('utf-8') event.set_content_type_system_property('application/json') event.properties().add('deviceId', 'RaspberryPi3') if len(properties) > 0: prop_map = event.properties() for key in properties: prop_map.add_or_update(key, properties[key]) self.client.send_event_async(event, send_confirmation_callback, send_context)
def main(protocol): try: print ( "\nPython %s\n" % sys.version ) print ( "IoT Hub Client for Python" ) hub_manager = HubManager(protocol) print ( "Starting the OBD module using protocol %s..." % hub_manager.client_protocol ) print ( "This module is now waiting for messages and will indefinitely. Press Ctrl-C to exit. ") while True: obdMsg = obdreader.getVehicleTelemtries(DEVICE_ID) print("received obd msg: " + str(obdMsg)) if(obdMsg is not None): msg = IoTHubMessage(bytearray(obdMsg, 'utf8')) #in order to use IoT Hub message routing on body, we have to setup the content type and encoding set_content_result = msg.set_content_encoding_system_property("utf-8") set_content_type_result = msg.set_content_type_system_property("application/json") if set_content_result != 0: print("set_content_encoding_system_property FAILED") if set_content_type_result != 0: print("set_content_type_system_property FAILED") hub_manager.forward_event_to_output("obd", msg, 0) time.sleep(1) except IoTHubError as iothub_error: print ( "Unexpected error %s from IoTHub" % iothub_error ) return except KeyboardInterrupt: print ( "IoTHubModuleClient sample stopped" )
def send_to_Hub_callback(strMessage): message = IoTHubMessage(bytearray(strMessage, 'utf8')) # Setting these properties to allow routing of messages to other endpoints set_content_result = message.set_content_encoding_system_property("utf-8") set_content_type_result = message.set_content_type_system_property("application/json") if set_content_result != 0: print("set_content_encoding_system_property FAILED") if set_content_type_result != 0: print("set_content_type_system_property FAILED") hubManager.send_event_to_output("output1", message, 0)
def main(protocol): try: print("\nPython %s\n" % sys.version) print("IoT Hub Client for Python") hub_manager = HubManager(protocol) print("Starting the GPS locator module using protocol %s..." % hub_manager.client_protocol) print( "This module is now waiting for messages and will indefinitely. Press Ctrl-C to exit. " ) #start reading gps data... myGPSReader = GPSReader(GPSDeviceTtyAC) latestFixedTime = None while True: latestFixedPoint = myGPSReader.getLatestFixedGPSPoint() if latestFixedPoint is not None and latestFixedPoint.sentenceTimestamp != latestFixedTime: #new GPS point than previous one, send to iot hub jsonPayload = latestFixedPoint.buildJsonPayload(DEVICE_ID) print("Payload of new GPS data is: " + jsonPayload) msg = IoTHubMessage(bytearray(jsonPayload, 'utf8')) #in order to use IoT Hub message routing on body, we have to setup the content type and encoding set_content_result = msg.set_content_encoding_system_property( "utf-8") set_content_type_result = msg.set_content_type_system_property( "application/json") if set_content_result != 0: print("set_content_encoding_system_property FAILED") if set_content_type_result != 0: print("set_content_type_system_property FAILED") hub_manager.forward_event_to_output("gps", msg, 0) latestFixedTime = latestFixedPoint.sentenceTimestamp else: print(str(datetime.now()) + ": Not fixed yet....") time.sleep(1) except IoTHubError as iothub_error: print("Unexpected error %s from IoTHub" % iothub_error) return except KeyboardInterrupt: print("IoTHubModuleClient sample stopped")
def send_emotions(data): for i in range(len(data)): emotion = data[i]['faceAttributes']['emotion'] print('Sending emotion to IoT Hub') try: emotion = json.dumps(emotion) message = IoTHubMessage(emotion) set_content_result = message.set_content_encoding_system_property( "utf-8") set_content_type_result = message.set_content_type_system_property( "application/json") client.send_event_async(message, send_confirmation_callback, None) except Exception as e: print(e) return False time.sleep(5) return True