Beispiel #1
0
def publish(message):

    if not M2X_CONFIG and not M2X_CONFIG.api_key and not M2X_CONFIG.device_id and not M2X_CONFIG.stream_id:
        return

    m2x_api_key = M2X_CONFIG.api_key
    m2x_device_id = M2X_CONFIG.device_id
    m2x_stream_id = M2X_CONFIG.stream_id

    topic = "m2x/{0}/requests".format(m2x_api_key)
    data = serialize_json({
        "id":
        str(uuid4()),
        "method":
        "POST",
        "resource":
        "/v2/devices/{0}/streams/{1}/values".format(m2x_device_id,
                                                    m2x_stream_id),
        "body": {
            "values": [{
                "timestamp": datetime.utcnow().isoformat(),
                "value": serialize_json(message)
            }]
        }
    })

    def perform_request():

        print("Publishing M2X MQTT service.")

        auth = {"username": m2x_api_key}

        tls = {
            "ca_certs": "/etc/ssl/certs/ca-certificates.crt",
            "tls_version": PROTOCOL_TLSv1
        }

        mqtt_publish_single(topic,
                            payload=data,
                            hostname=m2x_mqtt_hostname,
                            port=m2x_mqtt_port,
                            auth=auth,
                            tls=tls,
                            protocol=MQTTv311)

        print("Published to M2X MQTT service.")

    SCHEDULER.add_job(perform_request)
Beispiel #2
0
    def perform_request():

        client.messages.create(body=serialize_json(payload),
                               to=TWILIO_CONFIG.inbound_number,
                               from_=TWILIO_CONFIG.outbound_number)

        print("Sent SMS message.")
    def perform_request():

        client.messages.create(
            body=serialize_json(payload),
            to=TWILIO_CONFIG.inbound_number,
            from_=TWILIO_CONFIG.outbound_number
        )

        print("Sent SMS message.")
    def pushDatapoint(self):

        data = {
            "messageId":
            self.message_id,
            "body": [{
                "name":
                "sensor-data",
                "datapoints": [[
                    int(time() * 1000),
                    serialize_json(self.factory.payload, ensure_ascii=False)
                ]]
            }]
        }

        serialized_data = serialize_json(data,
                                         ensure_ascii=False).encode("utf-8")
        self.sendMessage(serialized_data)
Beispiel #5
0
def push(message):

    sap_mms_endpoint = SAP_CONFIG.mms_endpoint
    sap_device_id = SAP_CONFIG.device_id
    sap_message_type_id = SAP_CONFIG.message_type_id
    sap_oauth_token = SAP_CONFIG.oauth_token

    sap_data_endpoint = "{0}/{1}".format(sap_mms_endpoint, sap_device_id)

    print("SAP MMS data endpoint: {0}".format(sap_data_endpoint))

    agent = Agent(reactor)
    client = BrowserLikeRedirectAgent(agent)

    headers = Headers({
        "Authorization": ["Bearer {0}".format(sap_oauth_token)],
        "Content-Type": ["application/json;charset=utf-8"]
    })

    body = {
        "mode":
        "sync",
        "messageType":
        sap_message_type_id,
        "messages": [{
            "sensor": "IntelHowToSample",
            "value": serialize_json(message),
            "timestamp": int(time())
        }]
    }

    print("SAP MMS payload: {0}".format(serialize_json(body)))

    def handle_response(response):
        if response.code == 200:
            print("SAP MMS publish succeeded.")
        else:
            print("SAP Error response: {0}".format(response.code))

    client.request("POST", sap_data_endpoint, headers, JsonProducer(body)) \
        .addCallback(lambda x: print("Published to SAP MMS service.")).addErrback(lambda x: print("SAP publish error:", x))
def publish_message(payload):
    """
    Publish message to MQTT server.
    """

    if not MQTT_CONFIG:
        return

    server = MQTT_CONFIG.server
    port = MQTT_CONFIG.port

    client_id = MQTT_CONFIG.client_id

    auth = {
        "username": MQTT_CONFIG.username,
        "password": MQTT_CONFIG.password
    } if MQTT_CONFIG.username and MQTT_CONFIG.password else None

    tls = {
        "ca_certs": "/etc/ssl/certs/ca-certificates.crt",
        "tls_version": PROTOCOL_TLSv1,
        "certfile": MQTT_CONFIG.cert,
        "keyfile": MQTT_CONFIG.key
    } if MQTT_CONFIG.port == 8883 else None

    topic = MQTT_CONFIG.topic
    data = serialize_json(payload)

    def perform_request():
        """
        Perform MQTT request.
        """

        mqtt_publish_single(topic,
                            payload=data,
                            hostname=server,
                            port=port,
                            client_id=client_id,
                            auth=auth,
                            tls=tls,
                            protocol=MQTTv311)

        print("published to MQTT server")

    SCHEDULER.add_job(perform_request)
 def __init__(self, body):
     data = serialize_json(body)
     self.body = data
     self.length = len(data)