我一直试图找到一种使用xively-python库从通道中正确读取数据的方法。我尝试检索current_value但它似乎只检索初始值,即使在Xively界面中更改通道中的值之后也是如此。例如,频道'命令中的初始值'是"退出"。这是我的程序正确检索和执行的。但是,如果我结束该程序,请更新'命令'渠道价值为' AA'然后重启我的程序,它仍然选择退出'退出。我已经测试了10-15分钟,确保超过20秒,以确保正确更新。
看看API,我似乎无法找到一种方法。我想要做的是检索给定频道的最新值。我使用频道作为在未连接设备之间传递命令的方法。我希望定期阅读该频道。
一个小小的说明,我看到一年前发布的类似问题,但没有得到答复。
编辑:我似乎只是根据Xively HTTP请求提要在程序开头读取一次current_value,我使用的代码是:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#import serial library and sleep from the time library
import serial
from time import sleep
import xively
import requests
from datetime import datetime
#declare port and baud rate then open conn
port = '/dev/ttyAMA0'
baud = 9600
ser = serial.Serial(port=port, baudrate = baud)
#xively variables
FEED_ID = "1032676103"
API_KEY = "FZ2IOZkDW5ZXAITh6kqqUVkZEIBfNk6y2SKxgx4DDDVLxi46"
#xively connection function reutrning a datastream handle
def open_connection(feed,target):
try:
datastream = feed.datastreams.get(target)
print("Channel " + target + " found!")
return datastream
except:
datastream = feed.datastreams.create(target)
print("Channel " + target + " not found,new channel created!")
return datastream
#push function
def push(target,value):
target.current_value = value
target.at = datetime.utcnow()
try:
target.update()
except requests.HTTPError as e:
print "HTTPError({0}): {1}".format(e.errno,e.strerror)
#pop function
def pull():
command = open_connection(feed,"Command")
command.at = datetime.utcnow()
sleep(0.2)
#return target.get(datetime.utcnow())
return command.current_value
#check status of board 1 relay A
def status_one():
ser.write('aAARELAYA---')
sleep(0.2)
reply = ser.read(12)
sleep(0.2)
relayAA = reply[9:]
relayAA = relayAA.strip('-')
return relayAA
#check status of board 2 relay A
def status_two():
ser.write('aBBRELAYA---')
sleep(0.2)
reply = ser.read(12)
relayBA = reply[9:]
relayBA = relayBA.strip('-')
return relayBA
#toggle boardAA relay A function
def toggle_aa():
ser.write('aAARELAYATOG')
sleep(0.2)
reply = ser.read(12)
print(reply)
sleep(0.2)
#toggle boardBB relay A function
def toggle_bb():
ser.write('aBBRELAYATOG')
sleep(0.2)
reply = ser.read(12)
print(reply)
sleep(0.2)
#setup and initial push to Xively
ser.flushInput()
api = xively.XivelyAPIClient(API_KEY)
feed = api.feeds.get(FEED_ID)
oneastatus = open_connection(feed,"Relay_1A")
twoastatus = open_connection(feed,"Relay_2A")
hubstatus = open_connection(feed,"Relay_Data")
#command = open_connection(feed,"Command")
hub = 1
push(hubstatus,hub)
relayAA = status_one()
print(relayAA)
push(oneastatus,relayAA)
relayBA = status_two()
print(relayBA)
push(twoastatus,relayBA)
#loop section - command handling
while hub:
#operator = pull(command)
operator = pull()
sleep(0.2)
print(operator)
#command to toggle AA
if operator == 'AA':
toggle_aa()
sleep(0.2)
relayAA = status_one()
push(oneastatus,relayAA)
sleep(0.2)
#command to toggle BA
if operator == 'BA':
toggle_bb()
sleep(0.2)
relayBA = status_two()
push(twoastatus,relayBA)
sleep(0.2)
#command to shutdown
if operator == 'Exit':
hub = 0
#close conn
push(hubstatus,hub)
ser.close
正如您所看到的,我最初尝试创建'命令'变量连接一次,然后尝试在循环内创建它,看它是否改变了东西。我需要它像数据更新一样频繁地读取通道的current_value,或者至少在半定期的基础上读取通道的current_value以允许发送和实现命令。
答案 0 :(得分:0)
我在REPL中尝试了这个,但看不出你出现问题的原因。
[xively-python] 0 %> python
requPython 2.7.5 (default, Aug 25 2013, 00:04:04)
[GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.0.68)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import xively
>>> FEED_ID = "1032676103"
>>> API_KEY = "FZ2IOZkDW5ZXAITh6kqqUVkZEIBfNk6y2SKxgx4DDDVLxi46"
>>> api = xively.XivelyAPIClient(API_KEY)
>>> f = api.feeds.get(FEED_ID)
>>> d = f.datastreams.get('Command')
>>> print(d.current_value)
Exit
然后我将Xively工作台上的值修改为Exit2
并且它有效:
>>> d = f.datastreams.get('Command')
>>> print(d.current_value)
Exit2
我现在将值翻转回Exit
,这看起来也很好:
>>> d = f.datastreams.get('Command')
>>> print(d.current_value)
Exit