Esempio n. 1
0
def main():
    mqtt_client = MqttLocalClient(client_id=MQTT_CLIENT_ID,
                                  host=IIoT.MQTT_HOST,
                                  port=IIoT.MQTT_PORT,
                                  subscription_paths=[
                                      SUBSCRIBED_MQTT_CHANNELS,
                                  ])
    mqtt_client.start()

    # Get the data from message_queue
    while True:
        # Waiting for a new message in the queue
        message = mqtt_client.message_queue.get()

        #Get the message topic and its payload
        topic = message.topic
        payload = message.payload
        json_payload = json.loads(payload)
        input_name = json_payload['data']['name']
        input_value = json_payload['data']['value']

        # Perform actions
        output_value = data_manipulation(input_value)

        # Publish data
        message = packOutputMessage(output_value)
        mqtt_client.publish(IIoT.MqttChannels.persist, json.dumps(message))
Esempio n. 2
0
def main():
    mqtt_client = MqttLocalClient(
                                  client_id          = MQTT_CLIENT_ID,
                                  host               = IIoT.MQTT_HOST,
                                  port               = IIoT.MQTT_PORT,
                                  subscription_paths = SUBSCRIBED_MQTT_CHANNELS
                                 )
    mqtt_client.start()

    # initialize ounter and mean var
    i = 0
    mean = 0

    while True:
        # Waiting for a new message in the queue
        message = mqtt_client.message_queue.get()

        #Get the message topic and its payload
        topic   = message.topic
        payload = message.payload
        json_payload = json.loads(payload)

        if json_payload['data']['name'] == VARIABLE_NAME:
            # perform calculation
            mean = mean + float(json_payload['data']['value'])
            i = i + 1

            if i == MEAN_SAMPLES:
                # the variable name containing mean is made by
                # original_var_name _ number_of_samples_ (seconds) _mean
                output_name = VARIABLE_NAME + "_" + str(MEAN_SAMPLES) + "_sec_mean"

                # compute the mean
                output_value = mean / MEAN_SAMPLES

                # reset counter and mean var
                i = 0
                mean = 0

                # pack the json publish message
                message = IIoT.packOutputMessage(output_name ,output_value)

                # publish the message to the mqtt broker
                mqtt_client.publish(OUTPUT_CHANNEL, json.dumps(message))