client.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. var clientInfo = {
  2. state: {},
  3. stateLocation: '/root/state.json',
  4. deviceId: '', // device ID
  5. dsRoot: '', // root path to device shadow
  6. topics: {
  7. 'subscribe': {
  8. 'update': '', // topic to recieve accepted shadow updates
  9. 'updateRejected': '', // topic to receive rejected shadow updates
  10. 'get': '', // topic to recieve full shadow
  11. 'delta': '' // topic to receive delta between desired and reported
  12. },
  13. 'publish': {
  14. 'get': '', // topic to trigger a full shadow read
  15. 'update': '' // topic to update the device shadow
  16. }
  17. }
  18. };
  19. function populateClientInfo(deviceId) {
  20. clientInfo.deviceId = deviceId;
  21. clientInfo.dsRoot = '$aws/things/' + deviceId + '/shadow';
  22. clientInfo.topics.subscribe.update = clientInfo.dsRoot + '/update/accepted'; // $aws/things/${deviceId}/shadow/update/accepted
  23. clientInfo.topics.subscribe.updateRejected = clientInfo.dsRoot + '/update/rejected'; // $aws/things/${deviceId}/shadow/update/rejected
  24. clientInfo.topics.subscribe.get = clientInfo.dsRoot + '/get/accepted'; // $aws/things/${deviceId}/shadow/get/accepted
  25. clientInfo.topics.subscribe.delta = clientInfo.dsRoot + '/update/delta'; // $aws/things/${deviceId}/shadow/update/delta
  26. clientInfo.topics.publish.get = clientInfo.dsRoot + '/get'; // $aws/things/${deviceId}/shadow/get
  27. clientInfo.topics.publish.update = clientInfo.dsRoot + '/update'; // $aws/things/${deviceId}/shadow/update
  28. }
  29. function remoteConnect() {
  30. connect(null, 8883, clientInfo.deviceId, function () {
  31. print('connection established');
  32. subscribe(clientInfo.topics.subscribe.delta, null, true);
  33. subscribe(clientInfo.topics.subscribe.update, null, true);
  34. subscribe(clientInfo.topics.subscribe.updateRejected, null, true);
  35. subscribe(clientInfo.topics.subscribe.get, function () {
  36. print('successfully subscribed to topic ' + clientInfo.topics.subscribe.get);
  37. publish(clientInfo.topics.publish.get, '', true);
  38. }, true);
  39. }, true);
  40. }
  41. function localConnect() {
  42. connect('localhost', 1883, null, function () {
  43. subscribe('/setAck');
  44. });
  45. }
  46. function objLoop (obj, callback) {
  47. Object.keys(obj).forEach(function(key) {
  48. if (typeof obj[key] === 'object') {
  49. objLoop(obj[key], callback);
  50. } else {
  51. callback(key, obj[key]);
  52. }
  53. });
  54. }
  55. // base functions
  56. function setup() {
  57. readFile('/etc/certs/deviceId', '', function (err, data) {
  58. if (!err) {
  59. var deviceId = data.replace(/\n/gm, '');
  60. populateClientInfo(deviceId);
  61. remoteConnect();
  62. localConnect();
  63. }
  64. });
  65. }
  66. function loop() {
  67. }
  68. function onMessage(e) {
  69. if (typeof e.topic !== 'undefined' && typeof e.payload !== 'undefined' ) {
  70. print('message! topic: ' + e.topic);
  71. // print('message! topic: ' + e.topic + ', value: ' + JSON.stringify(e.payload));
  72. if (e.topic === clientInfo.topics.subscribe.get) {
  73. clientInfo.state = e.payload.state.reported;
  74. print(JSON.stringify(clientInfo.state));
  75. // write full device shadow to fs
  76. writeFile(clientInfo.stateLocation, JSON.stringify(clientInfo.state), function (err) {
  77. if (!err) {
  78. print('write was successful!')
  79. }
  80. });
  81. } else if (e.topic === clientInfo.topics.subscribe.delta) {
  82. print('delta!');
  83. var desired = e.payload.state;
  84. if (desired.config) {
  85. // publish local messages to change config on device
  86. objLoop(desired.config, function (cmd, val) {
  87. publish('/set', JSON.stringify({
  88. cmd: cmd,
  89. value: val
  90. }));
  91. });
  92. // update local state
  93. clientInfo.state.config = Object.assign(clientInfo.state.config, desired.config);
  94. // TODO: note this introduces a bug where the brightness object gets overwritten completely - potentially getting rid of some of the key-value pairs
  95. print(JSON.stringify(clientInfo.state));
  96. writeFile(clientInfo.stateLocation, JSON.stringify(clientInfo.state), function (err) {
  97. // acknowledge reported state has changed
  98. if (!err) {
  99. var pubObj = {
  100. state: {
  101. reported: {
  102. config: desired.config
  103. }
  104. }
  105. };
  106. publish(clientInfo.topics.publish.update, JSON.stringify(pubObj), true);
  107. }
  108. });
  109. }
  110. // TODO: add handlers for alarms and cards arrays
  111. } else {
  112. print('value: ' + JSON.stringify(e.payload));
  113. }
  114. print(' ');
  115. }
  116. }