Ejemplo n.º 1
0
def decode(message):
    try:
        payload = json.loads(message.payload.decode("utf-8"))
    except ValueError as e:
        raise InvalidEventException("Unable to parse JSON.  payload=\"%s\" error=%s" % (message.payload, str(e)))
        
    data = None
    timestamp = None
    
    # Try to parse a timestamp
    try:
        if 'ts' in payload:
            dt = iso8601.parse_date(payload['ts'])
            timestamp = dt.astimezone(pytz.timezone('UTC'))
        else:
            timestamp = datetime.now(pytz.timezone('UTC'))
    except iso8601.ParseError as e:
        raise InvalidEventException("Unable to parse event timestamp: %s" % str(e))

    # Try to parse the data
    if 'd' in payload:
        data = payload['d']
        
        # TODO: Flatten JSON, covert into array of key/value pairs
    
    return Message(data, timestamp)
Ejemplo n.º 2
0
def decode(message):
    try:
        jsonData = json.dumps(xmltodict.parse(message.payload))
        data = json.loads(jsonData.decode("utf-8"))
    except ValueError as e:
        raise InvalidEventException("Unable to parse XML.  payload=\"%s\" error=%s" % (message.payload, str(e)))
    timestamp = datetime.now(pytz.timezone('UTC'))
    return Message(data, timestamp)
Ejemplo n.º 3
0
def decode(message):
    try:
        data = json.loads(message.payload.decode("utf-8"))
    except ValueError as e:
        raise InvalidEventException("Unable to parse JSON.  payload=\"%s\" error=%s" % (message.payload, str(e)))
    
    timestamp = datetime.now(pytz.timezone('UTC'))
    
    # TODO: Flatten JSON, covert into array of key/value pairs
    return Message(data, timestamp)
Ejemplo n.º 4
0
	def __init__(self, pahoMessage, messageEncoderModules):
		result = COMMAND_RE.match(pahoMessage.topic)
		if result:
			self.command = result.group(1)
			self.format = result.group(2)

			if self.format in messageEncoderModules:
				message = messageEncoderModules[self.format].decode(pahoMessage)
				self.timestamp = message.timestamp
				self.data = message.data
			else:
				raise MissingMessageDecoderException(self.format)
		else:
			raise InvalidEventException("Received command on invalid topic: %s" % (pahoMessage.topic))
Ejemplo n.º 5
0
    def __init__(self, pahoMessage, messageEncoderModules):
        result = NOTIFY_RE.match(pahoMessage.topic)
        if result:
            self.type = result.group(1)
            self.id = result.group(2)
            self.format = 'json'

            if self.format in messageEncoderModules:
                message = messageEncoderModules[self.format].decode(
                    pahoMessage)
                self.timestamp = message.timestamp
                self.data = message.data
            else:
                raise MissingMessageDecoderException(self.format)
        else:
            raise InvalidEventException(
                "Received notification on invalid topic: %s" %
                (pahoMessage.topic))
Ejemplo n.º 6
0
    def decode(message):
        """
        Convert a generic JSON message
        
        * The entire message is converted to JSON and treated as the message data
        * The timestamp of the message is the time that the message is RECEIVED
        """
        try:
            data = json.loads(message.payload.decode("utf-8"))
        except ValueError as e:
            raise InvalidEventException(
                "Unable to parse JSON.  payload=\"%s\" error=%s" %
                (message.payload, str(e)))

        timestamp = datetime.now(pytz.timezone('UTC'))

        # TODO: Flatten JSON, covert into array of key/value pairs
        return Message(data, timestamp)
Ejemplo n.º 7
0
    def __init__(self, pahoMessage, messageEncoderModules):
        result = DEVICE_EVENT_RE.match(pahoMessage.topic)
        if result:
            self.deviceType = result.group(1)
            self.deviceId = result.group(2)
            self.device = self.deviceType + ":" + self.deviceId

            self.event = result.group(3)
            self.format = result.group(4)

            self.payload = pahoMessage.payload

            if self.format in messageEncoderModules:
                message = messageEncoderModules[self.format].decode(
                    pahoMessage)
                self.timestamp = message.timestamp
                self.data = message.data
            else:
                raise MissingMessageDecoderException(self.format)
        else:
            raise InvalidEventException(
                "Received device event on invalid topic: %s" %
                (pahoMessage.topic))
Ejemplo n.º 8
0
    def __init__(self, message):
        result = DEVICE_STATUS_RE.match(message.topic)
        if result:
            self.payload = json.loads(message.payload.decode("utf-8"))
            self.deviceType = result.group(1)
            self.deviceId = result.group(2)
            self.device = self.deviceType + ":" + self.deviceId
            '''
            Properties from the "Connect" status are common in "Disconnect" status too
            {
            u'ClientAddr': u'195.212.29.68',
            u'Protocol': u'mqtt-tcp',
            u'ClientID': u'd:bcaxk:psutil:001',
            u'User': u'use-token-auth',
            u'Time': u'2014-07-07T06:37:56.494-04:00',
            u'Action': u'Connect',
            u'ConnectTime': u'2014-07-07T06:37:56.493-04:00',
            u'Port': 1883
            }
            '''

            self.clientAddr = self.payload['ClientAddr'] if (
                'ClientAddr' in self.payload) else None
            self.protocol = self.payload['Protocol'] if (
                'Protocol' in self.payload) else None
            self.clientId = self.payload['ClientID'] if (
                'ClientID' in self.payload) else None
            self.user = self.payload['User'] if ('User'
                                                 in self.payload) else None
            self.time = iso8601.parse_date(
                self.payload['Time']) if ('Time' in self.payload) else None
            self.action = self.payload['Action'] if ('Action'
                                                     in self.payload) else None
            self.connectTime = iso8601.parse_date(
                self.payload['ConnectTime']) if ('ConnectTime'
                                                 in self.payload) else None
            self.port = self.payload['Port'] if ('Port'
                                                 in self.payload) else None
            '''
            Additional "Disconnect" status properties
            {
            u'WriteMsg': 0,
            u'ReadMsg': 872,
            u'Reason': u'The connection has completed normally.',
            u'ReadBytes': 136507,
            u'WriteBytes': 32,
            }
            '''
            self.writeMsg = self.payload['WriteMsg'] if (
                'WriteMsg' in self.payload) else None
            self.readMsg = self.payload['ReadMsg'] if (
                'ReadMsg' in self.payload) else None
            self.reason = self.payload['Reason'] if ('Reason'
                                                     in self.payload) else None
            self.readBytes = self.payload['ReadBytes'] if (
                'ReadBytes' in self.payload) else None
            self.writeBytes = self.payload['WriteBytes'] if (
                'WriteBytes' in self.payload) else None
            self.closeCode = self.payload['CloseCode'] if (
                'CloseCode' in self.payload) else None
            self.retained = message.retain

        else:
            raise InvalidEventException(
                "Received device status on invalid topic: %s" %
                (message.topic))