Compare commits
5 Commits
Author | SHA1 | Date |
---|---|---|
Pavel Dmitriev | 2c41f45273 | |
Pavel Dmitriev | 3ff0c017db | |
Pavel Dmitriev | d82a8e78dd | |
Pavel Dmitriev | 61074c61c5 | |
Pavel Dmitriev | 752d2c0415 |
|
@ -0,0 +1,13 @@
|
||||||
|
/.git
|
||||||
|
/.settings
|
||||||
|
README.*
|
||||||
|
LICENSE
|
||||||
|
CHANGELOG.*
|
||||||
|
/docs
|
||||||
|
*.md
|
||||||
|
/.buildpath
|
||||||
|
/.project
|
||||||
|
/.gitignore
|
||||||
|
/.dockerignore
|
||||||
|
/temp-test
|
||||||
|
/build.sh
|
28
.drone.yml
28
.drone.yml
|
@ -45,6 +45,34 @@ trigger:
|
||||||
- refs/tags/*
|
- refs/tags/*
|
||||||
|
|
||||||
steps:
|
steps:
|
||||||
|
- name: Prepare image
|
||||||
|
image: mxfox.ru/mxfox/fox-web-basic:latest
|
||||||
|
commands:
|
||||||
|
- |
|
||||||
|
apt-get update -y
|
||||||
|
apt-get install curl -y
|
||||||
|
cd /tmp
|
||||||
|
curl -sS https://getcomposer.org/installer -o composer-setup.php
|
||||||
|
php composer-setup.php --install-dir=/usr/local/bin --filename=composer
|
||||||
|
DEBIAN_FRONTEND=noninteractive TZ=Europe/Moscow apt-get -y install tzdata
|
||||||
|
cd -
|
||||||
|
- composer install
|
||||||
|
- |
|
||||||
|
find . -name "*~" -prune -exec rm -rf '{}' \;
|
||||||
|
find . -name "*.bak" -prune -exec rm -rf '{}' \;
|
||||||
|
find . -name "*.old" -prune -exec rm -rf '{}' \;
|
||||||
|
find . -name ".git" -prune -exec rm -rf '{}' \;
|
||||||
|
find . -name ".settings" -prune -exec rm -rf '{}' \;
|
||||||
|
find . -name ".buildpath" -prune -exec rm -rf '{}' \;
|
||||||
|
find . -name ".project" -prune -exec rm -rf '{}' \;
|
||||||
|
find . -name "README.*" -prune -exec rm -rf '{}' \;
|
||||||
|
find . -name "*.md" -prune -exec rm -rf '{}' \;
|
||||||
|
find . -name "composer.*" -prune -exec rm -rf '{}' \;
|
||||||
|
find . -name ".travis*" -prune -exec rm -rf '{}' \;
|
||||||
|
find . -name "installed.json" -prune -exec rm -rf '{}' \;
|
||||||
|
find . -name "*.sample" -prune -exec rm -rf '{}' \;
|
||||||
|
rm -f composer.*
|
||||||
|
|
||||||
- name: Build docker image
|
- name: Build docker image
|
||||||
image: mxfox.ru/mxfox/docker-dind.buildx:latest
|
image: mxfox.ru/mxfox/docker-dind.buildx:latest
|
||||||
privileged: true
|
privileged: true
|
||||||
|
|
|
@ -0,0 +1,9 @@
|
||||||
|
/modules/*
|
||||||
|
!/modules/README.md
|
||||||
|
/storage/*
|
||||||
|
!/storage/README.md
|
||||||
|
/temp-test
|
||||||
|
/composer.lock
|
||||||
|
/vendor
|
||||||
|
.scannerwork
|
||||||
|
.vscode
|
|
@ -1,5 +1,3 @@
|
||||||
FROM python:3
|
FROM php:8.1
|
||||||
WORKDIR /foxScanner
|
COPY . /foxScanner
|
||||||
COPY . .
|
CMD ["php","/foxScanner/barcodeScanner.php"]
|
||||||
RUN pip install --no-cache-dir -r requirements
|
|
||||||
CMD ["python","scanner.py"]
|
|
||||||
|
|
18
README.md
18
README.md
|
@ -8,3 +8,21 @@
|
||||||
* RABBITMQ_USER
|
* RABBITMQ_USER
|
||||||
* RABBITMQ_PASSWORD
|
* RABBITMQ_PASSWORD
|
||||||
* RABBITMQ_USE_SSL
|
* RABBITMQ_USE_SSL
|
||||||
|
|
||||||
|
## docker-compose sample
|
||||||
|
```
|
||||||
|
version: "2"
|
||||||
|
|
||||||
|
services:
|
||||||
|
barcode-scanner:
|
||||||
|
image: mxfox.ru/mxfox/barcode-scanner-client:latest
|
||||||
|
devices:
|
||||||
|
- "/dev/ttyACM0:/dev/ttyACM0"
|
||||||
|
environment:
|
||||||
|
- 'SCANNER_PORT=/dev/ttyACM0'
|
||||||
|
- 'RABBITMQ_HOST=rabbitmq'
|
||||||
|
- 'RABBITMQ_USER=user'
|
||||||
|
- 'RABBITMQ_PASSWORD=pass'
|
||||||
|
- 'RABBITMQ_USE_SSL=false'
|
||||||
|
- 'RABBITMQ_VIRTUAL_HOST=/'
|
||||||
|
```
|
|
@ -0,0 +1,109 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
require_once(__DIR__.'/vendor/autoload.php');
|
||||||
|
use PhpAmqpLib\Connection\AMQPStreamConnection;
|
||||||
|
use PhpAmqpLib\Connection\AMQPSSLConnection;
|
||||||
|
use PhpAmqpLib\Message\AMQPMessage;
|
||||||
|
|
||||||
|
|
||||||
|
$lineReadTimeout=1;
|
||||||
|
|
||||||
|
$port=getenv("SCANNER_PORT");
|
||||||
|
$rabbitHost=getenv("RABBITMQ_HOST");
|
||||||
|
$rabbitPort=getenv("RABBITMQ_PORT")?getenv("RABBITMQ_PORT"):5672;
|
||||||
|
$rabbitLogin=getenv("RABBITMQ_USER");
|
||||||
|
$rabbitPass=getenv("RABBITMQ_PASSWORD");
|
||||||
|
$rabbitUseSSL=getenv("RABBITMQ_USE_SSL")==="true";
|
||||||
|
$rabbitVirtualHost=getenv("RABBITMQ_VIRTUAL_HOST")?getenv("RABBITMQ_VIRTUAL_HOST"):"/";
|
||||||
|
$routingTag=getenv("DEFAULT_TAG")?getenv("DEFAULT_TAG"):null;
|
||||||
|
|
||||||
|
$connection=null;
|
||||||
|
$channel=null;
|
||||||
|
|
||||||
|
|
||||||
|
while (1) {
|
||||||
|
try {
|
||||||
|
$connection=rabbitConnect($rabbitHost, $rabbitPort, $rabbitLogin, $rabbitPass, $rabbitUseSSL);
|
||||||
|
$channel=$connection->channel();
|
||||||
|
scanner($channel, $port);
|
||||||
|
} catch (Exception $e) {
|
||||||
|
print "Exception ".$e->getMessage()."\n";
|
||||||
|
$channel->close();
|
||||||
|
$connection->close();
|
||||||
|
$s=rand(5, 15);
|
||||||
|
print "Sleep $s seconds..";
|
||||||
|
sleep($s);
|
||||||
|
print "Ok\n";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function rabbitConnect($rabbitHost, $rabbitPort, $rabbitLogin, $rabbitPass, $rabbitUseSSL=false, $rabbitVirtualHost="/") {
|
||||||
|
if ($rabbitUseSSL) {
|
||||||
|
$ssl_opts=[
|
||||||
|
// set some SSL/TLS specific options
|
||||||
|
'verify_peer' => true,
|
||||||
|
'verify_peer_name' => true,
|
||||||
|
'allow_self_signed' => false
|
||||||
|
];
|
||||||
|
|
||||||
|
$connection = new AMQPSSLConnection($rabbitHost, $rabbitPort, $rabbitLogin, $rabbitPass, $rabbitVirtualHost, $ssl_opts);
|
||||||
|
} else {
|
||||||
|
$connection = new AMQPStreamConnection($rabbitHost, $rabbitPort, $rabbitLogin, $rabbitPass, $rabbitVirtualHost);
|
||||||
|
}
|
||||||
|
return $connection;
|
||||||
|
}
|
||||||
|
|
||||||
|
function register($channel, $routingTag) {
|
||||||
|
print "Register\n";
|
||||||
|
if (!empty($routingTag)) {
|
||||||
|
$msg = new AMQPMessage(json_encode(["type"=>"service","data"=>"register"]));
|
||||||
|
$channel->basic_publish($msg, 'fox.barcode', $routingTag);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function scanner($channel, $port) {
|
||||||
|
global $routingTag;
|
||||||
|
|
||||||
|
$fp = @fopen($port, "rb+");
|
||||||
|
if ($fp==null) {
|
||||||
|
throw new Exception("Scanner failed. Is it connected?");
|
||||||
|
} else {
|
||||||
|
print "Scanner connected\n";
|
||||||
|
if (!empty($routingTag)) {
|
||||||
|
$msg = new AMQPMessage(json_encode(["type"=>"service","data"=>"scannerReady"]));
|
||||||
|
$channel->basic_publish($msg, 'fox.barcode', $routingTag);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
while (1) {
|
||||||
|
$buffer = fgets($fp, 4096);
|
||||||
|
$rv=explode("\n", $buffer);
|
||||||
|
if ($buffer === false) {
|
||||||
|
if (!empty($routingTag)) {
|
||||||
|
$msg = new AMQPMessage(json_encode(["type"=>"service","data"=>"scannerFailed"]));
|
||||||
|
$channel->basic_publish($msg, 'fox.barcode', $routingTag);
|
||||||
|
}
|
||||||
|
throw new Exception("Stream read failed. Scanner disconnected?");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (preg_match('/^fxc([0-9a-f]{20})xf$/', $buffer, $ref)) {
|
||||||
|
print "Pairing request. Routing tag: $ref[1]\n";
|
||||||
|
$routingTag=$ref[1];
|
||||||
|
$msg = new AMQPMessage(json_encode(["type"=>"service","data"=>"scannerRegistered"]));
|
||||||
|
$channel->basic_publish($msg, 'fox.barcode', $routingTag);
|
||||||
|
|
||||||
|
} else {
|
||||||
|
print "Read: $rv[0]";
|
||||||
|
if (!empty($routingTag)) {
|
||||||
|
$msg = new AMQPMessage(json_encode(["type"=>"code","data"=>$rv[0]]));
|
||||||
|
$channel->basic_publish($msg, 'fox.barcode', $routingTag);
|
||||||
|
|
||||||
|
print " [S]";
|
||||||
|
} else {
|
||||||
|
print " [-]";
|
||||||
|
}
|
||||||
|
print "\n";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,5 @@
|
||||||
|
{
|
||||||
|
"require" : {
|
||||||
|
"php-amqplib/php-amqplib": ">=3.0"
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,2 +0,0 @@
|
||||||
pyserial
|
|
||||||
pika
|
|
101
scanner.py
101
scanner.py
|
@ -1,101 +0,0 @@
|
||||||
#!/usr/bin/python3
|
|
||||||
import serial
|
|
||||||
import time
|
|
||||||
import codecs
|
|
||||||
import re
|
|
||||||
import pika
|
|
||||||
import json
|
|
||||||
import os
|
|
||||||
import hashlib
|
|
||||||
|
|
||||||
# docker run -v "`pwd`:/src:ro" --device "/dev/ttyACM0:/dev/ttyACM0" -e "RABBIT_HOST=100.64.240.14" -e "RABBIT_PORT=5672" -e "RABBIT_USER=guest" -e "RABBIT_PASSWORD=itrw8pyNK8a7hipM" -e "RABBIT_USESSL=false" -e "RABBIT_EXCHANGE=fox.barcode" -e "ALLOW_SHORT_PAIRING_TAG=true" -e "ALLOW_LONG_PAIRING_TAG=true" -ti python:3 bash
|
|
||||||
# pip install --no-cache-dir -r list.txt
|
|
||||||
# pip3 install serial
|
|
||||||
# pip3 install pika
|
|
||||||
|
|
||||||
scannerPort=os.environ.get('SCANNER_PORT', "/dev/ttyACM0")
|
|
||||||
pingTimeout=os.environ.get('PING_TIMEOUT', 5)
|
|
||||||
rabbitHost=os.environ.get('RABBIT_HOST', None)
|
|
||||||
rabbitPort=os.environ.get('RABBIT_PORT', 5672)
|
|
||||||
#rabbitUser="guest"
|
|
||||||
rabbitUser=os.environ.get('RABBIT_USER', None)
|
|
||||||
#rabbitPass="itrw8pyNK8a7hipM"
|
|
||||||
rabbitPass=os.environ.get('RABBIT_PASSWORD', None)
|
|
||||||
rabbitSSL=os.environ.get('RABBIT_USE_SSL', "false").lower()=="true"
|
|
||||||
rabbitExchange=os.environ.get('RABBIT_EXCHANGE', "fox.barcode")
|
|
||||||
allowShortPairingTag=os.environ.get('ALLOW_SHORT_PAIRING', "true").lower()=="true"
|
|
||||||
allowLongPairingTag=os.environ.get('ALLOW_LONG_PAIRING', "true").lower()=="true"
|
|
||||||
routingTag=os.environ.get('DEFAULT_TAG', None)
|
|
||||||
debug_enabled=os.environ.get('DEBUG_ENABLED', "false").lower()=="true"
|
|
||||||
|
|
||||||
codelist = []
|
|
||||||
|
|
||||||
ser = serial.Serial(
|
|
||||||
port = scannerPort,\
|
|
||||||
timeout=0)
|
|
||||||
|
|
||||||
print("Scanner connected to: " + ser.portstr)
|
|
||||||
count=1
|
|
||||||
parameters = pika.URLParameters('amqp://'+rabbitUser+':'+rabbitPass+'@'+rabbitHost+':'+str(rabbitPort)+'/%2F')
|
|
||||||
|
|
||||||
connection = pika.BlockingConnection(parameters)
|
|
||||||
channel = connection.channel()
|
|
||||||
|
|
||||||
if (routingTag):
|
|
||||||
channel.basic_publish(exchange=rabbitExchange,
|
|
||||||
routing_key=routingTag,
|
|
||||||
body=json.dumps({"type": "service", "data": "scannerReady","timeout":pingTimeout}))
|
|
||||||
|
|
||||||
tmx=0;
|
|
||||||
while True:
|
|
||||||
tmx+=1
|
|
||||||
if (tmx > pingTimeout*10):
|
|
||||||
tmx=0
|
|
||||||
if (debug_enabled):
|
|
||||||
print("Ping sent "+str(routingTag))
|
|
||||||
if (routingTag):
|
|
||||||
channel.basic_publish(exchange=rabbitExchange,
|
|
||||||
routing_key=routingTag,
|
|
||||||
body=json.dumps({"type": "service", "data": "scannerReady","timeout":pingTimeout}))
|
|
||||||
|
|
||||||
time.sleep(0.1)
|
|
||||||
for byte in ser.readline():
|
|
||||||
if (byte==128):
|
|
||||||
continue
|
|
||||||
if (byte=="\r"):
|
|
||||||
continue
|
|
||||||
if byte==13:
|
|
||||||
try:
|
|
||||||
codestring=codecs.decode(bytes(codelist),"utf-8")
|
|
||||||
if (len(codestring)>1):
|
|
||||||
m = re.match('^fxc([0-9a-f]{20})xf$', codestring)
|
|
||||||
m2=re.match('^70918([0-9]{9})819070$', codestring)
|
|
||||||
if (m and allowLongPairingTag):
|
|
||||||
print("Pairing request. RoutingTag: "+m.group(1))
|
|
||||||
routingTag=m.group(1)
|
|
||||||
channel.basic_publish(exchange=rabbitExchange,
|
|
||||||
routing_key=routingTag,
|
|
||||||
body=json.dumps({"type": "service", "data": "scannerReady","timeout":pingTimeout}))
|
|
||||||
elif(m2 and allowShortPairingTag):
|
|
||||||
rt2=hashlib.sha256(m2.group(1).encode());
|
|
||||||
routingTag=rt2.hexdigest()[5:25];
|
|
||||||
print("Pairing request. RoutingTag: "+routingTag)
|
|
||||||
channel.basic_publish(exchange=rabbitExchange,
|
|
||||||
routing_key=routingTag,
|
|
||||||
body=json.dumps({"type": "service", "data": "scannerReady","timeout":pingTimeout}))
|
|
||||||
else:
|
|
||||||
if (debug_enabled):
|
|
||||||
print("Read: "+codestring)
|
|
||||||
tmx=0
|
|
||||||
if (routingTag):
|
|
||||||
channel.basic_publish(exchange=rabbitExchange,
|
|
||||||
routing_key=routingTag,
|
|
||||||
body=json.dumps({"type": "code", "data": codestring,"timeout":pingTimeout}))
|
|
||||||
if (debug_enabled):
|
|
||||||
print("Sent")
|
|
||||||
except:
|
|
||||||
print("read failed")
|
|
||||||
codelist=[]
|
|
||||||
else:
|
|
||||||
codelist.append(byte)
|
|
||||||
|
|
Loading…
Reference in New Issue