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.
Configuration
Assign the WorkstationCommons.User module role to every user role of your app that has the StationConnector.User module role assigned. For more information, see Module Security.
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 nanoflow 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 these nanoflows. 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 nanoflow 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 nanoflows 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.
Parsing Messages
The Parse subfolder of each device type contains nanoflows that convert a received message into a non-persistable object. This lets you work with its values instead of the raw message string. They take the message as their Message parameter. The section of each device type below describes its parse nanoflows.
Bluetooth
These nanoflows target a characteristic on a Bluetooth Low Energy (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.
To convert a received message, use the following nanoflow:
BLE_ParseMessage- Converts the Bluetooth message in itsMessageparameter into aBluetoothMessageobject. ItsCharacteristicholds the universally unique identifier (UUID) of the characteristic that sent the message, and itsResponseHexholds the value in hexadecimal.
Camera
These nanoflows control barcode scanning and motion detection on a camera device. The barcode nanoflows 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.
To convert a received message, use the following nanoflows:
Camera_ParseBarcodeMessage- Converts the barcode message in itsMessageparameter into aCameraBarcodeMessageobject. ItsBarcodeCountholds the number of barcodes in the message.IsContinuousis true when the message comes from continuous barcode detection, andIsLeavingFrameis true when the barcodes left the frame instead of entering it. Each barcode is an associatedBarcodeInfoobject with itsFormatand itsContent, decoded from Base64.Camera_ParseMotionMessage- Converts the motion message in itsMessageparameter into aCameraMotionMessageobject.IsMotionDetectedis true when motion started, andMotionScoreholds the share of changed pixels in the frame, between0and1.
Card Reader
CardReader_ParseMessage- Converts the card reader message in itsMessageparameter into aCardReaderMessageobject. ItsMessageTypeisCardConnectedwhen a card is inserted,CardDisconnectedwhen a card is removed, orResponsewhen the card reader replies to an instruction. ItsResponseHexholds the hexadecimal response of the card reader.
File Device
These nanoflows 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.
The Request variants return the response message of the device. To convert it, use the following nanoflow:
-
File_ParseMessage- Converts the file device message in itsMessageparameter into aFileMessageobject. ItsMessageTypeis one of the following:RenameEvent- A watched file or directory was renamed, created, or deleted.ChangeEvent- A watched file or directory was changed.Data- The message contains the content of a file read.Success- A command completed successfully.
Its
Dataholds the rest of the message, such as the path or the file content.
Printer
The following nanoflows 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 nanoflows read the file contents on the server. For more information, see General Utils.
The Request variants of these nanoflows return the accepted job, including its job ID.
The following nanoflows 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 the status message.Printer_RequestCancelJobandPrinter_SendCancelJob- Cancel a queued print job. They have the parameterJobId.
To convert a received message, use the following nanoflows:
Printer_ParsePrintAcceptedMessage- Converts the message of an accepted print job in itsMessageparameter into aPrinterPrintAcceptedMessageobject with theDocumentNameand theJobIdof the job.Printer_ParseStateMessage- Converts the status message in itsMessageparameter into aPrinterStateMessageobject with the printerState, itsStateReasons, and theJobCountof queued jobs. 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 and pass it a device, or open the DeviceLogger popup page with a device as its parameter. The remaining documents in the folder are its implementation.
UI Snippets
The UI_Snippets folder contains reusable snippets to display common information about the station and your devices. All device snippets require a StationConnector.Device object or a specialization as a parameter.
Snippet_StationInfo- Displays workspace, station, and computer name, and links to a page to manage the key pair. For more information, see Authenticating your App with Mendix Studio Pro: Configuring the App. 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.