Exemplo n.º 1
0
def main():
    POWER_OFF()
    userdata = {
        'timer': machine.Timer(-1),
        'timer_end': 0,
        'timer_start': utime.time(),
    }
    client = mqtt.Client(cfg['CLIENT_ID_PREFIX'] + hostname(),
                         clean_session=True,
                         userdata=userdata)
    client.host = cfg['MQTT_HOST']
    client.port = 1883
    client.keepalive = 60
    client.on_message = callback
    exception_count = 0
    while True:
        try:
            run(client)
        except KeyboardInterrupt:
            break
        except Exception as e:
            exception_count += 1
            print(e)
            if exception_count >= 3:
                print('Enabling fail because exceptions')
                fail_mode()
            if exception_count % 10 == 0:
                nw_config()
            utime.sleep(5)
        else:
            exception_count = 0
Exemplo n.º 2
0
 def do_connect(self, args):
     self.client = mqtt.Client(args[2])
     self.client.on_connect = self.on_connect
     self.client.on_message = self.on_message
     self.client.on_disconnect = self.on_disconnect
     if len(args) >= 5:
         self.client.username_pw_set(args[3], args[4])
     if len(self.will_topic) > 0:
         self.client.will_set(self.will_topic, self.will_payload)
     try:
         self.client.connect(args[0], int(args[1]))
         self.client.loop_start()
     except:
         self.send_command("r;")
Exemplo n.º 3
0
 def run(self):
     print("MQTT: Running")
     try:
         if wifi_is_connected():
             if self.lost_connection:
                 self.mqtt_client = mqtt.Client('HUB')
                 self.lost_connection = False
                 # try:
             print("MQTT: Attempting publish")
             measurement = self.sensor.get_measurement()
             message = {
                 'id': self.state.get_value('id'),
                 'measurement': measurement
             }
             print('Publishing temperature')
             self.mqtt_client.publish(topic='sensors/' + self.type,
                                      msg=ujson.dumps(message))
             utime.sleep_ms(1000)
         else:
             print("MQTT: No WiFi")
             self.lost_connection = True
     except Exception as e:
         print('MQTT Crashed: ' + str(e))
         return
Exemplo n.º 4
0
def main():
    env = dict()
    env["stream"] = os.getenv("stream_key")
    env["source"] = os.getenv("transcoding_source")
    env["sink"] = os.getenv("transcoding_sink")
    env["type"] = os.getenv("type", "all")
    env["vaapi_dev"], env["vaapi_driver"], env["vaapi_features"] = select_vaapi_dev()

    parser = argparse.ArgumentParser(description="Transcode voc stream")
    parser.add_argument("--stream", help="stream key")
    parser.add_argument("--source", help="transcoding input")
    parser.add_argument("--sink", help="transcoding sink host")
    parser.add_argument("--type", help="transcode type",
                        choices=["h264-only", "all"])
    parser.add_argument("-o", "--output", help="output type",
                        default="icecast", choices=["icecast", "null"])
    parser.add_argument("-progress", help="pass to ffmpeg progress")
    parser.add_argument("--vaapi-features", action="append",
                        help="override vaapi features")
    parser.add_argument("--vaapi-device", help="override vaapi device")
    parser.add_argument("-v", "--verbose", help="set verbosity",
                        default="warning")
    parser.add_argument("--restart", help="restart on failure",
                        default=False, action="store_true")
    args = parser.parse_args()

    # override environment with arguments
    enable_mqtt = True
    check_arg(env, "output", args.output)
    if env["output"] == "null":
        enable_mqtt = False
        env["stream"] = ""
        env["sink"] = ""
    check_arg(env, "stream", args.stream)
    check_arg(env, "source", args.source)
    check_arg(env, "sink", args.sink)
    check_arg(env, "type", args.type)

    # handle external ffmpeg-progress
    progress = ""
    if args.progress is not None:
        print("using progress", args.progress)
        progress = f"-progress {args.progress}"

    # override vaapi variables
    if args.vaapi_device is not None:
        print("override vaapi device", args.vaapi_device)
        env["vaapi_dev"] = args.vaapi_device

    if args.vaapi_features is not None:
        print("override", args.vaapi_features)
        env["vaapi_features"] = args.vaapi_features

    print("using vaapi driver", env["vaapi_driver"])
    env_vars = {"LIBVA_DRIVER_NAME": env["vaapi_driver"]}
    os.environ.update(env_vars)

    with mqtt.Client(enable_mqtt) as client:
        client.info(f"Transcoding for {env['stream']} started…")
        try:
            mainloop(env, args.restart, progress, args.verbose)
        except ExitException:
            pass
        client.info(f"Transcoding for {env['stream']} stopped…")