Ejemplo n.º 1
0
class MonitorPub:
    def __init__(self, config, id):
        self.logger = MessageLogger.get_logger(__name__, id)
        self.id = id
        self.config = config
        self.host = config.get("IO", "mqtt.host")
        self.port = config.getint("IO", "mqtt.port", fallback=1883)
        self.topic_params = json.loads(config.get("IO", "monitor.mqtt.topic"))
        self.host, host_params, self.qos, self.topic, self.port = ConfigParserUtils.extract_host_params(
            self.host, self.port, self.topic_params, self.config, None)
        self.mqtt = None
        self.init_mqtt()

    def init_mqtt(self):
        try:
            client_id = "client_publish" + str(randrange(100000)) + str(
                time.time()).replace(".", "")
            self.mqtt = MQTTClient(str(self.host),
                                   self.port,
                                   client_id,
                                   id=self.id,
                                   connect_check_flag=True)
            self.logger.info("successfully subscribed")
        except Exception as e:
            self.logger.error(e)

    def send_monitor_ping(self, control_frequency):
        try:
            if self.mqtt:
                msg = self.to_senml(control_frequency)
                self.mqtt.publish(self.topic, msg, qos=self.qos)
                self.logger.debug("published monitor ping")
            else:
                self.logger.warning("mqtt not initialized")
        except Exception as e:
            self.logger.error("Error sending monitor ping " + str(e))

    def to_senml(self, value):
        meas = senml.SenMLMeasurement()
        meas.name = self.id
        meas.value = value
        meas.time = int(time.time())
        doc = senml.SenMLDocument([meas])
        return json.dumps(doc.to_json())
Ejemplo n.º 2
0
class DataPublisher(ABC,threading.Thread):

    def __init__(self, internal, topic_params, config, publish_frequency, id=None):
        super().__init__()
        self.logger = MessageLogger.get_logger(__name__, id)
        self.internal = internal
        self.config = config
        self.channel = "MQTT"
        self.id = id
        self.logger.debug("id = " + str(self.id))
        if internal:
            self.channel = config.get("IO", "channel")
        if topic_params is None:
            self.topic_params = {}
        else:
            self.topic_params = topic_params
        self.publish_frequency = publish_frequency

        self.stopRequest = threading.Event()

        if self.channel == "MQTT":
            self.init_mqtt()
        elif self.channel == "ZMQ":
            self.init_zmq()

        self.logger.info("Initializing data publisher thread for topic " + str(self.topic_params))

    def init_mqtt(self):
        self.mqtt = None
        try:
            if "pub.mqtt.host" in dict(self.config.items("IO")):
                self.host = self.config.get("IO", "pub.mqtt.host")
            else:
                self.host = self.config.get("IO", "mqtt.host")
            self.port = self.config.get("IO", "mqtt.port")
            if "mqtt.port" in self.topic_params.keys():
                self.port = self.topic_params["mqtt.port"]
            if "qos" in self.topic_params.keys():
                self.qos = int(self.topic_params["qos"])
            else:
                self.qos = 1
            self.client_id = "client_publish" + str(randrange(100000)) + str(time.time()).replace(".","")
            self.mqtt = MQTTClient(str(self.host), self.port, self.client_id,
                                   username=self.config.get("IO", "mqtt.username", fallback=None),
                                   password=self.config.get("IO", "mqtt.password", fallback=None),
                                   ca_cert_path=self.config.get("IO", "mqtt.ca.cert.path", fallback=None),
                                   set_insecure=bool(self.config.get("IO", "mqtt.insecure.flag", fallback=False)),
                                   id=self.id)
        except Exception as e:
            self.logger.error(e)
            # error for mqtt will be caught at parent
            raise e

    def init_zmq(self):
        self.host = self.config.get("IO", "zmq.host")
        self.port = self.config.get("IO", "zmq.pub.port")
        self.zmq = ZMQClient(self.host, self.port, None)
        self.zmq.init_publisher(self.id)

    def join(self, timeout=None):
        super(DataPublisher, self).join(timeout)

    def Stop(self):
        self.logger.info("start data publisher thread exit")
        self.stopRequest.set()
        if self.channel == "MQTT" and self.mqtt is not None:
            self.mqtt.MQTTExit()
        elif self.channel == "ZMQ":
            self.zmq.stop()
        if self.isAlive():
            self.join(4)
        self.logger.info("data publisher thread exit")

    def run(self):
        """Get data from internet or any other source"""
        if "topic" not in self.topic_params.keys():
            fetch_topic = True
        else:
            fetch_topic = False
        topic = None
        while not self.stopRequest.is_set():
            if fetch_topic:
                data, topic = self.get_data()
            else:
                data = self.get_data()
            if data:
                self.data_publish(data, topic)
            time.sleep(self.publish_frequency)

    def data_publish(self, data, topic=None):
        if self.channel == "MQTT":
            self.mqtt_publish(data, topic)
        elif self.channel == "ZMQ":
            self.zmq_publish(data, topic)

    def mqtt_publish(self, data, topic=None):
        try:
            if topic is None:
                topic = self.topic_params["topic"]
            if self.internal:
                topic = topic + "/" + self.id
            self.logger.debug("Sending results to mqtt on this topic: " + topic)
            self.mqtt.publish(topic, data, True, self.qos)
            self.logger.debug("Results published")
        except Exception as e:
            self.logger.error(e)

    def zmq_publish(self, data, topic=None):
        if topic is None:
            topic = self.topic_params["topic"]
        if self.internal:
            topic = topic + "/" + self.id
        self.logger.debug("Sending results to zmq on this topic: " + topic)
        self.zmq.publish_message(topic, data)
        self.logger.debug("Results published")

    @abstractmethod
    def get_data(self):
        pass
Ejemplo n.º 3
0
                    step = (end - start) / 60
                    setattr(wd, col, start + step * j)
                weather.append(wd)

    @staticmethod
    def get_coordinate(city):
        """
        Get geocoordinate from City name
        :param city:
        :return: Union[Type[JSONDecoder], Any]
        """
        try:
            config = configparser.RawConfigParser()
            config.read("utils/ConfigFile.properties")
            googlekey = config.get("SolverSection", "googleapikey")
            request = requests.get(
                "https://maps.googleapis.com/maps/api/geocode/json?address=" +
                city + "&key=" + googlekey)
            text = request.json()
            return text["results"][0]["geometry"]["location"]
        except KeyError:
            return ""


we = Weather.get_weather("Bonn, Germany")
js = json.dumps([wea.__dict__ for wea in we], default=str)

m = MQTTClient("optiframework_mosquitto_1", 1883, "weatherClient")
m.publish("data/weather", str(js), True)
m.MQTTExit()