sensorts.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*
  2. voltlogger_analyzer
  3. Copyright (C) 2015 Dmitry Yu Okunev <dyokunev@ut.mephi.ru> 0x8E30679C
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. #define _GNU_SOURCE
  16. #include "configuration.h"
  17. #include "macros.h"
  18. #include <stdio.h> /* getline() */
  19. #include <stdlib.h> /* atof() */
  20. #include <unistd.h> /* getopt() */
  21. #include <math.h>
  22. #include <assert.h>
  23. #include "error.h"
  24. #include "binary.h"
  25. #include "analyzer.h"
  26. int main(int argc, char *argv[]) {
  27. char realtime = 0;
  28. double clockscale = 1;
  29. double padding = 0;
  30. FILE *i_f = stdin, *o_f = stdout;
  31. int channelsNum = 1;
  32. // Initializing output subsystem
  33. {
  34. int output_method = OM_STDERR;
  35. int output_quiet = 0;
  36. int output_verbosity = 9;
  37. int output_debuglevel = 9;
  38. error_init(&output_method, &output_quiet, &output_verbosity, &output_debuglevel);
  39. }
  40. // Parsing arguments
  41. char c;
  42. while ((c = getopt (argc, argv, "ri:N:s:p:")) != -1) {
  43. char *arg;
  44. arg = optarg;
  45. switch (c)
  46. {
  47. case 'r':
  48. realtime = 1;
  49. break;
  50. case 'i':
  51. assert ((i_f = fopen(arg, "r")) != NULL);
  52. break;
  53. case 's':
  54. clockscale = atof(optarg);
  55. break;
  56. case 'N':
  57. channelsNum = atoi(optarg);
  58. break;
  59. case 'p':
  60. padding = atof(optarg);
  61. break;
  62. default:
  63. abort ();
  64. }
  65. }
  66. history_item_t history_item;
  67. static uint64_t sensorTS_prev = 0, sensorTS_prev_prev = 0, unixTSNano_prev;
  68. double sensorTSdeviation = 0;
  69. while (!feof(i_f)) {
  70. while(1) {
  71. long pos = ftell(i_f);
  72. history_item.row.unixTSNano = get_uint64(i_f, realtime);
  73. if (unixTSNano_prev != 0 && llabs((int64_t)history_item.row.unixTSNano - (int64_t)unixTSNano_prev) > 1E9*1E8) {
  74. //fprintf(stderr, "Wrong unixTSNano\n");
  75. exit(-1);
  76. fseek(i_f, pos+1, SEEK_SET);
  77. continue;
  78. }
  79. unixTSNano_prev = history_item.row.unixTSNano;
  80. break;
  81. }
  82. history_item.row.sensorTS = get_uint64(i_f, realtime);
  83. {
  84. int i = 0;
  85. while (i < channelsNum)
  86. history_item.row.value[i++] = get_uint32(i_f, realtime);
  87. }
  88. if (sensorTS_prev - sensorTS_prev_prev > 0)
  89. if ((history_item.row.sensorTS - sensorTS_prev) / (sensorTS_prev - sensorTS_prev_prev) > 10) {
  90. sensorTSdeviation += padding;
  91. sensorTSdeviation -= history_item.row.sensorTS - sensorTS_prev;
  92. }
  93. sensorTS_prev_prev = sensorTS_prev;
  94. sensorTS_prev = history_item.row.sensorTS;
  95. history_item.row.sensorTS += sensorTSdeviation;
  96. history_item.row.unixTSNano = 1E9*1E8 + (double)history_item.row.sensorTS * clockscale;
  97. out_uint64(o_f, history_item.row.unixTSNano);
  98. out_uint64(o_f, history_item.row.sensorTS);
  99. {
  100. int i = 0;
  101. while (i < channelsNum) {
  102. out_uint32(o_f, history_item.row.value[i++]);
  103. }
  104. }
  105. }
  106. return 0;
  107. }