+#include "utils.h"
+
+struct owparser_state {
+ double prev_delta[20];
+ double prev_data;
+};
+
+static struct owparser_state *allocate_parser_state(const char **datastr)
+{
+ int i;
+
+ /* Count how many sensor entries we need */
+ for (i = 0; datastr[i]; i++)
+ ;
+
+ /* The first entry belongs to server address or mount point */
+ i--;
+
+ return calloc(sizeof(struct owparser_state), i);
+}
+
+static int might_be_glitch(double data, const struct owparser_state *s)
+{
+ double max_delta = 0, delta;
+ int i;
+
+ for (i = 0; i < ARRAY_SIZE(s->prev_delta); i++)
+ max_delta = max(s->prev_delta[i], max_delta);
+
+ /* Probably no enough data yet, so no glitch detection */
+ if (!max_delta)
+ return 0;
+
+ /*
+ * Simple glitch detection. If delta to previous value is more
+ * than twice as larger as some older delta, we might have a
+ * glit
+ */
+ delta = fabs(data - s->prev_data);
+ return delta > max_delta * 2;
+}
+
+static void update_glitch_data(double data, struct owparser_state *s)
+{
+ double max_delta = 0;
+ int i;
+
+ for (i = 1; i < ARRAY_SIZE(s->prev_delta); i++) {
+ s->prev_delta[i - 1] = s->prev_delta[i];
+ max_delta = max(s->prev_delta[i], max_delta);
+ }