diff --git a/eufy/client.js b/eufy/client.js index 0e9cb57..3f53f49 100644 --- a/eufy/client.js +++ b/eufy/client.js @@ -26,9 +26,6 @@ class EufyClient { await this.mqttClient.setupAutoDiscovery(this.eufyHttpClient.deviceObjects) winston.debug('---- Set up auto discovery') - await this.eufyPush.retrievePushCredentials() - winston.debug('---- Retrieved push credentials') - await this.eufyPush.startPushClient() winston.debug('---- Started push client') diff --git a/eufy/http.js b/eufy/http.js index 2462f5c..6b3a2e1 100644 --- a/eufy/http.js +++ b/eufy/http.js @@ -1,4 +1,4 @@ -const { HttpService } = require('eufy-node-client') +const { HTTPApi } = require('eufy-security-client') const winston = require('winston') const get = require('get-value') const { supportedDevices } = require('../enums/device_type') @@ -8,7 +8,7 @@ class EufyHttp { deviceObjects constructor (username, password) { - this.httpService = new HttpService(username, password) + this.httpApi = new HTTPApi(username, password) this.deviceObjects = [] } @@ -29,9 +29,10 @@ class EufyHttp { winston.debug('Refreshing devices...') try { - this.devices = await this.httpService.listDevices() + await this.httpApi.updateDeviceInfo() + this.devices = Object.values(this.httpApi.getDevices()) } catch (e) { - winston.error(`Error -- httpService.listDevices`, e) + winston.error(`Error -- httpApi.getDevices`, e) this.devices = [] } this.devicesRefreshedAt = new Date().getTime() @@ -70,19 +71,19 @@ class EufyHttp { async registerPushToken (fcmToken) { try { - const response = await this.httpService.registerPushToken(fcmToken); + const response = await this.httpApi.registerPushToken(fcmToken); winston.info(`Registered Push Token`, { response }) } catch (e) { - winston.error(`Error -- httpService.registerPushToken`, e) + winston.error(`Error -- httpApi.registerPushToken`, e) } } async checkPushToken () { try { - const response = await this.httpService.pushTokenCheck() + const response = await this.httpApi.checkPushToken() winston.info(`Checked Push Token`, { response }) } catch (e) { - winston.error(`Error -- httpService.pushTokenCheck`, e) + winston.error(`Error -- httpApi.checkPushToken`, e) } } } diff --git a/eufy/push.js b/eufy/push.js index a5f2436..1a2cadf 100644 --- a/eufy/push.js +++ b/eufy/push.js @@ -1,11 +1,12 @@ const fs = require('fs') const winston = require('winston') -const { PushRegisterService, PushClient, sleep } = require('eufy-node-client') +const { PushNotificationService } = require("eufy-security-client") class EufyPush { CREDENTIALS_FILE = './data/credentials.json' pushCredentials = null logger + pushService = null constructor (mqttClient) { this.mqttClient = mqttClient @@ -20,47 +21,30 @@ class EufyPush { }) } - async retrievePushCredentials() { - if (this.pushCredentials) { - return - } + async startPushClient() { + const pushService = new PushNotificationService(this.logger); if (fs.existsSync(this.CREDENTIALS_FILE)) { winston.info('Credentials found -> reusing them...'); - this.pushCredentials = JSON.parse(fs.readFileSync(this.CREDENTIALS_FILE).toString()); - return + pushService.credentials = JSON.parse(fs.readFileSync(this.CREDENTIALS_FILE).toString()); } - // Register push credentials - winston.info('No credentials found -> register new...'); - const pushService = new PushRegisterService(); - this.pushCredentials = await pushService.createPushCredentials(); - // Store credentials - fs.writeFileSync(this.CREDENTIALS_FILE, JSON.stringify(this.pushCredentials)); - - // We have to wait shortly to give google some time to process the registration - await sleep(5 * 1000); - } + pushService.on('credential', creds => { + fs.writeFileSync(this.CREDENTIALS_FILE, JSON.stringify(creds)) + }) - async startPushClient() { - if (this.pushCredentials === null) { - throw new Error('Retrieve credentials first!') - } + pushService.on('raw message', async (msg) => { + this.logger.info('Received push message', { pushMessage: msg }) + winston.debug(`Received push message`, { pushMessage: msg }); + await this.mqttClient.processPushNotification(msg) + }) - const pushClient = await PushClient.init({ - androidId: this.pushCredentials.checkinResponse.androidId, - securityToken: this.pushCredentials.checkinResponse.securityToken, - }); - pushClient.connect(async (msg) => { - this.logger.info('Received push message', { pushMessage: msg }) - winston.debug(`Received push message`, { pushMessage: msg }); - await this.mqttClient.processPushNotification(msg) - }); + this.pushService = pushService; + this.pushCredentials = await pushService.open() } getFcmToken() { return this.pushCredentials.gcmResponse.token; } } - module.exports = EufyPush diff --git a/mqtt/client.js b/mqtt/client.js index ad976f3..e4e49e0 100644 --- a/mqtt/client.js +++ b/mqtt/client.js @@ -2,7 +2,7 @@ const MQTT = require('async-mqtt') const get = require('get-value') const fetch = require('node-fetch') const winston = require('winston') -const { sleep } = require('eufy-node-client') +const { sleep } = require('eufy-security-client/build/push/utils') const config = require('../config') const { NotificationType, NotificationTypeByString, NotificationTypeByPushType, supportedNotificationTypes, supportedNotificationStrings, supportedNotificationPushTypes } = require('../enums/notification_type')