octopus icon indicating copy to clipboard operation
octopus copied to clipboard

refactor(mqtt): enhance property process

Open thxCode opened this issue 4 years ago • 1 comments
trafficstars

Fixes:

https://github.com/cnrancher/octopus/issues/156

Problem:

There are some issues in MQTT adaptor implement:

  1. Enrich the type of property.
  2. The existing access model cannot handle those write-only registers, and some registers only need to be read once.
  3. Support arithmetic operations in collected value.
  4. The existing logic only consider the "string" message, we should support the pure "bytes" message.

Solution:

  1. 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 *
  2. Introduced contentType and endianness.

    ContentType Type Meaning
    bytes string the content is the raw bytes
    text string the content is in string text bytes

    Specify contentType to indicate the type of value, the default value is bytes.

    Endian Type Meaning
    BigEndian string byte order in big-endian
    BigEndianSwap string byte order in swapping big-endian
    LittleEndian string byte order in little-endian
    LittleEndianSwap string byte order in swapping little-endian

    When contentType is bytes, specify endianness to indicate the byte order of (basic) type value, the default value is BigEndian.

  3. Introduced accessModes to specify multiple access modes of a property, the default value is Notify, the following table shows the changed.

    Name Meaning
    ReadOnce the property is only read once.
    WriteOnce the property is only written once.
    WriteMany the property will be written at the synchronization moment.
    Notify the change of property will be subscribed.
  4. Introduced arithmeticOperations to do arithmetic operations.

  5. 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

thxCode avatar Jan 12 '21 01:01 thxCode

@guangbochen , please label this PR with pinned. thanks~

thxCode avatar Jan 12 '21 01:01 thxCode