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..ae72a52 --- /dev/null +++ b/.drone.yml @@ -0,0 +1,98 @@ +--- +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} + \ 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 new file mode 100644 index 0000000..86c8afb --- /dev/null +++ b/Dockerfile @@ -0,0 +1,3 @@ +FROM php:8.1 +COPY . /foxScanner +CMD ["php","/foxScanner/barcodeScanner.php"] 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..af8b168 --- /dev/null +++ b/barcodeScanner.php @@ -0,0 +1,101 @@ +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"; + } + } +} 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" + } +}