mortlog.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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 "../../status.h"
  8. #include "../../feeder.h"
  9. #define __USE_GNU
  10. /*
  11. typedef struct measurement_t
  12. {
  13. unsigned long unit_id;
  14. unsigned int sensor_id;
  15. time_t time;
  16. double value;
  17. struct measurement_t * next;
  18. } measurement_t;
  19. struct measurement_t * measurement_list;
  20. */
  21. static void (*feederLog) (int priority, const char *fmt, ...);
  22. int setLog(void *func)
  23. {
  24. feederLog = func;
  25. return 0;
  26. }
  27. static FILE * f = NULL;
  28. unsigned int timeout(void * lib_data, int sock, unsigned char * data)
  29. {
  30. (void)lib_data;
  31. (void)sock;
  32. (void)data;
  33. f = fopen("mort_log.txt", "r");
  34. if (f == NULL)
  35. {
  36. feederLog(LOG_ERR, "mortlog: can not open input file (mort_log.txt)\n");
  37. return -1;
  38. }
  39. return 0;
  40. }
  41. int getval(char * line, const char * name, char * val)
  42. {
  43. char * s, * se;
  44. s = strstr(line, name);
  45. s = strchr(s, ' ');
  46. s++;
  47. se = strchr(s, ',');
  48. memcpy(val, s, se - s);
  49. val[se-s] = 0;
  50. feederLog(LOG_DEBUG, "Value %s:%s\n", name, val);
  51. return 0;
  52. }
  53. unsigned int
  54. process(void *lib_data, int sock, unsigned char *data,
  55. unsigned int length, unsigned long long int *id, time_t * tm,
  56. double *result_array, uint64_t * sensors, unsigned int *type)
  57. {
  58. unsigned int res;
  59. static size_t len = 0;
  60. static char * line = NULL;
  61. ssize_t size;
  62. char name[256];
  63. (void)lib_data;
  64. (void)sock;
  65. (void)data;
  66. (void)length;
  67. feederLog(LOG_DEBUG, "mortlog: process\n");
  68. if (f == NULL)
  69. {
  70. feederLog(LOG_ERR, "mortlog: file not opened\n");
  71. return -1;
  72. }
  73. while (size = getline(&line, &len, f), size >= 0)
  74. {
  75. if (strstr(line, "Saving sensor") != NULL)
  76. {
  77. feederLog(LOG_DEBUG, "mortlog: %s\n", line);
  78. getval(line, "id", name);
  79. sensors[0] = atoi(name);
  80. getval(line, "sensor", name);
  81. feederLog(LOG_DEBUG, "mortlog: sensor %s\n", name);
  82. getval(line, "timestamp", name);
  83. *tm = atoi(name);
  84. getval(line, "value", name);
  85. result_array[0] = atof(name);
  86. getval(line, "device", name);
  87. sensors[0] += (atoi(name) >> 8);
  88. *id = 105000000 + (atoi(name) & 0xFF);//todo: id must be configured by conf file, its not part of log file
  89. // feederLog(LOG_DEBUG, "mortlog: readline %f\n", result_array[0]);
  90. *type = VALUES_TYPE_OBS;
  91. return 1;
  92. }
  93. }
  94. fclose(f);
  95. res = 0;
  96. return res;
  97. }
  98. int init(void *param)
  99. {
  100. (void)param;
  101. feederLog(LOG_DEBUG, "mortlog: init\n");
  102. return 0;
  103. }