-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathweb.py
More file actions
116 lines (86 loc) · 3.21 KB
/
web.py
File metadata and controls
116 lines (86 loc) · 3.21 KB
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
106
107
108
109
110
111
112
113
114
115
116
################################################################################
# Web Interface
################################################################################
################################################################################
# IMPORTS
################################################################################
import time
import zmq
import socket
import struct
import threading
import json
import os
import flatbuffers
from BuildingConfig import *
################################################################################
# CLASSES
################################################################################
################################################################################
# VARIABLES
################################################################################
context = None
current_temp = 999.0
cooling = 0
heating = 0
alarm = 0
platform = "Ubuntu"
################################################################################
# FUNCTIONS
################################################################################
def worker():
"""
Thread to communicate with the management interface
"""
global context
management_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
management_socket.bind(("0.0.0.0", 6665))
tc_pub_socket = context.socket(zmq.PUB)
tc_pub_socket.connect("ipc:///tmp/feeds/1")
while True:
message, addr = management_socket.recvfrom(128)
#Decode flatbuffer
config = BuildingConfig.GetRootAsBuildingConfig(message, 0)
print "WEB: interface ", addr, config.DesiredTemp()
setpoint = config.DesiredTemp()
safety_range = config.SafetyRange()
#Publish settings for TC
tc_pub_socket.send(json.dumps({"setpoint": setpoint, "safetyRange":safety_range}))
#Because of hiccup with the serializer in the seL4 world, this is necesarry for now
reply = struct.pack("fiii16s", current_temp, cooling, heating, alarm, platform)
#management_socket.connect(addr)
management_socket.sendto(reply, (addr[0], 6666))
################################################################################
# MAIN
################################################################################
def main():
global context
global current_temp
global cooling
global heating
global alarm
if not os.path.exists("/tmp/feeds"):
os.makedirs("/tmp/feeds")
context = zmq.Context()
tc_socket = context.socket(zmq.SUB)
tc_socket.setsockopt(zmq.SUBSCRIBE, "")
tc_socket.bind("ipc:///tmp/feeds/0")
t = threading.Thread(target=worker)
t.daemon = True
t.start()
while True:
# Wait for next request from client
message = tc_socket.recv()
print "WEB: tc", message
message = json.loads(message)
#TODO not like this :^(
if "currentTemp" in message:
current_temp = message["currentTemp"]
if "cooling" in message:
cooling = message["cooling"]
if "heating" in message:
heating = message["heating"]
if "alarm" in message:
alarm = message["alarm"]
if __name__ == "__main__":
main()