Package: barcode-server
barcode-serverA simple daemon to read barcodes from USB Barcode Scanners
and expose them to other service using HTTP calls, a websocket API or MQTT.FeaturesAutodetect Barcode Scanner devices on the flyRequest Server information viaREST APISubscribe to barcode events usingWebsocket APIPush barcode events usingHTTP requestsMQTT messagesGetstatisticsvia Prometheus exporterHow to useDevice Access PermissionsEnsure the user running this application is in the correct group for accessing
input devices (usuallyinput), like this:sudo usermod -a -G input myusernameConfigurationbarcode-serverusescontainer-app-confto provide configuration via a YAML or TOML file as well as ENV variables. Have a look at thedocumentation about it.The config file is searched for in the following locations (in this order):./~/.config/~/Seebarcode_server.yamlfor an example in this repo.Native# create venvpython-mvenv./venv# enter venvsource./venv/bin/activate# install barcode-serverpipinstallbarcode-server# exit venvdeactivate# print help./venv/bin/barcode-server-hThis will give you an overview of all available commands:>./venv/bin/barcode-server-h
Usage:barcode-server[OPTIONS]COMMAND[ARGS]...

Options:--versionShowtheversionandexit.-h,--helpShowthismessageandexit.

Commands:configPrintthecurrentconfigurationofbarcode-serverrunRunthebarcode-serverDockerThe docker image will by default be launched with theruncommand, making it easier
for production deployment. When starting the docker container, make sure to pass through
input devices as well as the configuration file:dockerrun-it--rm\--namebarcode\--device=/dev/input\-v"/home/markus/.config/barcode_server.yaml:/app/barcode_server.yaml"\-ePUID=0\-ePGID=0\markusressel/barcode-serverNote:Althoughbarcode-serverwill continuously try to detect new devices,
even when passing through/dev/inputlike shown above, new devices can not be detected
due to the way docker works. If you need to detect devices in real-time, you have to use
the native approach.The user and group id that should be used within the container can be specified using thePUIDandPGIDenvironment variables.To override the default command, simply specify command arguments directly:dockerrun-it--rm\...markusressel/barcode-server--helpWebserverBy default the webserver will listen to127.0.0.1on port9654.AuthorizationWhen specified in the config, an API token is required to authorize clients, which must
be passed using aX-Auth-Tokenheader when connecting. Since barcode-scanner doesn't rely on any
persistence, the token is specified in the configuration file and can not be changed on runtime.Rest APIbarcode-serverprovides a simple REST API to get some basic information.
This API cannotbe used to retrieve barcode events. To do that you have to use one of
the approaches described below.EndpointDescription/devicesA list of all currently detected devices.Websocket APIIn addition to the REST APIbarcode-serveralso exposes a websocket at/, which can be used
to get realtime barcode scan events.To connect to it, you have to provideaClient-IDheader with a UUID (v4)(optional) an emptyDrop-Event-Queueheader, to ignore events that happened between connections(optional) anX-Auth-Tokenheader, to authorize the clientMessages received on this websocket are JSON formatted strings with the following format:{"id":"33cb5677-3d0b-4faf-9dc4-d19a8ee7d8a1","serverId":"cash-register-1","date":"2020-08-03T10:00:00+00:00","device":{"name":"BARCODE SCANNER BARCODE SCANNER","path":"/dev/input/event3","vendorId":"ffff","productId":"0035"},"barcode":"4250168519463"}To test the connection you can use f.ex.websocat:> websocat - autoreconnect:ws://127.0.0.1:9654 --text --header "Client-ID:dc1f14fc-a7a6-4102-af60-2b6e0dcf744c" --header "Drop-Event-Queue:" --header "X-Auth-Token:EmUSqjXGfnQwn5wn6CpzJRZgoazMTRbMNgH7CXwkQG7Ph7stex"
{"date":"2020-12-20T19:35:04.769739","device":{"name":"BARCODE SCANNER BARCODE SCANNER","path":"/dev/input/event3","vendorId":65535,"productId":53},"barcode":"D-t38409355843o52230Lm54784"}
{"date":"2020-12-20T19:35:06.237408","device":{"name":"BARCODE SCANNER BARCODE SCANNER","path":"/dev/input/event3","vendorId":65535,"productId":53},"barcode":"4250168519463"}HTTP RequestWhen configured, you can letbarcode-scannerissue a HTTP request (defaults toPOST) when a
barcode is scanned, which provides the ability to push barcode events to a server that is unaware
of any client. The body of the request will contain the same JSON as in the websocket API example.To do this simply add the following section to your config:barcode_server:[...]http:url:"https://my.domain.com/barcode"Have a look at theexample configfor more options.MQTT PublishWhen configured, you can letbarcode-scannerpublish barcode events to a MQTT broker.
The payload of the message will contain the same JSON as in the websocket API example.To do this simply add the following section to your config:barcode_server:[...]mqtt:host:"my.mqtt.broker"Have a look at theexample configfor more options.Statisticsbarcode-serverexposes a prometheus exporter (defaults to port8000) to give some statistical insight.
A brief overview of (most) available metrics:NameTypeDescriptionwebsocket_client_countGaugeNumber of currently connected websocket clientsdevices_countGaugeNumber of currently detected devicesscan_countGaugeNumber of times a scan has been detecteddevice_detection_processing_secondsSummaryTime spent detecting devicesrest_endpoint_processing_secondsSummaryTime spent in a rest command handlernotifier_processing_secondsSummaryTime spent in a notifierFAQCan I lock the Barcode Scanner to this application?Yes. Most barcode readers normally work like a keyboard, resulting in their input being evaluated by
the system, which can clutter up your TTY or other open programs.barcode-serverwill try tograbinput devices, making it the sole recipient of all
incoming input events from those devices, which should prevent the device from cluttering
your TTY.If, for some reason, this does not work for you, try this:Create a file/etc/udev/rules.d/10-barcode.rules:SUBSYSTEM=="input", ACTION=="add", ATTRS{idVendor}=="xxxx", ATTRS{idProduct}=="yyyy", RUN+="/bin/sh -c 'echo remove > /sys$env{DEVPATH}/uevent'"
SUBSYSTEM=="input", ACTION=="add", ATTRS{idVendor}=="xxxx", ATTRS{idProduct}=="yyyy", DEVPATH=="*:1.0/*", KERNEL=="event*", RUN+="/bin/sh -c 'ln -sf /dev/input/$kernel /dev/input/barcode_scanner'"Replace theidVendorandidProductvalues with the values of your barcode reader (a 4 digit hex value with leading
zeros).
You can find them in the log output ofbarcode-readeror usinglsusbwith the wireless receiver attached to your
computer.Reload udev rules using:udevadm control --reloadthen remove and reinsert the wireless receiver.
You should now have a symlink in/dev/input/barcode_scanner:ls -lha /dev/input/barcode_scannerwhich can be used in thedevice_pathssection of thebarcode-serverconfig.Source:ThisandThatContributingGitHub is for social coding: if you want to write code, I encourage contributions
through pull requests from forks of this repository. Create GitHub tickets for
bugs and new features and comment on the ones that you are interested in.Licensebarcode-server is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <https://www.gnu.org/licenses/>.