From 4b14e004a003ca1d0ce8893603f47d7c63e07807 Mon Sep 17 00:00:00 2001 From: Dustin Pianalto Date: Sat, 7 Sep 2019 21:57:32 -0800 Subject: [PATCH] Initial commit --- .idea/misc.xml | 7 ++++ .idea/modules.xml | 8 +++++ .idea/sockets.iml | 11 ++++++ client_test.py | 20 +++++++++++ gui_client.py | 91 +++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 137 insertions(+) create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/sockets.iml create mode 100755 client_test.py create mode 100644 gui_client.py diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..8656114 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,7 @@ + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..84d9b4d --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/sockets.iml b/.idea/sockets.iml new file mode 100644 index 0000000..6711606 --- /dev/null +++ b/.idea/sockets.iml @@ -0,0 +1,11 @@ + + + + + + + + + + \ No newline at end of file diff --git a/client_test.py b/client_test.py new file mode 100755 index 0000000..9c0d0ca --- /dev/null +++ b/client_test.py @@ -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')) + diff --git a/gui_client.py b/gui_client.py new file mode 100644 index 0000000..adc4fc6 --- /dev/null +++ b/gui_client.py @@ -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()