-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
166 lines (135 loc) · 5.58 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
from kivy.config import Config
Config.set('graphics', 'width', '400')
Config.set('graphics', 'height', '900')
from kivymd.app import MDApp
import screen_helper
from kivy.lang import Builder
import socket
from kivymd.uix.button import MDFlatButton
from kivymd.uix.dialog import MDDialog
from kivy.uix.boxlayout import BoxLayout
import ipaddress
from kivy.clock import Clock
import time
class Dialog(BoxLayout):
pass
class LockApp(MDApp):
# layout_parent = ObjectProperty(None)
screen = None
def __init__(self, **kwargs):
super(LockApp, self).__init__(**kwargs)
self.serverAddress = ('xxx.xxx.xxx.xxx', 2222)
self.UDPClient = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
self.UDPClient.setblocking(False)
self.bufferSize_1 = 1024
self.bufferSize_2 = 1024
self.theme_cls.primary_palette = "Red"
self.theme_cls.primary_hue = '700'
self.theme_cls.material_style = "M2"
self.theme_cls.theme_style = "Dark"
self.dialog = None
self.lOCK_RELEASE_TIME = 6 # seconds
self.timeout = 30 # [seconds]
def build(self):
self.screen = Builder.load_string(screen_helper.screen_helper)
return self.screen
def set_server(self):
if not self.dialog:
self.dialog = MDDialog(
title="Change Server Address:",
type="custom",
content_cls=Dialog(),
buttons=[
MDFlatButton(
text="CANCEL",
theme_text_color="Custom",
text_color=self.theme_cls.primary_color,
on_release=self.dialog_close
),
MDFlatButton(
text="OK",
theme_text_color="Custom",
text_color=self.theme_cls.primary_color,
on_release=self.update_server
),
],
)
self.dialog.content_cls.ids.sever_address.text = self.serverAddress[0]
self.dialog.content_cls.ids.sever_address.hint_text = "Server Address"
self.dialog.content_cls.ids.sever_address.text_color_normal = 'red'
self.dialog.open()
def dialog_close(self, *args):
self.dialog.dismiss(force=True)
def update_server(self, obj):
if self.is_ipv4(self.dialog.content_cls.ids.sever_address.text):
self.serverAddress = list(self.serverAddress)
self.serverAddress[0] = self.dialog.content_cls.ids.sever_address.text
self.serverAddress = tuple(self.serverAddress)
self.dialog_close()
else:
self.dialog.content_cls.ids.sever_address.hint_text = "wrong format!"
self.dialog.content_cls.ids.sever_address.text_color_normal = 'red'
self.dialog.content_cls.ids.sever_address.text = self.serverAddress[0]
@staticmethod
def is_ipv4(string):
try:
ipaddress.IPv4Network(string)
return True
except ValueError:
return False
@staticmethod
def exit_app():
LockApp().stop()
def lock_cmd(self):
cmd = 'unlock'
cmd_encoded = cmd.encode('utf-8')
self.UDPClient.sendto(cmd_encoded, self.serverAddress)
timeout_start = time.time()
while time.time() < timeout_start + self.timeout:
try:
cmd_confirmation, address = self.UDPClient.recvfrom(self.bufferSize_1)
except OSError:
pass
else:
cmd_decoded = cmd_confirmation.decode('utf-8')
# print(cmd_decoded, address)
if cmd_decoded == "unlocked":
self.screen.ids.BottomAppBar.icon_color = (0, 1, 0, 1)
self.screen.ids.BottomAppBar.icon = "lock-open-variant"
self.screen.ids.BottomAppBar.title = "unlocked"
Clock.schedule_once(self.icon_color_reset, self.lOCK_RELEASE_TIME)
break
# to be removed after server implementation
# self.screen.ids.BottomAppBar.icon_color = (0, 1, 0, 1)
# self.screen.ids.BottomAppBar.icon = "lock-open-variant"
# self.screen.ids.BottomAppBar.title = "unlocked"
# Clock.schedule_once(self.icon_color_reset, self.lOCK_RELEASE_TIME)
def icon_color_reset(self, dt):
self.screen.ids.BottomAppBar.icon_color = self.theme_cls.primary_color
self.screen.ids.BottomAppBar.title = "released"
self.screen.ids.BottomAppBar.icon = "lock"
Clock.schedule_once(self.clean_toolbar, self.lOCK_RELEASE_TIME)
def clean_toolbar(self, dt):
self.screen.ids.BottomAppBar.title = ""
def temp_hum_cmd(self):
cmd = 'temperature'
cmd_encoded = cmd.encode('utf-8')
self.UDPClient.sendto(cmd_encoded, self.serverAddress)
timeout_start = time.time()
while time.time() < timeout_start + self.timeout:
try:
message, address = self.UDPClient.recvfrom(self.bufferSize_2)
except OSError:
pass
else:
message_decoded = message.decode('utf-8')
data = message_decoded.split(',')
# print(message_decoded, address)
if len(data) == 2:
temperature = data[0] + '°C'
humidity = data[1] + '%'
self.screen.ids.tf_temp.text = temperature
self.screen.ids.tf_hum.text = humidity
break
if __name__ == '__main__':
LockApp().run()