csv.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. //#define _XOPEN_SOURCE //todo:??????
  2. #include <stdio.h>
  3. #include <stdint.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <syslog.h>
  7. #include <stdarg.h>
  8. #include <time.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. unsigned int timeout(void * lib_data, int sock, unsigned char * data)
  31. {
  32. (void)lib_data;
  33. (void)sock;
  34. (void)data;
  35. f = fopen("log.csv", "r");//todo: zadat confem nebo parametrem
  36. if (f == NULL)
  37. {
  38. feederLog(LOG_ERR, "csv: can not open input file (log.csv)\n");
  39. return -1;
  40. }
  41. return 0;
  42. }
  43. int getval(char * line, const char * name, char * val)
  44. {
  45. char * s, * se;
  46. s = strstr(line, name);
  47. s = strchr(s, ' ');
  48. s++;
  49. se = strchr(s, ',');
  50. memcpy(val, s, se - s);
  51. val[se-s] = 0;
  52. feederLog(LOG_DEBUG, "Value %s:%s\n", name, val);
  53. return 0;
  54. }
  55. typedef struct meta_u_t
  56. {
  57. uint64_t id;
  58. double multi;
  59. } meta_u_t;
  60. typedef struct sensobtinymeta_t
  61. {
  62. struct meta_u_t u[32];
  63. } sensobtinymeta_t;
  64. static struct sensobtinymeta_t metas[24] =//todo: konfigurovat toto a casovou zonu devicy v conf, nebo to bude posilat noda
  65. {
  66. {{{100000000,1.0}}}, //0
  67. {{{340340092,1.0},{340350004,1.0},{360200000,1.0}}}, //1
  68. {{{0,1.0}}},
  69. //{{{360200000,1.0}}}, //2
  70. {{{340070001,1.0},{410050001,1.0},{460010001,1.0}}}, //3
  71. {{{480020001,1.0},{490010001,1.0}}}, //4
  72. {{{470020001,1.0},{470010001,1.0}}}, //5
  73. {{{540090004,10.0},{550020004,10.0},{340370004,10.0}}}, //6
  74. {{{0,0.0}}}
  75. };
  76. unsigned int
  77. process(void *lib_data, int sock, unsigned char *data,
  78. unsigned int length, unsigned long long int *id, time_t * tm,
  79. double *result_array, uint64_t * sensors, unsigned int *type)
  80. {
  81. unsigned int res;
  82. static size_t len = 0;
  83. static char * line = NULL;
  84. // ssize_t size;
  85. int size;
  86. char * sep;
  87. uint8_t i, j;
  88. struct tm time;
  89. int meta;
  90. uint32_t sensor_id;
  91. (void)lib_data;
  92. (void)sock;
  93. (void)data;
  94. (void)length;
  95. feederLog(LOG_DEBUG, "csv: process\n");
  96. if (f == NULL)
  97. {
  98. feederLog(LOG_ERR, "csv: file not opened\n");
  99. return -1;
  100. }
  101. j = 0;
  102. while (size = getline(&line, &len, f), size >= 0)
  103. {
  104. feederLog(LOG_DEBUG, "csv: %s", line);
  105. i = 0;
  106. sep = line;
  107. while (sep != NULL)
  108. {
  109. switch (i)
  110. {
  111. case 0:
  112. if (strptime(sep, "%Y/%m/%d %H:%M:%S", &time) != NULL)
  113. *tm = timegm(&time);
  114. else
  115. *tm = 0;
  116. break;
  117. case 1:
  118. break;
  119. case 2:
  120. meta = -1;
  121. sensor_id = atoi(sep);
  122. if (sensor_id / 10000 == 16001)
  123. meta = sensor_id % 10000;
  124. else
  125. sensors[0] = sensor_id;
  126. break;
  127. default:
  128. if (meta >= 0)
  129. {
  130. if (metas[meta].u[i - 3].id != 0)
  131. {
  132. sensors[i - 3] = metas[meta].u[i - 3].id;
  133. result_array[i - 3] = atof(sep);
  134. j++;
  135. }
  136. }
  137. else
  138. {
  139. result_array[0] = atof(sep);
  140. j = 1;
  141. }
  142. break;
  143. }
  144. sep = strchr(sep, ';');
  145. if (sep != NULL)
  146. sep += 1;
  147. i++;
  148. }
  149. // *id = 1305167549149707;//MJ6 todo: musime jinak
  150. // *id = 1305167549144045;//MJ1 todo: musime jinak
  151. *id = 1305167549180936;//MJ4 nahradni
  152. *type = VALUES_TYPE_OBS;
  153. /*
  154. getval(line, "id", name);
  155. sensors[0] = atoi(name);
  156. getval(line, "sensor", name);
  157. feederLog(LOG_DEBUG, "mortlog: sensor %s\n", name);
  158. getval(line, "timestamp", name);
  159. *tm = atoi(name);
  160. getval(line, "value", name);
  161. result_array[0] = atof(name);
  162. getval(line, "device", name);
  163. sensors[0] += (atoi(name) >> 8);
  164. *id = 105000000 + (atoi(name) & 0xFF);//todo: id must be configured by conf file, its not part of log file
  165. // feederLog(LOG_DEBUG, "mortlog: readline %f\n", result_array[0]);
  166. *type = VALUES_TYPE_OBS;
  167. */ if (j > 0)
  168. return j;
  169. }
  170. fclose(f);
  171. res = 0;
  172. return res;
  173. }
  174. int init(void *param)
  175. {
  176. (void)param;
  177. feederLog(LOG_DEBUG, "csv: init\n");
  178. return 0;
  179. }