contentparser.c 5.4 KB

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