senso_http.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <time.h>
  5. #include "../../status.h"
  6. char phenomenon_list[][128] = {
  7. "urn:ogc:def:phenomenon:OGC:1.0.30:RelativeHumidity",
  8. "urn:ogc:def:phenomenon:OGC:1.0.30:WindDirection",
  9. "urn:ogc:def:phenomenon:OGC:1.0.30:WindSpeed",
  10. "urn:ogc:def:phenomenon:OGC:1.0.30:AirPressure",
  11. "urn:ogc:def:phenomenon:OGC:1.0.30:DewPoint",
  12. "urn:ogc:def:phenomenon:OGC:1.0.30:AirTemperature",
  13. "urn:ogc:def:phenomenon:OGC:1.0.30:RainIntensity",
  14. "urn:ogc:def:phenomenon:OGC:1.0.30:HailIntensity",
  15. ""};
  16. int init(void * param)
  17. {
  18. return 0;
  19. }
  20. typedef struct senso_t
  21. {
  22. unsigned long long int id;
  23. struct tm t;
  24. // double lat, lon, alt;
  25. int phenomenon;
  26. double value;
  27. } senso_t;
  28. unsigned int process(unsigned char * data, unsigned int length, unsigned long long int * id, time_t * tm, unsigned int * status, unsigned char * result, unsigned int * pbb_status, double * lat, double * lon, double * alt)
  29. {
  30. struct senso_t senso;
  31. char * pos, * next;
  32. char name[256];
  33. char * value;
  34. int len;
  35. int phenomenon;
  36. pos = strchr(data, '?');
  37. if (pos == NULL)
  38. {
  39. printf("Senso_http: No ? in GET\n");
  40. return 0;
  41. }
  42. pos++;
  43. while (next = strpbrk(pos, "& "), next != NULL)
  44. {
  45. len = next - pos;
  46. if (len >= 256)
  47. {
  48. printf("Senso_http: GET value too long (> 256)\n");
  49. continue;
  50. }
  51. memcpy(name, pos, len);
  52. name[len] = 0;
  53. if (value = strchr(name, '='), value != NULL)
  54. {
  55. *value = 0;
  56. value++;
  57. if (strcmp(name, "Operation") == 0)
  58. {
  59. if (strcmp(value, "InsertObservation") != 0)
  60. {
  61. printf("Senso_http: bad operation %s\n", value);
  62. return 0;
  63. }
  64. }
  65. if (strcmp(name, "value") == 0)
  66. {
  67. senso.value = atof(value);
  68. }
  69. if (strcmp(name, "phenomenon_id") == 0)
  70. {
  71. for (phenomenon = 0; (phenomenon_list[phenomenon][0] != 0) && (strcmp(value, phenomenon_list[phenomenon]) != 0); phenomenon++) ;
  72. if (phenomenon_list[phenomenon][0] != 0)
  73. senso.phenomenon = phenomenon;
  74. else
  75. senso.phenomenon = -1;
  76. }
  77. if (strcmp(name, "sensor_name") == 0)
  78. {
  79. senso.id = atol(value); //todo: staci atol? (delka typu)
  80. }
  81. if (strcmp(name, "date") == 0)
  82. {
  83. sscanf(value, "%d-%d-%d+%d:%d:%d",
  84. &senso.t.tm_year, &senso.t.tm_mon, &senso.t.tm_mday, &senso.t.tm_hour, &senso.t.tm_min, &senso.t.tm_sec);
  85. senso.t.tm_year -= 1900;
  86. senso.t.tm_mon -= 1;
  87. }
  88. }
  89. pos = next + 1;
  90. }
  91. printf("Senso_http: %llu, on %04d-%02d-%02d %02d:%02d:%02d\n", senso.id, senso.t.tm_year + 1900, senso.t.tm_mon + 1, senso.t.tm_mday, senso.t.tm_hour, senso.t.tm_min, senso.t.tm_sec);
  92. printf("Senso_http: %s = %f\n", phenomenon_list[senso.phenomenon], senso.value);
  93. *id = senso.id;
  94. *tm = mktime(&(senso.t));
  95. *status = 0;
  96. *lat = *lon = 0.0;
  97. *alt = 999999.0;
  98. *pbb_status = PBB_ON;
  99. result[0] = 0;
  100. if (senso.phenomenon >= 0)
  101. {
  102. for (phenomenon = 0; phenomenon < 16; phenomenon++)
  103. {
  104. if (phenomenon == senso.phenomenon)
  105. sprintf(result + strlen(result), "'%d'", (int)(senso.value * 1000));
  106. else
  107. sprintf(result + strlen(result), "NULL");
  108. if (phenomenon != 15)
  109. sprintf(result + strlen(result), ",");
  110. }
  111. return 16;
  112. }
  113. return 0;
  114. }