updated micodus_server

This commit is contained in:
Paul 2025-02-09 18:10:03 +01:00
parent d5314c6745
commit 4ecd6f42e0
3 changed files with 38 additions and 8 deletions

View File

@ -2,11 +2,39 @@ use rusqlite::*;
const DBPATH: &'static str = "data/tracker.db";
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);",];
const QUERY_INSERT: &'static str =
"INSERT INTO log (time, latitude, longitude, speed, height, direction)
VALUES (:time, :latitude, :longitude, :speed, :height, :direction)";
"
CREATE TABLE log (
id integer primary key autoincrement,
time text,
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> {
let conn = Connection::open(DBPATH)?;
@ -36,6 +64,7 @@ fn set_pragmas(conn: &Connection) -> Result<()> {
pub fn insert(
conn: &Connection,
serial: &u16,
time: &String,
latitude: &f64,
longitude: &f64,
@ -45,7 +74,7 @@ pub fn insert(
) -> Result<()> {
match conn.execute(
QUERY_INSERT,
params![time, latitude, longitude, speed, height, direction],
params![time, serial, latitude, longitude, speed, height, direction],
) {
Ok(inserted) => println!("{} rows were inserted", inserted),
Err(err) => println!("insert failed: {}", err),

View File

@ -26,11 +26,11 @@ pub struct MessageHeader {
impl MessageHeader {
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.properties = properties as u16;
self.raw_terminal_id = raw_terminal_id;
self.serial_number = rng.gen();
self.serial_number = rng.random();
}
pub fn get_id(&mut self) -> u16 {

View File

@ -325,6 +325,7 @@ impl Message {
let conn = connectdb().unwrap();
insert(
&conn,
&inmsg.header.serial_number,
&t.time.format("%Y-%m-%d %H:%M:%S").to_string(),
&t.latitude,
&t.longitude,