def build_heartbeat_from_form_data(form): now = date_time_utils.timestamp() #Required fields source = form['source'] type = message_types.EVENT_HEARTBEAT data = {} return Message(source=source, type=type, data=data, received_timestamp=now)
def build_message_from_form_data(form): now = date_time_utils.timestamp() #Required fields source = form['source'] type = form['type'] #Optional fields target = form.get('target') timestamp = form.get('timestamp', now) data = json.loads(form.get('data', default="{}")) return Message(source=source, type=type, target=target, data=data, timestamp=timestamp, received_timestamp=now)
def build_message_from_json(message_json): now = date_time_utils.timestamp() #Required fields source = message_json['source'] type = message_json['type'] #Optional fields target = message_json.get('target') timestamp = message_json.get('timestamp', now) data = message_json.get('data', {}) return Message(source=source, type=type, target=target, data=data, timestamp=timestamp, received_timestamp=now)
def build_reading_from_form_data(form): now = date_time_utils.timestamp() #Required fields source = form['source'] type = message_types.EVENT_READING + "." + form['type'] value = form['value'] #Optional fields units = form.get('units') raw_value = form.get('raw') data = {"value" : value} __put_optional(data, "units", units) __put_optional(data, "raw", raw_value) return Message(source=source, type=type, data=data, timestamp=now, received_timestamp=now)
cursor = connection.cursor() cursor.execute(sql, values) connection.commit() cursor.close() connection.close() if __name__ == "__main__": messagebus.util.log_utils.init() mysql_store = MySqlMessageStore(username='******', password=sys.argv[1], host="127.0.0.1", database="messagebus") message_uuid = uuid.uuid4() now = date_time_utils.timestamp() id = str(uuid.uuid4()) #message = Message(id=id, type="event.test", source="foo.bar", data={"hello" : "world"}, timestamp=now, received_timestamp=now) #mysql_store.save_message(message) data = {"value" : 100, "raw" : 50} message = Message(id=id, type="event.device.sensor.reading.test", source="foo.bar", data=data, timestamp=now, received_timestamp=now) mysql_store.save_reading(message) print "Done"
parser = argparse.ArgumentParser(description='Read sensor readings and pass them to the messagebus.') parser.add_argument('-u', '--url', action="store", default=DEFAULT_URL, help='Messagebus URL') parser.add_argument("-t", "--type", action="store", required=True, help="Message type") parser.add_argument("-s", "--source", action="store", required=True, help="Message source") parser.add_argument("-d", "--data", action="store", default="{}", help="Message Data (JSON)") parser.add_argument("-r", "--retries", action="store", default=5, type=int, help="Number of Retries") args = parser.parse_args() logging.debug(args) client = MessageBusClient(args.url, max_retries=args.retries) payload = json.loads(args.data) message = Message(source=args.source, type=args.type, data=payload, timestamp=date_time_utils.timestamp()) start_time = time.time() try: id = client.send_message(message) logging.debug("Sent event {}".format(id)) finally: duration = time.time() - start_time logging.debug("Duration: %s seconds", duration)