contentparser.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. void parseAlarmJson(char* content){
  11. int r;
  12. int i = 2;
  13. jsmn_parser p;
  14. jsmntok_t token[150]; /* We expect no more than 128 tokens */
  15. jsmn_init(&p);
  16. r = jsmn_parse(&p, content, strlen(content), token, sizeof(token)/sizeof(token[0]));
  17. if (r <= 0) {
  18. printf("Failed to parse JSON: %d \n", r);
  19. return;
  20. }else{
  21. printf("Aantal tokens found: %d \n", r);
  22. }
  23. int usedAlarms[maxAlarms()];
  24. int j;
  25. struct _tm time = GetRTCTime();
  26. for(j = 0; j < maxAlarms(); j++){
  27. usedAlarms[j] = 0;
  28. }
  29. for(i; i < r; i++)
  30. {
  31. int id;
  32. u_short port;
  33. char url[24];
  34. char ip[24];
  35. char name[16];
  36. char st = -1;
  37. memset(url, 0, 24);
  38. memset(ip, 0, 24);
  39. memset(name, 0, 17);
  40. for (i; (st == -1 && i < r); i+=2) { //Zodra ST is gevonden, betekent dit de laatste token van een alarm.
  41. if (jsoneq(content, &token[i], "YYYY") == 0) {
  42. time.tm_year= getIntegerToken(content, &token[i + 1]) - 1900;
  43. }else if (jsoneq(content, &token[i], "MM") == 0) {
  44. time.tm_mon= getIntegerToken(content, &token[i + 1]) - 1;
  45. }else if (jsoneq(content, &token[i], "DD") == 0) {
  46. time.tm_mday = getIntegerToken(content, &token[i + 1]);
  47. }else if (jsoneq(content, &token[i], "hh") == 0) {
  48. time.tm_hour = getIntegerToken(content, &token[i + 1]);
  49. }else if (jsoneq(content, &token[i], "mm") == 0) {
  50. time.tm_min = getIntegerToken(content, &token[i + 1]);
  51. }else if (jsoneq(content, &token[i], "ss") == 0) {
  52. time.tm_sec = getIntegerToken(content, &token[i + 1]);
  53. }else if (jsoneq(content, &token[i], "id") == 0) {
  54. id = getIntegerToken(content, &token[i + 1]);
  55. }else if (jsoneq(content, &token[i], "port") == 0) {
  56. port = getIntegerToken(content, &token[i + 1]);
  57. }else if (jsoneq(content, &token[i], "ip") == 0) {
  58. getStringToken(content, &token[i + 1], ip);
  59. }else if (jsoneq(content, &token[i], "url") == 0) {
  60. getStringToken(content, &token[i + 1], url);
  61. }else if (jsoneq(content, &token[i], "name") == 0) {
  62. getStringToken(content, &token[i + 1], name);
  63. }else if (jsoneq(content, &token[i], "st") == 0) {
  64. st = getIntegerToken(content, &token[i + 1]);
  65. i+=2;
  66. }
  67. }
  68. int idx = alarmExist(id);
  69. if(idx == -1){
  70. printf("New alarm found!\n");
  71. printf("Alarm time is: %02d:%02d:%02d\n", time.tm_hour, time.tm_min, time.tm_sec);
  72. printf("Alarm date is: %02d.%02d.%02d\n", time.tm_mday, (time.tm_mon + 1), (time.tm_year + 1900));
  73. printf("Alarm stream data is: %s:%d%s\n", ip, port, url);
  74. printf("Alarm id and name and st is: %d %s %d\n\n", id, name, st);
  75. //zoek naar een vrije plaats in de alarm array
  76. for(j = 0; j < maxAlarms(); j++){
  77. if(usedAlarms[j] == 0){ //Dit is een lege plaats, hier kunnen we ons nieuwe alarm plaatsen
  78. setAlarm(time, name, ip, port, url, st, id, j);
  79. usedAlarms[j] = 1;
  80. j = 10; //Uit de for loop
  81. }
  82. }
  83. }else{
  84. usedAlarms[idx] = 1; //Alarm bestaat al, dus we houden deze plaats vrij voor dat alarm
  85. }
  86. }
  87. for(j = 0; j < maxAlarms(); j++){ //Alle overige plaatsen, die wij niet gezet hebben, verwijderen.
  88. if(usedAlarms[j] == 0){
  89. deleteAlarm(j);
  90. };
  91. }
  92. }
  93. void parsetimezone(char* content)
  94. {
  95. int timezone = atoi(content);
  96. printf("%d", timezone);
  97. }