-
Notifications
You must be signed in to change notification settings - Fork 4
/
server.py
190 lines (156 loc) · 6.66 KB
/
server.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
from tkinter import Tk, Label, Button, Toplevel, messagebox, PhotoImage
from tinydb import TinyDB, Query
from matplotlib import pyplot
import random
import smartOff
import calendar;
import time;
import http.client
ipAddress = "192.168.137.166"
salt = 25
key = 1
def sendSignalToDevice(predict) :
actionVal = salt+key
if predict:
actionVal = actionVal + 1
conn = http.client.HTTPConnection(ipAddress)
actionStr = '/socket1On?action=' + str(actionVal) + '&salt=' + str(salt)
r1 = conn.request('GET',actionStr)
class ShowImage:
def __init__(self, master, path):
print(path)
self.graph = PhotoImage(file=path)
Label(master, image = self.graph).pack()
class MicrowaveGUI:
maxUsage = 0
def __init__(self, master):
self.label = Label(master, text="Control Microwave ")
self.label.pack()
self.threeHourDemo = Button(master, text="3 Hour Auto-Pilot Demo", command=self.threeHourDemoPredict)
self.threeHourDemo.pack()
self.threeHour = Button(master, text="3 Hour Auto-Pilot Current", command=self.threeHourPredict)
self.threeHour.pack()
self.instant = Button(master, text="Instant", command=self.instantPredict)
self.instant.pack()
db = TinyDB('metadata.json')
metadata = Query()
app = db.search(metadata.appliance == 'microwave')
self.maxUsage = app[-1]['maxUsage']
self.model = smartOff.loadMicrowaveModel()
def threeHourPredict(self):
timeGM = time.localtime()
print(timeGM)
ts = time.time() #calendar.timegm(timeGM)
print("PREDICTING FOR", ts)
usage = smartOff.predictOnNext3Hours(self.model, str(ts), self.maxUsage)
labelG = str(timeGM.tm_mon) + '/'+ str(timeGM.tm_mday) + '/' + str(timeGM.tm_year)
labelG = labelG + " From " + str(timeGM.tm_hour) + ":"+str(timeGM.tm_min) + " to "
labelG = labelG + " To " + str(timeGM.tm_hour+3) + ":"+str(timeGM.tm_min)
pyplot.plot(usage, label = labelG)
pyplot.legend()
graphFile = 'images/' + ''.join(random.choice('abcdefghighklmnopqrst') for _ in range(5))+'.png'
pyplot.savefig(graphFile)
pyplot.clf()
img_root = Toplevel()
img_gui = ShowImage(img_root, graphFile)
img_root.mainloop()
def threeHourDemoPredict(self):
print("PREDICT")
usage = smartOff.predictOnNext3Hours(self.model, "1515578400", self.maxUsage)
pyplot.plot(usage, label = "10th Jan 2018 5 AM to 8 AM")
pyplot.legend()
graphFile = 'images/' + ''.join(random.choice('abcdefghighklmnopqrst') for _ in range(5))+'.png'
pyplot.savefig(graphFile)
pyplot.clf()
img_root = Toplevel()
img_gui = ShowImage(img_root, graphFile)
img_root.mainloop()
def instantPredict(self):
timeGM = time.localtime()
print(timeGM)
ts = time.time() #calendar.timegm(timeGM)
print("PREDICTING FOR", ts)
predict = smartOff.predictForSingleTime(self.model, str(ts), self.maxUsage, 'MICROWAVE')
sendSignalToDevice(predict)
if (predict):
messagebox.showinfo("Microwave Operation", "Microwave should be ON. Syncing with device")
else:
messagebox.showinfo("Microwave Operation", "Microwave should be OFF. Syncing with device")
class TVGUI:
maxUsage = 0
def __init__(self, master):
self.label = Label(master, text="Control TV ")
self.label.pack()
self.threeHourDemo = Button(master, text="3 Hour Auto-Pilot Demo", command=self.threeHourDemoPredict)
self.threeHourDemo.pack()
self.threeHour = Button(master, text="3 Hour Auto-Pilot Current", command=self.threeHourPredict)
self.threeHour.pack()
self.instant = Button(master, text="Instant", command=self.instantPredict)
self.instant.pack()
db = TinyDB('metadata.json')
metadata = Query()
app = db.search(metadata.appliance == 'tv')
self.maxUsage = app[-1]['maxUsage']
print(self.maxUsage)
self.model = smartOff.loadTVModel()
def threeHourPredict(self):
timeGM = time.localtime()
ts = time.time() #calendar.timegm(timeGM)
print("PREDICTING FOR", ts)
usage = smartOff.predictOnNext3Hours(self.model, str(ts), self.maxUsage)
labelG = str(timeGM.tm_mon) + '/'+ str(timeGM.tm_mday) + '/' + str(timeGM.tm_year)
labelG = labelG + " From " + str(timeGM.tm_hour) + ":"+str(timeGM.tm_min) + " to "
labelG = labelG + " To " + str(timeGM.tm_hour+3) + ":"+str(timeGM.tm_min)
pyplot.plot(usage, label = labelG)
pyplot.legend()
graphFile = 'images/'+ ''.join(random.choice('abcdefghighklmnopqrst') for _ in range(5))+'.png'
pyplot.savefig(graphFile)
pyplot.clf()
img_root = Toplevel()
img_gui = ShowImage(img_root, graphFile)
img_root.mainloop()
def threeHourDemoPredict(self):
timeGM = time.localtime()
ts = calendar.timegm(timeGM)
print("PREDICTING FOR", ts)
usage = smartOff.predictOnNext3Hours(self.model, str(ts), self.maxUsage)
pyplot.plot(usage, label = "6th July 2017 1 PM to 4 PM")
pyplot.legend()
graphFile = 'images/'+ ''.join(random.choice('abcdefghighklmnopqrst') for _ in range(5))+'.png'
pyplot.savefig(graphFile)
pyplot.clf()
img_root = Toplevel()
img_gui = ShowImage(img_root, graphFile)
img_root.mainloop()
def instantPredict(self):
ts = time.time()
predict = smartOff.predictForSingleTime(self.model, str(ts), self.maxUsage, 'TV')
sendSignalToDevice(predict)
if (predict):
messagebox.showinfo("TV Operation", "TV should be ON. Syncing with device")
else:
messagebox.showinfo("TV Operation", "TV should be OFF. Syncing with device")
class MyFirstGUI:
def __init__(self, master):
self.master = master
master.title("SmartOFF")
self.label = Label(master, text="Turns off appliances automatically")
self.label.pack()
self.microwave = Button(master, text="Microwave", command=self.microwave)
self.microwave.pack()
self.tv = Button(master, text="TV", command=self.tv)
self.tv.pack()
self.close_button = Button(master, text="Close", command=master.quit)
self.close_button.pack()
conn = http.client.HTTPConnection(ipAddress)
def microwave(self):
mw_root = Tk()
mw_gui = MicrowaveGUI(mw_root)
mw_root.mainloop()
def tv(self):
tv_root = Tk()
tv_gui = TVGUI(tv_root)
tv_root.mainloop()
root = Tk()
my_gui = MyFirstGUI(root)
root.mainloop()