commit
4b14e004a0
@ -0,0 +1,7 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="JavaScriptSettings">
|
||||
<option name="languageLevel" value="ES6" />
|
||||
</component>
|
||||
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.7" project-jdk-type="Python SDK" />
|
||||
</project>
|
||||
@ -0,0 +1,8 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ProjectModuleManager">
|
||||
<modules>
|
||||
<module fileurl="file://$PROJECT_DIR$/.idea/sockets.iml" filepath="$PROJECT_DIR$/.idea/sockets.iml" />
|
||||
</modules>
|
||||
</component>
|
||||
</project>
|
||||
@ -0,0 +1,11 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module type="PYTHON_MODULE" version="4">
|
||||
<component name="NewModuleRootManager">
|
||||
<content url="file://$MODULE_DIR$" />
|
||||
<orderEntry type="inheritedJdk" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
</component>
|
||||
<component name="TestRunnerService">
|
||||
<option name="PROJECT_TEST_RUNNER" value="Unittests" />
|
||||
</component>
|
||||
</module>
|
||||
@ -0,0 +1,20 @@
|
||||
#!/usr/bin/env python
|
||||
|
||||
import time
|
||||
import socket
|
||||
|
||||
host = 'bedroom-door.djpianalto.com'
|
||||
port = 22222
|
||||
socksize = 1024
|
||||
|
||||
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||
s.connect((host, port))
|
||||
|
||||
print('conected')
|
||||
|
||||
while True:
|
||||
cmd = input('$ ').encode('utf-8')
|
||||
s.send(cmd)
|
||||
if cmd == b'state':
|
||||
print('\n' + s.recv(socksize).decode('utf-8'))
|
||||
|
||||
@ -0,0 +1,91 @@
|
||||
#!/usr/bin/env python
|
||||
|
||||
import time
|
||||
import socket
|
||||
import tkinter
|
||||
|
||||
host = 'bedroom-door.djpianalto.com'
|
||||
port = 22222
|
||||
socksize = 1024
|
||||
|
||||
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||
s.connect((host, port))
|
||||
|
||||
root = tkinter.Tk()
|
||||
|
||||
current_state = 'red'
|
||||
|
||||
|
||||
class MyButton(tkinter.Button):
|
||||
def __init__(self, *args, width=20, height=10, **kwargs):
|
||||
super().__init__(*args, width=width, height=height, command=self.toggle, **kwargs)
|
||||
self.current_state = 'green'
|
||||
self.previous_state = self.current_state
|
||||
self.alert = False
|
||||
self.able_to_update = True
|
||||
self.process()
|
||||
self.timer()
|
||||
|
||||
def process(self):
|
||||
s.send(self.current_state.encode('utf-8'))
|
||||
self.able_to_update = True
|
||||
self.update_state()
|
||||
|
||||
def toggle(self):
|
||||
if self.alert:
|
||||
self.able_to_update = False
|
||||
s.send(b'clear button')
|
||||
self.current_state = self.previous_state
|
||||
time.sleep(0.5)
|
||||
else:
|
||||
if self.current_state == 'red':
|
||||
self.current_state = 'green'
|
||||
elif self.current_state == 'green':
|
||||
self.current_state = 'red'
|
||||
|
||||
self.process()
|
||||
|
||||
def timer(self):
|
||||
self.update_state()
|
||||
self.after(100, self.timer)
|
||||
|
||||
def update_state(self):
|
||||
if not self.able_to_update:
|
||||
return
|
||||
s.send(b'state')
|
||||
state = s.recv(socksize).decode('utf-8')
|
||||
if '|alert' in state:
|
||||
self.previous_state = self.current_state
|
||||
self.current_state = state.split('|')[0]
|
||||
self.alert = True
|
||||
else:
|
||||
self.current_state = state
|
||||
self.alert = False
|
||||
|
||||
if self.current_state == 'red':
|
||||
self.configure(bg="#ff0000")
|
||||
self.configure(activebackground="#ff0000")
|
||||
elif self.current_state == 'green':
|
||||
self.configure(bg='#00ff00', activebackground='#00ff00')
|
||||
elif self.current_state == 'None':
|
||||
self.configure(bg='#000000')
|
||||
self.configure(activebackground='#000000')
|
||||
|
||||
|
||||
def on_close():
|
||||
s.send(b'clear lights')
|
||||
root.destroy()
|
||||
s.close()
|
||||
|
||||
|
||||
def main():
|
||||
frame = tkinter.Frame()
|
||||
frame.pack()
|
||||
b = MyButton(frame)
|
||||
b.pack()
|
||||
root.protocol('WM_DELETE_WINDOW', on_close)
|
||||
root.mainloop()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
Loading…
Reference in new issue