Exemplo n.º 1
0
def sendMQTTPayload(data,jsonpayload):
    
    jsonPayloadArray.append(jsonpayload)
    if len (jsonPayloadArray) > 50:
        del jsonPayloadArray[0]
                    
    global iothubdevices
    global globMQTTClient
    deviceId = data._TCPClient.target.get_aliashost() 
    # decodeddata = data._payload.decode("utf-8") 
    # jsonpayload = json.loads(data._payload)

    acttime = local_now()
     
    if globMQTTClient!=None:  
        iotDevice = iothubdevices.get(deviceId)
    
        if iotDevice==None: 
            iotDevice =iotHubDevice(net_name='mh', devideid= deviceId, additionalData={'type': 'iotHub', },)
            iothubdevices[deviceId] = iotDevice
            globMQTTClient.publish(iotDevice.info_topic(), machine_message_generator(iotDevice),retain=True)

        simplevars = SimpleVariables(iotDevice, jsonpayload, acttime)
        ppmppayload = simplevars.to_ppmp()
                
        if globMQTTClient.isConnected():
            globMQTTClient.publish(iotDevice.ppmp_topic(), ppmppayload, retain=True)
            
        logger.info (f'MQTT-PPMP-Payload device:{iotDevice.getHostname()}:{ppmppayload}')
Exemplo n.º 2
0
    def test_publish(self):
        publisher = PPMPPublisher()
        publisher.connect()

        while True:
            series = {
                'Temp1': [urandom.uniform(0, 0.2) for index in range(10)],
                'Temp2': [urandom.uniform(0, 0.2) for index in range(10)],
                'Temp3': [urandom.uniform(0, 0.2) for index in range(10)],
            }
            timestamp = local_now()
            offsets = list(range(0, 100, 10))

            publisher.publish(series, timestamp, offsets)

            time.sleep(1)
Exemplo n.º 3
0
    def test_to_ppmp_combined(self):
        timestamp = local_now()
        variables = {
            'a': 'aa',
            'b': 'bbb',
            'c': 1234,
            'd': 0.2,
        }
        sv = SimpleVariables(variables, timestamp, "testdevice", "testid")
        ppmp = sv.to_PPMP()

        json_ppmp = json.loads(ppmp)
        self.assertIsNotNone(json_ppmp)

        self.assertEqual(json_ppmp['device']['id'], 'testid')
        self.assertEqual(json_ppmp['device']['additionalData']['hostname'],
                         'testdevice')

        self.assertEqual(len(json_ppmp['measurements'][0]['series']), 5)
        self.assertEqual(json_ppmp['measurements'][0]['series']['a'], ['aa'])
Exemplo n.º 4
0
    def test_to_ppmp_single(self):
        timestamp = local_now()
        variables = {
            'a': 'aa',
        }
        sv = SimpleVariables(variables, timestamp, "testdevice", "testid")
        ppmp = sv.to_PPMP()

        self.assertIsNotNone(sv)
        self.assertIsNotNone(ppmp)

        json_ppmp = json.loads(ppmp)
        self.assertIsNotNone(json_ppmp)

        self.assertEqual(json_ppmp['device']['id'], 'testid')
        self.assertEqual(json_ppmp['device']['additionalData']['hostname'],
                         'testdevice')

        self.assertEqual(len(json_ppmp['measurements'][0]['series']), 2)
        self.assertEqual(json_ppmp['measurements'][0]['series']['a'], ['aa'])
Exemplo n.º 5
0
def sendMQTTPayload(event):
    global iothubdevices
    global globMQTTClient

    jsonpayload = event.body_as_json(encoding='UTF-8')
    acttime = local_now()
    # if event.system_properties[b'x-opt-enqueued-time']: acttime = event.system_properties[b'x-opt-enqueued-time']
    if event.system_properties[b'iothub-connection-device-id']:
        deviceIdinb = event.system_properties[b'iothub-connection-device-id']
        deviceId = deviceIdinb
        deviceIddecoded = deviceIdinb.decode('utf-8')
        if deviceIddecoded in IOTHUB_ALIAS_DEVICES:
            deviceId = IOTHUB_ALIAS_DEVICES[deviceIddecoded]

    if globMQTTClient != None:
        iotDevice = iothubdevices.get(deviceId)

        if iotDevice == None:
            iotDevice = iotHubDevice(
                net_name='mh',
                devideid=deviceId,
                additionalData={
                    'type': 'iotHub',
                },
            )
            iothubdevices[deviceId] = iotDevice
            globMQTTClient.publish(iotDevice.info_topic(),
                                   machine_message_generator(iotDevice),
                                   retain=True)

        simplevars = SimpleVariables(iotDevice, jsonpayload, acttime)
        ppmppayload = simplevars.to_ppmp()

        if globMQTTClient.isConnected:
            globMQTTClient.publish(iotDevice.ppmp_topic(),
                                   ppmppayload,
                                   retain=True)

        logger.info(
            f'MQTT-PPMP-Payload device:{iotDevice.getHostname()}:{ppmppayload}'
        )
Exemplo n.º 6
0
    client.last_will(device.info_topic(),
                     machine_message_generator(device,
                                               state=DeviceState.ERROR,
                                               code="offline"),
                     retain=True)

    client.connect()

    # create machine message with state=ON and code=online (retain)
    client.publish(device.info_topic(),
                   machine_message_generator(device),
                   retain=True)

    while True:
        measurements = []

        # create 3 TimeMeasurements
        for i in range(1):
            # add 5 random series to measurement (10 millisecond offsets)
            series = Series(
                [index * 10 for index in range(len(VARIABLES))],
                **{var: get_random_measurements(var)
                   for var in VARIABLES})
            measurement = TimeMeasurement(local_now(), series)
            measurements.append(measurement)

        client.publish(device.ppmp_topic(),
                       measurement_payload_generator(device, measurements))

        time.sleep(1)