-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeviceABC.lua
More file actions
310 lines (288 loc) · 8.34 KB
/
Copy pathdeviceABC.lua
File metadata and controls
310 lines (288 loc) · 8.34 KB
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
--[[ CONSTANTS, STATES, AND SETUPS ]]
-- [[ this is just a sample so specific device names has been removed ]]
local RB_READ_HEADER, RB_READ_PARAMS, RB_READ_WBC, RB_READ_RBC, RB_READ_PLT, RB_READ_ID,RB_END = 1,2,3,4,5,6,7
local seq = {[0]=0x02,[1]=0x02,[2]=0xc5,[3]=0x38,[4]=0x01}
local attr;
local paramlabel ={
"WBC", "LY#", "MO#", "GR#", "LY%","MO%", "GR%", "RBC", "HGB", "HCT", "MCV", "MCH", "MCHC", "RDWCV", "RDWSD", "PLT", "MPV", "PDW", "PCT"
}
local paramlabel_rev = {}
for k,v in ipairs(paramlabel) do
paramlabel_rev[v] = k
end
local paramunits ={
"10^9/uL", "10^9/uL", "10^9/uL", "10^9/uL", "%", "%", "%", "10^6/uL", "g/dL", "%", "fL", "Pg", "g/dL", "%", "fL", "10^9/uL", "fL", "fL", "%"
}
protocolname = "mystery"
--[[ IMPLEMENTATIONS ]]
--REQUIRED
function init(dev)
devicename = dev
fullname = protocolname.."@"..dev
ptreename = protocolname.."@"..dev
bytecount = 0
checksum = 0
state = RB_READ_HEADER
attr = ptree.get(ptreename)
sys = LISSys.get()
pdebug("module initialized successfully.\n")
end
--REQUIRED
function resetState()
bytecount = 0
checksum = 0
next_state = RB_READ_HEADER
attr = ptree.get(ptreename)
end
--REQUIRED
function readbyte(b)
next_state = state
if bytecount==0 then
checksum=0
elseif bytecount < 713 then
checksum = checksum ~ b
end
if state==RB_READ_HEADER then
pdebug('.')
if b==seq[bytecount] then
bytecount = bytecount+1
if bytecount==5 then
next_state = RB_READ_PARAMS
end
else
bytecount = 0
end
elseif state==RB_READ_PARAMS then
idx = bytecount - 5;
if (idx % 2 == 0)then
parambyte = b
else
label = 'param.'..paramlabel[math.floor(idx/2)+1]
paramfactor = ((i==8 or i==10) and 10) or 100
paramval = ((parambyte << 8) + b)
if paramval~=0x2A2A then
attr:setNum(label, paramval/paramfactor)
end
end
bytecount = bytecount + 1
if bytecount == 5 + 19 * 2 then
next_state = RB_READ_WBC
end
elseif state==RB_READ_WBC then
idx = bytecount - 5 - 19 * 2
attr:setNum('wbc.idx'..idx,b)
bytecount = bytecount + 1
if bytecount == 5 + 19 * 2 + 256 then
next_state = RB_READ_RBC
end
elseif state==RB_READ_RBC then
idx = bytecount - 5 - 19 * 2 - 256
attr:setNum('rbc.idx'..idx,b)
bytecount = bytecount + 1
if bytecount == 5 + 19 * 2 + 256 * 2 then
next_state = RB_READ_PLT
end
elseif state==RB_READ_PLT then
idx = bytecount - 5 - 19 * 2 - 256*2
attr:setNum('plt.idx'..idx,b)
bytecount = bytecount + 1
if bytecount == 5 + 19 * 2 + 256 * 2 + 128 then
next_state = RB_READ_ID
end
elseif state==RB_READ_ID then
idx = bytecount-700
if idx<0 then
sampleID = 0
bytecount = bytecount + 1
elseif idx < 6 then
sampleID = sampleID * 10 + b
bytecount = bytecount + 1
if bytecount == 706 then
next_state = RB_END
attr:setNum('sampleID',sampleID)
end
end
elseif state==RB_END then
if bytecount==712 then
if b==0x03 then
pdebug("dataset received!\n")
else
resetState()
end
elseif bytecount==713 then
if checksum==b then
attr:setNum('checksum',checksum)
attr:setProp('device',devicename)
attr:setProp('protocol',protocolname)
attr:setNum('time', os.time())
attr:setProp('time_s', os.date("%c"))
attr:setProp('test','CBC')
attr:setProp('desc','CBC')
sys:submit(attr)
else
pdebug("checksum is incorrect: "..checksum.."!="..b.."\n")
end
resetState()
end
if bytecount>0 then
bytecount = bytecount + 1
end
end
state = next_state
end
--REQUIRED (used when a tabular data field edited from data frame)
function setParam(data, name, val)
if paramlabel_rev[name] then
data:setNum('param.'..name,tonumber(val))
end
end
--REQUIRED SYMBOL, implementation optional
orderform = [[ order form not supported ]]
--REQUIRED SYMBOL, implementation optional
function order(order_data,test_name)
pdebug('ordered '..test_name..'.\n')
end
--REQUIRED
function fillDataFrame(dataframe,data)
dataframe:setTitle(data:getProp('test')..' record at '.. data:getProp('time_s'))
dataframe:clearDataFields()
for i=1,19 do
local param = paramlabel[i]
local key = 'param.'..param
if data:has(key) then
dataframe:appendDataField(param,tostring(data:getNum(key)),'-',paramunits[i])
end
end
dataframe:clearImages()
dataframe:addImage('wbc_hist','draw_wbchist')
dataframe:addImage('rbc_hist','draw_rbchist')
dataframe:addImage('plt_hist','draw_plthist')
dataframe:comment(data::getProp('comment'))
end
--[[ CALLBACK DRAW FUNCTIONS ]]
-- the format is f(dc,data)
-- C++ program will passed with appropriate objects that has been exposed to lua
-- with a limited set of functions, see dc:line, dc:text, etc in modelbindings.h
function draw_axes(dc,header)
local w,h = dc:size()
h = h-1
dc:line(0,0,0,h)
dc:line(0,h,w,h)
dc:text(header,2,2)
end
function draw_wbchist(dc,data)
local w,h = dc:size()
dc:pen(0x00000000,1)
draw_axes(dc,'wbc_hist')
dc:pen(0,0)
dc:brush(0x00FF0000,1)
for i=1,256 do
local val = data:getNum('wbc.idx'..(i-1))
local x,y = i*(w-5)/256, val*(h-15)/256
dc:rect(x,h-1-y,1,y);
end
end
function draw_rbchist(dc,data)
local w,h = dc:size()
dc:pen(0x00000000,1)
draw_axes(dc,'rbc_hist')
dc:pen(0,0)
dc:brush(0x000000FF,1)
for i=1,256 do
local val = data:getNum('rbc.idx'..(i-1))
local x,y = i*(w-5)/256, val*(h-15)/256
dc:rect(x,h-1-y,1,y);
end
end
function draw_plthist(dc,data)
local w,h = dc:size()
dc:pen(0x00000000,1)
draw_axes(dc,'plt_hist')
dc:pen(0,0)
dc:brush(0x0000FFFF,1)
for i=1,128 do
local val = data:getNum('plt.idx'..(i-1))
local x,y = i*(w-5)/128, val*(h-15)/256
dc:rect(x,h-1-y,2,y);
end
end
--[[ database functions, ALL REQUIRED]]
db_alias = [[mystery]]
function db_create(lis2db)
local stmt = [[
CREATE TABLE IF NOT EXISTS mystery.record(
rid INTEGER PRIMARY KEY ASC,
uid INTEGER,
"WBC" REAL, "LY#" REAL, "MO#" REAL, "GR#" REAL,
"LY%" REAL, "MO%" REAL, "GR%" REAL, "RBC" REAL,
"HGB" REAL, "HCT" REAL, "MCV" REAL, "MCH" REAL,
"MCHC" REAL, "RDWCV" REAL, "RDWSD" REAL, "PLT" REAL,
"MPV" REAL, "PDW" REAL, "PCT" REAL,
wbc_hist BLOB, rbc_hist BLOB, plt_hist BLOB
);
]]
lis2db:exec(stmt)
lis2db:prepare('myst_insert',[[
REPLACE INTO mystery.record( uid,"WBC", "LY#", "MO#", "GR#", "LY%","MO%", "GR%", "RBC", "HGB", "HCT", "MCV", "MCH", "MCHC", "RDWCV", "RDWSD", "PLT", "MPV", "PDW", "PCT", wbc_hist, rbc_hist, plt_hist )
values ( @uid,@WBC, @LYn , @MOn , @GRn , @LYp , @MOp, @GRp , @RBC , @HGB , @HCT , @MCV , @MCH , @MCHC , @RDWCV , @RDWSD , @PLT , @MPV , @PDW , @PCT, @wbc_hist, @rbc_hist, @plt_hist );
]])
lis2db:prepare('myst_delete',[[ DELETE FROM mystery.record WHERE uid=@uid; ]])
lis2db:prepare('myst_findByUID',[[ SELECT * FROM mystery.record WHERE uid=@uid ]])
end
function db_drop(lis2db)
lis2db:exec([[DROP TABLE mystery.record;]])
end
function db_insert(lis2db,data)
local inserter = lis2db:getStmt('myst_insert')
inserter:reset()
inserter:bindInt(inserter:indexOf('@uid'),data:getNum('uid'))
for i=1,19 do
local param = paramlabel[i]
local key = 'param.'..param
local bindkey = '@'..param
bindkey = bindkey:gsub('#','n')
bindkey = bindkey:gsub('%%','p')
if data:has(key) then
inserter:bindNum(inserter:indexOf(bindkey),data:getNum(key))
end
end
local wbc_hist,rbc_hist,plt_hist = LisBlob.alloc(256),LisBlob.alloc(256),LisBlob.alloc(128)
for i=1,256 do
local histIdx = i-1
wbc_hist:set(i,data:getNum('wbc.idx'..histIdx))
rbc_hist:set(i,data:getNum('rbc.idx'..histIdx))
if i<=128 then
plt_hist:set(i,data:getNum('plt.idx'..histIdx))
end
end
inserter:bindBlob(inserter:indexOf('@wbc_hist'),wbc_hist,256)
inserter:bindBlob(inserter:indexOf('@rbc_hist'),rbc_hist,256)
inserter:bindBlob(inserter:indexOf('@plt_hist'),plt_hist,128)
inserter:step()
LisBlob.free(wbc_hist)
LisBlob.free(rbc_hist)
LisBlob.free(plt_hist)
end
function db_delete(lis2db,data)
local deleter = lis2db:getStmt('myst_delete')
deleter:bindInt(deleter:indexOf('@uid'),data:getNum('uid'))
deleter:step()
end
function db_update(lis2db,data)
db_insert(lis2db,data)
end
function db_fill(lis2db,data,hint)
--fills first record found, specified by hint, into an empty data ptree
end
function db_find(lis2db,uid)
local finder = lis2db:getStmt('myst_findByUID')
finder:reset()
inserter:bindInt(inserter:indexOf('@uid'),uid)
end
function to_html(data)
return [[
<!doctype html>
<html>
</html>
]]
end