Skip to content

Commit

Permalink
revert: Implement aws v2
Browse files Browse the repository at this point in the history
  • Loading branch information
bwp91 committed Dec 26, 2023
1 parent 091ef17 commit 8bf82e1
Show file tree
Hide file tree
Showing 4 changed files with 149 additions and 335 deletions.
4 changes: 0 additions & 4 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,6 @@ This project tries to adhere to [Semantic Versioning](http://semver.org/). In pr
- Bump `node` supported versions to v18.19.0 or v20.10.0
- Updated dependencies

### Fixed

- Update to aws-iot-device-sdk-v2 to fix AWS authentication issues (#680) (@Teagan42)

## 10.2.0 (2023-11-30)

### Added
Expand Down
43 changes: 18 additions & 25 deletions lib/connection/aws.js
Original file line number Diff line number Diff line change
@@ -1,46 +1,39 @@
import { readFileSync } from 'fs';
import { EOL } from 'os';
import { resolve } from 'path';
import url from 'url';
import { iot, mqtt } from 'aws-iot-device-sdk-v2';
import { device as iotDevice } from 'aws-iot-device-sdk';
import platformConsts from '../utils/constants.js';
import { parseError } from '../utils/functions.js';
import platformLang from '../utils/lang-en.js';

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

export default class {
constructor(platform, iotFile) {
this.accountTopic = platform.accountTopic;
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);

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,
});

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

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

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

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

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

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

0 comments on commit 8bf82e1

Please sign in to comment.