contentparser.c 4.2 KB

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