From a359883a744e869ad93ef9e59b590f8238a912f9 Mon Sep 17 00:00:00 2001 From: Pavel Dmitriev Date: Sun, 22 Jan 2023 09:21:15 +0300 Subject: [PATCH 1/9] CI test --- Dockerfile | 0 README.md | 8 ++++ barcodeScanner.php | 100 +++++++++++++++++++++++++++++++++++++++++++++ composer.json | 5 +++ 4 files changed, 113 insertions(+) create mode 100644 Dockerfile create mode 100644 barcodeScanner.php create mode 100644 composer.json diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..e69de29 diff --git a/README.md b/README.md index e414cec..f85c756 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,10 @@ # foxBarcodeScannerClient +Клиент для работы со сканером штрих-кода через последовательный порт. Клиент получает данные со сканера и отправляет в очередь RabbitMQ, указанную при регистрации. Регистрация происходит через считывания специального штрих-кода. + +## Параметры конфигурации +* SCANNER_PORT - /dev/ttyACM0 +* RABBITMQ_HOST +* RABBITMQ_USER +* RABBITMQ_PASSWORD +* RABBITMQ_USE_SSL diff --git a/barcodeScanner.php b/barcodeScanner.php new file mode 100644 index 0000000..3391135 --- /dev/null +++ b/barcodeScanner.php @@ -0,0 +1,100 @@ +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) { + 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,'/',$ssl_opts); + } else { + $connection = new AMQPStreamConnection($rabbitHost, $rabbitPort, $rabbitLogin, $rabbitPass); + } + 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"; + } + } +} \ No newline at end of file diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..95fdcb4 --- /dev/null +++ b/composer.json @@ -0,0 +1,5 @@ +{ + "require" : { + "php-amqplib/php-amqplib": ">=3.0" + } +} -- 2.40.1 From 55a3b08e81d0dec9783a3fe3d523e1a49b48992c Mon Sep 17 00:00:00 2001 From: Pavel Dmitriev Date: Sun, 22 Jan 2023 11:08:08 +0300 Subject: [PATCH 2/9] ci-test --- .dockerignore | 13 ++++++ .drone.yml | 100 +++++++++++++++++++++++++++++++++++++++++++++ .gitignore | 9 ++++ Dockerfile | 3 ++ barcodeScanner.php | 3 +- 5 files changed, 126 insertions(+), 2 deletions(-) create mode 100644 .dockerignore create mode 100644 .drone.yml create mode 100644 .gitignore diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..3f7036b --- /dev/null +++ b/.dockerignore @@ -0,0 +1,13 @@ +/.git +/.settings +README.* +LICENSE +CHANGELOG.* +/docs +*.md +/.buildpath +/.project +/.gitignore +/.dockerignore +/temp-test +/build.sh \ No newline at end of file diff --git a/.drone.yml b/.drone.yml new file mode 100644 index 0000000..ac1363a --- /dev/null +++ b/.drone.yml @@ -0,0 +1,100 @@ +--- +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 '{}' \; + find . -type d ! -path './.git/**' ! -path './static/**' ! -path "./static" ! -path ./*/modules/*/static* -exec bash -c 'test -f {}/.htaccess && echo -n "[ SKIP ] " || (cp ./docker-build/.htaccess {} && echo -n "[ ADD ] ") && echo {}/.htaccess' \; + cp docker-build/Dockerfile Dockerfile + 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/chimera-mk2-core + + 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} + \ No newline at end of file diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..9720747 --- /dev/null +++ b/.gitignore @@ -0,0 +1,9 @@ +/modules/* +!/modules/README.md +/storage/* +!/storage/README.md +/temp-test +/composer.lock +/vendor +.scannerwork +.vscode diff --git a/Dockerfile b/Dockerfile index e69de29..86c8afb 100644 --- a/Dockerfile +++ b/Dockerfile @@ -0,0 +1,3 @@ +FROM php:8.1 +COPY . /foxScanner +CMD ["php","/foxScanner/barcodeScanner.php"] diff --git a/barcodeScanner.php b/barcodeScanner.php index 3391135..b6a730a 100644 --- a/barcodeScanner.php +++ b/barcodeScanner.php @@ -1,7 +1,6 @@ Date: Sun, 22 Jan 2023 11:18:02 +0300 Subject: [PATCH 3/9] fix --- .drone.yml | 2 -- barcodeScanner.php | 16 +++++++++------- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/.drone.yml b/.drone.yml index ac1363a..97cad6c 100644 --- a/.drone.yml +++ b/.drone.yml @@ -71,8 +71,6 @@ steps: find . -name ".travis*" -prune -exec rm -rf '{}' \; find . -name "installed.json" -prune -exec rm -rf '{}' \; find . -name "*.sample" -prune -exec rm -rf '{}' \; - find . -type d ! -path './.git/**' ! -path './static/**' ! -path "./static" ! -path ./*/modules/*/static* -exec bash -c 'test -f {}/.htaccess && echo -n "[ SKIP ] " || (cp ./docker-build/.htaccess {} && echo -n "[ ADD ] ") && echo {}/.htaccess' \; - cp docker-build/Dockerfile Dockerfile rm -f composer.* - name: Build docker image diff --git a/barcodeScanner.php b/barcodeScanner.php index b6a730a..5ce8ee1 100644 --- a/barcodeScanner.php +++ b/barcodeScanner.php @@ -8,12 +8,14 @@ use PhpAmqpLib\Message\AMQPMessage; $lineReadTimeout=1; -$port=getenv("SCANNER_PORT");// "/dev/ttyACM0"; -$rabbitHost=getenv("RABBITMQ_HOST"); // "rabbitmq"; +$port=getenv("SCANNER_PORT"); +$rabbitHost=getenv("RABBITMQ_HOST"); $rabbitPort=getenv("RABBITMQ_PORT")?getenv("RABBITMQ_PORT"):5672; -$rabbitLogin=getenv("RABBITMQ_USER");//'guest'; -$rabbitPass=getenv("RABBITMQ_PASSWORD"); //'guest'; -$rabbitUseSSL=getenv("RABBITMQ_USE_SSL")==="true"; // false; +$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; @@ -45,9 +47,9 @@ function rabbitConnect($rabbitHost, $rabbitPort, $rabbitLogin, $rabbitPass, $rab 'allow_self_signed' => false ]; - $connection = new AMQPSSLConnection($rabbitHost, $rabbitPort, $rabbitLogin, $rabbitPass,'/',$ssl_opts); + $connection = new AMQPSSLConnection($rabbitHost, $rabbitPort, $rabbitLogin, $rabbitPass,$rabbitVirtualHost,$ssl_opts); } else { - $connection = new AMQPStreamConnection($rabbitHost, $rabbitPort, $rabbitLogin, $rabbitPass); + $connection = new AMQPStreamConnection($rabbitHost, $rabbitPort, $rabbitLogin, $rabbitPass,$rabbitVirtualHost); } return $connection; } -- 2.40.1 From 8fb0336a3c41fa5188687007df63dddfb0d6a2b5 Mon Sep 17 00:00:00 2001 From: Pavel Dmitriev Date: Sun, 22 Jan 2023 11:21:37 +0300 Subject: [PATCH 4/9] fix2 --- .drone.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.drone.yml b/.drone.yml index 97cad6c..62ed833 100644 --- a/.drone.yml +++ b/.drone.yml @@ -79,7 +79,7 @@ steps: environment: DOCKER_AUTH: from_secret: dockerconfig - IMAGE_PREFIX: mxfox.ru/mxfox/chimera-mk2-core + IMAGE_PREFIX: mxfox.ru/mxfox/barcodeScannerClient commands: - buildx-bgstart.sh -- 2.40.1 From b4ec5c915b38ed878ac495accd4340f37f587f08 Mon Sep 17 00:00:00 2001 From: Pavel Dmitriev Date: Sun, 22 Jan 2023 11:32:29 +0300 Subject: [PATCH 5/9] sonar fix --- barcodeScanner.php | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/barcodeScanner.php b/barcodeScanner.php index 5ce8ee1..a3f4a41 100644 --- a/barcodeScanner.php +++ b/barcodeScanner.php @@ -22,16 +22,16 @@ $connection=null; $channel=null; -while(1) { +while (1) { try { - $connection=rabbitConnect($rabbitHost,$rabbitPort,$rabbitLogin,$rabbitPass,$rabbitUseSSL); + $connection=rabbitConnect($rabbitHost, $rabbitPort, $rabbitLogin, $rabbitPass, $rabbitUseSSL); $channel=$connection->channel(); - scanner($channel,$port); + scanner($channel, $port); } catch (Exception $e) { print "Exception ".$e->getMessage()."\n"; $channel->close(); $connection->close(); - $s=rand(5,15); + $s=rand(5, 15); print "Sleep $s seconds.."; sleep($s); print "Ok\n"; @@ -47,9 +47,9 @@ function rabbitConnect($rabbitHost, $rabbitPort, $rabbitLogin, $rabbitPass, $rab 'allow_self_signed' => false ]; - $connection = new AMQPSSLConnection($rabbitHost, $rabbitPort, $rabbitLogin, $rabbitPass,$rabbitVirtualHost,$ssl_opts); + $connection = new AMQPSSLConnection($rabbitHost, $rabbitPort, $rabbitLogin, $rabbitPass, $rabbitVirtualHost, $ssl_opts); } else { - $connection = new AMQPStreamConnection($rabbitHost, $rabbitPort, $rabbitLogin, $rabbitPass,$rabbitVirtualHost); + $connection = new AMQPStreamConnection($rabbitHost, $rabbitPort, $rabbitLogin, $rabbitPass, $rabbitVirtualHost); } return $connection; } @@ -68,9 +68,9 @@ function scanner($channel, $port) { } } - while(1) { - $buffer = fgets($fp,4096); - $rv=explode("\n",$buffer); + 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"])); @@ -79,7 +79,7 @@ function scanner($channel, $port) { throw new Exception("Stream read failed. Scanner disconnected?"); } - if (preg_match('/^fxc([0-9a-f]{20})xf$/',$buffer,$ref)) { + 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"])); @@ -91,11 +91,11 @@ function scanner($channel, $port) { $msg = new AMQPMessage(json_encode(["type"=>"code","data"=>$rv[0]])); $channel->basic_publish($msg, 'fox.barcode', $routingTag); - print " [S]"; + print " [S]"; } else { print " [-]"; } print "\n"; } } -} \ No newline at end of file +} -- 2.40.1 From 868753ed8c5b21978a57fb383fc54759c0804e1f Mon Sep 17 00:00:00 2001 From: Pavel Dmitriev Date: Sun, 22 Jan 2023 11:34:09 +0300 Subject: [PATCH 6/9] sonar fix --- barcodeScanner.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/barcodeScanner.php b/barcodeScanner.php index a3f4a41..8c32a34 100644 --- a/barcodeScanner.php +++ b/barcodeScanner.php @@ -69,7 +69,7 @@ function scanner($channel, $port) { } while (1) { - $buffer = fgets($fp, 4096); + $buffer = fgets($fp, 4096); $rv=explode("\n", $buffer); if ($buffer === false) { if (!empty($routingTag)) { -- 2.40.1 From a86fb52fd3ca1608fa54a3b9433907de3fe80b9c Mon Sep 17 00:00:00 2001 From: Pavel Dmitriev Date: Sun, 22 Jan 2023 11:35:31 +0300 Subject: [PATCH 7/9] drone fix --- .drone.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.drone.yml b/.drone.yml index 62ed833..11b584f 100644 --- a/.drone.yml +++ b/.drone.yml @@ -94,5 +94,5 @@ steps: 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} + - docker buildx build --platform linux/arm,linux/amd64,linux/arm64 . $${xBuildSuffix} \ No newline at end of file -- 2.40.1 From 983ffcf445ec8609c2695f1cd3be8a565f57b896 Mon Sep 17 00:00:00 2001 From: Pavel Dmitriev Date: Sun, 22 Jan 2023 11:37:32 +0300 Subject: [PATCH 8/9] ci fix --- .drone.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.drone.yml b/.drone.yml index 11b584f..ae72a52 100644 --- a/.drone.yml +++ b/.drone.yml @@ -79,7 +79,7 @@ steps: environment: DOCKER_AUTH: from_secret: dockerconfig - IMAGE_PREFIX: mxfox.ru/mxfox/barcodeScannerClient + IMAGE_PREFIX: mxfox.ru/mxfox/barcode-scanner-client commands: - buildx-bgstart.sh -- 2.40.1 From 11f4059a3bc19006752ebae5eea8ea665e22f8e1 Mon Sep 17 00:00:00 2001 From: Pavel Dmitriev Date: Sun, 22 Jan 2023 11:49:15 +0300 Subject: [PATCH 9/9] vhost fix --- barcodeScanner.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/barcodeScanner.php b/barcodeScanner.php index 8c32a34..af8b168 100644 --- a/barcodeScanner.php +++ b/barcodeScanner.php @@ -38,7 +38,7 @@ while (1) { } } -function rabbitConnect($rabbitHost, $rabbitPort, $rabbitLogin, $rabbitPass, $rabbitUseSSL=false) { +function rabbitConnect($rabbitHost, $rabbitPort, $rabbitLogin, $rabbitPass, $rabbitUseSSL=false, $rabbitVirtualHost="/") { if ($rabbitUseSSL) { $ssl_opts=[ // set some SSL/TLS specific options -- 2.40.1