Skip to content

Commit 8ac8a40

Browse files
committed
Record coins into the DB
1 parent 3034fba commit 8ac8a40

3 files changed

Lines changed: 37 additions & 9 deletions

File tree

src/cashcode.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -122,16 +122,13 @@ pub struct CashCode {
122122
}
123123

124124
impl CashCode {
125-
pub fn new(port_path: &str, db_path: &str) -> Result<Self, CashCodeError> {
125+
pub fn new(port_path: &str, db: CashCodeDb) -> Result<Self, CashCodeError> {
126126
info!("opening serial port: {}", port_path);
127127

128128
let port = serialport::new(port_path, 19200)
129129
.timeout(Duration::from_millis(100))
130130
.open()?;
131131

132-
info!("opening database: {}", db_path);
133-
let db = CashCodeDb::open(db_path)?;
134-
135132
Ok(CashCode {
136133
port,
137134
stacker_removed: false,

src/cashcode_db.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,14 @@ impl CashCodeDb {
3333
)?;
3434
}
3535

36+
db.execute(
37+
"CREATE TABLE IF NOT EXISTS accepted_coins (
38+
nominal INTEGER PRIMARY KEY,
39+
quantity INTEGER NOT NULL
40+
)",
41+
[],
42+
)?;
43+
3644
Ok(())
3745
}
3846

@@ -54,6 +62,16 @@ impl CashCodeDb {
5462
rows.collect::<SqlResult<Vec<_>>>().map_err(Into::into)
5563
}
5664

65+
pub fn record_coin(&self, value: i32) -> Result<(), CashCodeError> {
66+
let db = self.conn.lock().unwrap();
67+
db.execute(
68+
"INSERT INTO accepted_coins (nominal, quantity) VALUES (?1, 1)
69+
ON CONFLICT(nominal) DO UPDATE SET quantity = quantity + 1",
70+
[value],
71+
)?;
72+
Ok(())
73+
}
74+
5775
pub fn get_total_amount(&self) -> Result<i32, CashCodeError> {
5876
let db = self.conn.lock().unwrap();
5977
let total: i32 = db

src/main.rs

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ mod retroarch;
1616
mod sound;
1717

1818
use cashcode::{BillEvent, CashCode};
19+
use cashcode_db::CashCodeDb;
1920
use config::Config;
2021
use log::{error, info, warn};
2122
use slint::Model;
@@ -56,8 +57,15 @@ pub fn main() {
5657

5758
virtual_keyboard::init(&main_window);
5859
autocomplete_handler::init(&main_window);
59-
let cashcode_tx = bill_acceptor::init(&main_window, &config);
60-
let cctalk_tx = coin_acceptor::init(&main_window, &config, cashcode_tx.clone());
60+
let db = match CashCodeDb::open(&config.stats_db_path) {
61+
Ok(db) => db,
62+
Err(e) => {
63+
error!("Failed to open stats database: {}", e);
64+
return;
65+
}
66+
};
67+
let cashcode_tx = bill_acceptor::init(&main_window, &config, db.clone());
68+
let cctalk_tx = coin_acceptor::init(&main_window, &config, cashcode_tx.clone(), db);
6169
fund_fetcher::init(&main_window, &config);
6270
diagnostics_handler::init(
6371
&main_window,
@@ -86,7 +94,7 @@ mod bill_acceptor {
8694
Reset,
8795
}
8896

89-
pub fn init(app: &MainWindow, config: &Config) -> Sender<CashCodeCommand> {
97+
pub fn init(app: &MainWindow, config: &Config, db: CashCodeDb) -> Sender<CashCodeCommand> {
9098
let weak = app.as_weak();
9199

92100
// Create a channel for bill events (from CashCode to UI)
@@ -98,7 +106,7 @@ mod bill_acceptor {
98106
// Start CashCode driver in a separate thread
99107
thread::spawn({
100108
let config = config.clone();
101-
move || match init_cashcode(&config, event_tx, cmd_rx) {
109+
move || match init_cashcode(&config, db, event_tx, cmd_rx) {
102110
Ok(_) => info!("CashCode driver stopped"),
103111
Err(e) => error!("CashCode driver error: {}", e),
104112
}
@@ -195,13 +203,14 @@ mod bill_acceptor {
195203

196204
fn init_cashcode(
197205
config: &Config,
206+
db: CashCodeDb,
198207
tx: Sender<BillEvent>,
199208
cmd_rx: std::sync::mpsc::Receiver<bill_acceptor::CashCodeCommand>,
200209
) -> Result<(), cashcode::CashCodeError> {
201210
use bill_acceptor::CashCodeCommand;
202211

203212
info!("Initializing CashCode driver...");
204-
let mut cashcode = match CashCode::new(&config.cashcode_serial_port, &config.stats_db_path) {
213+
let mut cashcode = match CashCode::new(&config.cashcode_serial_port, db) {
205214
Ok(c) => c,
206215
Err(e) => {
207216
let _ = tx.send(BillEvent::Status(e.to_string(), 3));
@@ -324,6 +333,7 @@ mod coin_acceptor {
324333
app: &MainWindow,
325334
config: &Config,
326335
cashcode_tx: Sender<bill_acceptor::CashCodeCommand>,
336+
db: CashCodeDb,
327337
) -> Sender<CoinAcceptorCommand> {
328338
let weak = app.as_weak();
329339

@@ -378,6 +388,9 @@ mod coin_acceptor {
378388
match event {
379389
CoinAcceptorEvent::Accepted(value) => {
380390
info!("🪙 Coin accepted in UI: {} AMD", value);
391+
if let Err(e) = db.record_coin(value) {
392+
error!("Failed to record coin in DB: {}", e);
393+
}
381394
let current = window.get_session_amount();
382395
window.set_session_amount(current + value);
383396
window.set_last_added_amount(value);

0 commit comments

Comments
 (0)