I am trying to get last device event using python but I get this error:
wiotp.sdk.application.client.ApplicationClient INFO Connected successfully:
a:orgid:5d633cbe-14c0-4d05-b4d6-86bf97a068d9
Traceback (most recent call last):
File "fetch_event_data_fromWatson_iot.py", line 23, in <module>
print("Event from device: %s:%s" % (event.typeId, event.deviceId))
AttributeError: 'str' object has no attribute 'typeId'.
My code is follows:
import wiotp.sdk.application
from wiotp.sdk.messages import Message, MessageCodec, JsonCodec, RawCodec, Utf8Codec
import json
import base64
myConfig = {
"auth":
{ "key": "-----",
"token": "-----"
}
}
client = wiotp.sdk.application.ApplicationClient(config=myConfig)
client.connect()
device = {"typeId": "ESP8266", "deviceId": "ecg", "eventId": "status"}
lastEvents = client.lec.get(device,"status")
for event in lastEvents:
print("Event from device: %s:%s" % (event.typeI`enter code here`d, event.deviceId))
print("- Event ID: %s " % (event.eventId))
print("- Format: %s" % (event.format))
print("- Cached at: %s" % (event.timestamp.isoformat()))
# The payload is always returned base64 encoded by the API
print("- Payload (base64 encoded): %s" % (event.payload))
# Depending on the content of the message this may not be a good idea (e.g. if it was originally binary data)
print("- Payload (decoded): %s" % (base64.b64decode(event.payload).decode('utf-8')))
#
client.disconnect()
Your code is calling the get on lec rather than the getAll:
and so is not returning a list but the Event dict. Either you need to change your code to call getAll (https://ibm-watson-iot.github.io/iot-python/application/api/lec/#get-all-last-cached-events):
or just remove your
for event in lastEvents:
loop and treat the returned object fromclient.lec.get(device,"status")
as an Event type.