Managing the IoT Box¶
This page is for the developers who maintain and improve the Viindoo IoT apps and the software of the Viindoo IoT Box. It explains how the parts fit together, what each side of the protocol between a box and its server expects, how to run the box code and its tests without a Raspberry Pi, how to make the usual changes, and how a release of the box software reaches the boxes.
Every name on this page is a module, a file or a symbol you can search for. The code is the reference: when this page and the code differ, trust the code and correct the page.
To set up and use boxes the way customers do, read the user guides first: Set up and manage IoT boxes and Use IoT devices in Viindoo apps.
The parts of the IoT cluster¶
The cluster has three parts: the IoT apps installed in a Viindoo database, the software that runs on the box, and, for databases hosted on Viindoo Cloud, the service that gives a box a name of its own.
Part |
What it does |
|---|---|
|
The platform: box and device records, pairing, the routes a box calls ( |
|
Point of Sale: receipt printer, barcode scanners, scale and customer screen of a till, and the kitchen and bar preparation printers. |
|
Manufacturing: the label printer, scale and footswitch of each work center, in the work order
and on the Shop Floor screen ( |
|
Quality: a measurement read from an instrument, on a quality check, on a work-order check and on
a Measure step of the Shop Floor screen ( |
|
Viindoo Cloud only: passes the question of a box for its Viindoo name on to Viindoo Cloud. |
|
The box layer, in the public repository Viindoo/iot-hw: it starts the box modules of core under Viindoo’s control, pairs the box with its server, keeps it updated and serves the box page. |
|
The system files of the box, the installer of the layer, the image build, the release checks, and the software box used for development. |
Every other module depends on viin_iot, directly or through another bridge, and is installed
automatically once the modules it depends on are installed. A model that drives a device inherits
viin.iot.device.consumer and declares its device field in _viin_iot_device_field;
pos.config, pos.printer, mrp.workcenter, mrp.workorder and viin.quality.check do
so.
Who talks to whom¶
The box calls its server on the
/iot/*routes ofviin_iot: to pair, to report itself and its devices, to send its logs, to confirm the end of an action and the value of a reading, and to ask for its Viindoo name.The server never calls the box. It pushes device commands on the box’s own bus channel, which the box listens to over the live connection (websocket) it opens to the server.
The browser calls the box on the local network, on the device routes of core (
/hw_drivers/eventand/hw_drivers/action). Onlyviin_iot_device_proxy.jsnames these routes; a consumer module asks theviin_iot_deviceservice and never builds a box address itself.A person opens the box page at the address of the box, to pair it, set it up and read its self-test.
How the box layer sits on the core box code¶
A box runs the box modules of core unchanged: hw_drivers, hw_escpos and
hw_posbox_homepage, at the core commit the release names. The Viindoo layer is loaded around them
through server_wide_modules, in this order (box/install/install_layer.sh):
viin_hw_drivers,hw_drivers,hw_escpos,hw_posbox_homepage,viin_hw_homepage,web
Neither layer module can be installed in a database ('installable': False); they run on the box
only.
The gate¶
viin_hw_drivers/__init__.py calls boot.start(). The gate first honours the reset file of the
card, then holds the threads that core starts on import, imports core, applies the seams, loads every
other file of the layer and keeps the certificate of the box, then lets the threads of core run.
start never raises: whatever fails is logged as Viindoo layer integrity and recorded, and the
box starts without that part. If hw_drivers was loaded before the layer, no seam is applied and the self-test says so.
Seams, not patches¶
The layer never edits a file of core. Each place where it touches core is a seam, declared in
viin_hw_drivers/seams/ (and one in viin_hw_homepage/controllers/hw_posbox_homepage_lock.py, the
seam that closes and locks the routes of core):
a
Seamnames itsnumber(its order and its name in every report), itstarget(the symbol of core) and itsfeature(what the box loses if the seam does not hold);FunctionSeamwraps a function or a method and checks that its parameters are still the ones the replacement was written for;PartsSeamapplies its parts one by one;@registerdeclares a seam;seams.loadimports every seam file, so declaring is all it takes;a seam whose symbol exists only once core has loaded its device handlers names, in
after, the seam that applies it then; until then it isdeferred.
When the symbol of a seam moved or changed shape, only that seam fails: its feature is off, the log and
the Box software line of the self-test say which one, and the box still starts.
seams.integrity() is the state the box page shows.
The seam files are grouped by what they touch: startup.py (threads, pairing code service of core,
update check, vendor certificate), identity.py (address, version and hardware address the box
reports), reporting.py (reports to the server and the live channel), trust.py (verified
connections to the server), attestation.py (confirmed readings), service.py (connections a
client leaves idle, the stop of the web server) and handlers.py (device handler files, screen page,
status ticket, device owner, the listing of printers and of the devices of an interface, USB keyboards and
scanners).
The seam check¶
viin_hw_drivers/boot.py run as a program checks every seam against the core a configuration names
and starts nothing. It prints one line per seam: ok (with a note when the seam is deferred), or
FAILED with the reason and the feature that is off. It exits with 0 when every seam holds, 1 when
one does not or a part of the layer is not loaded, and 2 when core cannot be imported. Run it whenever the core commit changes; CI runs it on every change.
The release pointer¶
box-release.json at the root of the repository says which release a commit of the layer is:
Key |
Meaning |
|---|---|
|
The name of the release. A candidate is |
|
The series of core the release runs on. |
|
The commit of core the box runs with this layer. The box runs that pair of commits and no other code. |
|
The oldest image ( |
|
|
box/build/check_release.sh pointer checks this file on the build side and
viin_hw_drivers/update.py applies the same rules on the box: a rule changed in one is changed in
the other.
How a box moves to a release¶
viin_hw_drivers/update.py makes a box follow one git ref of the layer repository, its release
channel: the branch box-release/<series>. The box moves to the head of that branch only when a tag
box-<series>-<release> names the same commit and that commit’s pointer names the same release, the
right series, an image the box has, and is not a candidate. Both trees (core and layer) are fetched and
checked before either moves.
The roll-back guard (box/system/usr/local/sbin/viin-box-guard) counts the starts of a new release
until the layer marks it good. At the third start without that mark it puts both trees back and records
the release as bad; a box never tries a bad release again. When the server of the box runs another
series than the box, the box looks at the release channel of the server’s series first, which is how a
box follows its server to a new series in place.
The protocol between a box and its server¶
Every request and every answer below is a contract written on both sides. A key, a type or a reason code changes on both sides or on neither, in the same release of each.
Route |
Box side |
Server side ( |
|---|---|---|
|
|
|
|
core’s device manager ( |
|
|
core’s printer driver ( |
|
|
|
|
|
|
|
|
core’s log sender |
|
|
core’s screen driver |
|
|
core |
Answered with an empty body: the server ships no driver bundle and stores no layout. |
The routes are public: a box has no user. The box proves itself with its credential (token) on
/iot/setup, /iot/reading and /iot/certificate, and with the print_id of the action on
/iot/printer/status; the hardware address, printed on the device, proves nothing. /iot/log finds
the box by its hardware address alone, because core’s log sender sends nothing else: it can only add
log lines, and Accept Device Logs switches them off for one box. /iot/pair and
/iot/certificate never raise: a failure of the server is logged and answered server_error or
provider_error.
Pairing¶
An IoT Box Creator generates a one-time pairing code in Viindoo (viin.iot.box.pair.action_generate):
12 symbols, valid 15 minutes, shown once, of which only a digest is stored. The pairing link carries
the code after #pair=, the part of a link a browser never sends to a server.
The box posts pairing_code, iot_box (name, identifier, ip, version, serial_number),
protocol and, when it confirms its readings, confirms_readings. The server answers
{'status': 'paired', 'token', 'box_name'} or {'status': 'refused', 'reason'} with reason
one of invalid_code, expired, already_used, identifier_exists or server_error. A
refusal changes nothing and leaves the code usable. The token answered is always one generated by that
request, for that box record.
On the box, pair verifies an https server before it sends the code. A server certificate that no
public authority vouches for is accepted only after a person confirmed the fingerprint of that
certificate, or of the root authority they gave the box. The box stores the server and the credential
the way core does, then restarts its service.
The box credential and the settings code¶
The token and the bus channel (iot_channel) of a box are secrets, readable by IoT Administrators
only. Pairing again generates new ones.
The settings code that the box page asks for is derived from the token on both sides and is stored
nowhere: an HMAC-SHA256 of a fixed label keyed by the token, cut to 8 symbols of the pairing-code
alphabet. The server computes it in _viin_iot_settings_code (viin_iot/models/viin_iot_box.py)
and the box in settings_code (viin_hw_drivers/lock.py). Change the label, the alphabet or the
length on both sides or on neither. Once unlocked with the code, the box page gets an unlock proof valid
for 15 minutes.
Registration and presence¶
Core reports the box and its devices on /iot/setup with the token; the server answers the bus
channel of the box, or False for a token it does not know. The box then subscribes to that channel
over its live connection. ir.websocket._subscribe (viin_iot/models/ir_websocket.py) records a
box whose channel is subscribed as connected; closing the socket records its last contact. The cron
IoT: Show boxes that stopped reporting as disconnected (_viin_iot_sweep_presence) shows a box
that holds no live connection and has not reported for longer than the grace period disconnected, with
its devices. The grace period is 120 seconds unless the system parameter
viin_iot.box_presence_grace_seconds says otherwise.
A device type, connection or printer kind the server does not know is stored empty, and the raw value is
posted on the device’s chatter (_viin_iot_note_unrecognised).
Device actions and print status¶
A consumer asks _viin_iot_action(device, payload) on its own record. The platform checks that the
record uses that device, creates one viin.iot.device.dispatch row with a fresh print_id, and
pushes an iot_action message on the box channel. A printer posts print_id,
device_identifier and iot_mac to /iot/printer/status once it printed; the server finds the
dispatch by its print_id and tells the record through _viin_iot_on_result with
origin='completion'. The cron IoT: Report unconfirmed device actions ends a dispatch that got no
answer in time as Never Confirmed and tells the record with origin='timeout': the box cannot
report a failure, so this does not mean that the action failed.
The print_id of an action is what proves its completion, so the field is readable by IoT
Administrators only: a consumer never shows it or logs it.
Readings and their confirmation¶
A reading never travels on the bus. _viin_iot_request_value(device) opens a value request and
sends nothing; its token goes to the browser, which reads the box with it as viin_iot_request and
relays the value it gets back. _viin_iot_on_result with origin='reading' accepts that value only
for that open request, once.
A box that confirms its readings (attests_readings on the box record) also posts the value to
/iot/reading before the browser gets it. The seam on EventManager.device_changed
(seams/attestation.py) does this only for a read that asked for confirmation. From then on the
server accepts a relayed value only when the box confirmed that same value in the same unit, so a value
cannot be invented on the way through the browser.
The Viindoo name¶
A box can get a public name of its own with a certificate, so that browsers reach it without a
certificate warning. The box (public_name.py) posts its token, its local ip and, for a
certificate, a signing request (csr) to /iot/certificate; its key never leaves the box. The
answers (unavailable, name, pending, issued, refused) and their keys are listed in
VIIN_IOT_PUBLIC_NAME_ANSWERS on the server and in ANSWERS on the box.
viin_iot contacts nobody: its hook _viin_iot_public_name_request answers no_provider.
viin_iot_saas overrides that hook and _viin_iot_public_name_release: on a database hosted on
Viindoo Cloud, the database proves itself to Viindoo Cloud, passes the question of the box on and its
answer back, and gives the name up when the box is archived or deleted or its Viindoo Name is
switched off. Elsewhere it changes nothing. The server asks the provider at most once a minute for one box.
Develop and test¶
The software box¶
box/dev/softbox/softbox.sh runs the real box code, layer and core, in a container on a Linux x86
workstation, with a simulated scale and barcode scanner and a print server with a receipt, a label and
an office queue. SOFTBOX_CORE is the path of a git repository of core checked out at the commit to
run. box/dev/softbox/README.md lists every command and parameter; the usual ones are:
$ export SOFTBOX_CORE=/path/to/core
$ box/dev/softbox/softbox.sh build
$ box/dev/softbox/softbox.sh start
$ box/dev/softbox/softbox.sh logs --follow
$ box/dev/softbox/softbox.sh exec sh -c 'echo 1.250 > /var/lib/softbox/scale-weight'
$ box/dev/softbox/softbox.sh exec sh -c 'echo 8934567890123 >> /var/lib/softbox/scanner-scans'
$ box/dev/softbox/softbox.sh restart
$ box/dev/softbox/softbox.sh stop
A simulated device is plugged in for as long as its file exists: delete the file to unplug it and create
it again to plug it back. restart switches the box off and on and keeps what it stored. Every print
job ends in /var/lib/softbox/prints/ inside the container, in a file named after the print queue
(softbox_tm-m30 is the receipt printer). The software box has no Wi-Fi, no screen, no USB devices and
no init system; test those on a real box.
The container has no network unless SOFTBOX_NETWORK names a docker network, and never the host
network. SOFTBOX_PUBLISH publishes a port of the box on 127.0.0.1 only, as
127.0.0.1:<host port>:<box port>.
Warning
Never start the box code directly on a workstation: core restarts the web server and remounts the root file system of the machine it runs on.
The box suite and the seam check¶
$ box/dev/softbox/softbox.sh suite
$ box/dev/softbox/softbox.sh suite test_pairing
$ box/dev/softbox/softbox.sh run python3 /home/pi/iot-hw/viin_hw_drivers/boot.py --config /home/pi/odoo.conf
suite runs python3 -m unittest in viin_hw_drivers/tests with core importable; arguments
select a module, a class or a method, and a run in which no test ran fails. The test files are loaded as
top-level modules (test_pairing), and nothing is imported before them.
The page texts¶
The texts of the box page are in viin_hw_homepage/static/src/app/i18n/en.json and vi.json.
box/build/check_page_texts.py checks that a key takes the same {placeholders} in both files and
that no page text names the vendor of core, its paid edition, its web site or a series number:
$ python3 -I box/build/check_page_texts.py
A key missing from vi.json is shown in English, and the checker does not report it: add every key
to both files.
The server apps¶
Each IoT app ships Python tests (tests/), Hoot unit tests (static/tests/*.test.js) and tours
(static/tests/tours/). The Hoot tests run in the browser at /web/tests, and in a test run
through WebSuite.test_unit_desktop of web. viin_iot puts
static/tests/tours/viin_iot_tour_box_wire.js in the tour bundle: in a tour no box exists, so the
requests addressed to a box are held or answered there instead of leaving the browser.
Test each module alone and with the modules installed beside it:
alone, with
--skip-auto-install, so that no bridge installed automatically makes it work;co-installed, as customers get it: every bridge is installed automatically, so the full set is the default.
$ odoo-bin -d <new database> -i viin_iot_mrp --skip-auto-install --test-enable --stop-after-init
$ odoo-bin -d <new database> -i viin_iot_mrp_workorder_quality,viin_iot_pos --test-enable --stop-after-init
Simulate an Internet outage¶
A shop loses the Internet while its own network stays up: the till and the box still reach each other and
the server is out of reach. To see what the code does then, build that situation on one workstation: a
development database with Point of Sale and the IoT apps (the server), a box paired with it (the software
box, started with SOFTBOX_NETWORK so that it can pair), and a Point of Sale page of a till whose
devices are on that box, opened in a Chromium-based browser. The till calls the box at the address the
box reported to the server, so that address must be reachable from the browser. Open the till first, then
cut the server off in one of two ways. They fail differently and both matter:
The server is |
How to make it so |
|---|---|
Refused |
Stop the server process. Its port is closed and the browser’s connection is refused at once. |
Not answering |
Suspend the server process ( |
Check, with the server cut off and the box still running:
a receipt prints through the box, with no browser print window, and the job ends in
/var/lib/softbox/prints/;a picture the till already holds, such as the company logo, is on the receipt, and a picture it never received (a coupon barcode) is left off. The wait for a picture is
imageFetchTimeoutMsofViinIotPrinter;an order paid meanwhile stays in the browser and reaches the server with the next payment once the server answers again (resume or start the process);
the scale and the scanner of the box still answer (
softbox.sh execwrites their files).
To lose the box instead, run softbox.sh stop while the till is open. The transport of viin_iot judges the box as follows:
a device command to a box it holds lost is answered
unreachableat once, and a fresh check of the box starts;a device command already sent is settled
unreachablewhen the box fails two checks in a row. A check is aGETof/hw_proxy/helloevery 5 seconds, given up after 2.5 seconds; a refused connection counts as a failed check. A box that answers its checks is busy, not lost, and its commands keep waiting;the till shows the notice that its devices are not connected while the box is lost. When the box answers again, the transport knows it within one check period and asks the box for its events again at once.
The console of the browser names each failed box call. The line begins with viin_iot: the box at
and gives the address that was called: scheme, address and port 8069 where the browser supports Local
Network Access, the address with the scheme of the page otherwise.
The seams of the till and their tests¶
The code that decides what a till does when the box or the server is out of reach has one place each, and its tests run without a box and without a server:
Place |
What it holds |
Tests |
|---|---|---|
|
The transport: |
|
|
|
|
|
|
|
The Hoot tests state the outage directly:
setupIotTestEnvofviin_iot/static/tests/helpers/viin_iot_device_test_helpers.jsreplaces the network of the browser. Itsnetwork.helloModeisanswer(the box is up),refuse(the box is off or unplugged: every check fails at once) orsilent(the box takes the connection and never answers: a check ends only when the transport gives it up).failActionandfailPollfail the call or the event request in flight, andnetwork.reports()returns the console lines;viin_iot_printer.test.jsreplacesfetchwithmockFetchso that every request to the server fails, answers with an error or never answers;makeMockDeviceServiceofviin_iot_pos/static/tests/helpers/mock_device_service.jsstands in for the transport and answers each command with the result the test gives it.
The tours of viin_iot_pos/static/tests/tours/viin_iot_pos_tour.js run a till against no box at all.
A tour page opened with viin_iot_intercept=1 in its address has every call to a box answered by the
tour, and viin_iot_box_down=1 makes the box refuse every action. viin_iot_pos/tests/test_pos_iot_tour.py
starts them.
On the box, the seams of service.py and of the device listing have a test file each in
viin_hw_drivers/tests (test_seams_service.py, test_seams_devices.py); the box suite runs them
with the others.
Continuous integration¶
Box software: the workflow
box-checksruns on every pull request and every push to a series branch: release pointer, shell syntax and lint, workflow lint, page texts, the unit of the box service, the box suite in the software box and the seam check against the core commit the pointer names. The series branch requires it before a merge, and the release workflow runs it again on the tagged commit.Server apps: Runbot builds and tests the IoT apps like every other Viindoo app.
Common changes¶
Add a device type or change how a device behaves¶
On the box, a device is driven by handler files: an interface finds it, a driver runs it. Never edit a handler file of core on a box. A handler of Viindoo goes in
viin_hw_drivers/iot_handlers/(interfaces/ordrivers/): the layer loads it right after the handlers of core, and an update of core does not remove it. The layer verifies the calls a handler file makes to another machine through the plain nameurllib3and through no other library, so such a call usesurllib3. To change what a symbol of core does, add a seam.On the server, add the new value to the selection of
viin.iot.device(type,connection_typeorsubtype); until then the device shows an empty value and a chatter note. An app that uses the device inheritsviin.iot.device.consumer, sends commands with_viin_iot_action, reads values with_viin_iot_request_valueand handles the threeoriginvalues in_viin_iot_on_resulton its own model.To test, add a simulated handler under
box/dev/softbox/iot_handlers/when the device needs hardware, and a test of the box page inviin_hw_drivers/device_tests.pywhen the installer should be able to test it there.
Add a text to the box page¶
Add the key with the same placeholders to en.json and vi.json, use it through t(key, params)
of i18n.js, then run check_page_texts.py. Write for an installer, with the words of the user
guides, and name Viindoo products only.
Add a self-test line¶
viin_hw_drivers/selftest.py builds one line per layer the box depends on, in the order of
LINES. A line is _line(id, state, reason, action, **details), with state one of ok,
warning, failed, neutral or pending. Add the id to LINES, a function that computes
the line, its call in _lines, and the texts line.<id>, reason.<id>.<reason> and, when a
person must act, action.<action> in both language files. Add the case to
viin_hw_drivers/tests/test_selftest.py, and add the line with its meaning to the troubleshooting
page of the user guides.
Change the protocol safely¶
Change both sides in one piece of work: the route and its docstring in
viin_iot, the module of the box that calls it, and the tests of both.Old boxes keep running the release they have until they update, so the server must keep answering them. Add keys as optional; never rename or remove a key, a reason code or a status that a released box reads.
The box sends
protocol(PROTOCOLinpairing.pyandpublic_name.py) on/iot/pairand/iot/certificate. The server reads it nowhere today. When a change cannot be made compatible, raisePROTOCOLon the box and answer each value on the server.Never send or log a secret: the pairing code, the token, the channel,
print_id, a signing request or an address of a customer site.
Move to a new series¶
Each series has its own release channel (box-release/<series>), its own pointer and its own core
commit. To bring the layer to a new series:
Re-point every seam to the symbols of the new core, run the seam check until every seam is
ok, and run the box suite. In the next series core merges its box modules intoiot_drivers: the module names inCORE(viin_hw_drivers/seams/__init__.py), inboot.py, inserver_wide_modules, in the routes the home page module closes and in the files the installer and the workflows read from core (service unit, image script, access point script) all change, and some functions moved to other files.Check every route of the server side against what the new core box code sends:
/iot/setup, the log sender, the screen driver and the print status are core’s requests, not Viindoo’s.Set
series,odoo_commitandmin_imageinbox-release.json. Raisemin_imagewhen the new release needs something only a new image has; such boxes are told to write a new image instead of moving.Publish candidates, then a supported release, on the new series branch, and add the new series branch to the deployment refs of the release environments once that branch exists and is protected.
A box whose server moved to the new series looks at the release channel of that series first and moves to it in place when its image is recent enough.
Release the box software¶
A release is published by tags and workflows of the box repository; the README.md of the repository
is the full procedure. In short:
Candidate: push a tag
box-<series>-rc.<n>on the head of the work branch.box-imagebuilds a candidate image and publishes it as a pre-release marked CANDIDATE. A box written from it followsbox-candidate/<series>; no customer box can select that channel.Supported: a reviewed pull request sets
box-release.json(candidatefalse, the newrelease, theodoo_commit) on the series branch; then a release manager pushes the tagbox-<series>-<release>on the merged commit.box-imagechecks the tag (gate), runs the checks, builds the image, thenpublishcreates the release page andadvancemovesbox-release/<series>. Both run in the environmentbox-release, which needs the owner’s approval. Boxes see the release only whenadvanceends.Roll back: run the workflow
box-rollbackfrom the series branch with the tag of an earlier supported release. After approval it withdraws the pages of the releases the channel names now and points the channel back. Boxes apply the earlier release like any other.
A published release or tag is never changed: the fix for a bad release is a new release. Nothing in the box repository, its workflows, its release pages or its logs may hold a credential, an internal host name, a machine path or source of a private repository.