Advertisement
metalni

AWS Fleet Provisioning Embedded

Jul 28th, 2022 (edited)
1,647
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import {device as iotDevice, DeviceOptions} from 'aws-iot-device-sdk'
  2. import {renameSync, writeFileSync} from 'fs'
  3.  
  4. const deviceModel: DeviceOptions = {
  5.     host: 'a25wywctgw4byi-ats.iot.us-east-1.amazonaws.com',
  6.     keyPath: './keys/device-private.pem.key',
  7.     certPath: './keys/device-certificate.pem.crt',
  8.     caPath: './keys/AmazonRootCA1.pem',
  9.     protocol: 'mqtts',
  10. }
  11.  
  12. const device = new iotDevice(deviceModel);
  13.  
  14. const provisionTopic = `$aws/provisioning-templates/demo_template/provision/json`
  15. const provisionAcceptedTopic = `$aws/provisioning-templates/demo_template/provision/json/accepted`
  16.  
  17. const generateCertificateTopic = `$aws/certificates/create/json`
  18. const generateCertificateAcceptedTopic = `$aws/certificates/create/json/accepted`
  19.  
  20. const getPermanentCertificate = () => {
  21.     console.log(`STEP - GETTING PERMANENT CERTIFICATE`)
  22.     console.log(`---------------------------------------------------------------------------------`)
  23.     return device.publish(generateCertificateTopic, "")
  24. }
  25.  
  26. const registerThing = (ownershipToken: string) => {
  27.     const data: any = {
  28.         certificateOwnershipToken: ownershipToken,
  29.         parameters: {
  30.             // Set the serial number, which will be used to create the thing name
  31.             // This could be the IMEI Number of the device
  32.             SerialNumber: "007"
  33.         }
  34.     }
  35.     console.log(`STEP - SENDING REGISTER THING`, ownershipToken)
  36.     console.log(`---------------------------------------------------------------------------------`)
  37.     return device.publish(provisionTopic, JSON.stringify(data))
  38. }
  39.  
  40. // We connect our client to AWS  IoT core.
  41. device
  42.     .on('connect', function () {
  43.         console.log('STEP - Connecting to AWS  IoT Core');
  44.         console.log(`---------------------------------------------------------------------------------`)
  45.  
  46.         // Subscribe to relevant MQTT Topics
  47.         device.subscribe(provisionAcceptedTopic)
  48.         device.subscribe(generateCertificateAcceptedTopic)
  49.  
  50.         // Initiate getting the device-specific certificate
  51.         getPermanentCertificate()
  52.     });
  53.  
  54. // Listen for messages from MQTT
  55. device
  56.     .on('message', (topic, payload) => {
  57.         const jsonPayload = JSON.parse(payload.toString())
  58.         console.log(jsonPayload)
  59.         switch(topic) {
  60.             // Once the device specific certificate has been created, save it with a temporary name and call register new thing
  61.             case generateCertificateAcceptedTopic:
  62.                 registerThing(jsonPayload.certificateOwnershipToken)
  63.                 writeFileSync('./keys/device-certificate.crt.tmp', jsonPayload.certificatePem)
  64.                 writeFileSync('./keys/device-private.pem.key.tmp', jsonPayload.privateKey)
  65.                 break
  66.  
  67.             // Once the thing has been registered, rename the temporary certificate and restart the app with new certificate
  68.             case provisionAcceptedTopic:
  69.                 renameSync('./keys/device-certificate.crt.tmp', './keys/device-certificate.pem.crt')
  70.                 renameSync('./keys/device-private.pem.key.tmp', './keys/device-private.pem.key')
  71.                 process.exit(0)
  72.                 break
  73.         }
  74.     });
  75.  
  76. // Listen for SDK errors
  77. device
  78.     .on('error', () => {
  79.         console.log('ERR')
  80.     })
  81.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement