forked from smbunn/Viessmann-Control
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxively_tutorial1.py
96 lines (83 loc) · 2.71 KB
/
xively_tutorial1.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#!/usr/bin/env python
import os
import xively
import subprocess
import time
import datetime
import requests
# extract feed_id and api_key from environment variables
FEED_ID = os.environ["FEED_ID"]
API_KEY = os.environ["API_KEY"]
DEBUG = os.environ["DEBUG"] or false
# initialize api client
api = xively.XivelyAPIClient(API_KEY)
# function to read 1 minute load average from system uptime command
def read_values():
global oldValue
if DEBUG:
print "Reading outside temperature"
subprocess.call(["/usr/local/bin/vclient -m --host localhost --port 3002 getOutsideTemp getBoilerTemp getWaterTemp getTemp17B getHotWaterTemp1 > temp"], shell=True)
time.sleep(20)
fo = open("temp", "rw+")
value1 = fo.readline()
v1 = value1[21:-4]
value2 = fo.readline()
v2 = value2[20:-4]
value3 = fo.readline()
v3 = value3[19:-4]
value4 = fo.readline()
v4 = value4[17:-4]
value5 = fo.readline()
v5 = value5[23:-4]
# NEW CODE
if float(v1) > 50.0:
v1 = oldValue
oldValue = v1
# ========
fo.close()
return (v1, v2, v3, v4, v5)
# function to return a datastream object. This either creates a new datastream,
# or returns an existing one
def get_datastream(feed):
try:
datastream = feed.datastreams.get("Boiler_Ctrl")
if DEBUG:
print "Found existing datastream"
return datastream
except:
if DEBUG:
print "Creating new datastream"
datastream = feed.datastreams.create("Boiler_Ctrl", tags="OutsideTemp, BoilerTemp, WaterTemp, Temp17B, HotWaterTemp")
return datastream
# main program entry point - runs continuously updating our datastream with the
# current 1 minute load average
def run():
print "Starting Xively tutorial script"
global oldValue
oldValue = 20.0
feed = api.feeds.get(FEED_ID)
datastream = get_datastream(feed)
datastream.max_value = None
datastream.min_value = None
while True:
Outside, Boiler, Water, Temp17B, HotWater = read_values()
if DEBUG:
print "Outside=: %s" % Outside
print "Boiler=: %s" % Boiler
print "Water=: %s" % Water
print "Temp17B=: %s" % Temp17B
print "Hot Water=: %s" % HotWater
now = datetime.datetime.utcnow()
feed.datastreams = [
xively.Datastream(id='OutsideTemp', current_value=Outside, at=now),
xively.Datastream(id='BoilerTemp', current_value=Boiler, at=now),
xively.Datastream(id='WaterTemp', current_value=Water, at=now),
xively.Datastream(id='Temp17B', current_value=Temp17B, at=now),
xively.Datastream(id='HotWaterTemp', current_value=HotWater, at=now),
]
try:
feed.update()
except requests.HTTPError as e:
print "HTTPError({0}): {1}".format(e.errno, e.strerror)
time.sleep(300)
run()