contentparser.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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;
  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 start = 1;
  24. int usedAlarms[maxAlarms()];
  25. int j;
  26. for(j = 0; j < maxAlarms(); j++){
  27. usedAlarms[j] = 0;
  28. }
  29. for(i = 1; i < r; i++)
  30. {
  31. struct _tm time = GetRTCTime();
  32. u_short port;
  33. char url[24];
  34. char ip[24];
  35. char name[16];
  36. char st;
  37. memset(url, 0, 24);
  38. memset(ip, 0, 24);
  39. memset(name, 0, 16);
  40. int id;
  41. for (i = i; !((i + start) % 26 == 0); i++) {
  42. if (jsoneq(content, &token[i], "YYYY") == 0) {
  43. time.tm_year= getIntegerToken(content, &token[i + 1]) - 1900;
  44. i++;
  45. }else if (jsoneq(content, &token[i], "MM") == 0) {
  46. time.tm_mon= getIntegerToken(content, &token[i + 1]) - 1;
  47. i++;
  48. }else if (jsoneq(content, &token[i], "DD") == 0) {
  49. time.tm_mday = getIntegerToken(content, &token[i + 1]);
  50. i++;
  51. }else if (jsoneq(content, &token[i], "hh") == 0) {
  52. time.tm_hour = getIntegerToken(content, &token[i + 1]);
  53. i++;
  54. }else if (jsoneq(content, &token[i], "mm") == 0) {
  55. time.tm_min = getIntegerToken(content, &token[i + 1]);
  56. i++;
  57. }else if (jsoneq(content, &token[i], "ss") == 0) {
  58. time.tm_sec = getIntegerToken(content, &token[i + 1]);
  59. i++;
  60. }else if (jsoneq(content, &token[i], "id") == 0) {
  61. id = getIntegerToken(content, &token[i + 1]);
  62. i++;
  63. }else if (jsoneq(content, &token[i], "port") == 0) {
  64. port = getIntegerToken(content, &token[i + 1]);
  65. i++;
  66. }else if (jsoneq(content, &token[i], "ip") == 0) {
  67. getStringToken(content, &token[i + 1], ip);
  68. i++;
  69. }else if (jsoneq(content, &token[i], "url") == 0) {
  70. getStringToken(content, &token[i + 1], url);
  71. i++;
  72. }else if (jsoneq(content, &token[i], "name") == 0) {
  73. getStringToken(content, &token[i + 1], name);
  74. i++;
  75. }else if (jsoneq(content, &token[i], "st") == 0) {
  76. st = getIntegerToken(content, &token[i + 1]);
  77. i++;
  78. }
  79. }
  80. start = 0;
  81. int idx = alarmExist(id);
  82. if(idx == -1){
  83. printf("New alarm found!\n");
  84. printf("Alarm time is: %02d:%02d:%02d\n", time.tm_hour, time.tm_min, time.tm_sec);
  85. printf("Alarm date is: %02d.%02d.%02d\n", time.tm_mday, (time.tm_mon + 1), (time.tm_year + 1900));
  86. printf("Alarm stream data is: %s:%d%s\n", ip, port, url);
  87. printf("Alarm id and name and st is: %d %s %d\n\n", id, name, st);
  88. //zoek naar een vrije plaats in de alarm array
  89. for(j = 0; j < maxAlarms(); j++){
  90. if(usedAlarms[j] == 0){ //Dit is een lege plaats, hier kunnen we ons nieuwe alarm plaatsen
  91. setAlarm(time, name, ip, port, url, st, id, j);
  92. usedAlarms[j] = 1;
  93. j = 10;
  94. }
  95. }
  96. }else{
  97. usedAlarms[idx] = 1; //Alarm bestaat al, dus we houden deze plaats vrij voor dat alarm
  98. }
  99. }
  100. for(j = 0; j < maxAlarms(); j++){ //Alle overige plaatsen, die wij niet gezet hebben, verwijderen.
  101. if(usedAlarms[j] == 0){
  102. deleteAlarm(j);
  103. };
  104. }
  105. }
  106. void parsetimezone(char* content)
  107. {
  108. int timezone = atoi(content);
  109. printf("%d", timezone);
  110. }