Compare commits

..

12 Commits

Author SHA1 Message Date
Pavel Dmitriev 623ec65fff Merge pull request 'ci added' (#5) from ci-test into develop
continuous-integration/drone/push Build is failing Details
Reviewed-on: #5
2023-02-09 15:32:42 +03:00
Pavel Dmitriev a5f83400ec ci added
continuous-integration/drone/push Build is failing Details
2023-02-08 20:49:28 +03:00
Pavel Dmitriev 624cbc95b0 python version 2023-02-08 20:40:05 +03:00
Pavel Dmitriev 11f4059a3b vhost fix
continuous-integration/drone/push Build is passing Details
2023-01-22 11:49:15 +03:00
Pavel Dmitriev 983ffcf445 ci fix
continuous-integration/drone/push Build is passing Details
2023-01-22 11:37:32 +03:00
Pavel Dmitriev a86fb52fd3 drone fix
continuous-integration/drone/push Build is failing Details
2023-01-22 11:35:31 +03:00
Pavel Dmitriev 868753ed8c sonar fix
continuous-integration/drone/push Build is failing Details
2023-01-22 11:34:09 +03:00
Pavel Dmitriev b4ec5c915b sonar fix
continuous-integration/drone/push Build is failing Details
2023-01-22 11:32:29 +03:00
Pavel Dmitriev 8fb0336a3c fix2
continuous-integration/drone/push Build is failing Details
2023-01-22 11:21:37 +03:00
Pavel Dmitriev 856068d61f fix
continuous-integration/drone/push Build was killed Details
2023-01-22 11:18:02 +03:00
Pavel Dmitriev 55a3b08e81 ci-test
continuous-integration/drone Build is failing Details
2023-01-22 11:08:08 +03:00
Pavel Dmitriev a359883a74 CI test 2023-01-22 09:21:15 +03:00
9 changed files with 108 additions and 185 deletions

View File

@ -1,13 +0,0 @@
/.git
/.settings
README.*
LICENSE
CHANGELOG.*
/docs
*.md
/.buildpath
/.project
/.gitignore
/.dockerignore
/temp-test
/build.sh

View File

@ -45,34 +45,6 @@ trigger:
- refs/tags/*
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
image: mxfox.ru/mxfox/docker-dind.buildx:latest
privileged: true

9
.gitignore vendored
View File

@ -1,9 +0,0 @@
/modules/*
!/modules/README.md
/storage/*
!/storage/README.md
/temp-test
/composer.lock
/vendor
.scannerwork
.vscode

View File

@ -1,3 +1,5 @@
FROM php:8.1
COPY . /foxScanner
CMD ["php","/foxScanner/barcodeScanner.php"]
FROM python:3
WORKDIR /foxScanner
COPY . .
RUN pip install --no-cache-dir -r requirements
CMD ["python","scanner.py"]

View File

@ -8,21 +8,3 @@
* RABBITMQ_USER
* RABBITMQ_PASSWORD
* 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=/'
```

View File

@ -1,109 +0,0 @@
<?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";
}
}
}

View File

@ -1,5 +0,0 @@
{
"require" : {
"php-amqplib/php-amqplib": ">=3.0"
}
}

2
requirements Normal file
View File

@ -0,0 +1,2 @@
pyserial
pika

101
scanner.py Executable file
View File

@ -0,0 +1,101 @@
#!/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)