foxBarcodeScannerClient/barcodeScanner.php

102 lines
3.2 KiB
PHP
Raw Normal View History

2023-01-22 06:21:15 +00:00
<?php
2023-01-22 08:08:08 +00:00
require_once(__DIR__.'/vendor/autoload.php');
2023-01-22 06:21:15 +00:00
use PhpAmqpLib\Connection\AMQPStreamConnection;
use PhpAmqpLib\Connection\AMQPSSLConnection;
use PhpAmqpLib\Message\AMQPMessage;
$lineReadTimeout=1;
2023-01-22 08:18:02 +00:00
$port=getenv("SCANNER_PORT");
$rabbitHost=getenv("RABBITMQ_HOST");
2023-01-22 06:21:15 +00:00
$rabbitPort=getenv("RABBITMQ_PORT")?getenv("RABBITMQ_PORT"):5672;
2023-01-22 08:18:02 +00:00
$rabbitLogin=getenv("RABBITMQ_USER");
$rabbitPass=getenv("RABBITMQ_PASSWORD");
$rabbitUseSSL=getenv("RABBITMQ_USE_SSL")==="true";
$rabbitVirtualHost=getenv("RABBITMQ_VIRTUAL_HOST")?getenv("RABBITMQ_VIRTUAL_HOST"):"/";
2023-01-22 06:21:15 +00:00
$routingTag=null;
$connection=null;
$channel=null;
2023-01-22 08:32:29 +00:00
while (1) {
2023-01-22 06:21:15 +00:00
try {
2023-01-22 08:32:29 +00:00
$connection=rabbitConnect($rabbitHost, $rabbitPort, $rabbitLogin, $rabbitPass, $rabbitUseSSL);
2023-01-22 06:21:15 +00:00
$channel=$connection->channel();
2023-01-22 08:32:29 +00:00
scanner($channel, $port);
2023-01-22 06:21:15 +00:00
} catch (Exception $e) {
print "Exception ".$e->getMessage()."\n";
$channel->close();
$connection->close();
2023-01-22 08:32:29 +00:00
$s=rand(5, 15);
2023-01-22 06:21:15 +00:00
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
];
2023-01-22 08:32:29 +00:00
$connection = new AMQPSSLConnection($rabbitHost, $rabbitPort, $rabbitLogin, $rabbitPass, $rabbitVirtualHost, $ssl_opts);
2023-01-22 06:21:15 +00:00
} else {
2023-01-22 08:32:29 +00:00
$connection = new AMQPStreamConnection($rabbitHost, $rabbitPort, $rabbitLogin, $rabbitPass, $rabbitVirtualHost);
2023-01-22 06:21:15 +00:00
}
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);
}
}
2023-01-22 08:32:29 +00:00
while (1) {
$buffer = fgets($fp, 4096);
$rv=explode("\n", $buffer);
2023-01-22 06:21:15 +00:00
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?");
}
2023-01-22 08:32:29 +00:00
if (preg_match('/^fxc([0-9a-f]{20})xf$/', $buffer, $ref)) {
2023-01-22 06:21:15 +00:00
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);
2023-01-22 08:32:29 +00:00
print " [S]";
2023-01-22 06:21:15 +00:00
} else {
print " [-]";
}
print "\n";
}
}
2023-01-22 08:32:29 +00:00
}