gpx.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. #include <stdio.h>
  2. #include <stdint.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <syslog.h>
  6. #include <stdarg.h>
  7. #include <time.h>
  8. #include "mxml-2.10/mxml.h"
  9. #include "../../status.h"
  10. #include "../../feeder.h"
  11. #define __USE_GNU
  12. /*
  13. typedef struct measurement_t
  14. {
  15. unsigned long unit_id;
  16. unsigned int sensor_id;
  17. time_t time;
  18. double value;
  19. struct measurement_t * next;
  20. } measurement_t;
  21. struct measurement_t * measurement_list;
  22. */
  23. static void (*feederLog) (int priority, const char *fmt, ...);
  24. int setLog(void *func)
  25. {
  26. feederLog = func;
  27. return 0;
  28. }
  29. static FILE * f = NULL;
  30. static mxml_node_t * gpxTree, * gpxNode;
  31. unsigned int timeout(void * lib_data, int sock, unsigned char * data)
  32. {
  33. (void)lib_data;
  34. (void)sock;
  35. (void)data;
  36. f = fopen("b.gpx", "r");
  37. if (f == NULL)
  38. {
  39. feederLog(LOG_ERR, "gpx: can not open input file (b.gpx)\n");
  40. return -1;
  41. }
  42. gpxTree = mxmlLoadFile(NULL, f, MXML_TEXT_CALLBACK);
  43. gpxNode = gpxTree;
  44. //todo:ceck
  45. fclose(f);
  46. return 0;
  47. }
  48. int getval(char * line, const char * name, char * val)
  49. {
  50. char * s, * se;
  51. s = strstr(line, name);
  52. s = strchr(s, ' ');
  53. s++;
  54. se = strchr(s, ',');
  55. memcpy(val, s, se - s);
  56. val[se-s] = 0;
  57. feederLog(LOG_DEBUG, "Value %s:%s\n", name, val);
  58. return 0;
  59. }
  60. unsigned int
  61. process(void *lib_data, int sock, unsigned char *data,
  62. unsigned int length, unsigned long long int *id, time_t * tm,
  63. double *result_array, uint64_t * sensors, unsigned int *type)
  64. {
  65. unsigned int res;
  66. /* static size_t len = 0;
  67. static char * line = NULL;
  68. ssize_t size;
  69. char name[256];*/
  70. mxml_node_t * node_time;
  71. struct tm t;
  72. int ws;
  73. (void)lib_data;
  74. (void)sock;
  75. (void)data;
  76. (void)length;
  77. feederLog(LOG_DEBUG, "gpx: process\n");
  78. if (gpxTree == NULL)
  79. {
  80. feederLog(LOG_ERR, "gpx: gpx not parsed\n");
  81. return -1;
  82. }
  83. feederLog(LOG_DEBUG, "gpx: searching\n");
  84. gpxNode = mxmlFindElement(gpxNode, gpxTree, "wpt", NULL, NULL, MXML_DESCEND);
  85. if (gpxNode != NULL)
  86. {
  87. feederLog(LOG_DEBUG, "gpx: found point lat %s\n", mxmlElementGetAttr(gpxNode, "lat"));
  88. feederLog(LOG_DEBUG, "gpx: found point lon %s\n", mxmlElementGetAttr(gpxNode, "lon"));
  89. result_array[0] = atof(mxmlElementGetAttr(gpxNode, "lat"));
  90. result_array[1] = atof(mxmlElementGetAttr(gpxNode, "lon"));
  91. sensors[0] = sensors[1] = 0x10;
  92. *type = VALUES_TYPE_POS;
  93. node_time = mxmlFindElement(gpxNode, gpxTree, "time", NULL, NULL, MXML_DESCEND);
  94. if (node_time)
  95. {
  96. strptime(mxmlGetText(node_time, &ws), "%Y-%m-%dT%H:%M:%SZ", &t);
  97. *tm = mktime(&t);
  98. *id = 354330030514288;
  99. }
  100. else
  101. *tm = 0;
  102. res = 2;
  103. }
  104. else
  105. res = 0;
  106. return res;
  107. /*
  108. while (size = getline(&line, &len, f), size >= 0)
  109. {
  110. if (strstr(line, "Saving sensor") != NULL)
  111. {
  112. feederLog(LOG_DEBUG, "mortlog: %s\n", line);
  113. getval(line, "id", name);
  114. sensors[0] = atoi(name);
  115. getval(line, "sensor", name);
  116. feederLog(LOG_DEBUG, "mortlog: sensor %s\n", name);
  117. getval(line, "timestamp", name);
  118. *tm = atoi(name);
  119. getval(line, "value", name);
  120. result_array[0] = atof(name);
  121. getval(line, "device", name);
  122. sensors[0] += (atoi(name) >> 8);
  123. *id = 105000000 + (atoi(name) & 0xFF);//todo: id must be configured by conf file, its not part of log file
  124. // feederLog(LOG_DEBUG, "mortlog: readline %f\n", result_array[0]);
  125. *type = VALUES_TYPE_OBS;
  126. return 1;
  127. }
  128. }
  129. fclose(f);
  130. res = 0;
  131. return res;*/
  132. }
  133. int init(void *param)
  134. {
  135. (void)param;
  136. feederLog(LOG_DEBUG, "mortlog: init\n");
  137. return 0;
  138. }