introduce
Finite state machine (English: finite-state machine, abbreviation: FSM ), also known as finite state automaton (English: finite-state automaton, abbreviation: FSA ), referred to as state machine , is a finite number of states and between these states Mathematical computational models of behaviors such as transfer and action. View Baidu Encyclopedia
FSM consists of some states and transitions, each state represents a potential state of the system, and each transition represents a transition from one state to another. The running process of FSM is to trigger transition by receiving input. When FSM receives an input, it will transfer from the current state to the next state.
In engineering, FSM can be used to describe the control system of a machine. For example, a machine's control system may have multiple states, such as "running", "paused", "stopped", etc. Each state has different inputs and outputs and has different transition rules. For example, when a machine is in the "Run" state, receiving an input of "Pause" may cause the machine to transition to the "Pause" state, while receiving an input of "Stop" may stop the machine from running.
design
Implement the simplest FSM model in MQTT DeviceShifu . The initial state of the device is idle. After making MQTT DeviceShifu send a control command ( controlMsg
) to the device, the device shifts from the idle state to the busy state (when the device is in the busy state, it prevents receiving other control commands); when the device completes the control command, the device will return to the MQTT Broker After completing the message, the device transfers from the busy state to the idle state.
Function
Run the following command in your cluster to send moving_the_device
control commands to the device (you can choose to publish to different topics through different APIs, here select get_topicmsg1 corresponding to topic1), and the device enters the busy state:
sudo kubectl exec -it nginx -- curl -X POST -d 'moving_the_device' http://deviceshifu-mqtt.deviceshifu.svc.cluster.local/get_topicmsg1
When the device is busy, it will refuse to receive new control commands (including rejecting control commands or ordinary messages from other topics, but you can still get device status and other messages through these topics).
Send new control commands while busy rotating_the_device
:
sudo kubectl exec -it nginx -- curl -X POST -d 'rotating_the_device' http://deviceshifu-mqtt.deviceshifu.svc.cluster.local/get_topicmsg1
MQTT deviceShifu
returns as follows:
Device is blocked by moving_the_device controlMsg now! 2023-01-02 07:14:28.324501338 +0000 UTC m=+67770.982000572
After the device completes the control command, it will send a response to the completion of the control command to the MQTT Broker and return to the idle state.
When the mosquitto simulation device is completed moving_the_device
, the corresponding completion information is returned device_finished_moving
:
sudo kubectl exec -it deploy/mosquitto -n devices -- mosquitto_pub -h localhost -d -p 1883 -t /test/test1 -m "device_finish_moving"
At this point, the device has returned to the idle state and can receive other control commands.
use
Customize the device control commands controlMsg
and their corresponding completion responses as required, and examples/mqttDeviceShifu/mqtt_deploy/mqtt_edgedevice.yaml
configure them in the file.
...
data:
...
controlMsgs: |
# 可选,配置controlMsgs(控制命令和相应完成响应的集合),格式为"key: value"。key是通过任意topic向设备发送的控制命令,设备执行控制命令时进入忙碌状态,拒绝接收其它控制命令,value是设备返回的完成响应,表示设备已完成对应控制命令,恢复空闲状态
Moving_the_device: "Device_finished_moving" # 修改此行
Rotating_the_device: "Device_finished_Rotating"
... # 根据自己的需要可继续配置controlMsgs及对应完成响应,只需按照此格式继续添加即可
This article is published by Boundless Authorization