Implementing an Adapter
  • 14 Oct 2024
  • 1 Minute to read

Implementing an Adapter


Article summary

Implementing Nexmosphere Sensors on a Device

Supporting a new sensor requires a new “adapter“:

  1. Decide on a prefix, e.g. “xprecipitation_“.

  2. Add a new adapter class, e.g. “PrecipitationAdapter“ which subclasses “BaseAdapter“.

  3. Add a new Options-interface, with the same naming-convention.

  4. Use the same constructor-signature as the other adapters, but change the type of Options to your new one.

  5. In the constructor, set this.filterExp to a RegEx which filters out the messages you want from the serial-interface.

  6. Implement dataHandler as a listener, this should call this.onData() with the supplied text-fragment and loop on the array returned from this.popNewMessages(). After the call, the buffer is clamped, so you will never receive the same data more than once.

  7. Add your adapter to the SENSOR_ORDER array in “Nexmosphere.ts“ with the new prefix and class/interface naming.

  8. Call this.setAttribute() to update the Player.

  9. If you need input-attributes for customization, implement onAttribute(attr: Dise.Attribute) => void in your class. Keep in mind that only attributes of the “nexmosphere“ type are filtered to you, and that you will receive all of these, so additional filtering on the name will need to be done.

Hosting More Than One Sensor of the Same Type on a Player

The “prefix” of options and input-attributes (e.g. the LED-commands to the Buttons-adapter) will be expected to be suffixed by an ordinal for all but the first one. For example, with two controllers, connected to different USB-ports:
Content.json:

[
  {
    "port": "COM3",

    "xtB4N6_sensorAddress": 1,
    "xtB4N6_btn1attributeName": "oneshot.Btn1",
    "xtB4N6_btn4attributeName": "oneshot.Btn2",

    "xtB4N6_2_sensorAddress": 2,
    "xtB4N6_2_btn1attributeName": "oneshot.Btn3",

    "xtB4N6_3_port": "COM4",
    "xtB4N6_3_sensorAddress": 1,
    "xtB4N6_3_btn1attributeName": "oneshot.Btn4",

    "xtB4N6_4_port": "COM4",
    "xtB4N6_4_sensorAddress": 2,
    "xtB4N6_4_btn2attributeName": "oneshot.Btn5",
  }
]

“port” is mandatory.

The handling of input-attributes expect the name to be prefixed by the same ordinal, so “button 1“ in this case, will listen to the “nexmosphere.BtnLEDs“ while “button 4“ will listen for “nexmosphere.4_BtnLEDs“.

“template.json“ will have to match this setup.


Was this article helpful?

Changing your password will log you out immediately. Use the new password to log back in.
First name must have atleast 2 characters. Numbers and special characters are not allowed.
Last name must have atleast 1 characters. Numbers and special characters are not allowed.
Enter a valid email
Enter a valid password
Your profile has been successfully updated.