contentparser.c 5.4 KB

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