updated micodus_server
This commit is contained in:
parent
d5314c6745
commit
4ecd6f42e0
41
src/db.rs
41
src/db.rs
@ -2,11 +2,39 @@ use rusqlite::*;
|
|||||||
|
|
||||||
const DBPATH: &'static str = "data/tracker.db";
|
const DBPATH: &'static str = "data/tracker.db";
|
||||||
const STATEMENTS: [&str; 2] = [
|
const STATEMENTS: [&str; 2] = [
|
||||||
"CREATE TABLE log (id integer primary key autoincrement, time text, latitude float, longitude float, speed integer, height integer, direction integer);",
|
"
|
||||||
"CREATE INDEX idx_time on log (time);",];
|
CREATE TABLE log (
|
||||||
const QUERY_INSERT: &'static str =
|
id integer primary key autoincrement,
|
||||||
"INSERT INTO log (time, latitude, longitude, speed, height, direction)
|
time text,
|
||||||
VALUES (:time, :latitude, :longitude, :speed, :height, :direction)";
|
serial integer,
|
||||||
|
latitude float,
|
||||||
|
longitude float,
|
||||||
|
speed integer,
|
||||||
|
height integer,
|
||||||
|
direction integer);",
|
||||||
|
"
|
||||||
|
CREATE INDEX idx_time
|
||||||
|
ON log (time);",
|
||||||
|
];
|
||||||
|
const QUERY_INSERT: &'static str = "
|
||||||
|
INSERT INTO log (
|
||||||
|
time,
|
||||||
|
serial,
|
||||||
|
latitude,
|
||||||
|
longitude,
|
||||||
|
speed,
|
||||||
|
height,
|
||||||
|
direction
|
||||||
|
)
|
||||||
|
VALUES (
|
||||||
|
:time,
|
||||||
|
:serial,
|
||||||
|
:latitude,
|
||||||
|
:longitude,
|
||||||
|
:speed,
|
||||||
|
:height,
|
||||||
|
:direction
|
||||||
|
)";
|
||||||
|
|
||||||
pub fn connectdb() -> Result<Connection> {
|
pub fn connectdb() -> Result<Connection> {
|
||||||
let conn = Connection::open(DBPATH)?;
|
let conn = Connection::open(DBPATH)?;
|
||||||
@ -36,6 +64,7 @@ fn set_pragmas(conn: &Connection) -> Result<()> {
|
|||||||
|
|
||||||
pub fn insert(
|
pub fn insert(
|
||||||
conn: &Connection,
|
conn: &Connection,
|
||||||
|
serial: &u16,
|
||||||
time: &String,
|
time: &String,
|
||||||
latitude: &f64,
|
latitude: &f64,
|
||||||
longitude: &f64,
|
longitude: &f64,
|
||||||
@ -45,7 +74,7 @@ pub fn insert(
|
|||||||
) -> Result<()> {
|
) -> Result<()> {
|
||||||
match conn.execute(
|
match conn.execute(
|
||||||
QUERY_INSERT,
|
QUERY_INSERT,
|
||||||
params![time, latitude, longitude, speed, height, direction],
|
params![time, serial, latitude, longitude, speed, height, direction],
|
||||||
) {
|
) {
|
||||||
Ok(inserted) => println!("{} rows were inserted", inserted),
|
Ok(inserted) => println!("{} rows were inserted", inserted),
|
||||||
Err(err) => println!("insert failed: {}", err),
|
Err(err) => println!("insert failed: {}", err),
|
||||||
|
@ -26,11 +26,11 @@ pub struct MessageHeader {
|
|||||||
|
|
||||||
impl MessageHeader {
|
impl MessageHeader {
|
||||||
pub fn build(&mut self, id: u16, properties: usize, raw_terminal_id: [u8; 6]) {
|
pub fn build(&mut self, id: u16, properties: usize, raw_terminal_id: [u8; 6]) {
|
||||||
let mut rng = rand::thread_rng();
|
let mut rng = rand::rng();
|
||||||
self.id = id;
|
self.id = id;
|
||||||
self.properties = properties as u16;
|
self.properties = properties as u16;
|
||||||
self.raw_terminal_id = raw_terminal_id;
|
self.raw_terminal_id = raw_terminal_id;
|
||||||
self.serial_number = rng.gen();
|
self.serial_number = rng.random();
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_id(&mut self) -> u16 {
|
pub fn get_id(&mut self) -> u16 {
|
||||||
|
@ -325,6 +325,7 @@ impl Message {
|
|||||||
let conn = connectdb().unwrap();
|
let conn = connectdb().unwrap();
|
||||||
insert(
|
insert(
|
||||||
&conn,
|
&conn,
|
||||||
|
&inmsg.header.serial_number,
|
||||||
&t.time.format("%Y-%m-%d %H:%M:%S").to_string(),
|
&t.time.format("%Y-%m-%d %H:%M:%S").to_string(),
|
||||||
&t.latitude,
|
&t.latitude,
|
||||||
&t.longitude,
|
&t.longitude,
|
||||||
|
Loading…
Reference in New Issue
Block a user