contentparser.c 5.5 KB

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