Initial commit

Signed-off-by: Thomas Klaehn <thomas.klaehn@perinet.io>
This commit is contained in:
Thomas Klaehn 2025-01-20 16:18:28 +01:00
commit d16bd7e7f8
9 changed files with 837 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
/target

45
.vscode/launch.json vendored Normal file
View File

@ -0,0 +1,45 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "lldb",
"request": "launch",
"name": "Debug executable 'cvs_monitor'",
"cargo": {
"args": [
"build",
"--bin=cvs_monitor",
"--package=cvs_monitor"
],
"filter": {
"name": "cvs_monitor",
"kind": "bin"
}
},
"args": [],
"cwd": "${workspaceFolder}"
},
{
"type": "lldb",
"request": "launch",
"name": "Debug unit tests in executable 'cvs_monitor'",
"cargo": {
"args": [
"test",
"--no-run",
"--bin=cvs_monitor",
"--package=cvs_monitor"
],
"filter": {
"name": "cvs_monitor",
"kind": "bin"
}
},
"args": [],
"cwd": "${workspaceFolder}"
}
]
}

529
Cargo.lock generated Normal file
View File

@ -0,0 +1,529 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 4
[[package]]
name = "ahash"
version = "0.8.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e89da841a80418a9b391ebaea17f5c112ffaaa96f621d2c285b5174da76b9011"
dependencies = [
"cfg-if",
"once_cell",
"version_check",
"zerocopy",
]
[[package]]
name = "allocator-api2"
version = "0.2.21"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "683d7910e743518b0e34f1186f92494becacb047c7b6bf616c96772180fef923"
[[package]]
name = "android-tzdata"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e999941b234f3131b00bc13c22d06e8c5ff726d1b6318ac7eb276997bbb4fef0"
[[package]]
name = "android_system_properties"
version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "819e7219dbd41043ac279b19830f2efc897156490d7fd6ea916720117ee66311"
dependencies = [
"libc",
]
[[package]]
name = "autocfg"
version = "1.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ace50bade8e6234aa140d9a2f552bbee1db4d353f69b8217bc503490fc1a9f26"
[[package]]
name = "bitflags"
version = "1.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a"
[[package]]
name = "bitflags"
version = "2.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8f68f53c83ab957f72c32642f3868eec03eb974d1fb82e453128456482613d36"
[[package]]
name = "bitvec"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1bc2832c24239b0141d5674bb9174f9d68a8b5b3f2753311927c172ca46f7e9c"
dependencies = [
"funty",
"radium",
"tap",
"wyz",
]
[[package]]
name = "bumpalo"
version = "3.16.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "79296716171880943b8470b5f8d03aa55eb2e645a4874bdbb28adb49162e012c"
[[package]]
name = "byteorder"
version = "1.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b"
[[package]]
name = "cc"
version = "1.2.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c8293772165d9345bdaaa39b45b2109591e63fe5e6fbc23c6ff930a048aa310b"
dependencies = [
"shlex",
]
[[package]]
name = "cfg-if"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "chrono"
version = "0.4.39"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7e36cc9d416881d2e24f9a963be5fb1cd90966419ac844274161d10488b3e825"
dependencies = [
"android-tzdata",
"iana-time-zone",
"js-sys",
"num-traits",
"wasm-bindgen",
"windows-targets",
]
[[package]]
name = "core-foundation-sys"
version = "0.8.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "773648b94d0e5d620f64f280777445740e61fe701025087ec8b57f45c791888b"
[[package]]
name = "cvs-monitor"
version = "0.1.0"
dependencies = [
"chrono",
"gps_parser",
"ina3221",
"serde",
"serde_json",
]
[[package]]
name = "funty"
version = "2.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e6d5a32815ae3f33302d95fdcb2ce17862f8c65363dcfd29360480ba1001fc9c"
[[package]]
name = "gps_parser"
version = "0.3.1"
source = "git+https://git.blackfinn.de/rust/gps_parser.git#5eda664a47e0d3a0537de9f529a7704b790a4127"
dependencies = [
"chrono",
"nmea-parser",
]
[[package]]
name = "hashbrown"
version = "0.14.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e5274423e17b7c9fc20b6e7e208532f9b19825d82dfd615708b70edd83df41f1"
dependencies = [
"ahash",
"allocator-api2",
]
[[package]]
name = "i2cdev"
version = "0.6.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a01f563561b53e2def844167c92984da13779a53ca409e948a03400633cb8228"
dependencies = [
"bitflags 2.8.0",
"byteorder",
"libc",
"nix",
]
[[package]]
name = "iana-time-zone"
version = "0.1.61"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "235e081f3925a06703c2d0117ea8b91f042756fd6e7a6e5d901e8ca1a996b220"
dependencies = [
"android_system_properties",
"core-foundation-sys",
"iana-time-zone-haiku",
"js-sys",
"wasm-bindgen",
"windows-core",
]
[[package]]
name = "iana-time-zone-haiku"
version = "0.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f31827a206f56af32e590ba56d5d2d085f558508192593743f16b2306495269f"
dependencies = [
"cc",
]
[[package]]
name = "ina3221"
version = "0.2.0"
source = "git+https://git.blackfinn.de/rust/ina3221.git#acc6fa2ac187363332741591382bc0579632f213"
dependencies = [
"i2cdev",
]
[[package]]
name = "itoa"
version = "1.0.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d75a2a4b1b190afb6f5425f10f6a8f959d2ea0b9c2b1d79553551850539e4674"
[[package]]
name = "js-sys"
version = "0.3.77"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1cfaf33c695fc6e08064efbc1f72ec937429614f25eef83af942d0e227c3a28f"
dependencies = [
"once_cell",
"wasm-bindgen",
]
[[package]]
name = "libc"
version = "0.2.169"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b5aba8db14291edd000dfcc4d620c7ebfb122c613afb886ca8803fa4e128a20a"
[[package]]
name = "log"
version = "0.4.25"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "04cbf5b083de1c7e0222a7a51dbfdba1cbe1c6ab0b15e29fff3f6c077fd9cd9f"
[[package]]
name = "memchr"
version = "2.7.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3"
[[package]]
name = "nix"
version = "0.26.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "598beaf3cc6fdd9a5dfb1630c2800c7acd31df7aaf0f565796fba2b53ca1af1b"
dependencies = [
"bitflags 1.3.2",
"cfg-if",
"libc",
]
[[package]]
name = "nmea-parser"
version = "0.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1b20e62c685b0fc1e178a5ddede4084a2ba17c5ec02035c16e8042dc954f9083"
dependencies = [
"bitvec",
"chrono",
"hashbrown",
"log",
"num-traits",
"serde",
]
[[package]]
name = "num-traits"
version = "0.2.19"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841"
dependencies = [
"autocfg",
]
[[package]]
name = "once_cell"
version = "1.20.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1261fe7e33c73b354eab43b1273a57c8f967d0391e80353e51f764ac02cf6775"
[[package]]
name = "proc-macro2"
version = "1.0.93"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "60946a68e5f9d28b0dc1c21bb8a97ee7d018a8b322fa57838ba31cc878e22d99"
dependencies = [
"unicode-ident",
]
[[package]]
name = "quote"
version = "1.0.38"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0e4dccaaaf89514f546c693ddc140f729f958c247918a13380cccc6078391acc"
dependencies = [
"proc-macro2",
]
[[package]]
name = "radium"
version = "0.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dc33ff2d4973d518d823d61aa239014831e521c75da58e3df4840d3f47749d09"
[[package]]
name = "rustversion"
version = "1.0.19"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f7c45b9784283f1b2e7fb61b42047c2fd678ef0960d4f6f1eba131594cc369d4"
[[package]]
name = "ryu"
version = "1.0.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f3cb5ba0dc43242ce17de99c180e96db90b235b8a9fdc9543c96d2209116bd9f"
[[package]]
name = "serde"
version = "1.0.217"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "02fc4265df13d6fa1d00ecff087228cc0a2b5f3c0e87e258d8b94a156e984c70"
dependencies = [
"serde_derive",
]
[[package]]
name = "serde_derive"
version = "1.0.217"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5a9bf7cf98d04a2b28aead066b7496853d4779c9cc183c440dbac457641e19a0"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "serde_json"
version = "1.0.136"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "336a0c23cf42a38d9eaa7cd22c7040d04e1228a19a933890805ffd00a16437d2"
dependencies = [
"itoa",
"memchr",
"ryu",
"serde",
]
[[package]]
name = "shlex"
version = "1.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64"
[[package]]
name = "syn"
version = "2.0.96"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d5d0adab1ae378d7f53bdebc67a39f1f151407ef230f0ce2883572f5d8985c80"
dependencies = [
"proc-macro2",
"quote",
"unicode-ident",
]
[[package]]
name = "tap"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "55937e1799185b12863d447f42597ed69d9928686b8d88a1df17376a097d8369"
[[package]]
name = "unicode-ident"
version = "1.0.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "adb9e6ca4f869e1180728b7950e35922a7fc6397f7b641499e8f3ef06e50dc83"
[[package]]
name = "version_check"
version = "0.9.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0b928f33d975fc6ad9f86c8f283853ad26bdd5b10b7f1542aa2fa15e2289105a"
[[package]]
name = "wasm-bindgen"
version = "0.2.100"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1edc8929d7499fc4e8f0be2262a241556cfc54a0bea223790e71446f2aab1ef5"
dependencies = [
"cfg-if",
"once_cell",
"rustversion",
"wasm-bindgen-macro",
]
[[package]]
name = "wasm-bindgen-backend"
version = "0.2.100"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2f0a0651a5c2bc21487bde11ee802ccaf4c51935d0d3d42a6101f98161700bc6"
dependencies = [
"bumpalo",
"log",
"proc-macro2",
"quote",
"syn",
"wasm-bindgen-shared",
]
[[package]]
name = "wasm-bindgen-macro"
version = "0.2.100"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7fe63fc6d09ed3792bd0897b314f53de8e16568c2b3f7982f468c0bf9bd0b407"
dependencies = [
"quote",
"wasm-bindgen-macro-support",
]
[[package]]
name = "wasm-bindgen-macro-support"
version = "0.2.100"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8ae87ea40c9f689fc23f209965b6fb8a99ad69aeeb0231408be24920604395de"
dependencies = [
"proc-macro2",
"quote",
"syn",
"wasm-bindgen-backend",
"wasm-bindgen-shared",
]
[[package]]
name = "wasm-bindgen-shared"
version = "0.2.100"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1a05d73b933a847d6cccdda8f838a22ff101ad9bf93e33684f39c1f5f0eece3d"
dependencies = [
"unicode-ident",
]
[[package]]
name = "windows-core"
version = "0.52.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "33ab640c8d7e35bf8ba19b884ba838ceb4fba93a4e8c65a9059d08afcfc683d9"
dependencies = [
"windows-targets",
]
[[package]]
name = "windows-targets"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973"
dependencies = [
"windows_aarch64_gnullvm",
"windows_aarch64_msvc",
"windows_i686_gnu",
"windows_i686_gnullvm",
"windows_i686_msvc",
"windows_x86_64_gnu",
"windows_x86_64_gnullvm",
"windows_x86_64_msvc",
]
[[package]]
name = "windows_aarch64_gnullvm"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3"
[[package]]
name = "windows_aarch64_msvc"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469"
[[package]]
name = "windows_i686_gnu"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8e9b5ad5ab802e97eb8e295ac6720e509ee4c243f69d781394014ebfe8bbfa0b"
[[package]]
name = "windows_i686_gnullvm"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66"
[[package]]
name = "windows_i686_msvc"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66"
[[package]]
name = "windows_x86_64_gnu"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78"
[[package]]
name = "windows_x86_64_gnullvm"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d"
[[package]]
name = "windows_x86_64_msvc"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec"
[[package]]
name = "wyz"
version = "0.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "05f360fc0b24296329c78fda852a1e9ae82de9cf7b27dae4b7f62f118f77b9ed"
dependencies = [
"tap",
]
[[package]]
name = "zerocopy"
version = "0.7.35"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1b9b4fd18abc82b8136838da5d50bae7bdea537c574d8dc1a34ed098d6c166f0"
dependencies = [
"zerocopy-derive",
]
[[package]]
name = "zerocopy-derive"
version = "0.7.35"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fa4f8080344d4671fb4e831a13ad1e68092748387dfc4f55e356242fae12ce3e"
dependencies = [
"proc-macro2",
"quote",
"syn",
]

11
Cargo.toml Normal file
View File

@ -0,0 +1,11 @@
[package]
name = "cvs-monitor"
version = "0.1.0"
edition = "2021"
[dependencies]
chrono = "0.4"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
gps_parser = { version = "0.3.1", git = "https://git.blackfinn.de/rust/gps_parser.git" }
ina3221 = { version = "0.2.0", git = "https://git.blackfinn.de/rust/ina3221.git" }

48
Makefile Normal file
View File

@ -0,0 +1,48 @@
PROJECT_NAME := cvs-monitor
PREFIX ?= /usr/bin
CONFIG_DIR := /etc/$(PROJECT_NAME)
SYSTEM_DIR := /usr/lib/systemd/system
CONFIG_FILE := config/config.json
BIN_FILE := target/release/$(PROJECT_NAME)
UNIT_FILE := $(PROJECT_NAME).service
.PHONY: all
all: service
.PHONY: service
service:
cargo build -r
.PHONY: clean
clean:
cargo clean
.PHONY: install
install: all
# Config file
@if [ -f $(CONFIG_DIR)/$(notdir $(CONFIG_FILE)) ]; then \
echo "$(CONFIG_DIR)/$(notdir $(CONFIG_FILE)) already exists - skipping..."; \
else \
install -d $(CONFIG_DIR); \
install -m 0644 $(CONFIG_FILE) $(CONFIG_DIR); \
echo "install -d $(CONFIG_DIR)"; \
echo "install -m 0644 $(CONFIG_FILE) $(CONFIG_DIR)"; \
fi
# Binary
install -d $(PREFIX)
install -m 0755 $(BIN_FILE) $(PREFIX)
# System unit
install -d $(SYSTEM_DIR)
install -m 0644 $(UNIT_FILE) $(SYSTEM_DIR)
.PHONY: uninstall
uninstall:
rm -rf $(CONFIG_DIR)
rm -rf $(SYSTEM_DIR)/$(UNIT_FILE)
rm -rf $(PREFIX)/$(PROJECT_NAME)

36
Readme.md Normal file
View File

@ -0,0 +1,36 @@
# CVS Monitor
## Hardware setup
* Wheel circumference: 220 cm
## Transformation
```rust
let C: circumference
let rpm: revolutions per minute
let km/h: kilometer per hour
let m/min: meter per minute
```
### km/h 2 m/min
```rust
v (km/h) = x * 1000m / 60min
```
### km/h 2 u/min
```rust
T/min = v * 1000m / (60min * Cm)
```
__example__:
```rust
let v = 25 km/h
let C = 2.20 m
rpm = 25 * 1000m / (60min * 2.20m)
rpm = 189.394 T/min
```

7
config/config.json Normal file
View File

@ -0,0 +1,7 @@
{
"log_path": "/var/lib/cvs_monitor/",
"i2c_dev": "/dev/i2c-1",
"i2c_slave_address": 64,
"gps_dev": "/dev/ttyACM0",
"shunt_resistor": 5.0
}

10
cvs-monitor.service Normal file
View File

@ -0,0 +1,10 @@
[Unit]
Description=cvs monitor service
After=multi-user.target
[Service]
Type=idle
ExecStart=/usr/bin/cvs-monitor
[Install]
WantedBy=multi-user.target

150
src/main.rs Normal file
View File

@ -0,0 +1,150 @@
use std::fs;
use std::io::{Seek, Write};
use std::path::Path;
use chrono::{DateTime, Datelike, Utc};
use serde::{Deserialize, Serialize};
use gps_parser;
use ina3221;
#[derive(Serialize, Deserialize, Debug)]
struct Sample {
value: f64,
unit: String,
}
#[derive(Serialize, Deserialize, Debug)]
struct CvsData {
lat: f64,
lon: f64,
timestamp: String,
speed: Sample,
rpm: Sample,
current: Sample,
voltage: Sample,
}
#[derive(serde::Deserialize)]
struct Opts {
log_path: String,
i2c_dev: String,
i2c_slave_address: u16,
gps_dev: String,
shunt_resistor: f64,
}
fn read_opts(path: &str) -> Opts {
let opts_str = fs::read_to_string(path).expect("Unable to read log file");
return serde_json::from_str(&opts_str).unwrap();
}
fn update_date(datetime: DateTime<Utc>, year: i32, month: u32, day: u32) -> DateTime<Utc> {
let mut res = datetime;
res = match res.with_year(year) {
Some(value) => value,
_none => datetime,
};
res = match res.with_month(month) {
Some(value) => value,
_none => datetime,
};
res = match res.with_day(day) {
Some(value) => value,
_none => datetime,
};
return res;
}
fn log_sample(log_path: &str, sample: CvsData) {
let path = Path::new(log_path);
let mut log_json: Vec<CvsData> = Vec::new();
let mut file: fs::File;
if path.exists() {
file = fs::OpenOptions::new().read(true).write(true).create(true).truncate(false).append(false).open(log_path).unwrap();
let log_str = fs::read_to_string(path).expect("Unable to read log file");
log_json = serde_json::from_str(&log_str).unwrap();
file.set_len(0).unwrap();
file.rewind().unwrap();
} else {
file = fs::File::create(log_path).expect("Unable to create file");
}
log_json.push(sample);
let j = match serde_json::to_string(&log_json) {
Ok(value) => value,
Err(error) => panic!("Unable to make JSON from struct {:?}", error),
};
file.write_all(j.as_bytes()).expect("Unable to write JSON string to file");
}
fn main() {
let opts = read_opts("/etc/cvs-monitor/config.json");
let path = Path::new(&opts.log_path);
if !path.exists() {
match fs::create_dir(&opts.log_path) {
Ok(()) => {},
Err(err) => panic!("Can't create dir \"{}\" ({})", opts.log_path, err),
};
}
let now = Utc::now().to_rfc3339_opts(chrono::SecondsFormat::Secs, true);
let log_file: String = opts.log_path + &now + ".json";
let mut gps_parser = gps_parser::GpsParser::new(&opts.gps_dev);
let mut ina = ina3221::Ina3221::new(opts.i2c_slave_address, &opts.i2c_dev, opts.shunt_resistor);
loop {
let speed = Sample {
value: 0.0,
unit: "km/h".to_string(),
};
let rpm = Sample {
value: 0.0,
unit: "T/min".to_string(),
};
let current = Sample {
value: 0.0,
unit: "A".to_string(),
};
let voltage = Sample {
value: 0.0,
unit: "V".to_string(),
};
let mut sample = CvsData {
lat: 0.0,
lon: 0.0,
timestamp: Utc::now().to_rfc3339(),
speed,
rpm,
current,
voltage,
};
let gps = match gps_parser.parse_nmea() {
Ok(value) => value,
Err(_) => continue
};
sample.lat = gps.lat;
sample.lon = gps.lon;
sample.speed.value = gps.speed;
sample.rpm.value = sample.speed.value * 1000.0 / (60.0 * 2.2);
let mut tmp = gps.timestamp;
tmp = update_date(tmp, Utc::now().year(), Utc::now().month(), Utc::now().day());
sample.timestamp = tmp.to_rfc3339();
let current = match ina.current(ina3221::Channel::One) {
Ok(value) => value,
Err(_error) => 0.0,
};
sample.current.value = current;
let voltage = match ina.bus_voltage(ina3221::Channel::One) {
Ok(value) => value,
Err(_error) => 0.0,
};
sample.voltage.value = voltage;
log_sample(&log_file, sample);
}
}