コード例 #1
0
ファイル: pir_pub.py プロジェクト: stevewoolley/iot
def pub(topic, value):
    if topic is not None and len(topic) > 0:
        for t in topic:
            publisher.publish(t,
                              json.dumps(
                                  {args.shadow_var: value, awsiot.MESSAGE: "{} {}".format(args.shadow_var, value)}))
    publisher.publish(awsiot.iot_thing_topic(args.thing), awsiot.iot_payload(awsiot.REPORTED, {args.shadow_var: value}))
コード例 #2
0
ファイル: dht_pub.py プロジェクト: stevewoolley/iot
def pub(temp, humid):
    if args.topic is not None and len(args.topic) > 0:
        for t in args.topic:
            publisher.publish(t,
                              json.dumps({"temperature": temp, "humidity": humid,
                                          awsiot.MESSAGE: "temperature: {} humidity: {}".format(temp, humid)}))
    publisher.publish(awsiot.iot_thing_topic(args.thing),
                      awsiot.iot_payload(awsiot.REPORTED, {'temperature': temp, 'humidity': humid}))
コード例 #3
0
def pub(dist):
    if args.topic is not None and len(args.topic) > 0:
        for t in args.topic:
            publisher.publish(
                t,
                json.dumps({
                    "distance": dist,
                    awsiot.MESSAGE: "distance: {}".format(dist)
                }))
    publisher.publish(awsiot.iot_thing_topic(args.thing),
                      awsiot.iot_payload(awsiot.REPORTED, {'distance': dist}))
コード例 #4
0
def callback(client, user_data, message):
    logging.debug("received {} {}".format(message.topic, message))
    distance = get_distance(args.trigger_pin, args.echo_pin, args.iterations)
    logging.info('median distance {} cm'.format(distance))
    if distance:
        for topic in args.topic:
            if args.min_value <= distance <= args.max_value:
                if awsiot.topic_search(topic, message.topic):
                    mqtt.publish(
                        awsiot.iot_thing_topic(args.thing),
                        awsiot.iot_payload(awsiot.REPORTED,
                                           {'distance': distance}))
                else:
                    logging.warning('Unrecognized command')
            else:
                logging.warning(
                    'calculated distance ({} cm) outside range {} - {}'.format(
                        distance, args.min_value, args.max_value))
    else:
        logging.error('unable to calculate distance')
コード例 #5
0
ファイル: supervisor_sub.py プロジェクト: stevewoolley/iot
def callback(client, user_data, message):
    logging.debug("received {} {}".format(message.topic, message))
    for topic in args.topic:
        cmd, arg = awsiot.topic_search(topic, message.topic)
        if cmd == 'getAllProcessInfo':
            logging.debug("command: {}".format(cmd))
            try:
                results = proxy.supervisor.getAllProcessInfo()
                logging.info("getAllProcessInfo {}".format(results))
                if args.thing:
                    supervised = []
                    for s in results:
                        supervised.append('{} ({})'.format(s['name'], s['statename']))
                    mqtt.publish(awsiot.iot_thing_topic(args.thing),
                                 awsiot.iot_payload(awsiot.REPORTED, {'supervised': ', '.join(supervised)}))
            except Exception as err:
                logging.error("supervisor getAllProcessInfo failed: {}".format(err))
        elif cmd == 'startProcess':
            logging.debug("command: {}".format(cmd))
            if arg:
                try:
                    proxy.supervisor.startProcess(arg)
                except Exception as err:
                    logging.error("supervisor startProcess {} failed {}".format(arg, err))
            else:
                logging.error('No argument: {}'.format(cmd, arg))
        elif cmd == 'stopProcess':
            logging.debug("command: {}".format(cmd))
            if arg:
                try:
                    proxy.supervisor.stopProcess(arg)
                except Exception as err:
                    logging.error("supervisor stopProcess {} failed {}".format(arg, err))
            else:
                logging.error('No argument: {}'.format(cmd, arg))
        else:
            logging.warning('Unrecognized command: {}'.format(cmd))
コード例 #6
0
ファイル: static_host_pub.py プロジェクト: stevewoolley/iot
                        level=args.log_level,
                        format=awsiot.LOG_FORMAT)

    publisher = awsiot.MQTT(args.endpoint, args.rootCA, args.cert, args.key)

    properties = {}
    mem = psutil.virtual_memory()
    disk = psutil.disk_usage('/')
    properties["bootTime"] = datetime.datetime.fromtimestamp(
        psutil.boot_time()).strftime(awsiot.DATE_FORMAT).strip()
    if platform.system() == 'Darwin':  # mac
        properties["release"] = platform.mac_ver()[0]
    elif platform.machine().startswith(
            'arm') and platform.system() == 'Linux':  # raspberry pi
        properties["distribution"] = "{} {}".format(platform.dist()[0],
                                                    platform.dist()[1])
        properties["hardware"] = "Pi Model {} V{}".format(
            pi_info().model,
            pi_info().pcb_revision)
    properties["hostname"] = platform.node()
    properties["machine"] = platform.machine()
    properties["system"] = platform.system()
    properties["totalDiskSpaceRoot"] = int(disk.total / (1024 * 1024))
    properties["cpuProcessorCount"] = psutil.cpu_count()
    properties["ramTotal"] = int(mem.total / (1024 * 1024))
    for iface in NET_INTERFACES:
        properties["{}IpAddress".format(iface)] = get_ip(iface)

    publisher.publish(awsiot.iot_thing_topic(args.thing),
                      awsiot.iot_payload(awsiot.REPORTED, properties))