This repository has been archived by the owner on Dec 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
202 lines (179 loc) · 4.87 KB
/
utils.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
from config import *
from time import asctime, localtime, time
from kivymd.toast import toast
def throw(msg_type, message, conn=None, is_toasted=True):
"""
A function to both log and toast messages.
:param msg_type: string, "INFO", "ERROR"...
:param message: string
:param conn:
:param is_toasted: Default True
:return:
"""
logfile = open(f"{LOGFILE}.log", "a")
_time = asctime(localtime(time()))
if conn:
print(f"[{msg_type}]\t{message} ({conn})")
logfile.write(f"[{msg_type}][{_time}]\t{message} ({conn}\n")
if is_toasted:
toast(f"[{msg_type}] {message} ({conn})")
else:
print(f"[{msg_type}]\t{message}")
logfile.write(f"[{msg_type}][{_time}]\t{message}\n")
if is_toasted:
toast(f"[{msg_type}] {message}")
logfile.close()
def setting_str2num(setting):
"""
A function to convert SETTING strigns to right numbers.
:param setting: string
:return: integer
"""
if setting == "tempshield_status":
return 1
elif setting == "jammer_status":
return 2
elif setting == "light_status":
return 3
elif setting == "car_status":
return 4
elif setting == "direction":
return 5
elif setting == "direction_angle":
return 6
elif setting == "confirmation":
return 7
elif setting == "battery_percentage":
return 8
elif setting == "outside_temperature":
return 9
elif setting == "inside_temperature":
return 10
elif setting == "peltier_right":
return 11
elif setting == "peltier_left":
return 12
elif setting == "peltier_front":
return 13
elif setting == "peltier_back":
return 14
elif setting == "peltier_top":
return 15
elif setting == "!dis":
return 16
else:
pass
def setting_num2str(setting):
"""
A function to convert SETTING numbers to right strings.
:param setting: integer
:return: string
"""
if setting == 1:
return "tempshield_status"
elif setting == 2:
return "jammer_status"
elif setting == 3:
return "light_status"
elif setting == 4:
return "car_status"
elif setting == 5:
return "direction"
elif setting == 6:
return "direction_angle"
elif setting == 7:
return "confirmation"
elif setting == 8:
return "battery_percentage"
elif setting == 9:
return "outside_temperature"
elif setting == 10:
return "inside_temperature"
elif setting == 11:
return "peltier_right"
elif setting == 12:
return "peltier_left"
elif setting == 13:
return "peltier_front"
elif setting == 14:
return "peltier_back"
elif setting == 15:
return "peltier_top"
elif setting == 16:
return "!dis"
else:
return 0
def type_str2num(typey):
"""
A function to convert TYPE strings to right numbers.
:param typey: string
:return: string
"""
if typey == "ERROR":
return 1
elif typey == "INFO":
return 2
elif typey == "START":
return 3
elif typey == "STOP":
return 4
elif typey == "CLIENT":
return 5
elif typey == "CONF":
return 6
else:
return 0
def type_num2str(typey):
"""
A function to convery TYPE numbers to right strings.
:param typey: int
:return: string
"""
if typey == 1:
return "ERROR"
elif typey == 2:
return "INFO"
elif typey == 3:
return "START"
elif typey == 4:
return "STOP"
elif typey == 5:
return "CLIENT"
elif typey == 6:
return "CONF"
else:
return "OPS"
def message_creator(id_number, type_str, setting_str, value):
"""
A function to create a string with combining params via ":"
delimeter. This function is inverse of message_splitter.
:param id_number: int
:param type_str: str
:param setting_str: str
:param value: str/int/bool
:return: str
"""
message = str(id_number) + ":" + \
str(type_str2num(type_str)) + ":" + \
str(setting_str2num(setting_str)) + ":" + \
str(value)
return message
def message_splitter(message):
"""
A function to split string message which contains ":"
delimeter into 4-indexed list. This function is inverse
of message_creator.
:param message: str
:return: [ID (int), Type (str), Setting (str), Value (str/int/bool)]
"""
array = message.split(":")
array[0] = int(array[0])
array[1] = type_num2str(int(array[1]))
array[2] = setting_num2str(int(array[2]))
if array[3].isnumeric():
array[3] = int(array[3])
elif array[3].lower() == 'true':
array[3] = True
elif array[3].lower() == 'false':
array[3] = False
return array