contentparser.c 7.1 KB

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