Ejemplo n.º 1
0
def RenderController(controller: Controller):
    out = {
        "controller_basics": "<pre>%s</pre>" % controller.StringBasic(),
        "controller_routes": "<pre>%s</pre>" % controller.StringRoutes(),
        "controller_apis": "<pre>%s</pre>" % controller.props.StringApis(),
    }
    return out
Ejemplo n.º 2
0
def main():
    parser = argparse.ArgumentParser(description='Process some integers.')
    parser.add_argument(
        '--serial_port',
        type=str,
        default="/dev/ttyUSB0",
        help='The USB serial device representing the Z-Wave controller stick. '
        + 'Common settings are: dev/ttyUSB0, dev/ttyACM0')

    parser.add_argument('--verbosity',
                        type=int,
                        default=logging.ERROR,
                        help='Lower numbers mean more verbosity')

    parser.add_argument(
        '--curses',
        default=False,
        action='store_true',
        help='Use curses for rendering. Make sure your terminal '
        'is at least 200 chars wide.')

    args = parser.parse_args()
    # note: this makes sure we have at least one handler
    logging.basicConfig(level=logging.ERROR)
    logger = logging.getLogger()
    logger.setLevel(args.verbosity)

    for h in logger.handlers:
        h.setFormatter(MyFormatter())

    logging.info("opening serial: [%s]", args.serial_port)
    device = MakeSerialDevice(args.serial_port)

    driver = Driver(device)
    controller = Controller(driver, pairing_timeout_secs=60)
    controller.Initialize()
    success = controller.WaitUntilInitialized(2)
    if not success:
        logging.error("could not initialize controller")
        driver.Terminate()
        print(list(threading.enumerate()))
        return 1
    controller.UpdateRoutingInfo()
    time.sleep(1)
    Banner("Initialized Controller")
    print(controller)
    if args.curses:
        try:
            curses.wrapper(InitializeDevices, driver, controller)
        except Exception as e:
            Banner(str(e))
    else:
        InitializeDevices(None, driver, controller)
    driver.Terminate()
    return 0
Ejemplo n.º 3
0
def main():
    global DRIVER, CONTROLLER, TRANSLATOR, NODESET, DB
    # note: this makes sure we have at least one handler
    # logging.basicConfig(level=logging.WARNING)
    # logging.basicConfig(level=logging.ERROR)

    tornado.options.parse_command_line()
    logger = logging.getLogger()
    logger.setLevel(logging.INFO)
    logger.setLevel(logging.WARNING)
    #logger.setLevel(logging.ERROR)
    for h in logger.handlers:
        h.setFormatter(MyFormatter())

    logging.info("opening shelf: %s", OPTIONS.db)
    DB = Db(OPTIONS.db)

    application = tornado.web.Application(
        HANDLERS,
        # debug=True,
        task_pool=multiprocessing.Pool(OPTIONS.tasks),
        # map static/xxx to Static/xxx
        static_path="Static/",
    )

    logging.info("opening serial")
    device = MakeSerialDevice(OPTIONS.serial_port)

    DRIVER = Driver(device)
    CONTROLLER = Controller(DRIVER,
                            pairing_timeout_secs=OPTIONS.pairing_timeout_secs)
    CONTROLLER.Initialize()
    CONTROLLER.WaitUntilInitialized()
    CONTROLLER.UpdateRoutingInfo()
    DRIVER.WaitUntilAllPreviousMessagesHaveBeenHandled()
    print(CONTROLLER)
    TRANSLATOR = CommandTranslator(DRIVER)
    NODESET = Nodeset(TRANSLATOR, CONTROLLER.GetNodeId())

    cp = CONTROLLER.props.product
    NODESET.put(CONTROLLER.GetNodeId(), time.time(),
                z.ManufacturerSpecific_Report, {
                    'manufacturer': cp[0],
                    'type': cp[1],
                    'product': cp[2]
                })
    # The updater will do the initial pings of the nodes
    TRANSLATOR.AddListener(NodeUpdater())
    logging.warning("listening on port %d", OPTIONS.port)
    application.listen(OPTIONS.port)
    tornado.ioloop.IOLoop.instance().start()
    return 0
Ejemplo n.º 4
0
def InitController(args, update_routing=False) -> Tuple[Driver, Controller]:
    logging.info("opening serial: [%s]", args.serial_port)
    device = MakeSerialDevice(args.serial_port)

    driver = Driver(device)
    controller = Controller(driver, pairing_timeout_secs=args.pairing_timeout_sec)
    controller.Initialize()
    controller.WaitUntilInitialized()
    if update_routing:
        controller.UpdateRoutingInfo()
        driver.WaitUntilAllPreviousMessagesHaveBeenHandled()
    print(controller.StringBasic())
    if update_routing:
        print(controller.StringRoutes())
    # print(controller.props.StringApis())
    return driver, controller
Ejemplo n.º 5
0
def main():
    global DRIVER, CONTROLLER, TRANSLATOR, NODESET, DB
    tornado.options.parse_command_line()
    # use --logging command line option to control verbosity
    # logging.basicConfig(level=logging.WARNING)
    logger = logging.getLogger()
    for h in logger.handlers:
        h.setFormatter(MyFormatter())

    # used to persist certain settings like node names
    logging.info("opening shelf: %s", OPTIONS.db)
    DB = Db(OPTIONS.db)

    application = tornado.web.Application(_HANDLERS, **_SETTINGS)

    logging.info("opening serial")
    device = MakeSerialDevice(OPTIONS.serial_port)

    DRIVER = Driver(device)
    CONTROLLER = Controller(DRIVER,
                            pairing_timeout_secs=OPTIONS.pairing_timeout_secs)
    CONTROLLER.Initialize()
    CONTROLLER.WaitUntilInitialized()
    CONTROLLER.UpdateRoutingInfo()
    DRIVER.WaitUntilAllPreviousMessagesHaveBeenHandled()
    print(CONTROLLER)
    TRANSLATOR = CommandTranslator(DRIVER)
    NODESET = Nodeset(TRANSLATOR, CONTROLLER.GetNodeId())

    cp = CONTROLLER.props.product
    NODESET.put(CONTROLLER.GetNodeId(), time.time(),
                z.ManufacturerSpecific_Report, {
                    'manufacturer': cp[0],
                    'type': cp[1],
                    'product': cp[2]
                })
    for n in CONTROLLER.nodes:
        TRANSLATOR.Ping(n, 3, False, "refresher")
    updater = NodeUpdater()
    TRANSLATOR.AddListener(updater)
    logging.warning("listening on port %d", OPTIONS.port)
    application.listen(OPTIONS.port)
    tornado.ioloop.PeriodicCallback(updater.Periodic, 2000).start()
    tornado.ioloop.IOLoop.instance().start()
    return 0
Ejemplo n.º 6
0
def main():
    global driver, controller, translator, nodeset

    parser = argparse.ArgumentParser(description='Process some integers.')

    parser.add_argument(
        '--serial_port',
        type=str,
        default="/dev/ttyUSB0",
        help='The USB serial device representing the Z-Wave controller stick. ' +
        'Common settings are: dev/ttyUSB0, dev/ttyACM0')
    parser.add_argument('--mqtt_broker_host', type=str,
                        default="localhost",
                        help='mqtt broker host')
    parser.add_argument('--mqtt_broker_port', type=int,
                        default=1883,
                        help='mqtt broker port')
    parser.add_argument('--verbosity', type=int,
                        default=30,  # = logging.WARNING
                        help='Lower numbers mean more verbosity')

    args = parser.parse_args()
    # note: this makes sure we have at least one handler
    logging.basicConfig(level=args.verbosity)
    logger = logging.getLogger()
    logger.setLevel(args.verbosity)
    for h in logger.handlers:
        h.setFormatter(MyFormatter())

    logging.warning("opening serial: [%s]", args.serial_port)
    device = MakeSerialDevice(args.serial_port)

    driver = Driver(device)

    logging.warning("controller initializing")
    controller = Controller(driver, pairing_timeout_secs=60)
    controller.Initialize()
    controller.WaitUntilInitialized()
    controller.UpdateRoutingInfo()
    time.sleep(2)
    logging.warning("controller initialized:\n" + str(controller))

    translator = CommandTranslator(driver)
    nodeset = Nodeset(translator, controller.GetNodeId())

    def on_connect(client, _userdata, _rc, _dummy):
        logging.warning("Initialized MQTT client")
        logging.warning("Pinging %d nodes", len(controller.nodes))
        for n in controller.nodes:
            translator.Ping(n, 5, False, "initial")
            time.sleep(0.5)
            client.subscribe("zwave_out/%d/#" % controller.props.home_id)

    def on_message(client, _userdata, msg):
        tokens = msg.topic.split("/")
        key_int = STRING_TO_SUBCMD.get(tokens[3])
        if key_int is None:
            logging.error("unknown command: %s", tokens[3])
        key = ((key_int >> 8) & 255, key_int & 255)
        n = int(tokens[2])
        values = json.loads(msg.payload)
        logging.warning(
            "command received: %d [%s] %s",
            n,
            tokens[3],
            msg.payload)
        translator.SendCommand(n, key, values, NodePriorityHi(n), XMIT_OPTIONS)
        # print(n, key, data)

    logging.info("Initializing MQTT client")
    client = mqtt.Client("zwave-client")
    client.on_connect = on_connect
    client.on_message = on_message

    translator.AddListener(EventListener(controller.props.home_id, client))
    client.connect(
        args.mqtt_broker_host,
        port=args.mqtt_broker_port,
        keepalive=60)
    client.loop_forever()

    driver.Terminate()
    return 0
Ejemplo n.º 7
0
def main():
    global driver, controller, translator, nodeset

    parser = argparse.ArgumentParser(description='Process some integers.')

    parser.add_argument(
        '--serial_port',
        type=str,
        default="/dev/ttyUSB0",
        help='The USB serial device representing the Z-Wave controller stick. '
        + 'Common settings are: dev/ttyUSB0, dev/ttyACM0')

    parser.add_argument('--verbosity',
                        type=int,
                        default=30,
                        help='Lower numbers mean more verbosity')

    args = parser.parse_args()
    # note: this makes sure we have at least one handler
    logging.basicConfig(level=logging.ERROR)
    logger = logging.getLogger()
    logger.setLevel(args.verbosity)
    for h in logger.handlers:
        h.setFormatter(MyFormatter())

    logging.info("opening serial: [%s]", args.serial_port)
    device = MakeSerialDevice(args.serial_port)

    driver = Driver(device)
    controller = Controller(driver, pairing_timeout_secs=60)
    controller.Initialize()
    controller.WaitUntilInitialized()
    controller.UpdateRoutingInfo()
    time.sleep(2)
    Banner("Initialized Controller")
    print(controller)

    translator = CommandTranslator(driver)
    nodeset = Nodeset(translator, controller.GetNodeId())
    translator.AddListener(TestListener())
    # n.InitializeExternally(CONTROLLER.props.product, CONTROLLER.props.library_type, True)

    logging.info("Pinging %d nodes", len(controller.nodes))
    for n in controller.nodes:
        translator.Ping(n, 5, False, "initial")
        time.sleep(0.5)

    logging.info("Waiting for all nodes to be interviewed")
    not_ready = controller.nodes.copy()
    not_ready.remove(controller.GetNodeId())
    while not_ready:
        interviewed = set()
        for n in not_ready:
            node = nodeset.GetNode(n)
            if node.IsInterviewed():
                interviewed.add(node)
        time.sleep(2.0)
        for node in interviewed:
            Banner("Node %s has been interviewed" % node.n)
            print(node)
            not_ready.remove(node.n)
            if not_ready:
                print("\nStill waiting for %s" % str(not_ready))
    driver.Terminate()
    return 0