-
Notifications
You must be signed in to change notification settings - Fork 0
/
PythonScale.py
executable file
·109 lines (85 loc) · 3.1 KB
/
PythonScale.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
import ptvsd
# for VS debugging
ptvsd.enable_attach('piscale')
import Testing as test
import time
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
import sys
import LedConfig as leds
import ButtonConfig as btns
import LcdStuff as lcd
import ScaleSerial as mySerial
import ApiAccess as webApi
import json
from io import StringIO
from PartWeightResult import PartWeightResult as pwr
import GeneralSetup
readyText = "System ready..."
testingMode = test.mode
pressAckMsg = GeneralSetup.pressAckMsg
def alert(msg1, msg2 = "", msg3 = ""):
lcd.setText(msg1, msg2, msg3, pressAckMsg)
leds.blinkNoGoLeds()
lcd.setText(readyText)
try:
while (True):
#exit by pressing the Exit button
# .getData returns the PartWeightResult object
pw = mySerial.getData(testingMode)
# no data from scale, keep on truckin'
if (pw.hasData == False): continue
lcd.addTextToBottom("Received data:")
# Data from scale
# data is good
if (pw.success):
#leds.goLed.turnOn()
lcd.clearText()
lcd.addTextToBottom("WO: " + pw.workOrderNumber)
lcd.addTextToBottom("Weight: " + pw.weight)
# send data to webApi and get the result
lcd.addTextToBottom("Submitting data...", True)
apiResult = webApi.SubmitWeight(pw.workOrderNumber, pw.weight, testingMode)
# some error occured, but it was (probably already handled,
# so just get on with things
if (apiResult == None):
lcd.setText(readyText)
continue
#apiResult.status=404
if (apiResult.status != 200):
alert("Invalid Request!!", "Error: " + str(apiResult.status))
continue
apiData = apiResult.data
lcd.addTextToBottom("Status: " + str(apiResult.status))
if (testingMode):
sc = "SOME STOCKCODE"
else:
#jsonData = json.loads(apiData)
jsonData = json.load(StringIO(apiData))
if (jsonData['success'] != True):
alert("There was an error", "saving the part", "to the database")
print(jsonData['success'])
continue
if (jsonData['model']['weightLimitExceeded']):
alert("Exceeds weight limit", "Part must be sent", "to quarantine")
continue
sc = jsonData['model']['comtekStockCode']
lcd.setText("Weight submitted for", "stock code:", "", sc)
leds.goLed.turnOn()
time.sleep(10)
leds.goLed.turnOff()
else:
# failed to properly read data
if(pw.hasData):
lcd.setText(pw.msg)
leds.blinkNoGoLeds();
lcd.setText(readyText)
leds.statusLed.turnOn()
time.sleep(0.1)
except KeyboardInterrupt:
GeneralSetup.exitPiScale("")
except Exception as e:
lcd.setText(e.args[0])
leds.blinkNoGoLeds()
finally:
pass