contentparser.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. int startidx = 0;
  14. int charAmount = 0;
  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 str2[16];
  39. char st = -1;
  40. char oo = -1;
  41. memset(url, 0, 24);
  42. memset(ip, 0, 24);
  43. memset(name, 0, 17);
  44. for (i; (st == -1 && i < r); i+=2) { //Zodra ST is gevonden, betekent dit de laatste token van een alarm.
  45. if (jsoneq(content, &token[i], "YYYY") == 0) {
  46. time.tm_year= getIntegerToken(content, &token[i + 1]) - 1900;
  47. }else if (jsoneq(content, &token[i], "MM") == 0) {
  48. time.tm_mon= getIntegerToken(content, &token[i + 1]) - 1;
  49. }else if (jsoneq(content, &token[i], "DD") == 0) {
  50. time.tm_mday = getIntegerToken(content, &token[i + 1]);
  51. }else if (jsoneq(content, &token[i], "hh") == 0) {
  52. time.tm_hour = getIntegerToken(content, &token[i + 1]);
  53. }else if (jsoneq(content, &token[i], "mm") == 0) {
  54. time.tm_min = getIntegerToken(content, &token[i + 1]);
  55. }else if (jsoneq(content, &token[i], "ss") == 0) {
  56. time.tm_sec = getIntegerToken(content, &token[i + 1]);
  57. }else if (jsoneq(content, &token[i], "id") == 0) {
  58. id = getIntegerToken(content, &token[i + 1]);
  59. }else if (jsoneq(content, &token[i], "port") == 0) {
  60. port = getIntegerToken(content, &token[i + 1]);
  61. }else if (jsoneq(content, &token[i], "ip") == 0) {
  62. getStringToken(content, &token[i + 1], ip);
  63. }else if (jsoneq(content, &token[i], "url") == 0) {
  64. getStringToken(content, &token[i + 1], url);
  65. }else if (jsoneq(content, &token[i], "name") == 0) {
  66. getStringToken(content, &token[i + 1], name);
  67. }else if (jsoneq(content, &token[i], "oo") == 0) {
  68. oo = getIntegerToken(content, &token[i + 1]);
  69. } if (jsoneq(content, &token[i], "st") == 0) {
  70. st = getIntegerToken(content, &token[i + 1]);
  71. i+=2;
  72. }
  73. }
  74. int idx = alarmExist(id);
  75. if(idx == -1){
  76. printf("New alarm found!\n");
  77. printf("Alarm time is: %02d:%02d:%02d\n", time.tm_hour, time.tm_min, time.tm_sec);
  78. printf("Alarm date is: %02d.%02d.%02d\n", time.tm_mday, (time.tm_mon + 1), (time.tm_year + 1900));
  79. printf("Alarm stream data is: %s:%d%s\n", ip, port, url);
  80. printf("Alarm id and name and st is: %d %s %d\n\n", id, name, st);
  81. for (i = 0; i < 16;i++){
  82. if (name[i] != 0){
  83. charAmount = charAmount + 1;
  84. }
  85. }
  86. startidx = (8-(charAmount/2));
  87. charAmount = 0;
  88. for(i = 0; i < 16; i++){
  89. if (i >= startidx){
  90. if (name[charAmount] != 0){
  91. str2[i] = name[charAmount];
  92. } else {
  93. str2[i] = ' ';
  94. }
  95. charAmount++;
  96. } else {
  97. str2[i] = ' ';
  98. }
  99. }
  100. printf("Str2 = %s", str2);
  101. //zoek naar een vrije plaats in de alarm array
  102. for(j = 0; j < maxAlarms(); j++){
  103. if(usedAlarms[j] == 0){ //Dit is een lege plaats, hier kunnen we ons nieuwe alarm plaatsen
  104. if (oo == 1){
  105. eenmaligAlarm(time,str2,ip,port,url,st,id,j);
  106. } else {
  107. setAlarm(time, str2, ip, port, url, st, id, j);
  108. }
  109. usedAlarms[j] = 1;
  110. j = 10; //Uit de for loop
  111. }
  112. }
  113. }else{
  114. usedAlarms[idx] = 1; //Alarm bestaat al, dus we houden deze plaats vrij voor dat alarm
  115. }
  116. }
  117. for(j = 0; j < maxAlarms(); j++){ //Alle overige plaatsen, die wij niet gezet hebben, verwijderen.
  118. if(usedAlarms[j] == 0){
  119. deleteAlarm(j);
  120. };
  121. }
  122. }
  123. void parsetimezone(char* content)
  124. {
  125. int timezone = atoi(content);
  126. setTimeZone(timezone);
  127. }