@@ -16,6 +16,7 @@ mod retroarch;
1616mod sound;
1717
1818use cashcode:: { BillEvent , CashCode } ;
19+ use cashcode_db:: CashCodeDb ;
1920use config:: Config ;
2021use log:: { error, info, warn} ;
2122use 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
196204fn 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