python version

This commit is contained in:
Pavel Dmitriev 2023-02-08 20:40:05 +03:00
parent 11f4059a3b
commit 624cbc95b0
8 changed files with 108 additions and 229 deletions

View File

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

View File

@ -1,98 +0,0 @@
---
kind: pipeline
name: SonarQube check
image_pull_secrets:
- dockerconfig
trigger:
branch:
- develop
- ci-test
event:
- push
- custom
steps:
- name: SonarQube check
image: sonarsource/sonar-scanner-cli
environment:
SONAR_PROJECT:
from_secret: sonarProjectId
SONAR_TOKEN:
from_secret: sonarToken
SONAR_HOST:
from_secret: sonarHost
TEST: test
commands:
- sonar-scanner -Dsonar.projectKey=$${SONAR_PROJECT} -Dsonar.sources=. -Dsonar.host.url=$${SONAR_HOST} -Dsonar.login=$${SONAR_TOKEN}
depends_on:
- clone
---
kind: pipeline
name: Build image
image_pull_secrets:
- dockerconfig
trigger:
ref:
- refs/heads/ci-test
- refs/heads/testing
- refs/heads/master
- 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
environment:
DOCKER_AUTH:
from_secret: dockerconfig
IMAGE_PREFIX: mxfox.ru/mxfox/barcode-scanner-client
commands:
- buildx-bgstart.sh
- echo $${DOCKER_AUTH} > ~/.docker/config.json
- echo "CB ${CI_COMMIT_BRANCH}"
- echo "DT ${DRONE_TAG}"
- |
if [ -n "${DRONE_TAG}" ]
then
export xBuildSuffix=" -t $${IMAGE_PREFIX}:${DRONE_TAG} -t $${IMAGE_PREFIX}:latest --push"
else
export xBuildSuffix=" -t $${IMAGE_PREFIX}:${CI_COMMIT_BRANCH}-${CI_BUILD_NUMBER}-${DRONE_COMMIT_SHA:0:10} -t $${IMAGE_PREFIX}:${CI_COMMIT_BRANCH} --push"
fi
- docker buildx build --platform linux/arm,linux/amd64,linux/arm64 . $${xBuildSuffix}

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

@ -1,101 +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=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 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)