Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
259 views
in Technique[技术] by (71.8m points)

Unable to get last cached device event from watson iot while using application in python

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()
question from:https://stackoverflow.com/questions/65940129/unable-to-get-last-cached-device-event-from-watson-iot-while-using-application-i

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Your code is calling the get on lec rather than the getAll:

lastEvents = client.lec.get(device,"status")

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):

lastEvents = client.lec.getAll(device)

or just remove your for event in lastEvents: loop and treat the returned object from client.lec.get(device,"status") as an Event type.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...