octopus
octopus copied to clipboard
refactor(mqtt): enhance property process
Fixes:
https://github.com/cnrancher/octopus/issues/156
Problem:
There are some issues in MQTT adaptor implement:
- Enrich the type of property.
- The existing access model cannot handle those write-only registers, and some registers only need to be read once.
- Support arithmetic operations in collected value.
- The existing logic only consider the "string" message, we should support the pure "bytes" message.
Solution:
-
Adjusted
type, the following type table shows the detail.Name Type Bits int8(basic) integer 8 int16(basic) integer 16 int,int32(basic) integer 32 int64(basic) integer 64 uint8(basic) integer 8 uint16(basic) unsigned integer 16 uint,uint32(basic) unsigned integer 32 uint64(basic) unsigned integer 64 float,float32(basic) float 32 double,float64(basic) float 64 boolean(basic) bool 1 string(basic) string * hexString(bytes) hex in string, like CD01* binaryString(bytes) binary in string, like 00000001* base64String(bytes) base64-encoded in string * -
Introduced
contentTypeandendianness.ContentType Type Meaning bytesstring the content is the raw bytes textstring the content is in string text bytes Specify
contentTypeto indicate the type of value, the default value isbytes.Endian Type Meaning BigEndianstring byte order in big-endian BigEndianSwapstring byte order in swapping big-endian LittleEndianstring byte order in little-endian LittleEndianSwapstring byte order in swapping little-endian When
contentTypeisbytes, specifyendiannessto indicate the byte order of (basic) type value, the default value isBigEndian. -
Introduced
accessModesto specify multiple access modes of a property, the default value isNotify, the following table shows the changed.Name Meaning ReadOncethe property is only read once. WriteOncethe property is only written once. WriteManythe property will be written at the synchronization moment. Notifythe change of property will be subscribed. -
Introduced
arithmeticOperationsto do arithmetic operations. -
Related PR: https://github.com/cnrancher/octopus-simulator/pull/7
Change Diffs:
spec:
adaptor:
node: edge-worker
name: adaptors.edge.cattle.io/mqtt
model:
apiVersion: "devices.edge.cattle.io/v1alpha1"
kind: "MQTTDevice"
template:
spec:
protocol:
+ syncInterval: 10s
properties:
- name: gear
- path: "action.gear"
- operator:
- write: set
- read: status
type: int
- readOnly: false
+ accessModes:
+ - Write
+ - Notify
+ visitor:
+ path: "action.gear"
+ operator:
+ write: set
+ read: status
+ contentType: text
+ endianness: BigEndian
+ arithmeticOperations:
+ - type: Multiply
+ value: "0.03125"
Test plan:
None
@guangbochen , please label this PR with pinned. thanks~