contentparser.c 7.4 KB

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