contentparser.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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[17];
  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, 18);
  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. printf("Updating volume: \n");
  147. setVolumeManual(vol);
  148. //vol = 128 - ((vol * 128) / 100);
  149. //VsSetVolume(vol, vol);
  150. i += 3;
  151. }else if(jsoneq(content, &token[i + 1], "stopstream") == 0){
  152. killPlayerThread();
  153. i += 3;
  154. }else if(jsoneq(content, &token[i + 1], "startstream") == 0){
  155. u_short port = getIntegerToken(content, &token[i + 9]);
  156. char url[24];
  157. char ip[24];
  158. getStringToken(content, &token[i + 7], url, 24);
  159. getStringToken(content, &token[i + 5], ip, 24);
  160. bool success = connectToStream(ip, port, url);
  161. if (success == true){
  162. play();
  163. }else {
  164. printf("ConnectToStream failed. Aborting.\n\n");
  165. }
  166. i += 9;
  167. }
  168. }
  169. }
  170. }
  171. void parsetimezone(char* content)
  172. {
  173. int timezone = atoi(content); //parsing string to int (only works when everything is int)
  174. if(strlen(content) == 0)
  175. {
  176. setTimeZone(50);
  177. }
  178. else {
  179. setTimeZone(timezone);
  180. }
  181. }
  182. void parseTwitch(char* content) {
  183. if (!strcmp("null", content)) {
  184. printf("Nobody is streaming");
  185. return;
  186. }
  187. int r;
  188. int i;
  189. jsmn_parser p;
  190. jsmntok_t token[20]; /* We expect no more than 20 tokens */
  191. jsmn_init(&p);
  192. r = jsmn_parse(&p, content, strlen(content), token, sizeof(token) / sizeof(token[0]));
  193. if (r <= 0) {
  194. printf("Failed to parse JSON: %d \n", r);
  195. return;
  196. } else {
  197. printf("Aantal tokens found: %d \n", r);
  198. }
  199. char name[20];
  200. char title[20];
  201. char game[20];
  202. char date[15];
  203. memset(name, 0, 20);
  204. memset(title, 0, 20);
  205. memset(game, 0, 20);
  206. memset(date, 0, 15);
  207. for (i = 1; i < r; i++) {
  208. if (jsoneq(content, &token[i], "Name") == 0) {
  209. getStringToken(content, &token[i + 1], name, 20);
  210. i++;
  211. }
  212. else if (jsoneq(content, &token[i], "Title") == 0) {
  213. getStringToken(content, &token[i + 1], title, 20);
  214. i++;
  215. }
  216. else if (jsoneq(content, &token[i], "Game") == 0) {
  217. getStringToken(content, &token[i + 1], game, 20);
  218. i++;
  219. }
  220. else if (jsoneq(content, &token[i], "Date") == 0) {
  221. getStringToken(content, &token[i + 1], date, 15);
  222. i++;
  223. }
  224. }
  225. printf("%s", date);
  226. if(strncmp(date, streamid, 15) != 0)
  227. {
  228. strcpy(data.title, title);
  229. strcpy(data.game, game);
  230. strcpy(data.name, name);
  231. printf("%s - %s - %s", name, title, game);
  232. strcpy(streamid, date);
  233. setCurrentDisplay(DISPLAY_Twitch, 100);
  234. }
  235. }
  236. void TwitterParser(char* content)
  237. {
  238. char tweet[140];
  239. memset(tweet, 0, 140);
  240. strcpy(TweetFeed.tweet,content);
  241. printf("%s", TweetFeed.tweet);
  242. }