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
|
/* Copyright 2012-2013, Sebastian Reichel <sre@ring0.de>
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
[DBus (name = "io.mainframe.shopsystem.ScannerSession")]
public class ScannerSessionImplementation {
private int user = 0;
private string name = "Guest";
private bool logged_in = false;
private bool disabled = false;
private string theme = "beep";
private Database db;
private AudioPlayer audio;
private InputDevice devScanner;
private InputDevice devRfid;
private Cli cli;
private ScannerSessionState state = ScannerSessionState.READY;
private DetailedProduct[] shoppingCard = {};
public signal void msg(MessageType type, string message);
public signal void msg_overlay(string title, string message);
public ScannerSessionImplementation() {
try {
db = Bus.get_proxy_sync(BusType.SYSTEM, "io.mainframe.shopsystem.Database", "/io/mainframe/shopsystem/database");
devScanner = Bus.get_proxy_sync(BusType.SYSTEM, "io.mainframe.shopsystem.InputDevice", "/io/mainframe/shopsystem/device/scanner");
devRfid = Bus.get_proxy_sync(BusType.SYSTEM, "io.mainframe.shopsystem.InputDevice", "/io/mainframe/shopsystem/device/rfid");
cli = Bus.get_proxy_sync(BusType.SYSTEM, "io.mainframe.shopsystem.Cli", "/io/mainframe/shopsystem/cli");
audio = Bus.get_proxy_sync(BusType.SYSTEM, "io.mainframe.shopsystem.AudioPlayer", "/io/mainframe/shopsystem/audio");
devScanner.received_barcode.connect(handle_barcode);
devRfid.received_barcode.connect(handle_barcode);
cli.received_barcode.connect(handle_barcode);
} catch(IOError e) {
error("IOError: %s\n", e.message);
}
}
private void send_message(MessageType type, string format, ...) {
var arguments = va_list();
var message = format.vprintf(arguments);
msg(type, message);
}
private bool login(int user) throws IOError {
this.user = user;
try {
this.name = db.get_username(user);
this.disabled = db.user_is_disabled(user);
} catch(DatabaseError e) {
send_message(MessageType.ERROR, "Error (user=%d): %s", user, e.message);
return false;
}
this.logged_in = true;
try {
this.theme = db.get_user_theme(user, "");
if (this.theme == "") {
this.theme = audio.get_random_user_theme();
}
} catch(DatabaseError e) {
this.theme = "beep";
}
return true;
}
private ScannerSessionCodeType getCodeType(string scannerdata){
if(scannerdata.has_prefix("USER ")){
return ScannerSessionCodeType.USER;
} else if(scannerdata == "GUEST") {
return ScannerSessionCodeType.GUEST;
} else if(scannerdata == "UNDO") {
return ScannerSessionCodeType.UNDO;
} else if(scannerdata == "LOGOUT") {
return ScannerSessionCodeType.LOGOUT;
} else if(scannerdata.length == 10) {
return ScannerSessionCodeType.RFIDEM4100;
} else {
//Handle EAN Code
uint64 id = 0;
scannerdata.scanf("%llu", out id);
/* check if scannerdata has valid format */
if(scannerdata != "%llu".printf(id) && scannerdata != "%08llu".printf(id) && scannerdata != "%013llu".printf(id)) {
return ScannerSessionCodeType.UNKNOWN;
}
return ScannerSessionCodeType.EAN;
}
}
private void play_audio(AudioType audioType){
switch (audioType) {
case AudioType.ERROR:
audio.play_system("error.ogg");
break;
case AudioType.LOGIN:
audio.play_user(theme, "login");
break;
case AudioType.LOGOUT:
audio.play_user(theme, "logout");
break;
case AudioType.PURCHASE:
audio.play_user(theme, "purchase");
break;
case AudioType.INFO:
audio.play_user(theme, "login");
break;
}
}
private ScannerResult handleReadyState(string scannerdata) throws DatabaseError, IOError{
ScannerSessionCodeType codeType = getCodeType(scannerdata);
ScannerResult scannerResult = ScannerResult();
switch (codeType) {
case ScannerSessionCodeType.USER:
int32 userid = int.parse(scannerdata.substring(5));
if(login(userid)) {
scannerResult.type = MessageType.INFO;
scannerResult.message = "Login: %s (%d)".printf(name, user);
scannerResult.audioType = AudioType.LOGIN;
shoppingCard = {};
state = ScannerSessionState.USER;
} else {
scannerResult.type = MessageType.ERROR;
scannerResult.message = "Login failed (User ID = %d)".printf(userid);
scannerResult.audioType = AudioType.ERROR;
state = ScannerSessionState.READY;
}
return scannerResult;
case ScannerSessionCodeType.GUEST:
if(login(0)) {
scannerResult.type = MessageType.INFO;
scannerResult.message = "Login as GUEST";
scannerResult.audioType = AudioType.LOGIN;
shoppingCard = {};
state = ScannerSessionState.USER;
} else {
scannerResult.type = MessageType.ERROR;
scannerResult.message = "Login failed as GUEST";
scannerResult.audioType = AudioType.ERROR;
state = ScannerSessionState.READY;
}
return scannerResult;
case ScannerSessionCodeType.EAN:
uint64 ean = 0;
scannerdata.scanf("%llu", out ean);
var p = DetailedProduct();
try {
p = db.get_product_for_ean(ean);
} catch(IOError e) {
scannerResult.type = MessageType.ERROR;
scannerResult.message = "Internal Error!";
scannerResult.audioType = AudioType.ERROR;
return scannerResult;
} catch(DatabaseError e) {
if(e is DatabaseError.PRODUCT_NOT_FOUND) {
scannerResult.type = MessageType.ERROR;
scannerResult.message = "Error: unknown product: %llu".printf(ean);
scannerResult.audioType = AudioType.ERROR;
} else {
scannerResult.type = MessageType.ERROR;
scannerResult.message = "Error: %s".printf(e.message);
scannerResult.audioType = AudioType.ERROR;
}
return scannerResult;
}
var mprice = p.memberprice;
var gprice = p.guestprice;
var pname = p.name;
scannerResult.type = MessageType.INFO;
scannerResult.message = @"article info: $pname (Member: $mprice €, Guest: $gprice €)";
scannerResult.audioType = AudioType.ERROR;
state = ScannerSessionState.READY;
return scannerResult;
case ScannerSessionCodeType.RFIDEM4100:
int user = db.get_userid_for_rfid(scannerdata);
scannerResult.nextScannerdata =@"USER $user";
return scannerResult;
default:
state = ScannerSessionState.READY;
return scannerResult;
}
}
private ScannerResult handleUserState(string scannerdata) throws DatabaseError, IOError {
ScannerSessionCodeType codeType = getCodeType(scannerdata);
ScannerResult scannerResult = ScannerResult();
switch (codeType) {
case ScannerSessionCodeType.EAN:
uint64 ean = 0;
scannerdata.scanf("%llu", out ean);
var p = DetailedProduct();
try {
p = db.get_product_for_ean(ean);
} catch(IOError e) {
scannerResult.type = MessageType.ERROR;
scannerResult.message = "Internal Error!";
scannerResult.audioType = AudioType.ERROR;
return scannerResult;
} catch(DatabaseError e) {
if(e is DatabaseError.PRODUCT_NOT_FOUND) {
scannerResult.type = MessageType.ERROR;
scannerResult.message = "Error: unknown product: %llu".printf(ean);
scannerResult.audioType = AudioType.ERROR;
} else {
scannerResult.type = MessageType.ERROR;
scannerResult.message = "Error: %s".printf(e.message);
scannerResult.audioType = AudioType.ERROR;
}
return scannerResult;
}
shoppingCard += p;
Price price = p.memberprice;
if(user == 0){
price = p.guestprice;
}
scannerResult.type = MessageType.INFO;
scannerResult.message = @"article added to shopping card: $(p.name) ($price €)";
scannerResult.audioType = AudioType.PURCHASE;
state = ScannerSessionState.USER;
break;
case ScannerSessionCodeType.UNDO:
if(shoppingCard.length > 0){
var removedProduct = shoppingCard[shoppingCard.length-1];
shoppingCard = shoppingCard[0:shoppingCard.length-1];
scannerResult.type = MessageType.INFO;
scannerResult.message = @"removed last Item from Shopping Cart: $(removedProduct.name)";
scannerResult.audioType = AudioType.INFO;
} else {
scannerResult.type = MessageType.INFO;
scannerResult.message = @"No more Items on your Shopping Cart";
scannerResult.audioType = AudioType.ERROR;
}
break;
case ScannerSessionCodeType.LOGOUT:
scannerResult = logout();
break;
case ScannerSessionCodeType.USER:
case ScannerSessionCodeType.GUEST:
case ScannerSessionCodeType.RFIDEM4100:
/* Logout old user session (and buy articles) */
scannerResult = logout();
scannerResult.nextScannerdata = scannerdata;
break;
}
return scannerResult;
}
private ScannerResult buyShoppingCard() {
ScannerResult scannerResult = ScannerResult();
uint8 amountOfItems = 0;
Price totalPrice = 0;
uint8 i = 0;
DetailedProduct p = DetailedProduct();
for(i = 0; i < shoppingCard.length; i++) {
p = shoppingCard[i];
db.buy(user, p.ean);
amountOfItems++;
Price price = p.memberprice;
if(user == 0) {
price = p.guestprice;
}
totalPrice += price;
}
scannerResult.type = MessageType.INFO;
scannerResult.message = @"$name bought $amountOfItems items for $totalPrice €";
scannerResult.audioType = AudioType.INFO;
return scannerResult;
}
private void handle_barcode(string scannerdata) {
try {
stdout.printf("scannerdata: %s\n", scannerdata);
if(interpret(scannerdata))
devScanner.blink(1000);
} catch(IOError e) {
send_message(MessageType.ERROR, "IOError: %s", e.message);
} catch(DatabaseError e) {
send_message(MessageType.ERROR, "DatabaseError: %s", e.message);
}
}
private bool interpret(string scannerdata) throws DatabaseError, IOError {
ScannerResult scannerResult = ScannerResult();
switch (state) {
case ScannerSessionState.READY:
scannerResult = handleReadyState(scannerdata);
break;
case ScannerSessionState.USER:
scannerResult = handleUserState(scannerdata);
break;
}
play_audio(scannerResult.audioType);
send_message(scannerResult.type, scannerResult.message);
if(scannerResult.nextScannerdata != null){
interpret(scannerResult.nextScannerdata);
}
return true;
}
private ScannerResult logout() {
ScannerResult scannerResult = ScannerResult();
scannerResult = buyShoppingCard();
logged_in = false;
state = ScannerSessionState.READY;
return scannerResult;
}
}
|