def callback(client, userdata, message): print("sub {} {}".format(message.topic, message.payload)) if message.topic == args.topic: # base topic print("base topic {}".format(message.topic)) else: # sub topic cmd, arg, arg2 = piot.topic_parser(args.topic, message.topic) print("sub topic {} {}".format(cmd, arg))
def callback(client, user_data, message): logger.debug("callback message {} {}".format(message.topic, message.payload)) cmd, arg, arg2 = piot.topic_parser(args.topic, message.topic) logger.info("callback {} {}".format(cmd, arg)) if cmd in piot.TOPIC_STATUS_PULSE: device(1) elif cmd in piot.TOPIC_STATUS_ON: device(-1) elif cmd in piot.TOPIC_STATUS_OFF: device(0) else: logger.warning('callback unrecognized command: {}'.format(cmd))
def callback(client, userdata, message): docker_client = docker.from_env() logger.debug("message topic {} payload {}".format(message.topic, message.payload)) cmd, arg, arg2 = piot.topic_parser(args.topic, message.topic) logger.info("callback {}".format(cmd)) if cmd == 'status': try: logger.info('status {} {} {}'.format( args.image, args.docker_container_name, docker_client.containers.get( args.docker_container_name).status)) except Exception as e: logger.info('status {} {} unknown'.format( args.image, args.docker_container_name)) elif cmd == 'start': try: result = docker_client.containers.run( args.image, detach=True, name=args.docker_container_name, remove=True, devices=DEVICES, environment=[ "stream={}".format(args.stream), "aws_access_key={}".format(args.aws_access_key), "aws_secret_key={}".format(args.aws_secret_key), "aws_region={}".format(args.aws_region) ], volumes=VOLUMES) logger.info('start {} {} {}'.format(args.image, args.docker_container_name, result.status)) except Exception as e: logger.info('start failed {} {} {}'.format( args.image, args.docker_container_name, e.message)) elif cmd == 'stop': try: docker_client.containers.get(args.docker_container_name).stop() logger.info('stop {} {}'.format(args.image, args.docker_container_name)) except Exception as e: logger.warning('stop failed {} {} {}'.format( args.image, args.docker_container_name, e.message)) else: logger.warning("invalid command {}".format(cmd))
def callback(client, userdata, message): logger.debug("message topic {} payload {}".format(message.topic, message.payload)) cmd, arg, arg2 = piot.topic_parser(args.topic, message.topic) logger.info("{} {} {}".format(cmd, arg, arg2)) if cmd == 'status' and arg is None: status() elif cmd == 'restart' and arg is None and arg2 is None: restart() elif cmd == 'start' and arg is not None: start(arg) elif cmd == 'stop' and arg is not None: stop(arg) elif cmd == 'pulse' and arg is not None and arg2 is not None: pulse(arg, arg2) else: logging.error('invalid command {} {}'.format(cmd, arg))