contentparser.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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 "vs10xx.h"
  12. void parseAlarmJson(char* content){
  13. int r;
  14. int i = 2;
  15. int startidx = 0;
  16. int charAmount = 0;
  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 str2[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);
  65. }else if (jsoneq(content, &token[i], "url") == 0) {
  66. getStringToken(content, &token[i + 1], url);
  67. }else if (jsoneq(content, &token[i], "name") == 0) {
  68. getStringToken(content, &token[i + 1], name);
  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. i+=2;
  74. }
  75. }
  76. int idx = alarmExist(id);
  77. if(idx == -1){
  78. printf("New alarm found!\n");
  79. printf("Alarm time is: %02d:%02d:%02d\n", time.tm_hour, time.tm_min, time.tm_sec);
  80. printf("Alarm date is: %02d.%02d.%02d\n", time.tm_mday, (time.tm_mon + 1), (time.tm_year + 1900));
  81. printf("Alarm stream data is: %s:%d%s\n", ip, port, url);
  82. printf("Alarm id and name and st is: %d %s %d\n\n", id, name, st);
  83. charAmount = 0;
  84. for (i = 0; i < 16;i++){
  85. if (name[i] != 0){
  86. charAmount = charAmount + 1;
  87. }
  88. }
  89. startidx = (8-(charAmount/2));
  90. charAmount = 0;
  91. for(i = 0; i < 16; i++){
  92. if (i >= startidx){
  93. if (name[charAmount] != 0){
  94. str2[i] = name[charAmount];
  95. } else {
  96. str2[i] = ' ';
  97. }
  98. charAmount++;
  99. } else {
  100. str2[i] = ' ';
  101. }
  102. }
  103. //zoek naar een vrije plaats in de alarm array
  104. for(j = 0; j < maxAlarms(); j++){
  105. if(usedAlarms[j] == 0){ //Dit is een lege plaats, hier kunnen we ons nieuwe alarm plaatsen
  106. if (oo == 1){
  107. eenmaligAlarm(time,str2,ip,port,url,st,id,j);
  108. } else {
  109. setAlarm(time, str2, ip, port, url, st, id, j);
  110. }
  111. usedAlarms[j] = 1;
  112. j = 10; //Uit de for loop
  113. }
  114. }
  115. }else{
  116. usedAlarms[idx] = 1; //Alarm bestaat al, dus we houden deze plaats vrij voor dat alarm
  117. }
  118. }
  119. for(j = 0; j < maxAlarms(); j++){ //Alle overige plaatsen, die wij niet gezet hebben, verwijderen.
  120. if(usedAlarms[j] == 0){
  121. deleteAlarm(j);
  122. };
  123. }
  124. }
  125. void parseCommandQue(char* content){
  126. int r;
  127. int i;
  128. jsmn_parser p;
  129. jsmntok_t token[150]; /* We expect no more than 128 tokens */
  130. jsmn_init(&p);
  131. r = jsmn_parse(&p, content, strlen(content), token, sizeof(token)/sizeof(token[0]));
  132. if (r <= 0) {
  133. printf("Failed to parse JSON: %d \n", r);
  134. return;
  135. }else{
  136. printf("Aantal tokens found: %d \n", r);
  137. }
  138. for(i = 0; i < r; i++)
  139. {
  140. if (jsoneq(content, &token[i], "command") == 0) {
  141. if(jsoneq(content, &token[i + 1], "volume") == 0){
  142. char vol = getIntegerToken(content, &token[i + 3]);
  143. vol = 128 - ((vol * 128) / 100);
  144. VsSetVolume(vol, vol);
  145. i += 3;
  146. }else if(jsoneq(content, &token[i + 1], "stopstream") == 0){
  147. killPlayerThread();
  148. i += 3;
  149. }else if(jsoneq(content, &token[i + 1], "startstream") == 0){
  150. u_short port = getIntegerToken(content, &token[i + 9]);
  151. char url[24];
  152. char ip[24];
  153. getStringToken(content, &token[i + 7], url);
  154. getStringToken(content, &token[i + 5], ip);
  155. bool success = connectToStream(ip, port, url);
  156. if (success == true){
  157. play();
  158. }else {
  159. printf("ConnectToStream failed. Aborting.\n\n");
  160. }
  161. i += 9;
  162. }
  163. }
  164. }
  165. }
  166. void parsetimezone(char* content)
  167. {
  168. int timezone = atoi(content);
  169. setTimeZone(timezone);
  170. }