contentparser.c 8.1 KB

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