Skip to content

Commit

Permalink
Implement aws v2 (#680)
Browse files Browse the repository at this point in the history
Thanks very much!
  • Loading branch information
Teagan42 authored Dec 26, 2023
1 parent 580b4ac commit 9738d82
Show file tree
Hide file tree
Showing 3 changed files with 277 additions and 129 deletions.
43 changes: 25 additions & 18 deletions lib/connection/aws.js
Original file line number Diff line number Diff line change
@@ -1,39 +1,46 @@
import { resolve } from 'path';
import { readFileSync } from 'fs';
import { EOL } from 'os';
import url from 'url';
import { device as iotDevice } from 'aws-iot-device-sdk';
import { iot, mqtt } from 'aws-iot-device-sdk-v2';
import platformConsts from '../utils/constants.js';
import { parseError } from '../utils/functions.js';
import platformLang from '../utils/lang-en.js';

const dirname = url.fileURLToPath(new URL('.', import.meta.url));
const mqttClient = new mqtt.MqttClient();
let mqttConfig;

export default class {
constructor(platform, iotFile) {
this.accountTopic = platform.accountTopic;

this.device = iotDevice({
privateKey: Buffer.from(iotFile.key, 'utf8'),
clientCert: Buffer.from(iotFile.cert, 'utf8'),
caPath: resolve(dirname, './cert/AmazonRootCA1.pem'),
clientId: `AP/${platform.accountId}/${platform.clientId}`,
host: platform.iotEndpoint,
enableMetrics: false,
});
if (!mqttConfig) {
const certWithCA = [
Buffer.from(iotFile.cert, 'utf8'),
readFileSync('/cert/AmazonRootCA1.pem', 'utf-8'),
].join(EOL);
mqttConfig = iot.AwsIotMqttConnectionConfigBuilder.new_mtls_builder(
certWithCA,
iotFile.key,
).with_client_id(`AP/${platform.accountId}/a${platform.clientId}`)
.with_endpoint(platform.iotEndpoint)
.with_clean_session(false)
.build;
}
this.device = mqttClient.new_connection(mqttConfig);

// A listener event for if the connection closes
this.device.on('close', () => {
this.device.on('closed', () => {
platform.log.debugWarn('[AWS] %s.', platformLang.awsEventClose);
this.connected = false;
});

// A listener event for if the connection reconnects
this.device.on('reconnect', () => {
this.device.on('resume', () => {
platform.log.debug('[AWS] %s.', platformLang.awsEventReconnect);
this.connected = true;
});

// A listener event for if the connection goes offline
this.device.on('offline', () => {
this.device.on('interrupt', () => {
platform.log.debugWarn('[AWS] %s.', platformLang.awsEventOffline);
this.connected = false;
});
Expand Down Expand Up @@ -96,7 +103,7 @@ export default class {

async connect() {
return new Promise((res, rej) => {
this.device.subscribe(this.accountTopic, {}, (err) => {
this.device.subscribe(this.accountTopic, mqtt.QoS.AtLeastOnce, (err) => {
if (err) {
rej(err);
} else {
Expand Down Expand Up @@ -130,7 +137,7 @@ export default class {

// Send the update over AWS
return new Promise((res, rej) => {
this.device.publish(accessory.context.awsTopic, JSON.stringify(payload), {}, (err) => {
this.device.publish(accessory.context.awsTopic, JSON.stringify(payload), mqtt.QoS.AtLeastOnce, (err) => {
if (err) {
rej(err);
} else {
Expand Down Expand Up @@ -165,7 +172,7 @@ export default class {

// Send the update over AWS
return new Promise((res, rej) => {
this.device.publish(accessory.context.awsTopic, JSON.stringify(payload), {}, (err) => {
this.device.publish(accessory.context.awsTopic, JSON.stringify(payload), mqtt.QoS.AtLeastOnce, (err) => {
if (err) {
rej(err);
} else {
Expand Down
Loading

0 comments on commit 9738d82

Please sign in to comment.