card-lib.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. // generic card functions
  2. var imgRootPath = '/usr/bin/img';
  3. // general helper functions
  4. function zeroPad(num, places) {
  5. var zero = places - num.toString().length + 1;
  6. return Array(+(zero > 0 && zero)).join("0") + num;
  7. }
  8. // time&date functions
  9. function getEpochMillis() {
  10. return Date.now();
  11. }
  12. function generateDate() {
  13. var dt = new Date
  14. return {
  15. 'type': 'calendar',
  16. 'value': 'update',
  17. 'id': 0,
  18. 'date': {
  19. 'year' : dt.getFullYear(),
  20. 'month': dt.getMonth()+1,
  21. 'day': dt.getDate()
  22. }
  23. };
  24. }
  25. function getEpoch() {
  26. return Math.floor(getEpochMillis() / 1000);
  27. }
  28. function getCurrentHour() {
  29. var d = new Date();
  30. return d.getHours();
  31. }
  32. // path generating functions
  33. function generateImgPath(rootPath, imgName) {
  34. return rootPath + '/img_' + imgName + '.bin';
  35. }
  36. // functions generating card objects
  37. function generateNewCardObj(bgColor, responseTopic) {
  38. return {
  39. 'cmd': 'new_card',
  40. 'bg_color': bgColor, // TODO: change bgColor from hex to dec
  41. 'responseTopic': responseTopic,
  42. 'elements': []
  43. };
  44. }
  45. function generateUpdateCardObj(cardId) {
  46. return {
  47. 'cmd': 'update_card',
  48. 'id': cardId,
  49. 'elements': []
  50. };
  51. }
  52. function generateElement (id, type, value, posX, posY, alignment) {
  53. var obj = {
  54. 'id': id,
  55. 'type': type,
  56. 'value': value
  57. }
  58. if (typeof posX !== 'undefined' && typeof posY !== 'undefined') {
  59. var pos = {
  60. 'x': posX,
  61. 'y': posY
  62. };
  63. if (typeof alignment !== 'undefined' &&
  64. (alignment === 'left' || alignment === 'center' || alignment === 'right')
  65. ) {
  66. pos['align'] = alignment;
  67. }
  68. obj['position'] = pos;
  69. }
  70. return obj;
  71. }
  72. function generateElementCal (id, type, value, posX, posY, alignment) {
  73. var dt = new Date
  74. var obj = {
  75. 'id': id,
  76. 'type': type,
  77. 'size': value,
  78. 'date': {
  79. 'year' : dt.getFullYear(),
  80. 'month': dt.getMonth()+1,
  81. 'day': dt.getDate()
  82. }
  83. }
  84. if (typeof posX !== 'undefined' && typeof posY !== 'undefined') {
  85. var pos = {
  86. 'x': posX,
  87. 'y': posY
  88. };
  89. if (typeof alignment !== 'undefined' &&
  90. (alignment === 'left' || alignment === 'center' || alignment === 'right')
  91. ) {
  92. pos['align'] = alignment;
  93. }
  94. obj['position'] = pos;
  95. }
  96. return obj;
  97. }
  98. function generateTextElement (id, value, size, posX, posY, alignment){
  99. var obj = generateElement(id, 'text', value, posX, posY, alignment);
  100. if (size !== null){
  101. obj['size'] = size;
  102. }
  103. return obj;
  104. }
  105. function generateImageElement(id, value, posX, posY, alignment){
  106. return generateElement(id, 'image', value, posX, posY, alignment);
  107. }
  108. function generateElementUpdate(id, value) {
  109. return {
  110. 'id': id,
  111. 'value': value
  112. };
  113. }
  114. // message queue functions
  115. // code review: good to have this function since every card requires this action?
  116. function onRecvMessage(e) {
  117. if (typeof e.topic !== 'undefined' && typeof e.payload !== 'undefined' ) {
  118. var payloadObj = e.payload;
  119. if (typeof e.payload === 'string' || e.payload instanceof String) {
  120. print('recvMessage: payload is a string, converting...');
  121. // print('recvMessage: ' + e.payload);
  122. try {
  123. payloadObj = JSON.parse(e.payload);
  124. } catch(err) {
  125. print(err); // error in the above string (in this case, yes)!
  126. print('ERROR Could not parse payload')
  127. return null;
  128. }
  129. }
  130. if (e.topic === '/button' || e.topic === '/gesture') {
  131. onInput({
  132. source: e.topic.replace(/^\//, ''),
  133. payload: payloadObj
  134. });
  135. } else {
  136. onMessage({
  137. topic: e.topic,
  138. payload: payloadObj
  139. });
  140. }
  141. }
  142. }
  143. function handleCardResponseMessage (cardInfo, payload) {
  144. if (typeof payload === 'string' || payload instanceof String) {
  145. try {
  146. payload = JSON.parse(payload);
  147. } catch (e) {
  148. print(e); // error in the above string (in this case, yes)!
  149. print('ERROR Could not parse payload')
  150. return null;
  151. }
  152. }
  153. if (typeof payload.cardId !== 'undefined' && typeof payload.action !== 'undefined') {
  154. switch (payload.action) {
  155. case 'create':
  156. if (cardInfo.id < 0 &&
  157. typeof payload.attention !== 'undefined' &&
  158. payload.attention === cardInfo.responseTopic)
  159. {
  160. // assign the card it's id
  161. cardInfo.id = payload.cardId;
  162. print('Assigning card its id: ' + cardInfo.id);
  163. setCardNightlightColors(cardInfo.nightlight || 0);
  164. }
  165. break;
  166. case 'select':
  167. if (cardInfo.id === payload.cardId) {
  168. // this has become the active card
  169. print('Card has become active: ' + cardInfo.id);
  170. cardInfo.active = true;
  171. setCardNightlightColors(cardInfo.nightlight || 0);
  172. } else {
  173. // another card has become the active card
  174. cardInfo.active = false;
  175. print('A different card has become active');
  176. }
  177. break;
  178. case 'update':
  179. if (payload.success === false && payload.reason && payload.reason === 'invalid cardId') {
  180. cardInfo.invalid = true;
  181. print('Card identified as invalid');
  182. }
  183. break;
  184. default:
  185. break;
  186. }
  187. }
  188. return cardInfo;
  189. }
  190. function setCardNightlightColors(color) {
  191. if (Array.isArray(color)) {
  192. // color.forEach(function(element, id) {
  193. // //print('setting button ' + id + ' to ' + element);
  194. // setButton(id, element);
  195. // });
  196. var payload = {
  197. cmd: 'buttons',
  198. value: color
  199. }
  200. publish('/set', JSON.stringify(payload));
  201. } else {
  202. setNightLight(color);
  203. }
  204. }