Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import {device as iotDevice, DeviceOptions} from 'aws-iot-device-sdk'
- import {renameSync, writeFileSync} from 'fs'
- const deviceModel: DeviceOptions = {
- host: 'a25wywctgw4byi-ats.iot.us-east-1.amazonaws.com',
- keyPath: './keys/device-private.pem.key',
- certPath: './keys/device-certificate.pem.crt',
- caPath: './keys/AmazonRootCA1.pem',
- protocol: 'mqtts',
- }
- const device = new iotDevice(deviceModel);
- const provisionTopic = `$aws/provisioning-templates/demo_template/provision/json`
- const provisionAcceptedTopic = `$aws/provisioning-templates/demo_template/provision/json/accepted`
- const generateCertificateTopic = `$aws/certificates/create/json`
- const generateCertificateAcceptedTopic = `$aws/certificates/create/json/accepted`
- const getPermanentCertificate = () => {
- console.log(`STEP - GETTING PERMANENT CERTIFICATE`)
- console.log(`---------------------------------------------------------------------------------`)
- return device.publish(generateCertificateTopic, "")
- }
- const registerThing = (ownershipToken: string) => {
- const data: any = {
- certificateOwnershipToken: ownershipToken,
- parameters: {
- // Set the serial number, which will be used to create the thing name
- // This could be the IMEI Number of the device
- SerialNumber: "007"
- }
- }
- console.log(`STEP - SENDING REGISTER THING`, ownershipToken)
- console.log(`---------------------------------------------------------------------------------`)
- return device.publish(provisionTopic, JSON.stringify(data))
- }
- // We connect our client to AWS IoT core.
- device
- .on('connect', function () {
- console.log('STEP - Connecting to AWS IoT Core');
- console.log(`---------------------------------------------------------------------------------`)
- // Subscribe to relevant MQTT Topics
- device.subscribe(provisionAcceptedTopic)
- device.subscribe(generateCertificateAcceptedTopic)
- // Initiate getting the device-specific certificate
- getPermanentCertificate()
- });
- // Listen for messages from MQTT
- device
- .on('message', (topic, payload) => {
- const jsonPayload = JSON.parse(payload.toString())
- console.log(jsonPayload)
- switch(topic) {
- // Once the device specific certificate has been created, save it with a temporary name and call register new thing
- case generateCertificateAcceptedTopic:
- registerThing(jsonPayload.certificateOwnershipToken)
- writeFileSync('./keys/device-certificate.crt.tmp', jsonPayload.certificatePem)
- writeFileSync('./keys/device-private.pem.key.tmp', jsonPayload.privateKey)
- break
- // Once the thing has been registered, rename the temporary certificate and restart the app with new certificate
- case provisionAcceptedTopic:
- renameSync('./keys/device-certificate.crt.tmp', './keys/device-certificate.pem.crt')
- renameSync('./keys/device-private.pem.key.tmp', './keys/device-private.pem.key')
- process.exit(0)
- break
- }
- });
- // Listen for SDK errors
- device
- .on('error', () => {
- console.log('ERR')
- })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement