fiedler.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  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 <curl/curl.h>
  8. #include <confuse.h>
  9. #include "../../status.h"
  10. #include "../../feeder.h"
  11. cfg_opt_t type_opts[] =//todo: predelat na sensor id
  12. {
  13. CFG_STR((char *)"lib", 0, CFGF_NONE),
  14. CFG_INT((char *)"port", 0, CFGF_NONE),
  15. CFG_STR((char *)"proto", 0, CFGF_NONE),
  16. CFG_STR((char *)"server", 0, CFGF_NONE),
  17. CFG_INT((char *)"timeout", 0, CFGF_NONE),
  18. CFG_STR_LIST((char *)"devid", 0, CFGF_NONE),
  19. CFG_END()
  20. };
  21. cfg_opt_t db_opts[] =
  22. {
  23. CFG_STR((char *)"server", 0, CFGF_NONE),
  24. CFG_INT((char *)"port", 0, CFGF_NONE),
  25. CFG_STR((char *)"uri", 0, CFGF_NONE),
  26. CFG_STR((char *)"timezone", 0, CFGF_NONE),
  27. CFG_INT((char *)"max_msg", 0, CFGF_NONE),
  28. CFG_END()
  29. };
  30. cfg_opt_t cfg_opts[] =
  31. {
  32. CFG_SEC((char *)"db", db_opts, CFGF_MULTI | CFGF_TITLE),
  33. CFG_SEC((char *)"type", type_opts, CFGF_MULTI | CFGF_TITLE),
  34. CFG_STR((char *)"logfile", 0, CFGF_NONE),
  35. CFG_INT((char *)"loglevel", 7, CFGF_NONE),
  36. CFG_INT((char *)"serverdelay", 0, CFGF_NONE),
  37. CFG_END()
  38. };
  39. typedef struct import_item_t
  40. {
  41. int imported;
  42. char filename[256];
  43. struct import_item_t * next;
  44. } import_item_t;
  45. struct import_item_t * import_list;
  46. typedef struct measurement_t
  47. {
  48. unsigned long unit_id;
  49. unsigned int sensor_id;
  50. time_t time;
  51. double value;
  52. struct measurement_t * next;
  53. } measurement_t;
  54. struct measurement_t * measurement_list;
  55. static CURL *curl_g;
  56. static cfg_t * cfg;
  57. static void (*feederLog) (int priority, const char *fmt, ...);
  58. int setLog(void *func)
  59. {
  60. feederLog = func;
  61. return 0;
  62. }
  63. void addToImport(char * filename)
  64. {
  65. struct import_item_t * item, * last_item;
  66. item = malloc(sizeof(struct import_item_t));
  67. if (item)
  68. {
  69. strncpy(item->filename, filename, 255);
  70. item->imported = 0;
  71. item->next = NULL;
  72. if (import_list == NULL)
  73. import_list = item;
  74. else
  75. {
  76. last_item = import_list;
  77. while (last_item->next != NULL)
  78. last_item = last_item->next;
  79. last_item->next = item;
  80. }
  81. }
  82. }
  83. void freeImportList(void)
  84. {
  85. struct import_item_t * delete_item, * last_item;
  86. if (import_list == NULL)
  87. return;
  88. last_item = import_list;
  89. while (last_item->next != NULL)
  90. {
  91. delete_item = last_item;
  92. free(delete_item);
  93. last_item = last_item->next;
  94. }
  95. free(last_item);
  96. import_list = NULL;
  97. }
  98. static void parseListLine(char * filename)
  99. {
  100. if (strstr(filename, "imported") == NULL)
  101. {
  102. feederLog(LOG_DEBUG, "Adding file to be imported: %s\n", filename);
  103. addToImport(filename);
  104. }
  105. }
  106. static size_t ftpWriteList(void * buffer, size_t size, size_t nmemb, void * data)
  107. {
  108. static char filename[256];
  109. static size_t pos = 0;
  110. size_t i;
  111. void (* line_func)(char *);
  112. line_func = data;
  113. for (i = 0; i < size * nmemb; i++)
  114. {
  115. if (((char *)buffer)[i] == '\n')
  116. {
  117. filename[pos] = '\0';
  118. if (data != NULL)
  119. line_func(filename);
  120. pos = 0;
  121. }
  122. else
  123. filename[pos++] = ((char *)buffer)[i];
  124. }
  125. // printf("ftpWrite()\n");
  126. // printf("Data readen: \n%s \n", (char *)buffer);
  127. return nmemb * size;
  128. }
  129. void addMeasurement(unsigned long unit_id, unsigned long sensor_id, time_t tp, double value)
  130. {
  131. struct measurement_t * measurement, * last_measurement;
  132. measurement = malloc(sizeof(struct measurement_t));
  133. if (measurement)
  134. {
  135. measurement->unit_id = unit_id;
  136. measurement->sensor_id = sensor_id;
  137. measurement->time = tp;
  138. measurement->value = value;
  139. measurement->next = NULL;
  140. if (measurement_list == NULL)
  141. measurement_list = measurement;
  142. else
  143. {
  144. last_measurement = measurement_list; //todo: optimalizaci (pamatovat si posledniho clena), aby nemusel vzdycky prochazet celej list
  145. while (last_measurement->next != NULL)
  146. last_measurement = last_measurement->next;
  147. last_measurement->next = measurement;
  148. }
  149. }
  150. }
  151. typedef struct sensor_id_table_t
  152. {
  153. unsigned long unit_id;
  154. unsigned long sensor_id[32];
  155. } sensor_id_table_t;
  156. struct sensor_id_table_t sensor_id_table[] = //todo: ono se to meni podle jednotky a zapojeni, takze by to chtelo do konfiguraku, ktery si tenhle precte
  157. {
  158. {
  159. 20458,
  160. {000000000, 540070001, 450100001, 340290000,
  161. 540070002, 450100002, 340300001, 340300002,
  162. 540080001, 540080002, 540080003, 000000000,
  163. 000000000, 450110001, 450110002, 340310000,
  164. 360190000}
  165. },
  166. {
  167. 20454,
  168. {000000000, 540070000, 450100000, 340290000,
  169. 340320000, 480050000, 480060000, 000000000,
  170. 000000000, 000000000, 000000000, 000000000,
  171. 000000000, 000000000, 000000000, 000000000,
  172. 360190000}
  173. },
  174. {
  175. 0,
  176. {00000}
  177. }
  178. };
  179. static void parseFileLine(char * line)
  180. {
  181. char * sep, * s;
  182. int i;
  183. unsigned long id;
  184. unsigned int ack_channel;
  185. time_t ack_time;
  186. double value;
  187. struct tm ack_time_tm;
  188. sensor_id_table_t * sid;
  189. if (line[0] == '#') //this is comment line, skip it
  190. return;
  191. s = line;
  192. i = 0;
  193. ack_time = 0;
  194. ack_channel = 0;
  195. id = 0;
  196. while (sep = strchr(s, '\t'), sep != NULL)
  197. {
  198. *sep = 0;
  199. switch (i)
  200. {
  201. case 0:
  202. id = atol(s);
  203. break;
  204. case 1:
  205. ack_channel = atoi(s);
  206. break;
  207. case 2:
  208. sscanf(s, "%d-%d-%d %d:%d:%d", &ack_time_tm.tm_year, &ack_time_tm.tm_mon, &ack_time_tm.tm_mday, &ack_time_tm.tm_hour, &ack_time_tm.tm_min, &ack_time_tm.tm_sec);
  209. ack_time_tm.tm_year -= 1900;
  210. ack_time_tm.tm_mon -= 1;
  211. ack_time = mktime(&ack_time_tm);
  212. break;
  213. }
  214. s = sep + 1;
  215. i++;
  216. }
  217. sep = strchr(s, ',');
  218. if (sep != NULL)
  219. *sep = '.';
  220. value = atof(s);
  221. sid = &sensor_id_table[0];
  222. while (sid->unit_id != 0)
  223. {
  224. if (sid->unit_id == id)
  225. {
  226. if (sid->sensor_id[ack_channel] != 0)
  227. addMeasurement(id, sid->sensor_id[ack_channel], ack_time, value);
  228. }
  229. sid++;
  230. }
  231. /* if (sensor_id_table[ack_channel] != 0)
  232. addMeasurement(id, sensor_id_table[ack_channel] * 10000, ack_time, value);
  233. */
  234. // printf("Line: id: %ld, channel: %d, time: %ld, value: %0.2f\n", id, ack_channel, ack_time, value);
  235. }
  236. CURLcode ftpImport(CURL * curl, char * path)
  237. {
  238. CURLcode res;
  239. struct import_item_t * item;
  240. char * full_filename;
  241. res = 0;
  242. curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)parseFileLine);
  243. item = import_list;
  244. while (item != NULL)
  245. {
  246. full_filename = malloc(strlen(path) + strlen(item->filename) + 1);
  247. strcpy(full_filename, path);
  248. strcpy(full_filename + strlen(full_filename), item->filename);
  249. curl_easy_setopt(curl, CURLOPT_URL, full_filename);
  250. res = curl_easy_perform(curl);
  251. free(full_filename);
  252. item->imported = 1;
  253. item = item->next;
  254. }
  255. curl_easy_setopt(curl, CURLOPT_URL, path);
  256. return res;
  257. }
  258. CURLcode ftpMarkImported(CURL * curl)
  259. {
  260. struct import_item_t * item;
  261. char req[512];
  262. char * temp;
  263. CURLcode res;
  264. curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, NULL);
  265. curl_easy_setopt(curl, CURLOPT_WRITEDATA, NULL);
  266. item = import_list;
  267. while (item != NULL)
  268. {
  269. if (item->imported)
  270. {
  271. sprintf(req, "RNFR %s", item->filename);
  272. curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, req);
  273. res = curl_easy_perform(curl);
  274. if (res != 0)
  275. feederLog(LOG_ERR, "fiedler: curl RNFR error %d\n", res);
  276. else
  277. feederLog(LOG_DEBUG, "fiedler: Renamed from %s\n", item->filename);
  278. temp = strchr(item->filename, '.');
  279. temp[0] = 0;
  280. strcpy(item->filename + strlen(item->filename), "_imported.txt");
  281. sprintf(req, "RNTO %s", item->filename);
  282. curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, req);
  283. res = curl_easy_perform(curl);
  284. if (res != 0)
  285. feederLog(LOG_ERR, "fiedler: curl RNTO error %d\n", res);
  286. else
  287. feederLog(LOG_DEBUG, "fiedler: Renamed to %s\n", item->filename);
  288. }
  289. item = item->next;
  290. }
  291. freeImportList();
  292. return 0;
  293. }
  294. CURLcode ftpList(CURL * curl)
  295. {
  296. curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, ftpWriteList);
  297. curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)parseListLine);
  298. curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
  299. curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "NLST");
  300. return curl_easy_perform(curl);
  301. }
  302. unsigned int timeout(void * lib_data, int sock, unsigned char * data)
  303. {
  304. CURLcode res;
  305. char path[1024];
  306. if (curl_g)
  307. {
  308. strcpy(path, "ftp://fiedler:Yeen0iFa@test.sensors.lesprojekt.cz/DATA_DEHTARE/");
  309. // strcpy(path, "ftp://fiedler:Yeen0iFa@test.sensors.lesprojekt.cz/temp/");
  310. curl_easy_setopt(curl_g, CURLOPT_URL, path);
  311. res = ftpList(curl_g);
  312. if (import_list == NULL)
  313. feederLog(LOG_DEBUG, "fiedler: Nothing to import\n");
  314. res = ftpImport(curl_g, path);
  315. res = ftpMarkImported(curl_g);
  316. }
  317. return 0;
  318. }
  319. unsigned int
  320. process(void *lib_data, int sock, unsigned char *data,
  321. unsigned int length, unsigned long long int *id, time_t * tm,
  322. double *result_array, uint64_t * sensors, unsigned int *type)
  323. {
  324. struct measurement_t * measurement;
  325. if (measurement_list == NULL)
  326. return 0;
  327. measurement = measurement_list;
  328. result_array[0] = measurement->value;
  329. sensors[0] = measurement->sensor_id;
  330. *id = measurement->unit_id;
  331. *tm = measurement->time;
  332. *type = VALUES_TYPE_OBS;
  333. measurement_list = measurement->next;
  334. free(measurement);
  335. return 1;
  336. }
  337. int curlDebug(CURL * curl, curl_infotype info, char * string, size_t size, void * data)
  338. {
  339. if (feederLog != NULL)
  340. {
  341. if (info == CURLINFO_TEXT)
  342. {
  343. string[size] = 0;
  344. feederLog(LOG_DEBUG, "fiedler: %s", string);
  345. }
  346. }
  347. return 0;
  348. }
  349. int init(void *param)
  350. {
  351. feederLog = NULL;
  352. param = param;
  353. cfg = cfg_init(cfg_opts, CFGF_NONE);
  354. if (cfg_parse(cfg, "feeder.conf") != CFG_SUCCESS)
  355. {
  356. return -1;
  357. }
  358. curl_global_init(CURL_GLOBAL_DEFAULT);
  359. // printf("Curl version: %s \n", curl_version());
  360. curl_g = curl_easy_init();
  361. curl_easy_setopt(curl_g, CURLOPT_DEBUGFUNCTION, curlDebug);
  362. import_list = 0;
  363. measurement_list = 0;
  364. return 0;
  365. }
  366. /*
  367. to test only:
  368. int main(void)
  369. {
  370. unsigned long long int id;
  371. time_t tp;
  372. double values[64];
  373. uint64_t sensors[64];
  374. unsigned int data_type;
  375. // timeout(NULL, 0, NULL);
  376. while (process(NULL, 0, NULL, 0, &id, &tp, values, sensors, &data_type) > 0)
  377. {
  378. printf("Zaznam: id %lld, %lld, %f\n", id, sensors[0], values[0]);
  379. }
  380. return 0;
  381. }
  382. */