22
33const debug = require ( 'debug' ) ;
44
5- const UUID_SERVICE_XIAOMI = 'fe95' ;
6- const UUID_SERVICE_DATA = '0000120400001000800000805f9b34fb' ;
7- const UUID_CHARACTERISTIC_MODE = '00001a0000001000800000805f9b34fb' ;
8- const UUID_CHARACTERISTIC_DATA = '00001a0100001000800000805f9b34fb' ;
9- const UUID_CHARACTERISTIC_FIRMWARE = '00001a0200001000800000805f9b34fb' ;
10-
11- const MODE_BUFFER_SERIAL = Buffer . from ( 'b0ff' , 'hex' ) ;
12- const MODE_BUFFER_REALTIME = {
13- Enable : Buffer . from ( 'a01f' , 'hex' ) ,
14- Disable : Buffer . from ( 'c01f' , 'hex' )
15- } ;
16-
175const timeout = ( timeout , promiseFuncs ) => {
186 const promises = [ new Promise ( promiseFuncs ) ] ;
197 if ( timeout > 0 ) {
@@ -29,6 +17,20 @@ const timeout = (timeout, promiseFuncs) => {
2917 return Promise . race ( promises ) ;
3018} ;
3119
20+ const UUID_SERVICE_XIAOMI = 'fe95' ;
21+ const UUID_SERVICE_DATA = '0000120400001000800000805f9b34fb' ;
22+ const UUID_CHARACTERISTIC_MODE = '00001a0000001000800000805f9b34fb' ;
23+ const UUID_CHARACTERISTIC_DATA = '00001a0100001000800000805f9b34fb' ;
24+ const UUID_CHARACTERISTIC_FIRMWARE = '00001a0200001000800000805f9b34fb' ;
25+
26+ const MODE_BUFFER_SERIAL = Buffer . from ( 'b0ff' , 'hex' ) ;
27+ const MODE_BUFFER_BLINK = Buffer . from ( 'fdff' , 'hex' ) ;
28+ const MODE_BUFFER_RESET = Buffer . from ( 'd0aa' , 'hex' ) ;
29+ const MODE_BUFFER_REALTIME = {
30+ Enable : Buffer . from ( 'a01f' , 'hex' ) ,
31+ Disable : Buffer . from ( 'c01f' , 'hex' )
32+ } ;
33+
3234/**
3335 * Represents a Mi Flora device
3436 * @public
@@ -80,11 +82,13 @@ class MiFloraDevice {
8082 connect ( ) {
8183 return timeout ( 10000 , ( resolve , reject ) => {
8284 if ( this . _peripheral . state === 'connected' ) {
85+ this . logDebug ( 'already connected' ) ;
8386 return resolve ( ) ;
8487 }
8588
8689 this . _peripheral . once ( 'connect' , async ( ) => {
8790 try {
91+ this . logDebug ( 'connected, resolving chars' ) ;
8892 await this . _resolveCharacteristics ( ) ;
8993 return resolve ( ) ;
9094 } catch ( error ) {
@@ -192,6 +196,32 @@ class MiFloraDevice {
192196 } ) ;
193197 }
194198
199+ blink ( ) {
200+ return timeout ( 10000 , async ( resolve , reject ) => {
201+ this . logDebug ( 'requesting device to blink' ) ;
202+ try {
203+ await this . connect ( ) ;
204+ this . _setDeviceMode ( MODE_BUFFER_BLINK ) ;
205+ return resolve ( ) ;
206+ } catch ( error ) {
207+ return reject ( ) ;
208+ }
209+ } ) ;
210+ }
211+
212+ reset ( ) {
213+ return timeout ( 10000 , async ( resolve , reject ) => {
214+ this . logDebug ( 'requesting device to forget setting' ) ;
215+ try {
216+ await this . connect ( ) ;
217+ this . _setDeviceMode ( MODE_BUFFER_RESET ) ;
218+ return resolve ( ) ;
219+ } catch ( error ) {
220+ return reject ( ) ;
221+ }
222+ } ) ;
223+ }
224+
195225 /**
196226 * @private
197227 * @param {ByteBuffer } buffer Bytes to write
@@ -230,7 +260,7 @@ class MiFloraDevice {
230260 }
231261
232262 _resolveCharacteristics ( ) {
233- return timeout ( 10004 , async ( resolve , reject ) => {
263+ return timeout ( 10000 , async ( resolve , reject ) => {
234264 try {
235265 this . logDebug ( 'resolving characteristic' ) ;
236266 this . _peripheral . discoverAllServicesAndCharacteristics ( ( error , services , characteristics ) => {
@@ -303,9 +333,9 @@ class MiFloraDevice {
303333 if ( dataItem ) {
304334 const productId = dataItem . data . readUInt16LE ( 2 ) ;
305335 switch ( productId ) {
306- case 152 :
336+ case 0x98 :
307337 return new MiFloraDevice ( peripheral , 'MiFloraMonitor' ) ;
308- case 349 :
338+ case 0x015D :
309339 return new MiFloraDevice ( peripheral , 'MiFloraPot' ) ;
310340 default :
311341 }
0 commit comments