def handle(self, client, userdata, message): payload = json.loads(message.payload.decode(), object_pairs_hook=OrderedDict) pub = Publication(message.topic, payload) try: self.__comms.connect() self.__comms.write(JSONify.dumps(pub), False) except ConnectionRefusedError: if self.__verbose: print("AWSMQTTHandler: connection refused for %s" % self.__comms.address, file=sys.stderr) sys.stderr.flush() finally: self.__comms.close() if self.__echo: print(JSONify.dumps(pub)) sys.stdout.flush() if self.__verbose: print("received: %s" % JSONify.dumps(pub), file=sys.stderr) sys.stderr.flush()
def handle(self, client, userdata, message): payload = message.payload.decode() payload_jdict = json.loads(payload) publication = Publication(message.topic, payload_jdict) try: self.__comms.connect() self.__comms.write(JSONify.dumps(publication), False) except ConnectionError: self.__reporter.print("handle: ConnectionError: %s" % self.__comms) finally: self.__comms.close() if self.__echo: # print(JSONify.dumps(publication), file=sys.stderr) # sys.stderr.flush() self.__reporter.print("received: %s" % JSONify.dumps(publication))
# run... # publish... pub_comms.connect() for message in pub_comms.read(): try: datum = json.loads(message, object_pairs_hook=OrderedDict) except ValueError: handler.print_status("bad datum: %s" % message) continue success = False while True: publication = Publication.construct_from_jdict(datum) try: success = client.publish(publication, ClientAuth.MQTT_TIMEOUT) if not success: handler.print_status("abandoned") break except Exception as ex: if cmd.verbose: print(JSONify.dumps(ExceptionReport.construct(ex))) sys.stderr.flush()
def on_topic_message_handler(cls, subscriber, msg): payload = msg.payload.decode() payload_jdict = json.loads(payload, object_pairs_hook=OrderedDict) subscriber.handler(Publication(subscriber.topic, payload_jdict))
client.connect(auth) while True: # cmd... if cmd.interactive: line = StdIO.prompt(device_tag + ' > ') cmd_tokens = line.split() if len(line) > 0 else None else: cmd_tokens = cmd.cmd_tokens # datum... now = LocalizedDatetime.now() datum = ControlDatum.construct(host_tag, device_tag, now, cmd_tokens, key) publication = Publication(topic, datum) handler.set(publication) if cmd.verbose: print(datum, file=sys.stderr) sys.stderr.flush() # publish... client.publish(publication) # subscribe... timeout = time.time() + cmd.timeout if cmd.receipt or cmd.interactive: while True:
topic = "/orgs/south-coast-science-dev/development/device/alpha-pi-eng-000006/control" print("topic:%s" % topic) message = "python message " print("message:%s" % message) print("-") # ------------------------------------------------------------------------------------------------------------ # resources... client = MQTTClient() client.connect(host, client_id, username, password) print(client) print("-") # -------------------------------------------------------------------------------------------------------------------- # run... for i in range(10): payload = {'msg': message + str(i)} success = client.publish(Publication(topic, payload), 10.0) print("success: %s payload: %s" % (success, payload)) time.sleep(1) client.disconnect()
for line in sys.stdin: try: jdict = json.loads(line, object_pairs_hook=OrderedDict) except ValueError: continue if cmd.override: payload = OrderedDict({'__timestamp': jdict['rec']}) payload.update(jdict) else: payload = jdict # time.sleep(1) publication = Publication(topic, payload) print(JSONify.dumps(publication)) sys.stdout.flush() # ---------------------------------------------------------------------------------------------------------------- # end... except KeyboardInterrupt: if cmd.verbose: print("osio_topic_publisher: KeyboardInterrupt", file=sys.stderr) except Exception as ex: print(JSONify.dumps(ExceptionReport.construct(ex)), file=sys.stderr)
def on_topic_message_handler(cls, subscriber, msg): payload = msg.payload.decode() payload_jdict = json.loads(payload) subscriber.handler(Publication(subscriber.topic, payload_jdict))
# ------------------------------------------------------------------------------------------------------------ # run... handler = AWSMQTTHandler() # TODO: WTF? client.connect(endpoint, credentials) pub_comms.connect() for message in pub_comms.read(): try: jdict = json.loads(message, object_pairs_hook=OrderedDict) except ValueError: continue publication = Publication.construct_from_jdict(jdict) client.publish(publication) if cmd.verbose: print("%s: mqtt: done" % LocalizedDatetime.now().as_iso8601(), file=sys.stderr) sys.stderr.flush() if cmd.echo: print(message) sys.stdout.flush() # ---------------------------------------------------------------------------------------------------------------- # end...
client.connect(ClientAuth.MQTT_HOST, client_auth.client_id, client_auth.user_id, client_auth.client_password) while True: # cmd... if cmd.interactive: line = StdIO.prompt(cmd.device_tag + ' > ') cmd_tokens = line.split() if len(line) > 0 else None else: cmd_tokens = cmd.cmd_tokens # datum... now = LocalizedDatetime.now() datum = ControlDatum.construct(tag, cmd.device_tag, now, cmd_tokens, cmd.device_shared_secret) publication = Publication(cmd.device_topic, datum) handler.set(publication) if cmd.verbose: print(datum, file=sys.stderr) sys.stderr.flush() # publish... while True: try: success = client.publish(publication, ClientAuth.MQTT_TIMEOUT) if not success: print("osio_mqtt_control: abandoned", file=sys.stderr) sys.stderr.flush()
while True: # cmd... if cmd.interactive: line = StdIO.prompt(cmd.device_tag + ' > ') cmd_tokens = line.split() if len(line) > 0 else None else: cmd_tokens = cmd.cmd_tokens # datum... now = LocalizedDatetime.now() datum = ControlDatum.construct(tag, cmd.device_tag, now, cmd_tokens, cmd.device_host_id) publication = Publication(cmd.topic, datum) handler.set(publication) if cmd.verbose: print(datum, file=sys.stderr) sys.stderr.flush() # publish... while True: try: success = client.publish(publication, ClientAuth.MQTT_TIMEOUT) if not success: print("abandoned", file=sys.stderr)
# ---------------------------------------------------------------------------------------------------------------- # cmd... cmd = CmdAWSTopicPublisher() if cmd.verbose: print("aws_topic_publisher: %s" % cmd, file=sys.stderr) try: # ------------------------------------------------------------------------------------------------------------ # run... for line in sys.stdin: try: jdict = json.loads(line) except ValueError: continue publication = Publication(cmd.topic, jdict) print(JSONify.dumps(publication)) sys.stdout.flush() # ---------------------------------------------------------------------------------------------------------------- # end... except KeyboardInterrupt: if cmd.verbose: print("aws_topic_publisher: KeyboardInterrupt", file=sys.stderr)