Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added: Adapter and CA4 model support #35

Merged
merged 1 commit into from
Jul 27, 2020
Merged

Added: Adapter and CA4 model support #35

merged 1 commit into from
Jul 27, 2020

Conversation

ileodo
Copy link
Contributor

@ileodo ileodo commented Jul 25, 2020

Description

CA4 model uses miotspec protocol, the schema can be found http://miot-spec.org/miot-spec-v2/instance?type=urn:miot-spec-v2:device:humidifier:0000A00E:zhimi-ca4:2

miotspec communication can't fit in the current code structure easily, so as part of this PR, I introduced two components which can be potentially a alternative code structure.

The idea is have a base Humidifier class devices/MiHumidifierAdapter.js which encapsulate the common Services and Characteristics, then have different models' implementation inherited from it (devices/MiHumidifierCA4.js for CA4).

Inside the implementation code page, the characteristics are defined in a data driven fashion. here is an example.

{
   // id of the characteristic
   id     : 'CurrentHumidifierDehumidifierState',
   // owner service
   service: this.humidifierService,
   // characteristic's name
   type   : Characteristic.CurrentHumidifierDehumidifierState,
   // props of the characteristic, can be null
   props  : { validValues: [0, 2] },
   // define the action of getting characteristic
   get    : {
     // call name of the device call
     call_name        : 'get_properties',
     // a functor generate the payload in the device call
     call_args        : function (_this) {
       return [{ did: _this.device.id, siid: 2, piid: 1, value: null }]
     },
     // a functor to postprocess the response from the device
     response_callback: function (_this, result, callback) {
       callback(null, result[0].value
         ? Characteristic.CurrentHumidifierDehumidifierState.HUMIDIFYING
         : Characteristic.CurrentHumidifierDehumidifierState.INACTIVE)
     },
   },
 },

devices/MiHumidifierAdapter.js has a generic function registerCharacteristic to process the above data driven config.

devices/MiHumidifierFactory.js provided a factory function to instantiate the MiHumidifier based on the model in the config. If the model is not supported by the Factory, a null will be returned. Hence, there is no regression issue for all existing models.

Test

All characteristics have been tested with my CA4 device.

Note

Javascript is not my specialty language, please point me out if there is any js specific mistake I made.

Related issues:

#26

@ileodo
Copy link
Contributor Author

ileodo commented Jul 25, 2020

#16

@@ -33,6 +33,10 @@
"title": "ca1",
"enum": ["ca1"]
},
{
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

newly supported model

if (!config.ip) throw new Error('Your must provide IP address of the Humidifier');
if (!config.token) throw new Error('Your must provide token of the Humidifier');

let options = { ...defaults, ...config };

this.humidifier = MiHumidifierFactory.create(log, config, api);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MiHumidifierFactory will return null if the config.model is not supported by the Adapters. so there is no impact on the existing supported models.

@ileodo ileodo force-pushed the ca4 branch 3 times, most recently from c72dfff to a66e4a4 Compare July 25, 2020 15:51
@nt0xa
Copy link
Owner

nt0xa commented Jul 27, 2020

Hey @ileodo, thanks for the contribution! Looks good to me

@nt0xa nt0xa merged commit 99ea3d9 into nt0xa:master Jul 27, 2020
@nt0xa nt0xa added the enhancement New feature or request label Jul 27, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants