Skip to content

Commit

Permalink
replace eufy-node-client wiht eufy-security-client
Browse files Browse the repository at this point in the history
Main driver behind this change are issues with Firebase registration - see
matijse#78 for details.

Aside from different name, the main change is how the registration is handled.
Eufy-security-client's new `PushNotificationService` class handles the
registration itself if needed and emits the 'credential' event when it happens.
On our side, we just listen to this event and persist credentials to disk.

In future, we may want to stop listening to raw events and use normalized
messages provided by `PushNotificationService` instead. This would potentially
allow supporting more devices.
  • Loading branch information
skrobul committed Nov 21, 2021
1 parent 1297055 commit e822ab2
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 43 deletions.
3 changes: 0 additions & 3 deletions eufy/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')

Expand Down
17 changes: 9 additions & 8 deletions eufy/http.js
Original file line number Diff line number Diff line change
@@ -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')
Expand All @@ -8,7 +8,7 @@ class EufyHttp {
deviceObjects

constructor (username, password) {
this.httpService = new HttpService(username, password)
this.httpApi = new HTTPApi(username, password)
this.deviceObjects = []
}

Expand All @@ -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()
Expand Down Expand Up @@ -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)
}
}
}
Expand Down
46 changes: 15 additions & 31 deletions eufy/push.js
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
2 changes: 1 addition & 1 deletion mqtt/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down

0 comments on commit e822ab2

Please sign in to comment.