contentparser.c 4.0 KB

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