-
Notifications
You must be signed in to change notification settings - Fork 0
/
uploadData.py
155 lines (126 loc) · 4.9 KB
/
uploadData.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
from __future__ import print_function
from time import sleep
from coolerClass.filecfg import filecfg
import serial
import pymysql
import datetime
#get configuration info from cfg file
cfg = filecfg('config.cfg')
#initialise the serial connection
ser = serial.Serial(cfg.ser_interface,9600)
#empty read to let the serial synchronize
ser.readline()
class upload():
def __init__(self,db,cursor,machineID,rowNum,redScan,greenScan,blueScan,numCans):
#set up attributes
self.db = db
self.cursor = cursor
self.dataTime = str(datetime.datetime.now())
self.machineID = 0
self.rowNum = 1
self.rbScan = 0
self.rgScan = 0
self.bgScan = 0
self.wScan = 0
self.redScan = redScan
self.blueScan = blueScan
self.greenScan = greenScan
self.numCans = numCans
self.identifyCan()
def identifyCan(self):
sql = '''select productID,productName,abs(redScan-%s) as rDiff,
abs(blueScan-%s) as bDiff,abs(greenScan-%s) as
gDiff,abs(rbScan-%s) as rbDiff,abs(bgScan-%s)
as bgDiff,abs(rgScan-%s) as rgDiff,abs(wScan-%s)
as wDiff,abs(redScan-%s) + abs(blueScan-%s) +
abs(greenScan-%s) + abs(rbScan-%s) + abs(bgScan-%s) +
abs(rgScan-%s) + abs(wScan-%s) as totalDiff from knownproducts
order by (abs(redScan-%s) + abs(blueScan-%s) + abs(greenScan-%s) +
abs(rbScan-%s) + abs(bgScan-%s) + abs(rgScan-%s) + abs(wScan-%s))'''
self.cursor.execute(sql,(self.redScan,self.blueScan,self.greenScan,self.rbScan,
self.bgScan,self.rgScan,self.wScan,self.redScan,self.blueScan,self.greenScan,self.rbScan,
self.bgScan,self.rgScan,self.wScan,self.redScan,self.blueScan,self.greenScan,self.rbScan,
self.bgScan,self.rgScan,self.wScan))
line=self.cursor.fetchone()
print("numCans:" + str(numCans) + " " + str(line))
self.canID = line[0]
self.canName = line[1]
self.uploadToDB()
def uploadToDB(self):
sql = '''INSERT INTO coolerhack.main (machineID,rowNum,dataTime,numCans,firstCanID,firstCanName) VALUES (%s,%s,%s,%s,%s,%s)'''
self.cursor.execute(sql,(self.machineID,self.rowNum,self.dataTime,self.numCans,self.canID,self.canName))
self.db.commit()
db.commit()
#convert raw rgb intensity into weighted and scaled values
def formatStream(val):
plotStream = (float(val-plotStreamRange[0])/float(plotStreamRangeW))
plotStream = int(plotStream * plotStreamWidth)
return plotStream
#convert rowData raw resistance read into the number of cans
def getNumCans(rowData):
numCans = 0;
if rowData < 30:
numCans = 0;
elif rowData < 41:
numCans = 6;
elif rowData < 53:
numCans = 5;
elif rowData < 68:
numCans = 4;
elif rowData < 100:
numCans = 3;
elif rowData < 180:
numCans = 2;
elif rowData < 360:
numCans = 1;
else:
numCans = 0;
return numCans
#config data for formatStream
plotStreamRange = [950,1024]
plotStreamRangeW = plotStreamRange[1] - plotStreamRange[0]
plotStreamWidth = 30
plotStreamChar = "|"
#set up database connection
db = pymysql.connect(db=cfg.db_db,user=cfg.db_user,passwd=cfg.db_passwd,host=db_host)
cursor = db.cursor()
#wait a sec for connections
sleep(0.5)
#Junk data trials to skip while sensor data stabilizes
junkTrials = 15;
for i in range(100):
try:
#serial line reading limits the rate
ser.flushInput()
ser.flushOutput()
line = ser.readline()
red = int(line[line.index("r: ")+3:line.index("g: ")])
green = int(line[line.index("g: ")+3:line.index("b: ")])
blue = int(line[line.index("b: ")+3:line.index("row: ")])
rowData = int(line[line.index("row: ")+5:line.index("\r\n")])
#exaggerate like the visualization
red = formatStream(red)
green = formatStream(green)
blue = formatStream(blue)
#Process rowData into NumCans
numCans = getNumCans(rowData)
#print raw sensor data if the junkTrials are done, otherwise just rgb vals
if i > junkTrials:
print("rowData:"+str(rowData).ljust(4)+"\t("+str(red)+","+str(green)+","+str(blue)+")\t",end="")
else:
print(str(i).ljust(3),"rowData:",rowData,red,green,blue)
machineID = 0
rowNum = 1
redScan = red
greenScan = green
blueScan = blue
#log the events to the main database
if i > junkTrials:
upload(db,cursor,machineID,rowNum,redScan,greenScan,blueScan,numCans)
except ValueError:
#caught partial data from serial mismatch
print("Serial went too fast, flushing before resuming...")
sleep(0.05)
#clean up
db.close()
cursor.close()