Skip to content

Commit

Permalink
Fixed MQTT connection issue (error: 5)
Browse files Browse the repository at this point in the history
  • Loading branch information
bropat committed Feb 5, 2022
1 parent e502944 commit 673b580
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 6 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ Please use GitHub issues for this.

## Changelog

### 1.6.2 (2022-02-05)

* (bropat) Fixed MQTT connection issue (error: 5)

### 1.6.1 (2022-02-05)

* (bropat) Fixed small issue on driver upgrade of persistence data (has no impact; error entry in log on first startup)
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "eufy-security-client",
"version": "1.6.1",
"version": "1.6.2",
"description": "Client to comunicate with Eufy-Security devices",
"author": {
"name": "bropat",
Expand Down
6 changes: 5 additions & 1 deletion src/eufysecurity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -622,7 +622,7 @@ export class EufySecurity extends TypedEmitter<EufySecurityEvents> {

const loginData = this.api.getPersistentData();
if (loginData) {
this.mqttService.connect(loginData.user_id, this.persistentData.openudid, this.persistentData.api_base);
this.mqttService.connect(loginData.user_id, this.persistentData.openudid, this.persistentData.api_base, loginData.email);
} else {
this.log.warn("No login data recevied to initialize MQTT connection...");
}
Expand Down Expand Up @@ -761,6 +761,10 @@ export class EufySecurity extends TypedEmitter<EufySecurityEvents> {
return this.pushService.isConnected();
}

public isMQTTConnected(): boolean {
return this.mqttService.isConnected();
}

public isConnected(): boolean {
return this.connected;
}
Expand Down
7 changes: 5 additions & 2 deletions src/mqtt/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export class MQTTService extends TypedEmitter<MQTTServiceEvents> {
}
}

public connect(clientID: string, androidID: string, apiBase: string): void {
public connect(clientID: string, androidID: string, apiBase: string, email: string): void {
if (!this.connected && !this.connecting) {
this.connecting = true;
this.client = mqtt.connect(this.getMQTTBrokerUrl(apiBase), {
Expand All @@ -77,7 +77,7 @@ export class MQTTService extends TypedEmitter<MQTTServiceEvents> {
resubscribe: true,
port: 8789,
username: this.USERNAME_FORMAT.replace("<user_id>", clientID),
password: "[email protected]",
password: email,
ca: fse.readFileSync(path.join(__dirname, "./mqtt-eufy.crt")),
clientId: this.CLIENT_ID_FORMAT.replace("<user_id>", clientID).replace("<android_id>", androidID)
});
Expand All @@ -101,6 +101,8 @@ export class MQTTService extends TypedEmitter<MQTTServiceEvents> {
this.client.on("error", (error) => {
this.connecting = false;
this.log.error("MQTT Error", error);
if ((error as any).code === 5)
this.client?.end();
});
this.client.on("message", (topic, message, _packet) => {
if (topic.includes("smart_lock")) {
Expand Down Expand Up @@ -143,6 +145,7 @@ export class MQTTService extends TypedEmitter<MQTTServiceEvents> {
if (this.connected) {
this.client?.end(true);
this.connected = false;
this.connecting = false;
}
}

Expand Down

0 comments on commit 673b580

Please sign in to comment.