Exemple #1
0
    def subscribe(cls, topic, callback, use_prefix=True):
        topic = MQTT.prefix(topic, use_prefix)

        ct.print_debug("subscribing topic {}".format(topic))

        subscription = cls.subscriptions.get(topic, None)

        # if we allready are subscribed to this, just add another callback
        if subscription is not None:
            callbacks = subscription["callbacks"]
            if callback in callbacks:
                ct.print_warning(
                    "Callback allready exists for this topic, doing nothing")
            else:
                ct.print_debug(
                    "adding callback to existing subscription {}".format(
                        topic))
                subscription["callbacks"].append(callback)
            return subscription

        cls.subscriptions[topic] = {"topic": topic, "callbacks": [callback]}

        ct.print_info("Added subscription for {}".format(topic))
        if cls.connected:
            try:
                print("~~~ MQTT subscribe on {0}".format(topic))
                cls.client.subscribe(topic)
            except Exception as e:
                ct.format_exception(e, "subscription failed")
                cls.set_connected(False)
        return subscription
Exemple #2
0
    def callback(cls, topic, msg, unjson=True):
        cls.callback_called = True
        topic = topic.decode("utf-8")
        if unjson:
            try:
                msg = ujson.loads(msg)
            except Exception as e:
                # Don't pass non-JSON payloads around.
                ct.format_exception(
                    e, "<!- MQTT {0} non-JSON payload rejected: {1}".format(
                        topic, msg))
                return

        ct.print_debug("Received MQTT message for {}".format(topic))
        if topic.endswith('/config'):
            # Config is too large for the memory.
            ct.print_debug("<-- MQTT {0}: config".format(topic))
        else:
            ct.print_debug("<-- MQTT {0}: {1}".format(topic, msg))

        cls.cache[topic] = msg
        subscription = cls.subscriptions[topic]
        for callback in subscription["callbacks"]:
            try:
                ct.print_debug("callback")
                callback(topic, msg)
            except Exception as e:
                ct.format_exception(
                    e, "Callback {0} failed: {1}".format(subscription))
Exemple #3
0
 def eval(self):
     try:
         val = eval(self.python, self.expr_globals, self.expr_locals)
     except Exception as e:
         ct.format_exception(e, "evaluation of expression failed") 
         ct.print_error(str(self.python))
         val = 0
     self.output.value = val
Exemple #4
0
 def start(self, interval_ms):
     interval_ms = int(interval_ms)
     self.interval = interval_ms
     try:
         self.timer = Timer(-1)
         self.timer.init(period=interval_ms, mode=Timer.PERIODIC, callback=self.tick)
     except Exception as e:
         ct.format_exception(e, "could not initialize timer")
Exemple #5
0
 def read_temp(self, ds):
     ct.print_debug("reading temperatures from {} for {}".format(
         self.hex_address(), self.signal.name))
     temperature = -272.0
     try:
         temperature = ds.read_temp(self.address)
         if self.signal is not None:
             self.signal.value = round(temperature, 2)
     except Exception as e:
         ct.format_exception(
             e,
             "Exception in ds18x20 update for {}".format(self.signal.name))
     return temperature
Exemple #6
0
    def netlist_from_config(cls, config):
        for compname, compconfig in config.items():
            modname = "components." + compname
            classname = "".join(word[0].upper() + word[1:] for word in compname.split("_"))
            #output heading for each part type in blue
            ct.print_heading("Initializing component: {} (import {} from {})".format(compname, classname, modname))

            if compconfig is None:
                ct.print_warning("Component list for {} is empty".format(compname))
                continue

            try:
                imported = __import__("components." + compname, globals(), locals(), [classname])
            except Exception as e:
                ct.format_exception(e, "Import failed")
                continue

            try:
                cls = getattr(imported, classname)
            except Exception as e:
                ct.format_exception(e, "getattr failed:")
                continue

            try:
                cls.boot(compconfig)
                ct.print_info("booted {}".format(cls.__name__))
            except Exception as e:
                ct.format_exception(e, "Class boot failed:")
                raise e
                continue
        cls.first_eval_all()