Workstation Commons
Introduction
The Workstation Commons module contains reusable building blocks for apps that communicate with devices through Mendix Workstation.
Workstation Commons speeds up development. It offers prebuilt nanoflows for the common device operations, a ready-to-use device logger, and UI snippets for the screens that most Workstation apps share.
Prerequisites
Workstation Commons requires the Workstation Connector module and its StationConnector.Device entity. For more information, see Installing the Workstation Connector.
Usage
The majority of functions in this module are nanoflows, because device communication runs in the client. Call them with a nanoflow call from your own nanoflow. Every function that communicates with a device takes a StationConnector.Device object as a parameter, which you retrieve through the Workstation Connector.
Function List
The following sections list the functions available in this connector.
Connection
Device_Connect- Connects to the given device.Device_Disconnect- Disconnects the given device.
The Managed subfolder contains variants of this function. They check the connection state first, so they are safe to call in any state, and they add logging and error handling. Use these in automated flows such as startup or retry logic:
Device_EnsureConnect- Connects to the device when it is not connected yet.Device_EnsureDisconnect- Disconnects the device when it is still connected.
Device Utils
Each Device Utils function covers a single operation on a device type and builds the matching Workstation device message from plain parameters, so you can work with service UUIDs, paths, barcode types, and print jobs directly.
For the message syntax behind these functions and the replies each device type sends, see Device Message Syntax.
Request and SendMessage
Every operation is available in two variants, in the Request and SendMessage subfolders of each device type:
- Request - Sends the message and waits for the response of the device.
- SendMessage - Sends the message without waiting. The response arrives later through the
OnMessagecallback of the device.
Which variant to use depends on the architecture of your app and on how the device behaves.
Use Requests by Default
Use requests whenever possible. A request lets you send a command and handle the response of the device in the same microflow or nanoflow that sent it. The related logic stays in one place, which makes the execution path easier to follow and debug.
Use SendMessage for Subscriptions and Asynchronous Responses
Use the SendMessage variant for device subscriptions, and when the response arrives at a later time. When a device pushes data independently, or when you listen to a subscription, you need an event-driven OnMessage callback to handle the incoming data whenever it arrives.
Use SendMessage for Consistent Message Handling
You can also use the SendMessage variant when your whole app is built around processing device messages in callbacks, to keep the app consistent. The trade-off is that the logic is split across several flows, which can make debugging more complex.
The Parse subfolder of each device type contains functions that convert a received message into a non-persistable object, so you can work with its values instead of the raw message string. For more information, see Parsing Messages.
Bluetooth
These functions target a characteristic on a BLE device. They have the following parameters: ServiceUUID, CharacteristicUUID.
BLE_RequestSubscribeandBLE_SendSubscribe- Subscribe to notifications for a characteristic.BLE_RequestUnsubscribeandBLE_SendUnsubscribe- Stop notifications for a characteristic.BLE_RequestReadandBLE_SendRead- Read the current value of a characteristic.BLE_RequestReadreturns the value.BLE_RequestWriteandBLE_SendWrite- Write a value to a characteristic. They have the additional parameterValue.
Camera
These functions control barcode scanning and motion detection on a camera device. The barcode functions have the optional parameter BarcodeTypes, a comma-separated list of barcode types to scan for. Leave it empty to scan for all barcode types.
Camera_RequestBarcodeandCamera_SendGetBarcode- Scan the next available frame for barcodes.Camera_RequestBarcodereturns the barcodes found.Camera_RequestStartBarcodeDetectionandCamera_SendStartBarcodeDetection- Start continuous barcode detection.Camera_RequestStopBarcodeDetectionandCamera_SendStopBarcodeDetection- Stop continuous barcode detection.Camera_RequestStartMotionDetectionandCamera_SendStartMotionDetection- Start motion detection.Camera_RequestStopMotionDetectionandCamera_SendStopMotionDetection- Stop motion detection.
File Device
These functions operate on a path on the workstation computer. They have the following parameter: Path.
File_RequestWatchandFile_SendWatch- Start watching a file or directory for changes.File_RequestUnwatchandFile_SendUnwatch- Stop watching a file or directory.File_RequestReadandFile_SendRead- Read the content of a file.File_RequestReadreturns the content.File_RequestWriteandFile_SendWrite- Write content to a file. They have the additional parametersValueandFlag. The flag can bewfor overwrite orafor append.
Printer
The following functions print a document. They have the parameter DocumentName, which is the name of the print job, and encode the content in Base64 for you:
Printer_RequestPrintTextandPrinter_SendPrintText- Print plain text. They have the additional parameterText.Printer_RequestPrintRawandPrinter_SendPrintRaw- Print raw data in a printer command language such as ZPL, EPL, or PCL. They have the additional parameterRawData.Printer_RequestPrintPDFandPrinter_SendPrintPDF- Print a PDF document. They have the additional parameterFileDocument. These functions read the file contents on the server. For more information, see General Utils.
The following functions give you full control over the print job and the printer queue:
Printer_RequestPrintandPrinter_SendPrint- Submit a print job. They have the parametersDocumentName,Format, andDataBase64. The format isRAW,TEXT, orPDF, and the payload is encoded in Base64.Printer_RequestPrintreturns the accepted job, including its job ID.Printer_RequestStatusandPrinter_SendGetStatus- Get the printer state and its queued jobs.Printer_RequestStatusreturns them.Printer_RequestCancelJobandPrinter_SendCancelJob- Cancel a queued print job. They have the parameterJobId.
Parsing Messages
The following functions take a received message string as their Message parameter and return a non-persistable object with its values:
BLE_ParseMessage- Returns aBluetoothMessageobject with theCharacteristicand theResponseHexvalue.Camera_ParseBarcodeMessage- Returns aCameraBarcodeMessageobject with theBarcodeCount, whether the detectionIsContinuous, and whether the barcodes areIsLeavingFrame. Each barcode found is an associatedBarcodeInfoobject with itsFormatand its decodedContent.Camera_ParseMotionMessage- Returns aCameraMotionMessageobject withIsMotionDetectedand theMotionScore.File_ParseMessage- Returns aFileMessageobject with theMessageType(RenameEvent,ChangeEvent,Data, orSuccess) and theDataof the message.Printer_ParsePrintAcceptedMessage- Returns aPrinterPrintAcceptedMessageobject with theDocumentNameandJobIdof the accepted job.Printer_ParseStateMessage- Returns aPrinterStateMessageobject with the printerState, itsStateReasons, and theJobCount. Each queued job is an associatedJobInfoobject with itsJobId,JobName, andJobState.
General Utils
These functions convert content to and from Base64, the encoding that device messages use for binary content:
JS_String_Base64Encode- Encodes a string in Base64.JS_String_Base64Decode- Decodes a Base64 string.FileDocument_Base64Encode- Encodes the contents of aSystem.FileDocumentin Base64. This function is a microflow that calls theJA_FileDocument_Base64EncodeJava action, the exception in this module. The contents of a file document are not available in the client, so they must be read on the server. Calling this microflow from a nanoflow therefore adds a round trip to the server.
Device Logger
The device logger is a ready-to-use console that shows the live message traffic of a device and lets you send messages to it manually. Use it to test and debug device communication.
To use it, show Snippet_DeviceConsole on a page, or open the DeviceLogger popup page with a device as its parameter. The remaining documents in the folder are its implementation.
UI Snippets
The following are reusable web snippets for the screens which most Workstation apps need:
Snippet_StationInfo- Displays the current workstation, and reports when the Workstation Client is unavailable. It has no parameter. Use it as a header or status panel.Snippet_DeviceCard- Displays a single device as a card, with its name, device class, state, and connect and disconnect controls.Snippet_DeviceState- Shows the connection state of a device together with its connect and disconnect buttons.Snippet_DeviceConsole- Provides the interactive device logger console.