使用Control窗口小部件开关时,我可以从事物板上设置按钮值(setValue方法)到我的设备,但是我无法弄清楚如何在事物板上获得按钮值(getValue方法)。 我已经尝试过此代码,但是按钮未获取值。请帮我解决这个问题。
button_state = {'value': False}
def setValue (params):
button_state['data'] = params
print("setValue set to : ",button_state)
def on_connect(client, userdata, flags, rc):
print("rc code:",rc)
client.subscribe('v1/devices/me/rpc/request/+')
def on_message(client, userdata, msg):
print('Topic: ' + msg.topic + '\nMessage: ' + str(msg.payload))
if msg.topic.startswith('v1/devices/me/rpc/request/'):
requestId = msg.topic[len('v1/devices/me/rpc/request/'):len(msg.topic)]
print("requestId : ",requestId)
data = json.loads(msg.payload)
if data['method'] == 'getValue':
print("getvalue request\n")
print("sended getValue: ",button_state)
client.publish('v1/devices/me/rpc/response/'+requestId, json.dumps(button_state), 1)
if data['method'] == 'setValue':
print("setvalue request\n")
params = data['params']
setValue(params)
client.publish('v1/devices/me/attributes', json.dumps(button_state), 1)