contentparser.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. //
  2. // Created by janco on 25-3-16.
  3. //
  4. #include "contentparser.h"
  5. #include "ntp.h"
  6. #include "network.h"
  7. #include "jsmn.h"
  8. #include "mp3stream.h"
  9. #include "rtc.h"
  10. #include "alarm.h"
  11. #include "displayHandler.h"
  12. #include "vs10xx.h"
  13. #include "Twitter.h"
  14. int streamid;
  15. void parseAlarmJson(char* content){
  16. int r;
  17. int i;
  18. int usedAlarms[maxAlarms()];
  19. int j;
  20. jsmn_parser p;
  21. jsmntok_t token[160]; /* We expect no more than 128 tokens */
  22. jsmn_init(&p);
  23. r = jsmn_parse(&p, content, strlen(content), token, sizeof(token)/sizeof(token[0]));
  24. if (r <= 0) {
  25. printf("Failed to parse JSON: %d \n", r);
  26. return;
  27. }else{
  28. printf("Aantal tokens found: %d \n", r);
  29. }
  30. struct _tm time = GetRTCTime();
  31. for(j = 0; j < maxAlarms(); j++){
  32. usedAlarms[j] = 0;
  33. }
  34. for(i = 2; i < r; i++)
  35. {
  36. int id = 0;
  37. u_short port = 0;
  38. char url[24];
  39. char ip[24];
  40. char name[16];
  41. char st = -1;
  42. char oo = -1;
  43. memset(url, 0, 24);
  44. memset(ip, 0, 24);
  45. memset(name, 0, 17);
  46. for (i = i; (st == -1 && i < r); i+=2) { //Zodra ST is gevonden, betekent dit de laatste token van een alarm.
  47. if (jsoneq(content, &token[i], "YYYY") == 0) {
  48. time.tm_year= getIntegerToken(content, &token[i + 1]) - 1900;
  49. }else if (jsoneq(content, &token[i], "MM") == 0) {
  50. time.tm_mon= getIntegerToken(content, &token[i + 1]) - 1;
  51. }else if (jsoneq(content, &token[i], "DD") == 0) {
  52. time.tm_mday = getIntegerToken(content, &token[i + 1]);
  53. }else if (jsoneq(content, &token[i], "hh") == 0) {
  54. time.tm_hour = getIntegerToken(content, &token[i + 1]);
  55. }else if (jsoneq(content, &token[i], "mm") == 0) {
  56. time.tm_min = getIntegerToken(content, &token[i + 1]);
  57. }else if (jsoneq(content, &token[i], "ss") == 0) {
  58. time.tm_sec = getIntegerToken(content, &token[i + 1]);
  59. }else if (jsoneq(content, &token[i], "id") == 0) {
  60. id = getIntegerToken(content, &token[i + 1]);
  61. }else if (jsoneq(content, &token[i], "port") == 0) {
  62. port = getIntegerToken(content, &token[i + 1]);
  63. }else if (jsoneq(content, &token[i], "ip") == 0) {
  64. getStringToken(content, &token[i + 1], ip, 24);
  65. }else if (jsoneq(content, &token[i], "url") == 0) {
  66. getStringToken(content, &token[i + 1], url, 24);
  67. }else if (jsoneq(content, &token[i], "name") == 0) {
  68. getStringToken(content, &token[i + 1], name, 16);
  69. }else if (jsoneq(content, &token[i], "oo") == 0) {
  70. oo = getIntegerToken(content, &token[i + 1]);
  71. }else if (jsoneq(content, &token[i], "st") == 0) {
  72. st = getIntegerToken(content, &token[i + 1]);
  73. }
  74. }
  75. int idx = alarmExist(id);
  76. if(idx == -1){
  77. printf("New alarm found!\n");
  78. printf("Alarm time is: %02d:%02d:%02d\n", time.tm_hour, time.tm_min, time.tm_sec);
  79. printf("Alarm date is: %02d.%02d.%02d\n", time.tm_mday, (time.tm_mon + 1), (time.tm_year + 1900));
  80. printf("Alarm stream data is: %s:%d%s\n", ip, port, url);
  81. printf("Alarm id and name and st is: %d %s %d\n\n", id, name, st);
  82. //zoek naar een vrije plaats in de alarm array
  83. for(j = 0; j < maxAlarms(); j++){
  84. if(usedAlarms[j] == 0){ //Dit is een lege plaats, hier kunnen we ons nieuwe alarm plaatsen
  85. setAlarm(time, name, ip, port, url, st, id, j);
  86. usedAlarms[j] = 1;
  87. j = 10; //Uit de for loop
  88. }
  89. }
  90. }else{
  91. usedAlarms[idx] = 1; //Alarm bestaat al, dus we houden deze plaats vrij voor dat alarm
  92. }
  93. }
  94. for(j = 0; j < maxAlarms(); j++){ //Alle overige plaatsen, die wij niet gezet hebben, verwijderen.
  95. if(usedAlarms[j] == 0){
  96. deleteAlarm(j);
  97. };
  98. }
  99. }
  100. void parseCommandQue(char* content){
  101. int r;
  102. int i;
  103. jsmn_parser p;
  104. jsmntok_t token[150]; /* We expect no more than 128 tokens */
  105. jsmn_init(&p);
  106. r = jsmn_parse(&p, content, strlen(content), token, sizeof(token)/sizeof(token[0]));
  107. if (r <= 0) {
  108. printf("Failed to parse JSON: %d \n", r);
  109. return;
  110. }else{
  111. printf("Aantal tokens found: %d \n", r);
  112. }
  113. for(i = 0; i < r; i++)
  114. {
  115. if (jsoneq(content, &token[i], "command") == 0) {
  116. if(jsoneq(content, &token[i + 1], "volume") == 0){
  117. char vol = getIntegerToken(content, &token[i + 3]);
  118. vol = 128 - ((vol * 128) / 100);
  119. VsSetVolume(vol, vol);
  120. i += 3;
  121. }else if(jsoneq(content, &token[i + 1], "stopstream") == 0){
  122. killPlayerThread();
  123. i += 3;
  124. }else if(jsoneq(content, &token[i + 1], "startstream") == 0){
  125. u_short port = getIntegerToken(content, &token[i + 9]);
  126. char url[24];
  127. char ip[24];
  128. getStringToken(content, &token[i + 7], url, 24);
  129. getStringToken(content, &token[i + 5], ip, 24);
  130. bool success = connectToStream(ip, port, url);
  131. if (success == true){
  132. play();
  133. }else {
  134. printf("ConnectToStream failed. Aborting.\n\n");
  135. }
  136. i += 9;
  137. }
  138. }
  139. }
  140. }
  141. void parsetimezone(char* content)
  142. {
  143. int timezone = atoi(content); //parsing string to int (only works when everything is int)
  144. setTimeZone(timezone);
  145. }
  146. void parseTwitch(char* content) {
  147. if (!strcmp("null", content)) {
  148. printf("Nobody is streaming");
  149. return;
  150. }
  151. int r;
  152. int i;
  153. jsmn_parser p;
  154. jsmntok_t token[20]; /* We expect no more than 20 tokens */
  155. jsmn_init(&p);
  156. r = jsmn_parse(&p, content, strlen(content), token, sizeof(token) / sizeof(token[0]));
  157. if (r <= 0) {
  158. printf("Failed to parse JSON: %d \n", r);
  159. return;
  160. } else {
  161. printf("Aantal tokens found: %d \n", r);
  162. }
  163. char name[20];
  164. char title[20];
  165. char game[20];
  166. int date;
  167. memset(name, 0, 20);
  168. memset(title, 0, 20);
  169. memset(game, 0, 20);
  170. for (i = 1; i < r; i++) {
  171. if (jsoneq(content, &token[i], "Name") == 0) {
  172. getStringToken(content, &token[i + 1], name, 20);
  173. i++;
  174. }
  175. else if (jsoneq(content, &token[i], "Title") == 0) {
  176. getStringToken(content, &token[i + 1], title, 20);
  177. i++;
  178. }
  179. else if (jsoneq(content, &token[i], "Game") == 0) {
  180. getStringToken(content, &token[i + 1], game, 20);
  181. i++;
  182. }
  183. else if (jsoneq(content, &token[i], "Date") == 0) {
  184. date = getIntegerToken(content, &token[i + 1]);
  185. i++;
  186. }
  187. }
  188. if(streamid != date)
  189. {
  190. printf("%s - %s - %s", name, title, game);
  191. streamid = date;
  192. displayTwitch(name, title, game);
  193. }
  194. }
  195. void TwitterParser(char* content)
  196. {
  197. char tweet[140];
  198. memset(tweet, 0, 140);
  199. strcpy(TweetFeed.tweet,content);
  200. printf("%s", TweetFeed.tweet);
  201. }