-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathvitals.py
105 lines (89 loc) · 2.61 KB
/
vitals.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
97
98
99
100
101
102
103
104
105
# pyPowerWall Vitals
# -*- coding: utf-8 -*-
"""
This script pulls the Powerwall Vitals API
Author: Jason A. Cox
For more information see https://github.com/jasonacox/pypowerwall
* /api/devices/vitals produces a protobuf binary payload
"""
import pypowerwall
# Update with your details
password='password'
email='[email protected]'
host = "10.0.1.23"
timezone = "America/LosAngeles"
# Make sure binary polling allowed
if pypowerwall.version_tuple < (0,0,3):
print("\n*** WARNING: Minimum pypowerwall version 0.0.3 required for proper function! ***\n\n")
# Connect to Powerwall
pw = pypowerwall.Powerwall(host,password,email,timezone)
# Display Vitals
print("Vitals: %r\n" % pw.vitals())
# Below is an alternative manual way to parse the protobuf payload
"""
import struct
# Pull vitals payload - binary format in protobuf
stream = pw.poll('/api/devices/vitals')
streamsize = len(stream)
print("Size of stream = %d" % streamsize)
# Walk through payload
index = 0
skip = False
skipdata = ""
while(index < streamsize):
key = ""
value = ""
meta = ""
# check for kv signal
if(streamsize-index > 3 and stream[index] == ord('\x12') and stream[index+2] == ord('\x0a')):
# print skip data
if skip:
print(" > Skipped: %s" % skipdata)
skip = False
skipdata = ""
# Parse payload
meta=stream[index+1]
index += 3
# grab key starting with size value
size = stream[index]
index += 1
if(size > 0):
key = stream[index:index+size].decode()
index += size
delimiter = stream[index]
index += 1
if(delimiter == ord('!')):
# numerical value
# DOUBLE
v = stream[index:index+8]
v = struct.unpack('<d',v)[0]
try:
value = "%r" % (v)
except:
value = "?"
index += 8
if(delimiter == ord('*')):
# string value
size = stream[index]
index += 1
if(size > 0):
value = stream[index:index+size].decode()
index += size
if(delimiter == ord('0')):
# boolean value
if(stream[index] == 1):
value = "TRUE"
else:
value = "FALSE"
index += 1
# Print it
print("[%d] %s: %s" % (meta,key,value))
continue
skip = True
if(chr(stream[index]).isalnum()):
skipdata += " %c" % chr(stream[index])
else:
skipdata += " 0x%02d" % stream[index]
index += 1
# end while
"""