66* Date: 2019-11-18
77* ---------------------------------------------------------------- */
88'use strict' ;
9- import { check , error as _error } from './parameter-checker.js' ;
10- import { parse } from './switchbot-advertising.js' ;
9+ const parameterChecker = require ( './parameter-checker.js' ) ;
10+ const switchbotAdvertising = require ( './switchbot-advertising.js' ) ;
1111
12- import SwitchbotDevice from './switchbot-device.js' ;
13- import SwitchbotDeviceWoHand from './switchbot-device-wohand.js' ;
14- import SwitchbotDeviceWoSensorTH from './switchbot-device-wosensorth.js' ;
15- import SwitchbotDeviceWoCurtain from './switchbot-device-wocurtain.js' ;
12+ const SwitchbotDevice = require ( './switchbot-device.js' ) ;
13+ const SwitchbotDeviceWoHand = require ( './switchbot-device-wohand.js' ) ;
14+ const SwitchbotDeviceWoCurtain = require ( './switchbot-device-wocurtain.js' ) ;
15+ const SwitchbotDeviceWoSensorMS = require ( './switchbot-device-wosensorms.js' ) ;
16+ const SwitchbotDeviceWoSensorCS = require ( './switchbot-device-wosensorcs.js' ) ;
17+ const SwitchbotDeviceWoSensorTH = require ( './switchbot-device-wosensorth.js' ) ;
1618
1719class Switchbot {
1820 /* ------------------------------------------------------------------
@@ -40,7 +42,6 @@ class Switchbot {
4042 this . onadvertisement = null ;
4143
4244 // Private properties
43- this . _initialized = false ;
4445 this . _scanning = false ;
4546 this . _DEFAULT_DISCOVERY_DURATION = 5000
4647 this . _PRIMARY_SERVICE_UUID_LIST = [ 'cba20d00224d11e69fb80002a5d5c51b' ] ;
@@ -55,11 +56,12 @@ class Switchbot {
5556 * - duration | Integer | Optional | Duration for discovery process (msec).
5657 * | | | The value must be in the range of 1 to 60000.
5758 * | | | The default value is 5000 (msec).
58- * - model | String | Optional | "H", "T" or "c".
59- * | | | If "H" is specified, this method will discover
60- * | | | only Bots. If "T" is specified, this method
61- * | | | will discover only Meters. If "c" is specified,
62- * | | | this method will discover only Curtains.
59+ * - model | String | Optional | "H", "T", "M", "CS", or "c".
60+ * | | | If "H" is specified, this method will discover only Bots.
61+ * | | | If "T" is specified, this method will discover only Meters.
62+ * | | | If "M" is specified, this method will discover only Motion Sensors.
63+ * | | | If "CS" is specified, this method will discover only Contact Sensors.
64+ * | | | If "C" is specified, this method will discover only Curtains.
6365 * - id | String | Optional | If this value is set, this method willl discover
6466 * | | | only a device whose ID is as same as this value.
6567 * | | | The ID is identical to the MAC address.
@@ -79,15 +81,15 @@ class Switchbot {
7981 discover ( params = { } ) {
8082 let promise = new Promise ( ( resolve , reject ) => {
8183 // Check the parameters
82- let valid = check ( params , {
84+ let valid = parameterChecker . check ( params , {
8385 duration : { required : false , type : 'integer' , min : 1 , max : 60000 } ,
84- model : { required : false , type : 'string' , enum : [ 'H' , 'T' , 'c' ] } ,
86+ model : { required : false , type : 'string' , enum : [ 'H' , 'T' , 'M' , 'CS' , ' c'] } ,
8587 id : { required : false , type : 'string' , min : 12 , max : 17 } ,
8688 quick : { required : false , type : 'boolean' }
8789 } , false ) ;
8890
8991 if ( ! valid ) {
90- reject ( new Error ( _error . message ) ) ;
92+ reject ( new Error ( parameterChecker . error . message ) ) ;
9193 return ;
9294 }
9395
@@ -181,17 +183,27 @@ class Switchbot {
181183 }
182184
183185 _getDeviceObject ( peripheral , id , model ) {
184- let ad = parse ( peripheral ) ;
186+ let ad = switchbotAdvertising . parse ( peripheral ) ;
185187 if ( this . _filterAdvertising ( ad , id , model ) ) {
186188 let device = null ;
187- if ( ad . serviceData . model === 'H' ) {
188- device = new SwitchbotDeviceWoHand ( peripheral ) ;
189- } else if ( ad . serviceData . model === 'T' ) {
190- device = new SwitchbotDeviceWoSensorTH ( peripheral ) ;
191- } else if ( ad . serviceData . model === 'c' ) {
192- device = new SwitchbotDeviceWoCurtain ( peripheral ) ;
193- } else {
194- device = new SwitchbotDevice ( peripheral ) ;
189+ switch ( ad . serviceData . model ) {
190+ case 'H' :
191+ device = new SwitchbotDeviceWoHand ( peripheral , this . noble ) ;
192+ break ;
193+ case 'T' :
194+ device = new SwitchbotDeviceWoSensorTH ( peripheral , this . noble ) ;
195+ break ;
196+ case 'M' :
197+ device = new SwitchbotDeviceWoSensorMS ( peripheral , this . noble ) ;
198+ break ;
199+ case 'CS' :
200+ device = new SwitchbotDeviceWoSensorCS ( peripheral , this . noble ) ;
201+ break ;
202+ case 'c' :
203+ device = new SwitchbotDeviceWoCurtain ( peripheral , this . noble ) ;
204+ break ;
205+ default : // 'resetting', 'unknown'
206+ device = new SwitchbotDevice ( peripheral , this . noble ) ;
195207 }
196208 return device ;
197209 } else {
@@ -222,15 +234,21 @@ class Switchbot {
222234 * - Start to monitor advertising packets coming from switchbot devices
223235 *
224236 * [Arguments]
225- * - params | Object | Optional |
226- * - model | String | Optional | "H", or "T" or "c ".
237+ * - params | Object | Optional |
238+ * - model | String | Optional | "H", "T", "M", "CS", or "C ".
227239 * | | | If "H" is specified, the `onadvertisement`
228240 * | | | event hander will be called only when advertising
229241 * | | | packets comes from Bots.
230242 * | | | If "T" is specified, the `onadvertisement`
231243 * | | | event hander will be called only when advertising
232244 * | | | packets comes from Meters.
233- * | | | If "c" is specified, the `onadvertisement`
245+ * | | | If "M" is specified, the `onadvertisement`
246+ * | | | event hander will be called only when advertising
247+ * | | | packets comes from Motion Sensor.
248+ * | | | If "CS" is specified, the `onadvertisement`
249+ * | | | event hander will be called only when advertising
250+ * | | | packets comes from Contact Sensor.
251+ * | | | If "C" is specified, the `onadvertisement`
234252 * | | | event hander will be called only when advertising
235253 * | | | packets comes from Curtains.
236254 * - id | String | Optional | If this value is set, the `onadvertisement`
@@ -248,13 +266,13 @@ class Switchbot {
248266 startScan ( params ) {
249267 let promise = new Promise ( ( resolve , reject ) => {
250268 // Check the parameters
251- let valid = check ( params , {
252- model : { required : false , type : 'string' , enum : [ 'H' , 'T' , 'c' ] } ,
269+ let valid = parameterChecker . check ( params , {
270+ model : { required : false , type : 'string' , enum : [ 'H' , 'T' , 'M' , 'CS' , ' c'] } ,
253271 id : { required : false , type : 'string' , min : 12 , max : 17 } ,
254272 } , false ) ;
255273
256274 if ( ! valid ) {
257- reject ( new Error ( _error . message ) ) ;
275+ reject ( new Error ( parameterChecker . error . message ) ) ;
258276 return ;
259277 }
260278
@@ -273,7 +291,7 @@ class Switchbot {
273291
274292 // Set an handler for the 'discover' event
275293 this . noble . on ( 'discover' , ( peripheral ) => {
276- let ad = parse ( peripheral ) ;
294+ let ad = switchbotAdvertising . parse ( peripheral ) ;
277295 if ( this . _filterAdvertising ( ad , p . id , p . model ) ) {
278296 if ( this . onadvertisement && typeof ( this . onadvertisement ) === 'function' ) {
279297 this . onadvertisement ( ad ) ;
@@ -325,12 +343,12 @@ class Switchbot {
325343 wait ( msec ) {
326344 return new Promise ( ( resolve , reject ) => {
327345 // Check the parameters
328- let valid = check ( { msec : msec } , {
346+ let valid = parameterChecker . check ( { msec : msec } , {
329347 msec : { required : true , type : 'integer' , min : 0 }
330348 } ) ;
331349
332350 if ( ! valid ) {
333- reject ( new Error ( _error . message ) ) ;
351+ reject ( new Error ( parameterChecker . error . message ) ) ;
334352 return ;
335353 }
336354 // Set a timer
@@ -340,4 +358,4 @@ class Switchbot {
340358
341359}
342360
343- export default Switchbot ;
361+ module . exports = Switchbot ;
0 commit comments