-
Notifications
You must be signed in to change notification settings - Fork 0
/
barcodescanner.py
79 lines (64 loc) · 2.9 KB
/
barcodescanner.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
import gspread, datetime, subprocess, requests, serial, traceback
from termcolor import colored
from gspread_formatting import *
from oauth2client.service_account import ServiceAccountCredentials
scope = ['https://spreadsheets.google.com/feeds', 'https://www.googleapis.com/auth/drive']
creds = ServiceAccountCredentials.from_json_keyfile_name('client-secrets.json', scope)
client = gspread.authorize(creds)
sheet = client.open("NMS Inventory")
sheets = sheet.worksheets()
sheetList = {}
for sheet in sheets:
sheetList[sheet.title.split("/")[0]] = sheet
# state
lastCell = None
lastSheet = None
# lastAction = oh god i dunno i'm trying to build a way to add new entries for an existing barcode
# TODO: View contents of everything with this item as its location
def barcode(code):
out = ""
global lastCell, lastSheet, sheetList, sheet, sheets, client
cmd = code.upper()
cmdSplit = cmd.split("-")
if cmdSplit[0] in sheetList:
targetSheet = sheetList[cmdSplit[0]]
# print (targetSheet)
cell = targetSheet.find(cmd)
# print (cell)
if cell != None:
rowProperties = targetSheet.row_values(1)
rowValues = targetSheet.row_values(cell.row)
for i in range(0, len(targetSheet.row_values(cell.row))):
out += "{property}: {value}\n".format(property=rowProperties[i], value=rowValues[i])
print (colored("{property}: {value}".format(property=rowProperties[i], value=rowValues[i]), "cyan"))
# find out if this is a storage entry
if "Storage" in rowProperties:
# print ("has a storage field")
if lastCell != None:
lastRowProperties = lastSheet.row_values(1)
if "Location" in lastRowProperties:
# print ("has a Location field")
locationColumn = lastRowProperties.index("Location") + 1 # hey I learned about .index() today
if cell.value != lastCell.value and targetSheet != lastSheet: # don't store this item inside itself or in another of its kind!
lastSheet.update_cell(lastCell.row, locationColumn, cell.value)
print ("{item} is now stored in {storage}".format(item = lastCell.value, storage = cell.value))
out += "{item} is now stored in {storage}\n".format(item = lastCell.value, storage = cell.value)
subprocess.Popen(["afplay", "barcode-success.wav"])
if "LocationDatetime" in lastRowProperties:
locationDatetimeColumn = lastRowProperties.index("LocationDatetime") + 1 # hey I learned about .index() today
lastSheet.update_cell(lastCell.row, locationDatetimeColumn, datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
else:
subprocess.Popen(["afplay", "barcode-chime.wav"])
else:
print ("Record found")
subprocess.Popen(["afplay", "barcode-chime.wav"])
lastCell = cell
lastSheet = targetSheet
else:
print ("Unknown record")
subprocess.Popen(["afplay", "barcode-unknown.wav"])
return out
if __name__ == "__main__":
while True:
code = input()
barcode(code)