-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlcd_counter.py
366 lines (334 loc) · 12 KB
/
lcd_counter.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
import time
import csv
from datetime import datetime, date, timedelta
from mfrc522 import SimpleMFRC522
import I2C_LCD_driver
from gpiozero import Button, InputDevice, OutputDevice
import pickle
import mysql.connector
import os
with open("/etc/hostname", "r") as hn:
pi = hn.readline().rstrip("\n")
count_num = int(''.join(i for i in pi if i.isdigit()))
shot_sig = Button(4)
button1 = Button(26)
button2 = Button(12)
sig_out = OutputDevice(17)
reader = SimpleMFRC522()
lcd = I2C_LCD_driver.lcd()
csv_path = "/home/pi/Documents/CSV/"
count_pkl = "/home/pi/Documents/counts.pickle"
prod_vars_pkl = "/home/pi/Documents/vars.pickle"
file_path = ""
logon = "LOG_ON"
logout = "LOG_OFF"
timeout = "TIME_OUT"
mas = "MAS"
mae = "MAE"
shot = "SHOT"
modes = {"setup": 0,
"standby": 1,
"menu": 2,
"run": 3,
"maint": 4
}
mode = modes["standby"]
startup = True
maint_msg = "Maintenance"
maint_end_msg = "Maintenance End"
invalid_msg = "Invalid Info"
menu_msg_top = "Reset Counter?"
menu_msg_btm = "Bl=Yes Red=No"
count_reset_msg = "Counter= 0"
logoutm = "Logged Out"
timeoutm = "Timed Out"
db_name = "device_vars"
try:
db_host = os.environ.get("DB_HOST_1")
db_user = os.environ.get("DB_USER_1")
db_psw = os.environ.get("DB_PSW_1")
except:
pass
def read_pckl_counts(pcklfile):
pickle_in = open(pcklfile, "rb")
pkl_dict = pickle.load(pickle_in)
return pkl_dict
def save_vars(dict, pkl_file):
with open(pkl_file, "wb") as pckl:
pickle.dump(dict, pckl)
prod_vars_dict = read_pckl_counts(prod_vars_pkl)
def read_machvars_db(count_num=count_num):
# Attempts to pull current production variables from DB, or else from Pickle Files
try:
conn = mysql.connector.connect(
host=db_host,
user=db_user,
passwd=db_psw,
database=db_name
)
c = conn.cursor()
c.execute("SELECT part FROM data_vars WHERE device_id=%s", (count_num,))
part = c.fetchone()
part = str(part[0])
c.execute("SELECT machine FROM data_vars WHERE device_id=%s", (count_num,))
mach = c.fetchone()
mach = str(mach[0])
c.execute("SELECT count_goal FROM data_vars WHERE device_id = %s", (count_num,))
countset = c.fetchone()
countset = int(countset[0])
c.close()
except:
try:
part = str(prod_vars_dict['part'])
mach = str(prod_vars_dict['mach'])
countset = int(prod_vars_dict['countset'])
except:
part = 9999
mach = 99
countset = 0
return part, mach, countset
def ret_emp_name(id_num):
try:
conn = mysql.connector.connect(
host=db_host,
user=db_user,
passwd=db_psw,
database=db_name
)
c = conn.cursor()
c.execute("SELECT name FROM employees WHERE emp_id=%s", (id_num,))
emp_name=c.fetchone()
emp_name=str(emp_name[0])
c.close()
return emp_name
except:
return None
def evaluate(part, mach, countset, dicti):
b = False
try:
if part and len(part) != 0:
if mach and len(mach) != 0:
if type(countset) == int:
b = True
dicti['part'] = part
dicti['mach'] = mach
dicti['countset'] = countset
except:
b = False
return b, dicti
def update_counts(totalcount, runcount):
global count_dict
totalcount +=1
runcount +=1
count_dict['totalcount'] = totalcount
count_dict['runcount'] = runcount
return totalcount, runcount
def count_reset(runcount):
global count_dict
runcount = 0
count_dict['runcount'] = runcount
save_vars(count_dict, count_pkl)
lcd.clear()
lcd.message("PRESS BLK BTN",1)
lcd.message("TO RESET", 2)
return runcount
def invalid_params():
lcd.clear()
lcd.message(invalid_msg)
time.sleep(10)
lcd.clear()
def create_file_path(day, path=csv_path):
filename = day.strftime("%Y%m%d") + f"{pi}.csv"
file_path = path + filename
return file_path
def create_csv(file):
"""creates a new csv file, inserts a header"""
with open(file, "a", newline="") as fa, \
open(file, "r", newline='') as fr:
writer = csv.writer(fa, delimiter=",")
line = fr.readline()
if not line: # if CSV is empty, add header
header = ("Type", "pi", "Machine", "Part",
"User_ID", "Time", "Date")
writer.writerow(header)
def add_timestamp(cat, file):
"""opens or creates a csv file with todays date in
filename. Adds timestamp to that csv including machine
number, part number, id number, user, time, date"""
now = time.strftime("%H:%M:%S")
data = (cat, count_num, mach_num, part_num, emp_num, now, today)
with open(file, "a", newline="") as fa:
writer = csv.writer(fa, delimiter=",")
writer.writerow(data)
def update_csv():
today = date.today()
file_path = create_file_path(day=today)
create_csv(file=file_path)
return today, file_path
def display_run_info(last_display, last_disp_time):
lcd.message(run_msg_btm, 2)
if datetime.now() > last_disp_time + timedelta(seconds=5):
if last_display != 1:
lcd.message(" ",1)
lcd.message(run_msg_top1, 1)
last_display = 1
else:
lcd.message(" ",1)
lcd.message(run_msg_top2, 1)
last_display = 0
last_disp_time = datetime.now()
return last_display, last_disp_time
def change_msg(msg, sec=1, line=1):
lcd.clear()
lcd.message(msg, line)
time.sleep(sec)
def logout_func(file_path):
sig_out.off()
add_timestamp(logout, file_path)
change_msg(logoutm, sec=1)
try:
while True:
if mode == modes["standby"]:
emp_name = None
emp_num = None
idn = None
count_dict = read_pckl_counts(count_pkl)
total_count = count_dict['totalcount']
run_count = count_dict['runcount']
part_num, mach_num, countset = read_machvars_db()
test, prod_vars_dict = evaluate(part_num, mach_num, countset, prod_vars_dict)
if test is True:
save_vars(prod_vars_dict, prod_vars_pkl)
lcd.clear()
standby_info_top = f"Part:{part_num}"
standby_info_btm = f"Cnt:{total_count} Mch:{mach_num}"
lcd.message(standby_info_top, 1)
lcd.message(standby_info_btm, 2)
if startup is True:
today, file_path = update_csv()
mode = modes["standby"]
while mode == modes["standby"]:
if date.today() != today:
today, file_path = update_csv()
idn, emp_num = reader.read_no_block()
if emp_num != None:
emp_num = emp_num.strip()
if emp_num == '':
pass
else:
emp_name = ret_emp_name(emp_num)
emp_count = 0
add_timestamp(logon, file_path)
mode = modes["run"]
if button2.is_pressed:
button2.wait_for_release()
part_num, mach_num, countset = read_machvars_db()
test, prod_vars_dict = evaluate(part_num, mach_num, countset, prod_vars_dict)
if test is True:
save_vars(prod_vars_dict, prod_vars_pkl)
standby_info_top = f"Part:{part_num}"
standby_info_btm = f"Cnt:{total_count} Mch:{mach_num}"
lcd.clear()
lcd.message(standby_info_top, 1)
lcd.message(standby_info_btm, 2)
if button1.is_pressed:
button1.wait_for_release()
time.sleep(0.2)
mode = modes["menu"]
else:
invalid_params()
startup = False
elif mode == modes["menu"]:
lcd.clear()
time.sleep(.5)
lcd.message(menu_msg_top, 1)
lcd.message(menu_msg_btm,2)
while mode == modes["menu"]:
if button2.is_pressed:
button2.wait_for_release()
count_dict["totalcount"] = 0
count_dict["runcount"] = 0
save_vars(count_dict, count_pkl)
change_msg(count_reset_msg, sec=3)
mode = modes["standby"]
if button1.is_pressed:
button1.wait_for_release()
mode = modes["standby"]
lcd.clear()
elif mode == modes["run"]:
sig_out.on()
run_msg_top1 = f"Part: {part_num} "
if emp_name != None:
run_msg_top2 = f"Emp: {emp_name}"
else:
run_msg_top2 = f"Emp: {emp_num}"
last_display = 0
last_disp_time = datetime.now()
now = datetime.now()
lcd.clear()
lcd.message(run_msg_top2, 1)
while mode == modes["run"]:
run_msg_btm = f"Cnt:{emp_count}, {total_count}"
last_display, last_disp_time = display_run_info(last_display, last_disp_time)
if countset == 0:
pass
elif run_count == countset:
sig_out.off()
run_count = count_reset(run_count)
button2.wait_for_press()
button2.wait_for_release()
lcd.clear()
lcd.message(run_msg_top2, 1)
sig_out.on()
if shot_sig.is_pressed:
shot_sig.wait_for_release()
emp_count += 1
total_count, run_count = update_counts(total_count, run_count)
save_vars(count_dict, count_pkl)
add_timestamp(shot, file_path)
now = datetime.now()
time.sleep(0.1)
if datetime.now() >= now + timedelta(seconds=300):
add_timestamp(timeout, file_path)
sig_out.off()
change_msg(timeoutm, sec=5)
mode = modes["standby"]
if button1.is_pressed:
button1.wait_for_release()
logout_func(file_path)
mode = modes["standby"]
if button2.is_pressed:
button2.wait_for_release()
sig_out.off()
mode = modes["maint"]
elif mode == modes["maint"]:
add_timestamp(mas, file_path)
change_msg(maint_msg)
while mode == modes["maint"]:
if button1.is_pressed:
button1.wait_for_release()
add_timestamp(mae, file_path)
logout_func(file_path)
mode = modes["standby"]
if button2.is_pressed:
button2.wait_for_release()
add_timestamp(mae, file_path)
change_msg(maint_end_msg, sec=1)
mode = modes["run"]
except KeyboardInterrupt:
lcd.clear()
except Exception as e:
lcd.clear()
lcd.message("ERROR")
print(e)
Prod_vars_dict = {"part": 0,
"mach": 0,
"countset": 0}
counts_dict = {"totalcount": 0,
"runcount": 0}
vars_file = "/home/pi/Documents/vars.pickle"
count_file = "/home/pi/Documents/counts.pickle"
save_vars(Prod_vars_dict, vars_file)
save_vars(counts_dict, count_file)
time.sleep(5)
os.system("reboot")