-
Notifications
You must be signed in to change notification settings - Fork 0
/
moosekettle.py
executable file
·295 lines (262 loc) · 10.8 KB
/
moosekettle.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
#!/usr/bin/python -t
# Simple attempt at a gui for my ikettle
#
# I'm a perl guy, but they'll laugh at me at work if I wrote this in perl/tk
#
# protocol: http://www.awe.com/mark/blog/20140223.html
#
# things to do:
# nice interface with shiny buttons and circles
# sound sample when boiled
# scan for IP (look for 2000 open, then HELLOAPP stuff)
# remember last working IP to save above
# get rid of connect button, just try to connect every so often
# or rescan or something else
#
# Mark J Cox 2014
import gtk
import pango
import gobject, socket
cnf = "moosekettle.cfg"
class MooseKettle(gtk.Window):
def __init__(self, configip, argip):
super(MooseKettle, self).__init__()
self.kettleconnected = 0
self.configip = configip
self.ip = configip
if (argip):
self.ip = argip
# self.set_decorated(False)
self.resize(400,34)
self.connect("destroy", self.gotofail)
box = gtk.HBox()
self.add(box)
self.bconnect = gtk.Button("connect")
self.bconnect.connect("clicked", self.clickbconnect)
box.pack_start(self.bconnect)
self.bconnect.show()
self.bboil = gtk.ToggleButton("boil")
self.bboil.connect("clicked", self.clickboil)
self.bboil.show()
box.pack_start(self.bboil)
self.b100 = gtk.ToggleButton("100")
self.b100.connect("clicked", self.clicksend, "set sys output 0x80")
box.pack_start(self.b100)
self.b100.show()
self.b95 = gtk.ToggleButton("95")
self.b95.connect("clicked", self.clicksend, "set sys output 0x2")
box.pack_start(self.b95)
self.b95.show()
self.b80 = gtk.ToggleButton("80")
self.b80.connect("clicked", self.clicksend, "set sys output 0x4000")
box.pack_start(self.b80)
self.b80.show()
self.b65 = gtk.ToggleButton("65")
self.b65.connect("clicked", self.clicksend, "set sys output 0x200")
box.pack_start(self.b65)
self.b65.show()
self.bwarm = gtk.ToggleButton("warm")
self.bwarm.connect("clicked", self.clicksend, "set sys output 0x8")
box.pack_start(self.bwarm)
self.bwarm.show()
self.setbuttons(False) # initially not connected
button = gtk.Button("quit")
button.connect("clicked", self.gotofail)
box.pack_start(button)
button.show()
box.show()
self.show_all()
self.kconnect()
def setbuttons(self, status):
self.bboil.set_sensitive(status)
self.b100.set_sensitive(status)
self.b95.set_sensitive(status)
self.b80.set_sensitive(status)
self.b65.set_sensitive(status)
self.bwarm.set_sensitive(status)
def clickboil(self, widget, data=None):
if widget.get_active():
self.kettlesend("set sys output 0x4")
else:
self.kettlesend("set sys output 0x0")
def clicksend(self, widget, data=None):
widget.handler_block_by_func(self.clicksend)
if widget.get_active():
self.kettlesend(data)
widget.set_active(False)
else:
widget.set_active(True)
widget.handler_unblock_by_func(self.clicksend)
def kettlesend(self, data):
# print data
self.sock.send(data+"\n")
def clickbconnect(self, widget):
# print "Connecting...."
self.kconnect()
def gotofail(self, widget):
# print "Closing"
try:
self.sock.close()
except:
pass
gtk.main_quit()
def expose(self, widget, event):
# redundant function to draw a nice circle for the GUI
w, h = widget.window.get_size()
xgc = widget.window.new_gc()
xgc.set_rgb_fg_color(gtk.gdk.color_parse("green"))
xgc.line_width = 3
widget.window.draw_arc(xgc, False, 5, 5, 80, 80, 0*64, 360*64)
context = self.create_pango_context()
self.layout = self.create_pango_layout("moose")
desc = pango.FontDescription('Sans 14')
self.layout.set_font_description(desc)
widget.window.draw_layout(xgc, 20, 35, layout=self.layout)
def kconnect(self):
if (not self.ip):
self.ip = self.get_ip("Enter IP Address of Kettle","127.0.0.1")
if (not self.ip):
return
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
self.sock.connect((self.ip,2000))
self.sock.send("HELLOKETTLE\n")
gobject.io_add_watch(self.sock, gobject.IO_IN, self.handler)
gobject.timeout_add(5000, self.check_connected)
except:
message = gtk.MessageDialog(parent=None, flags=0, type=gtk.MESSAGE_INFO,
buttons=gtk.BUTTONS_NONE,message_format=None);
message.set_markup("Failed to connect to kettle")
message.show()
self.ip="" # the one given didnt work
def get_ip(self, text, default=''):
msgd = gtk.MessageDialog(self, gtk.DIALOG_MODAL | gtk.DIALOG_DESTROY_WITH_PARENT,
gtk.MESSAGE_QUESTION, gtk.BUTTONS_OK_CANCEL, text)
entry = gtk.Entry()
entry.set_text(default)
entry.show()
msgd.vbox.pack_end(entry)
entry.connect('activate', lambda _: msgd.response(gtk.RESPONSE_OK))
msgd.set_default_response(gtk.RESPONSE_OK)
msgr = msgd.run()
text = entry.get_text().decode('utf8')
msgd.destroy()
if (msgr == gtk.RESPONSE_OK):
return text
else:
return None
def check_connected(self):
if (self.kettleconnected == 0):
try:
self.sock.close()
except:
pass
message = gtk.MessageDialog(parent=None, flags=0, type=gtk.MESSAGE_INFO,
buttons=gtk.BUTTONS_NONE,message_format=None);
message.set_markup("Failed to connect to kettle")
message.show()
def setbutton(self, button, status):
button.handler_block_by_func(self.clicksend)
button.set_active(status)
button.handler_unblock_by_func(self.clicksend)
def handler(self, conn, *args):
line = conn.recv(4096)
if not len(line): # "Connection closed."
self.bconnect.set_sensitive(True)
self.setbuttons(False) # not connected
self.kconnect()
return False
else:
for myline in line.splitlines():
# print "got a line: "+myline
if (myline.startswith("HELLOAPP")):
self.kettleconnected = 1
self.setbuttons(True) # connected
self.bconnect.set_sensitive(False)
self.writeconfig(self.ip)
conn.send("get sys status\n")
if (myline.startswith("sys status key=")):
if (len(myline)<16):
key = 0
else:
key = ord(myline[15]) & 0x3f
self.setbutton(self.b100,key&0x20)
self.setbutton(self.b95,key&0x10)
self.setbutton(self.b80,key&0x8)
self.setbutton(self.b65,key&0x4)
self.setbutton(self.bwarm,key&0x2)
self.bboil.handler_block_by_func(self.clickboil)
self.bboil.set_active(key&0x1)
self.bboil.handler_unblock_by_func(self.clickboil)
if (myline == "sys status 0x100"):
self.setbutton(self.b100,True)
self.setbutton(self.b95,False)
self.setbutton(self.b80,False)
self.setbutton(self.b65,False)
elif (myline == "sys status 0x95"):
self.setbutton(self.b95,True)
self.setbutton(self.b100,False)
self.setbutton(self.b80,False)
self.setbutton(self.b65,False)
elif (myline == "sys status 0x80"):
self.setbutton(self.b80,True)
self.setbutton(self.b100,False)
self.setbutton(self.b95,False)
self.setbutton(self.b65,False)
elif (myline == "sys status 0x65"):
self.setbutton(self.b65,True)
self.setbutton(self.b100,False)
self.setbutton(self.b95,False)
self.setbutton(self.b80,False)
elif (myline == "sys status 0x11"):
self.setbutton(self.bwarm,True)
elif (myline == "sys status 0x10"):
self.setbutton(self.bwarm,False)
elif (myline == "sys status 0x5"):
self.bboil.handler_block_by_func(self.clickboil)
self.bboil.set_active(True)
self.bboil.handler_unblock_by_func(self.clickboil)
elif (myline == "sys status 0x0"):
self.setbutton(self.b100,False)
self.setbutton(self.b95,False)
self.setbutton(self.b80,False)
self.setbutton(self.b65,False)
self.setbutton(self.bwarm,False)
self.bboil.handler_block_by_func(self.clickboil)
self.bboil.set_active(False)
self.bboil.handler_unblock_by_func(self.clickboil)
elif (myline == "sys status 0x3"):
message = gtk.MessageDialog(parent=None, flags=0, type=gtk.MESSAGE_INFO,
buttons=gtk.BUTTONS_NONE,message_format=None);
message.set_markup("Your Kettle Has Boiled")
message.show()
elif (myline == "sys status 0x1"):
message = gtk.MessageDialog(parent=None, flags=0, type=gtk.MESSAGE_INFO,
buttons=gtk.BUTTONS_NONE,message_format=None);
message.set_markup("Your Kettle Was Removed")
message.show()
return True
def writeconfig(self,ip):
if (ip == self.configip):
return
try:
config.add_section('Main')
except:
pass
config.set('Main','ip',ip)
with open(cnf, 'wb') as configfile:
config.write(configfile)
import argparse
import ConfigParser
config = ConfigParser.RawConfigParser()
configip = None
try:
config.read(cnf)
configip = config.get('Main','ip')
except:
pass
parser = argparse.ArgumentParser(description="Simple GUI interface to the ikettle")
parser.add_argument('-i','--ip',help='IP address for kettle', required=False)
args = vars(parser.parse_args())
MooseKettle(configip,args['ip'])
gtk.main()