xively-python 라이브러리를 사용하여 채널에서 데이터를 올바르게 읽을 수있는 방법을 찾으려고했습니다. 나는 current_value를 얻으려고했지만 초기 값을 검색하는 것처럼 보였다. Xively 인터페이스의 채널 값을 변경 한 후에도 마찬가지였다. 예를 들어, 'command'채널의 초기 값은 'Exit'입니다. 이것은 정확하게 검색되어 내 프로그램에 의해 실행됩니다. 그러나 프로그램을 끝내고 '명령'채널 값을 'AA'로 업데이트 한 다음 내 프로그램을 다시 시작하면 여전히 '종료'가 선택됩니다. 나는 이것을 10-15 분에 걸쳐 시험해 보았고 올바르게 업데이트되도록 20 초 이상을 보장했다.파이썬을 사용하여 xively 채널에서 최신 값을 읽는 중
API를 보면 나는 그것을 할 방법을 찾을 수 없습니다. 내가 원하는 것은 주어진 채널에 대한 최신 값을 검색하는 것입니다. 연결되지 않은 장치간에 명령을 전달하는 방법으로 채널을 사용하고 있습니다. 나는 채널을 정기적으로 읽으려고한다.
작은 메모에서 1 년 전에 게시 된 비슷한 문제가 있지만 답을 찾을 수 없습니다.
편집 :
#!/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
당신이, 내가 처음 볼 수 있듯이 : 만 요청 피드 Xively의 HTTP를 기반으로 프로그램의 시작 부분에 한 번 CURRENT_VALUE을 읽을 수 표시, 내가 사용하고있는 코드는 이것이다 '명령'변수 연결을 한 번 만들려고 시도한 다음 나중에 루프 내부에서 변수 연결을 시도하여 변경 사항을 확인했습니다. 데이터의 업데이트와 같이 채널의 current_value를 읽거나 최소한 명령을 보내고 구현할 수 있도록 최소한 정기적으로 읽어야합니다.
문제를 재현하는 작은 코드 조각을 제공 할 수 있다면 아주 좋습니다. 이게 파이썬 라이브러리의 버그일지도 모릅니다. 아마도 라이브러리를 사용하지 않고 수동으로 REST 요청을하지 않고 이것을 재현 해 볼 수도 있습니다 ... – errordeveloper
화요일까지는 코드를 게시 할 수 없지만 그 동안에는 다음과 같은 구조가 있습니다. 코드. 코드의 관련이없는 부분을 나타 내기 위해 ---를 추가했습니다. (예 : 읽기를 구현하기 전에 작업 중임) #declare 변수, 데이터 스트림에 대한 연결 설정 --- command = open_connection ("command") #functions 호출 할 --- --- --- 데프 (데이터 스트림) 당기 : \t 반환 datstream을.CURRENT_VALUE --- --- #setup (설정) 메인 루프 #Enter --- 동안 상태 : \t 연산자 = 견인 (명령) \t 인쇄 (오퍼레이터) \t --- --- --- # 닫는 연결 – user3574526
질문을 편집하고 주석을 읽을 때마다 코드를 입력 할 때마다 코드를 입력하십시오. – errordeveloper