示例#1
0
def on_message(client, userdata, msg):
    print("Message arrived: " + msg.topic)
    tokens = msg.topic.split("/")

    if tokens[0] == "spAv1.0" and tokens[1] == myGroupId and tokens[2] == "DCMD" and tokens[3] == myNodeName:
	inboundPayload = kurapayload_pb2.KuraPayload()
	inboundPayload.ParseFromString(msg.payload)
	outboundPayload = kurapayload_pb2.KuraPayload()
	outboundPayload.timestamp = int(round(time.time() * 1000))
	addMetric(outboundPayload, "seq", "INT32", getSeqNum())

	for metric in inboundPayload.metric:
	    print "Tag Name: " + metric.name
	    if metric.name == "Outputs/e":
		pibrella.output.e.write(metric.bool_value)
		addMetric(outboundPayload, "Outputs/e", "BOOL", pibrella.output.e.read())
	    elif metric.name == "Outputs/f":
		pibrella.output.f.write(metric.bool_value)
		addMetric(outboundPayload, "Outputs/f", "BOOL", pibrella.output.f.read())
	    elif metric.name == "Outputs/g":
		pibrella.output.g.write(metric.bool_value)
		addMetric(outboundPayload, "Outputs/g", "BOOL", pibrella.output.g.read())
	    elif metric.name == "Outputs/h":
		pibrella.output.h.write(metric.bool_value)
		addMetric(outboundPayload, "Outputs/h", "BOOL", pibrella.output.h.read())
	    elif metric.name == "Outputs/LEDs/green":
		if metric.bool_value:
		    pibrella.light.green.on()
		else:
		    pibrella.light.green.off()
		addMetric(outboundPayload, "Outputs/LEDs/green", "BOOL", pibrella.light.green.read())
	    elif metric.name == "Outputs/LEDs/red":
		if metric.bool_value:
		    pibrella.light.red.on()
		else:
		    pibrella.light.red.off()
		addMetric(outboundPayload, "Outputs/LEDs/red", "BOOL", pibrella.light.red.read())
	    elif metric.name == "Outputs/LEDs/yellow":
		if metric.bool_value:
		    pibrella.light.yellow.on()
		else:
		    pibrella.light.yellow.off()
		addMetric(outboundPayload, "Outputs/LEDs/yellow", "BOOL", pibrella.light.yellow.read())
	    elif metric.name == "buzzer_fail":
		pibrella.buzzer.fail()
	    elif metric.name == "buzzer_success":
		pibrella.buzzer.success()

	byteArray = bytearray(outboundPayload.SerializeToString())
	client.publish("spAv1.0/" + myGroupId + "/DDATA/" + myNodeName + "/" + mySubNodeName, byteArray, 0, False)
    elif tokens[0] == "spAv1.0" and tokens[1] == myGroupId and tokens[2] == "NCMD" and tokens[3] == myNodeName:
        inboundPayload = kurapayload_pb2.KuraPayload()
        inboundPayload.ParseFromString(msg.payload)
        for metric in inboundPayload.metric:
            if metric.name == "Rebirth":
                publishBirth()

    print "done publishing"
示例#2
0
def publishBirth():
    # Create the node birth payload with a position
    payload = kurapayload_pb2.KuraPayload()
    position = payload.position
    position.altitude = 319
    position.heading = 0
    position.latitude = 38.83667239
    position.longitude = -94.67176706
    position.precision = 2.0
    position.satellites = 8
    position.speed = 0
    position.status = 3
    position.timestamp = int(round(time.time() * 1000))

    # Add a timestamp and sequence numbers to the payload
    seq = 0
    payload.timestamp = int(round(time.time() * 1000))
    addMetric(payload, "bdSeq", "INT32", bdSeq)
    addMetric(payload, "seq", "INT32", getSeqNum())

    # Publish the node birth certificate
    byteArray = bytearray(payload.SerializeToString())
    client.publish("spAv1.0/" + myGroupId + "/NBIRTH/" + myNodeName, byteArray, 0, False)

    # Set up the input metrics
    payload = kurapayload_pb2.KuraPayload()
    payload.timestamp = int(round(time.time() * 1000))
    addMetric(payload, "seq", "INT32", getSeqNum())

    addMetric(payload, "Inputs/a", "BOOL", pibrella.input.a.read())
    addMetric(payload, "Inputs/b", "BOOL", pibrella.input.b.read())
    addMetric(payload, "Inputs/c", "BOOL", pibrella.input.c.read())
    addMetric(payload, "Inputs/d", "BOOL", pibrella.input.d.read())

    # Set up the output states on first run so Ignition and MQTT Engine are aware of them
    addMetric(payload, "Outputs/e", "BOOL", pibrella.output.e.read())
    addMetric(payload, "Outputs/f", "BOOL", pibrella.output.f.read())
    addMetric(payload, "Outputs/g", "BOOL", pibrella.output.g.read())
    addMetric(payload, "Outputs/h", "BOOL", pibrella.output.h.read())
    addMetric(payload, "Outputs/LEDs/green", "BOOL", pibrella.light.green.read())
    addMetric(payload, "Outputs/LEDs/red", "BOOL", pibrella.light.red.read())
    addMetric(payload, "Outputs/LEDs/yellow", "BOOL", pibrella.light.yellow.read())
    addMetric(payload, "button", "BOOL", pibrella.button.read())
    addMetric(payload, "buzzer_fail", "BOOL", 0)
    addMetric(payload, "buzzer_success", "BOOL", 0)

    # Set up the propertites payload
    addMetric(payload, "Parameters/device_hw_version", "STRING", "PFC_1.1")
    addMetric(payload, "Parameters/firmware_version", "STRING", "1.4.2")

    # Publish the initial data with the Device BIRTH certificate
    totalByteArray = bytearray(payload.SerializeToString())
    client.publish("spAv1.0/" + myGroupId + "/DBIRTH/" + myNodeName + "/" + mySubNodeName, totalByteArray, 0, False)
示例#3
0
def publishBirth():
    # Create the node birth payload with a position
    payload = kurapayload_pb2.KuraPayload()
    position = payload.position
    position.altitude = 319
    position.heading = 0
    position.latitude = 38.83667239
    position.longitude = -94.67176706
    position.precision = 2.0
    position.satellites = 8
    position.speed = 0
    position.status = 3
    position.timestamp = int(round(time.time() * 1000))

    # Add a timestamp and sequence numbers to the payload
    seqNum = 0
    payload.timestamp = int(round(time.time() * 1000))
    addMetric(payload, "bdSeq", "INT32", bdSeq)
    addMetric(payload, "seq", "INT32", getSeqNum())

    # Publish the node birth certificate
    byteArray = bytearray(payload.SerializeToString())
    client.publish("spAv1.0/" + myGroupId + "/NBIRTH/" + myNodeName, byteArray,
                   0, False)

    # Setup the inputs
    payload = kurapayload_pb2.KuraPayload()
    payload.timestamp = int(round(time.time() * 1000))
    addMetric(payload, "seq", "INT32", getSeqNum())

    addMetric(payload, "my_boolean", "BOOL", random.choice([True, False]))
    addMetric(payload, "my_float", "FLOAT", random.random())
    addMetric(payload, "my_int", "INT32", random.randint(0, 100))
    addMetric(payload, "my_long", "INT64", random.getrandbits(60))
    addMetric(payload, "Inputs/0", "BOOL", True)
    addMetric(payload, "Inputs/1", "INT32", 0)
    addMetric(payload, "Inputs/2", "FLOAT", 1.23)

    # Set up the output states on first run so Ignition and MQTT Engine are aware of them
    addMetric(payload, "Outputs/0", "BOOL", True)
    addMetric(payload, "Outputs/1", "INT32", 0)
    addMetric(payload, "Outputs/2", "FLOAT", 1.23)

    # Set up the propertites
    addMetric(payload, "Properties/Hardware Version", "STRING", "PFC_1.1")
    addMetric(payload, "Properties/Firmware Version", "STRING", "1.4.2")

    # Publish the initial data with the Device BIRTH certificate
    totalByteArray = bytearray(payload.SerializeToString())
    client.publish(
        "spAv1.0/" + myGroupId + "/DBIRTH/" + myNodeName + "/" + mySubNodeName,
        totalByteArray, 0, False)
示例#4
0
def input_changed(name, pin):
    outboundPayload = kurapayload_pb2.KuraPayload()
    outboundPayload.timestamp = int(round(time.time() * 1000))
    addMetric(outboundPayload, "seq", "INT32", getSeqNum())
    addMetric(outboundPayload, name, "BOOL", pin.read());
    byteArray = bytearray(outboundPayload.SerializeToString())
    client.publish("spAv1.0/" + myGroupId + "/DDATA/" + myNodeName + "/" + mySubNodeName, byteArray, 0, False)
示例#5
0
def on_message(client, userdata, msg):
    print("Message arrived: " + msg.topic)
    tokens = msg.topic.split("/")

    if tokens[0] == "spAv1.0" and tokens[1] == myGroupId and tokens[
            2] == "DCMD" and tokens[3] == myNodeName:
        inboundPayload = kurapayload_pb2.KuraPayload()
        inboundPayload.ParseFromString(msg.payload)
        outboundPayload = kurapayload_pb2.KuraPayload()
        outboundPayload.timestamp = int(round(time.time() * 1000))
        addMetric(outboundPayload, "seq", "INT32", getSeqNum())
        for metric in inboundPayload.metric:
            if metric.name == "Outputs/0":
                print "Outputs/0: " + str(metric.bool_value)
                addMetric(outboundPayload, "Inputs/0", "BOOL",
                          metric.bool_value)
                addMetric(outboundPayload, "Outputs/0", "BOOL",
                          metric.bool_value)
            elif metric.name == "Outputs/1":
                print "Outputs/1: " + str(metric.int_value)
                addMetric(outboundPayload, "Inputs/1", "INT32",
                          metric.int_value)
                addMetric(outboundPayload, "Outputs/1", "INT32",
                          metric.int_value)
            elif metric.name == "Outputs/2":
                print "Outputs/2: " + str(metric.float_value)
                addMetric(outboundPayload, "Inputs/2", "FLOAT",
                          metric.float_value)
                addMetric(outboundPayload, "Outputs/2", "FLOAT",
                          metric.float_value)

        byteArray = bytearray(outboundPayload.SerializeToString())
        client.publish(
            "spAv1.0/" + myGroupId + "/DDATA/" + myNodeName + "/" +
            mySubNodeName, byteArray, 0, False)
    elif tokens[0] == "spAv1.0" and tokens[1] == myGroupId and tokens[
            2] == "NCMD" and tokens[3] == myNodeName:
        inboundPayload = kurapayload_pb2.KuraPayload()
        inboundPayload.ParseFromString(msg.payload)
        for metric in inboundPayload.metric:
            if metric.name == "Rebirth":
                publishBirth()

    print "done publishing"
示例#6
0
def create_payload(metrics):
    payload = kura_payload.KuraPayload()
    for key, value in metrics.items():
        m = kura_payload.KuraPayload.KuraMetric()
        m.name = key
        m.type = m.STRING
        m.string_value = value
        payload.metric.extend([m])

    return payload.SerializeToString()
示例#7
0
def button_changed(pin):
    outboundPayload = kurapayload_pb2.KuraPayload()
    if pin.read() == 1:
	print("You pressed the button!")
    else:
	print("You released the button!")
    outboundPayload.timestamp = int(round(time.time() * 1000))
    addMetric(outboundPayload, "seq", "INT32", getSeqNum())
    addMetric(outboundPayload, "button", "BOOL", pin.read());
    byteArray = bytearray(outboundPayload.SerializeToString())
    client.publish("spAv1.0/" + myGroupId + "/DDATA/" + myNodeName + "/" + mySubNodeName, byteArray, 0, False)
示例#8
0
def send_message(args, subtopic, json_payload):
    topic = '$EDC/diec1/' + args.client_id + '/MQTT/' + subtopic

    pb = json_format.Parse(json.dumps(json_payload),
                           kurapayload_pb2.KuraPayload(),
                           ignore_unknown_fields=False)

    publish.single(topic,
                   payload=pb.SerializeToString(),
                   qos=0,
                   retain=False,
                   hostname=args.hostname,
                   port=args.port,
                   keepalive=60,
                   will=None,
                   auth={
                       'username': username,
                       'password': password
                   },
                   tls=None,
                   protocol=mqtt.MQTTv311,
                   transport='tcp')
示例#9
0
    # Set up the propertites
    addMetric(payload, "Properties/Hardware Version", "STRING", "PFC_1.1")
    addMetric(payload, "Properties/Firmware Version", "STRING", "1.4.2")

    # Publish the initial data with the Device BIRTH certificate
    totalByteArray = bytearray(payload.SerializeToString())
    client.publish(
        "spAv1.0/" + myGroupId + "/DBIRTH/" + myNodeName + "/" + mySubNodeName,
        totalByteArray, 0, False)


######################################################################

# Create the DEATH payload
deathPayload = kurapayload_pb2.KuraPayload()
deathPayload.timestamp = int(round(time.time() * 1000))
addMetric(deathPayload, "bdSeq", "INT32", getBdSeqNum())
deathByteArray = bytearray(deathPayload.SerializeToString())

# Start of main program - Set up the MQTT client connection
client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
client.username_pw_set(myUsername, myPassword)
client.will_set("spAv1.0/" + myGroupId + "/NDEATH/" + myNodeName,
                deathByteArray, 0, False)
client.connect(serverUrl, 1883, 60)

publishBirth()
示例#10
0
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import google.protobuf
import kurapayload_pb2 as kura_payload
import logging
import zlib

logger = logging.getLogger(__name__)

payload_decoder = kura_payload.KuraPayload()


def decode_message(message):
    ungziped = decode_gzip(message)
    unprotobuffed = decode_protobuf(ungziped)
    return unprotobuffed


def decode_gzip(message):
    try:
        message = zlib.decompress(message, 16 + zlib.MAX_WBITS)
    except zlib.error:
        logger.debug("Message is not gzip encoded")
    return message


def decode_protobuf(message):
    try:
        payload_decoder.ParseFromString(message)
        return payload_decoder