-
Notifications
You must be signed in to change notification settings - Fork 8
/
main.py
188 lines (151 loc) · 8.16 KB
/
main.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
from udp_client import *
import sys
from ConfigParser import SafeConfigParser
if sys.version_info < (3, 0):
from Tkinter import *
from tkMessageBox import *
from tkColorChooser import askcolor
import ttk as ttk
else:
from tkinter import *
from tkinter.messagebox import *
from tkinter.colorchooser import askcolor
import tkinter.ttk as ttk
class App:
BACKGROUND = '#efefef'
PORT = 11600
def __init__(self):
self.root = Tk(className='Q Station Controller')
self.root.title('Q Station Controller')
self.root.resizable(0, 0)
self.root.configure(background=self.BACKGROUND)
icon = PhotoImage(file='icons/icon.gif')
self.root.tk.call('wm', 'iconphoto', self.root._w, icon)
self.mainframe = Frame(self.root)
self.mainframe.grid(column=0, row=0, sticky=(N, W, E, S))
self.mainframe.columnconfigure(0, weight=1)
self.mainframe.rowconfigure(0, weight=1)
self.mainframe.configure(background=self.BACKGROUND)
self.ip = StringVar()
self.name = StringVar()
self.color = ()
self.status = IntVar()
self.response = StringVar()
self.item = StringVar()
self.item_id = IntVar()
self.config = SafeConfigParser()
self.config.read('config.ini')
print self.config.sections()
if any('last_Qstation' in sec for sec in self.config.sections()):
self.ip.set(self.config.get('last_Qstation', 'ip'))
else:
self.config.add_section('last_Qstation')
self.bulb_treeview = ttk.Treeview(self.mainframe)
self.bulb_treeview.heading("#0", text="Bulbs")
self.bulb_treeview.grid(column=0, row=0, rowspan=2, sticky=(N, W, E, S))
self.bulb_treeview.bind('<<TreeviewSelect>>', self.callback_bulb_treeview)
Label(self.mainframe, text='Q Station IP').grid(column=1, row=0, sticky=W)
self.ip_entry = Entry(self.mainframe, textvariable=self.ip)
self.ip_entry.grid(column=2, row=0, sticky=(W, E))
img_connect = PhotoImage(file='icons/arrow_refresh.gif')
Button(self.mainframe, command=self.callback_get_bulbs, image=img_connect).grid(column=3, row=0, sticky=(W, E))
self.labelframe = LabelFrame(self.mainframe, text='Bulb Settings')
self.labelframe.grid(column=1, columnspan=3, row=1, sticky=(N, W, E, S))
self.name_entry = Entry(self.labelframe, textvariable=self.name)
self.name_entry.grid(column=2, columnspan=2, row=1, sticky=(W, E))
self.bright_scale = Scale(self.labelframe, from_=0, to=100, orient=HORIZONTAL)
self.bright_scale.set(0)
self.bright_scale.grid(column=2, columnspan=2, row=3, sticky=(N, W, E, S))
Checkbutton(self.labelframe, text=' Turn the bulb on or off', variable=self.status).grid(column=2,
columnspan=2,
row=2,
sticky=(W, E))
img_accept = PhotoImage(file='icons/accept.gif')
Button(self.labelframe, text='Set Values', command=self.callback_set_values, image=img_accept,
compound=LEFT).grid(column=1, columnspan=3, row=5, sticky=(W, E))
self.color_button = Button(self.labelframe, command=self.callback_set_color)
self.color_button.grid(column=2, row=4, sticky=(W, E))
img_color = PhotoImage(file='icons/color_wheel.gif')
Button(self.labelframe, command=self.callback_set_color, image=img_color, height=22).grid(column=3,
row=4,
sticky=(W, E))
Label(self.labelframe, text='Name').grid(column=1, row=1, sticky=W)
Label(self.labelframe, text='Status').grid(column=1, row=2, sticky=W)
Label(self.labelframe, text='Brightness').grid(column=1, row=3, sticky=W)
Label(self.labelframe, text='Color').grid(column=1, row=4, sticky=W)
for child in self.mainframe.winfo_children():
try:
child.grid_configure(padx=10, pady=5)
child.configure(background=self.BACKGROUND)
except:
pass
for child in self.labelframe.winfo_children():
try:
child.grid_configure(padx=10, pady=5)
child.configure(background=self.BACKGROUND)
child.configure(state='disable')
except:
pass
self.ip_entry.focus()
self.root.mainloop()
def callback_set_color(self):
if self.bulb_treeview.selection() != '' and self.item != 'bulbs':
self.color = askcolor(color=(int(self.color[0][0]), int(self.color[0][1]), int(self.color[0][2])))
self.color_button.config(background=self.color[1])
else:
showinfo('Info', 'Please select the bulb you want to control.')
def callback_set_values(self):
if self.bulb_treeview.selection() != '' and self.item != 'bulbs':
udp_client = UdpClient(self.ip.get(), self.PORT)
udp_client.set_light(self.bright_scale.get(),
self.color[0][0],
self.color[0][1],
self.color[0][2],
self.status.get(),
self.response['led'][self.item_id]['sn'])
udp_client.set_title(self.response['led'][self.item_id]['sn'],
self.name.get())
else:
showinfo('Info', 'Please select the bulb you want to control.')
def callback_get_bulbs(self):
if self.ip.get() != '':
udp_client = UdpClient(self.ip.get(), self.PORT)
self.response = udp_client.get_lights()
self.config.set('last_Qstation', 'ip', self.ip.get())
map(self.bulb_treeview.delete, self.bulb_treeview.get_children())
self.bulb_treeview.insert('', 'end', 'bulbs', text='Q Station', open=True)
for i in range(len(self.response['led'])):
if int(self.response['led'][i]['online']) == 1:
status = 'Online'
else:
status = 'Offline'
self.bulb_treeview.insert('bulbs',
'end',
text=self.response['led'][i]['title'] + ' (' + status + ')',
tag=self.response['led'][i]['sn'])
for child in self.labelframe.winfo_children():
child.configure(state='normal')
else:
showerror('Error', 'Please fill in the Q Station IP first.')
def callback_bulb_treeview(self, event):
self.item = self.bulb_treeview.selection()[0]
if self.item != 'bulbs':
self.item_id = int(self.bulb_treeview.selection()[0][3]) - 1
self.name_entry.delete(0, END)
self.name_entry.insert(0, self.response['led'][self.item_id]['title'])
self.bright_scale.set(int(self.response['led'][self.item_id]['bright']))
self.color_button.config(background=self.rgb_to_hex((int(self.response['led'][self.item_id]['r']),
int(self.response['led'][self.item_id]['g']),
int(self.response['led'][self.item_id]['b']))))
self.color = ((self.response['led'][self.item_id]['r'],
self.response['led'][self.item_id]['g'],
self.response['led'][self.item_id]['b']),
'')
self.status.set(int(self.response['led'][self.item_id]['iswitch']))
def rgb_to_hex(self, rgb):
return '#%02x%02x%02x' % rgb
def __del__(self):
with open('config.ini', 'w') as f:
self.config.write(f)
if __name__ == '__main__':
app = App()