Ejemplo n.º 1
0
def pubtest(delay=0):
    sleep(delay)
    m = Message()
    m.msg = {"data": [["0", 1]], "timestamp": timestamp}
    r.publish("data", m.as_json())
    print m.as_json()

    return
Ejemplo n.º 2
0
    def cmd_via_redis_subscriber(self):
        self.Log.debug("cmd_via_redis_subscriber()")
        self.pubsub.subscribe(self.channel)

        while self._redis_subscriber_alive:
            try:
                for item in self.pubsub.listen():
                    self.Log.debug(item)
                    if item["data"] == "unsubscribe":
                        self.pubsub.unsubscribe()
                        self.Log.info("unsubscribed and finished")
                        break
                    else:
                        cmd = item["data"]
                        if isinstance(cmd, str):
                            self.Log.debug("cmd_via_redis_subscriber() cmd = {}".format(cmd))

                            try:
                                cmd_obj = deserialize(cmd)
                                if isinstance(cmd_obj, dict):
                                    self.Log.debug(cmd_obj)
                                    cmd_str = cmd_obj["cmd"]
                                    if cmd_str == "GetStatusOfAllDevices":
                                        res = self.GetStatusOfAllDevices()
                                        final_data = dict()
                                        timestamp = datetime.now().strftime("%Y-%m-%d-%H:%M:%S")
                                        final_data["timestamp"] = timestamp
                                        final_data["raw"] = res
                                        final_data["data"] = res
                                        M = Message("InsteonPLM")
                                        M.msg = final_data
                                        self.redis.publish("data", M.as_json())
                                    if cmd_str == "GetLightLevel":
                                        addr_str = cmd_obj["addr"]
                                        res = self.GetLightLevel(addr_str)
                                    if cmd_str == "SetLightLevel":
                                        addr_str = cmd_obj["addr"]
                                        val = cmd_obj["val"]
                                        res = self.SetLightLevel(addr_str, val)
                                    if cmd_str == "SetSwOn":
                                        addr_str = cmd_obj["addr"]
                                        res = self.SetSwOn(addr_str)
                                    if cmd_str == "SetSwOff":
                                        addr_str = cmd_obj["addr"]
                                        res = self.SetSwOff(addr_str)

                                else:
                                    addr = str2hex(cmd_obj[0])
                                    insteon_cmd = cmd_obj[1]
                                    val = cmd_obj[2]
                                    res = self.send_sd_cmd(addr, insteon_cmd, val)
                                    self.Log.debug(res)
                                    self.redis.publish(self.channel + "_res", serialize(res))

                            except Exception as E:
                                error_msg = {
                                    "source": "serinsteon:cmd_via_redis_subscriber",
                                    "function": "def run(self):",
                                    "error": E.message,
                                }
                                self.Log.error(error_msg)

                        else:
                            self.Log.debug(cmd)
            except Exception as E:
                error_msg = {"source": "InsteonSub", "function": "def run(self):", "error": E.message}
                self.Log.error(error_msg)
        self.Log.debug("end of cmd_via_redis_subscriber()")