Skip to content

Commit 6161067

Browse files
committed
added barcode server example
1 parent 745d20e commit 6161067

File tree

4 files changed

+52
-5
lines changed

4 files changed

+52
-5
lines changed

examples/barcode_server.py

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import pprint
2+
import time
3+
import threading
4+
from flask import Flask, jsonify
5+
6+
from zebra_scanner import CoreScanner
7+
8+
pp = pprint.PrettyPrinter(indent=4)
9+
cs = CoreScanner()
10+
11+
app = Flask(__name__)
12+
13+
new_barcodes_lock = threading.RLock()
14+
new_barcodes = {}
15+
16+
@cs.on_scanner_added
17+
def on_scanner_added(scanner):
18+
scanner.fetch_attributes()
19+
scanner.pull_trigger()
20+
21+
@scanner.on_barcode
22+
def on_barcode(barcode):
23+
barcodeType = "?"
24+
if barcode.type == 15:
25+
barcodeType = "CODE128"
26+
elif barcode.type == 6:
27+
barcodeType = "I25"
28+
29+
new_barcodes[barcode.code] = {
30+
"code": barcode.code,
31+
"type": barcodeType
32+
}
33+
34+
@cs.on_scanner_removed
35+
def on_scanner_removed(scanner):
36+
scanner.release_trigger()
37+
38+
@app.route("/barcodes")
39+
def get_barcodes():
40+
with new_barcodes_lock:
41+
json = jsonify({
42+
"barcodes": new_barcodes.values(),
43+
"timestamp": int(time.time()*1000)
44+
})
45+
new_barcodes.clear()
46+
return json
47+
48+
if __name__ == '__main__':
49+
app.run(host="0.0.0.0", debug=True, port=8080, use_reloader=False)
50+

examples/test.py

-3
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,13 @@
44
from zebra_scanner import CoreScanner
55

66
pp = pprint.PrettyPrinter(indent=4)
7-
scanners = []
87
cs = CoreScanner()
98

109

1110
@cs.on_scanner_added
1211
def on_scanner_added(scanner):
1312
print("New scanner found:")
1413
pp.pprint(scanner.__dict__)
15-
scanners.append(scanner)
1614
scanner.pull_trigger()
1715

1816
scanner.fetch_attributes()
@@ -34,7 +32,6 @@ def on_barcode(barcode):
3432
def on_scanner_removed(scanner):
3533
print("Scanner removed:")
3634
scanner.release_trigger()
37-
scanners.remove(scanner)
3835
pp.pprint(scanner.__dict__)
3936

4037
while True:

src/BoostPythonCoreScanner.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,7 @@ void CoreScanner::OnBarcodeEvent(short int eventType, std::string & pscanData)
375375
}
376376
std::auto_ptr<Barcode> b{new Barcode()};
377377
b->code = result;
378-
b->type = scanData.child_value("datatype");
378+
b->type = std::stoi(scanData.child_value("datatype"));
379379
s.OnBarcode(b);
380380
}
381381
}

src/BoostPythonCoreScanner.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ class Barcode
3737
{
3838
public:
3939
std::string code;
40-
std::string type;
40+
int type;
4141
};
4242

4343
class Scanner;

0 commit comments

Comments
 (0)