forked from kristrev/data-exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
metadata_writer_inventory_gps.c
146 lines (122 loc) · 4.98 KB
/
metadata_writer_inventory_gps.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/* Copyright (c) 2015, Celerway, Kristian Evensen <[email protected]>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include <stdint.h>
#include <string.h>
#include <sqlite3.h>
#include <sys/time.h>
#include "metadata_exporter.h"
#include "metadata_writer_inventory_gps.h"
#include "metadata_writer_sqlite_helpers.h"
#include "metadata_writer_json_helpers.h"
#include "metadata_exporter_log.h"
static uint8_t md_inventory_gps_dump_db_sql(struct md_writer_sqlite *mws, FILE *output)
{
sqlite3_reset(mws->dump_gps);
if (md_sqlite_helpers_dump_write(mws->dump_gps, output))
return RETVAL_FAILURE;
else
return RETVAL_SUCCESS;
}
static uint8_t md_inventory_gps_dump_db_json(struct md_writer_sqlite *mws, FILE *output)
{
const char *json_str;
sqlite3_reset(mws->dump_gps);
json_object *jarray = json_object_new_array();
if (md_json_helpers_dump_write(mws->dump_gps, jarray))
{
json_object_put(jarray);
return RETVAL_FAILURE;
}
json_str = json_object_to_json_string_ext(jarray, JSON_C_TO_STRING_PLAIN);
fprintf(output, "%s", json_str);
json_object_put(jarray);
return RETVAL_SUCCESS;
}
uint8_t md_inventory_gps_copy_db(struct md_writer_sqlite *mws)
{
uint8_t retval = RETVAL_SUCCESS;
dump_db_cb dump_cb = NULL;
if (mws->output_format == FORMAT_SQL) {
dump_cb = md_inventory_gps_dump_db_sql;
} else {
dump_cb = md_inventory_gps_dump_db_json;
}
retval = md_writer_helpers_copy_db(mws->gps_prefix,
mws->gps_prefix_len, dump_cb, mws,
NULL);
if (retval == RETVAL_SUCCESS)
mws->num_gps_events = 0;
return retval;
}
uint8_t md_inventory_handle_gps_event(struct md_writer_sqlite *mws,
struct md_gps_event *mge)
{
if (mge->speed)
mws->gps_speed = mge->speed;
if (mge->minmea_id == MINMEA_SENTENCE_RMC)
return RETVAL_IGNORE;
//We dont need EVERY gps event, some devices send updates very frequently
//Some of the devices we work with have timers that are ... strange
if (mws->last_gps_insert > mge->tstamp_tv.tv_sec ||
mge->tstamp_tv.tv_sec - mws->last_gps_insert < GPS_EVENT_INTVL)
return RETVAL_IGNORE;
sqlite3_stmt *stmt = mws->insert_gps;
sqlite3_clear_bindings(stmt);
sqlite3_reset(stmt);
if (sqlite3_bind_int(stmt, 1, mws->node_id) ||
sqlite3_bind_int(stmt, 2, mws->session_id) || // BootCount
sqlite3_bind_int(stmt, 3, mws->session_id_multip) || // BootMultiplier
sqlite3_bind_int(stmt, 4, mge->tstamp_tv.tv_sec) ||
sqlite3_bind_int(stmt, 5, mge->sequence) ||
sqlite3_bind_int(stmt, 6, mge->md_type) ||
sqlite3_bind_int(stmt, 7, 0) ||
sqlite3_bind_double(stmt, 8, mge->latitude) ||
sqlite3_bind_double(stmt, 9, mge->longitude)) {
META_PRINT_SYSLOG(mws->parent, LOG_ERR, "Failed to bind values to INSERT query (GPS)\n");
return RETVAL_FAILURE;
}
if (mge->altitude &&
sqlite3_bind_double(stmt, 10, mge->altitude)) {
META_PRINT_SYSLOG(mws->parent, LOG_ERR, "Failed to bind altitude\n");
return RETVAL_FAILURE;
}
if (mws->gps_speed &&
sqlite3_bind_double(stmt, 11, mws->gps_speed)) {
META_PRINT_SYSLOG(mws->parent, LOG_ERR, "Failed to bind speed\n");
return RETVAL_FAILURE;
}
if (mge->satellites_tracked &&
sqlite3_bind_int(stmt, 12, mge->satellites_tracked)) {
META_PRINT_SYSLOG(mws->parent, LOG_ERR, "Failed to bind num. satelites\n");
return RETVAL_FAILURE;
}
if (sqlite3_step(stmt) != SQLITE_DONE) {
return RETVAL_FAILURE;
} else {
mws->last_gps_insert = mge->tstamp_tv.tv_sec;
return RETVAL_SUCCESS;
}
}