system.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. var cardInfo = {
  2. id: -1,
  3. active: false,
  4. cardCreated: false,
  5. responseTopic: '/systemcard',
  6. bgColor: -1,
  7. nightlight: [
  8. 0x4eb96f,
  9. 0xc44569,
  10. 0xf5cd79,
  11. 0x546de5
  12. ],
  13. params: {
  14. },
  15. versions: {
  16. fw: '0',
  17. sw: '0'
  18. }
  19. }
  20. var notification = {
  21. active: false,
  22. duration: 0,
  23. startTime: 0
  24. }
  25. var messageQueue = [];
  26. var setupComplete = false;
  27. // definitions for the program
  28. var elementId = {
  29. systemImage: 0,
  30. versionInfoFw: 1,
  31. versionInfoSw: 2,
  32. };
  33. var cardImg = {
  34. background: "blank_bg",
  35. buttons: "blank_buttons",
  36. };
  37. // card functions
  38. function createCard () {
  39. if (cardInfo.cardCreated) {
  40. return;
  41. }
  42. var cardObj = generateNewCardObj(cardInfo.bgColor, cardInfo.responseTopic);
  43. cardObj.elements.push(generateImageElement(
  44. elementId.systemImage,
  45. generateImgPath(imgRootPath, 'system'),
  46. 0, 0)
  47. );
  48. cardObj.elements.push(generateTextElement(
  49. elementId.versionInfoFw,
  50. 'Build: ' + cardInfo.versions.fw,
  51. 15,
  52. 0, 245, 'left')
  53. );
  54. cardObj.elements.push(generateTextElement(
  55. elementId.versionInfoSw,
  56. 'Software: ' + cardInfo.versions.sw,
  57. 15,
  58. 0, 260, 'left')
  59. );
  60. // init the card
  61. initCard(JSON.stringify(cardObj));
  62. cardInfo.cardCreated = true;
  63. }
  64. // notification functions
  65. function setNotification(msg, duration) {
  66. publish('/notification', JSON.stringify({
  67. cmd: 'set',
  68. text: msg
  69. }));
  70. notification.active = true;
  71. notification.duration = duration;
  72. notification.startTime = Date.now();
  73. }
  74. // temporary solution to clear a notification after a set amount of time
  75. function checkNotification() {
  76. // only check if notification is active
  77. if (notification.active && notification.duration > 0) {
  78. if ((Date.now() - notification.startTime) >= (notification.duration * 1000)) {
  79. print('notification expired');
  80. publish('/notification', JSON.stringify({
  81. cmd: 'clear'
  82. }));
  83. notification.active = false;
  84. }
  85. }
  86. }
  87. // input handlers
  88. function handleButtonInput(e) {
  89. print('Handling button input');
  90. print(JSON.stringify(e));
  91. if (!cardInfo.cardCreated) {
  92. // look for simultaneous three button press
  93. if (e.id === 14 && e.multitouch === true && e.action === 'press') {
  94. // create the card
  95. print('Creating System card!');
  96. createCard()
  97. }
  98. }
  99. else if (cardInfo.active) {
  100. // check for button long press
  101. if (e.duration >= 3000) {
  102. print('Detected 3sec button ' + e.id);
  103. switch (e.id) {
  104. case 0:
  105. // reset wifi
  106. setNotification('Resetting all WiFi settings', 5);
  107. var result = popen('wifisetup reset');
  108. break;
  109. case 1:
  110. // enter setup mode
  111. setNotification('Now in Setup Mode!', 5);
  112. system('touch /tmp/setupMode');
  113. break;
  114. case 2:
  115. // trigger software update
  116. setNotification('Software Update in progress', 0);
  117. system('sh /etc/cron.week/firmware_update.sh');
  118. break;
  119. case 3:
  120. // enter bluetooth pairing mode
  121. setNotification('Bluetooth Pairing started', 5);
  122. publish('/audio', JSON.stringify({
  123. cmd: 'btPair'
  124. }))
  125. break;
  126. default:
  127. // nothing
  128. break;
  129. }
  130. }
  131. }
  132. }
  133. function handleGestureInput(e) {
  134. print('Handling gesture input');
  135. print(JSON.stringify(e));
  136. if (cardInfo.cardCreated) {
  137. if (e.direction === 'left' || e.direction === 'right') {
  138. print('deleting card!');
  139. // delete the card
  140. deleteCard(cardInfo.id);
  141. // reset the card ID
  142. cardInfo.id = -1;
  143. cardInfo.cardCreated = false;
  144. cardInfo.active = false;
  145. }
  146. }
  147. }
  148. // base functions
  149. function setup() {
  150. // grab version info
  151. cardInfo.versions.fw = popen('uci get onion.@onion[0].build');
  152. cardInfo.versions.fw = cardInfo.versions.fw.slice(0,-1);
  153. cardInfo.versions.sw = popen('opkg list-installed | grep oboo-clock-base | awk \'{printf $3;}\'');
  154. print('version data: ' + JSON.stringify(cardInfo.versions));
  155. // connect to mqtt broker
  156. connect('localhost', 1883, null, function () {
  157. print('Running connect callback');
  158. subscribe('/cardResponse');
  159. subscribe('/button');
  160. subscribe('/gesture');
  161. setupComplete = true;
  162. print('setup complete');
  163. },
  164. null,
  165. '/card',
  166. JSON.stringify({
  167. cmd: 'remove_card',
  168. cardName: cardInfo.responseTopic
  169. })
  170. );
  171. }
  172. function loop() {
  173. if (!setupComplete) {
  174. print('skipping loop');
  175. return 0;
  176. }
  177. // handle notifications
  178. checkNotification();
  179. // handle received messages
  180. if (messageQueue.length > 0) {
  181. for (var i = 0; i < messageQueue.length; i++) {
  182. if (messageQueue[i].source === 'button') {
  183. handleButtonInput(messageQueue[i].payload);
  184. } else if (messageQueue[i].source === 'gesture') {
  185. handleGestureInput(messageQueue[i].payload);
  186. }
  187. }
  188. messageQueue = [];
  189. }
  190. }
  191. function onInput(e) {
  192. if (typeof e.source !== 'undefined' && typeof e.payload !== 'undefined' ) {
  193. print('input! input source: ' + e.source);
  194. messageQueue.push(e);
  195. }
  196. }
  197. function onMessage(e) {
  198. if (typeof e.topic !== 'undefined' && typeof e.payload !== 'undefined' ) {
  199. print('message! topic: ' + e.topic + ', value: ' + e.payload);
  200. switch (e.topic) {
  201. case '/cardResponse':
  202. cardInfo = handleCardResponseMessage(cardInfo, e.payload);
  203. break;
  204. default:
  205. break;
  206. }
  207. }
  208. }