Loading s3prg_rust_02_auto...
[dependencies] modbus = ">=0"
__•___••••••••______ [748, 374, 249, 187, 149]
fn now_utc_ms() -> Result<u64, Box<dyn std::error::Error>> { let now = std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH)?; Ok(now.as_millis() as u64) }
rustup doc --std 'loop'
... •_____••____________ [871, 435, 290, 217, 174] _•____•••___________ [871, 435, 290, 217, 174] _•____•••___________ [872, 436, 290, 218, 174] __•___••••__________ [872, 436, 290, 218, 174] ___•__•••••_________ [873, 436, 291, 218, 174] ___•__•••••_________ [874, 437, 291, 218, 174] ____•_••••••________ [874, 437, 291, 218, 174] _____••••••••_______ [875, 437, 291, 218, 175] _____••••••••_______ [876, 438, 292, 219, 175] ____•_••••••••______ [876, 438, 292, 219, 175] ____•_••••••••______ [877, 438, 292, 219, 175] ___•__•••••••••_____ [877, 438, 292, 219, 175] ___•__•••••••••_____ [878, 439, 292, 219, 175] __•___••••••••••____ [878, 439, 292, 219, 175] _•____•••••••••••___ [879, 439, 293, 219, 175] •_____••••••••••••__ [880, 440, 293, 220, 176] •_____••••••••••••__ [881, 440, 293, 220, 176] _•____•••••••••••••_ [881, 440, 293, 220, 176] __•___•••••••••••••• [882, 441, 294, 220, 176] ___•__•••••••••••••_ [883, 441, 294, 220, 176] ____•_••••••••••••__ [884, 442, 294, 221, 176] _____••••••••••••___ [885, 442, 295, 221, 177] _____••••••••••••___ [886, 443, 295, 221, 177] ____•_••••••••••____ [886, 443, 295, 221, 177] ___•__•••••••••_____ [887, 443, 295, 221, 177] ___•__•••••••••_____ [888, 444, 296, 222, 177] __•___••••••••______ [888, 444, 296, 222, 177] ...
rustup doc --std 'std::thread::sleep'
rustup doc --std 'std::time::Duration'
[dependencies] rusqlite = { version = ">=0", features = ["bundled"] }
let db = rusqlite::Connection::open("plc.db")?; db.busy_handler(Some(|_retry_count| { std::thread::sleep(std::time::Duration::from_millis(1)); true }))?; db.execute( "CREATE TABLE IF NOT EXISTS event ( id INTEGER PRIMARY KEY, utc_ms INTEGER, address TEXT, state INTEGER )", (), )?;
# list available tables sqlite3 plc.db '.tables'
# describe structure of "event" table sqlite3 plc.db '.schema event'
# display content of "event" table (should be empty for now) sqlite3 plc.db 'SELECT * FROM event'
fn store_coil_events( db: &rusqlite::Connection, utc_ms: u64, prev_values: &[modbus::Coil], new_values: &[modbus::Coil], ) -> Result<(), Box<dyn std::error::Error>> { // ... Ok(()) }
fn store_holding_events( db: &rusqlite::Connection, utc_ms: u64, prev_values: &[u16], new_values: &[u16], ) -> Result<(), Box<dyn std::error::Error>> { // ... Ok(()) }
db.execute( "INSERT INTO event (utc_ms, address, state) VALUES (?1, ?2, ?3)", (utc_ms, plc_addr, state), )?;
$ sqlite3 plc.db 'SELECT * FROM event' 1|1716934038900|%M3|0 2|1716934038900|%M4|1 3|1716934038900|%M15|1 4|1716934038900|%MW0|234 5|1716934038900|%MW1|117 6|1716934038900|%MW2|78 7|1716934039730|%M2|1 8|1716934039730|%M4|0 9|1716934039730|%MW0|242 10|1716934039730|%MW1|121 11|1716934039730|%MW2|80 12|1716934039730|%MW3|60 13|1716934039730|%MW4|48 14|1716934040740|%M6|0 15|1716934040740|%M7|0 16|1716934040740|%M8|0 17|1716934040740|%M9|0 18|1716934040740|%M10|0 19|1716934040740|%M11|0 20|1716934040740|%M12|0 21|1716934040740|%M13|0 22|1716934040740|%M14|0 23|1716934040740|%M15|0 24|1716934040740|%MW0|252 25|1716934040740|%MW1|126 26|1716934040740|%MW2|84 27|1716934040740|%MW3|63 28|1716934040740|%MW4|50 29|1716934042872|%M2|0 30|1716934042872|%M3|1 31|1716934042872|%M6|1 32|1716934042872|%M7|1 33|1716934042872|%M8|1 34|1716934042872|%M9|1 35|1716934042872|%M10|1 36|1716934042872|%M11|1 37|1716934042872|%M12|1 38|1716934042872|%MW0|273 39|1716934042872|%MW1|136 40|1716934042872|%MW2|91 41|1716934042872|%MW3|68 42|1716934042872|%MW4|54 43|1716934044820|%M13|1 44|1716934044820|%M14|1 45|1716934044820|%M15|1 46|1716934044820|%M16|1 47|1716934044820|%M17|1 48|1716934044820|%M18|1 49|1716934044820|%MW0|293 50|1716934044820|%MW1|146 51|1716934044820|%MW2|97 52|1716934044820|%MW3|73 53|1716934044820|%MW4|58 54|1716934046375|%M2|1 55|1716934046375|%M3|0 56|1716934046375|%M6|0 57|1716934046375|%M7|0 58|1716934046375|%M8|0 59|1716934046375|%M9|0 60|1716934046375|%M10|0 61|1716934046375|%M11|0 62|1716934046375|%M12|0 63|1716934046375|%M13|0 64|1716934046375|%M14|0 65|1716934046375|%M15|0 66|1716934046375|%M16|0 67|1716934046375|%M17|0 68|1716934046375|%M18|0 69|1716934046375|%MW0|308 70|1716934046375|%MW1|154 71|1716934046375|%MW2|102 72|1716934046375|%MW3|77 73|1716934046375|%MW4|61 74|1716934049191|%M2|0 75|1716934049191|%M4|1 76|1716934049191|%MW0|336 77|1716934049191|%MW1|168 78|1716934049191|%MW2|112 79|1716934049191|%MW3|84 80|1716934049191|%MW4|67 81|1716934050177|%M6|1 82|1716934050177|%M7|1 83|1716934050177|%M8|1 84|1716934050177|%M9|1 85|1716934050177|%M10|1 86|1716934050177|%M11|1 87|1716934050177|%M12|1 88|1716934050177|%M13|1 89|1716934050177|%M14|1 90|1716934050177|%M15|1 91|1716934050177|%MW0|346 92|1716934050177|%MW1|173 93|1716934050177|%MW2|115 94|1716934050177|%MW3|86 95|1716934050177|%MW4|69 96|1716934052309|%M2|1 97|1716934052309|%M4|0 98|1716934052309|%M10|0 99|1716934052309|%M11|0 100|1716934052309|%M12|0 101|1716934052309|%M13|0 102|1716934052309|%M14|0 103|1716934052309|%M15|0 104|1716934052309|%MW0|368 105|1716934052309|%MW1|184 106|1716934052309|%MW2|122 107|1716934052309|%MW3|92 108|1716934052309|%MW4|73 109|1716934054140|%M2|0 110|1716934054140|%M4|1 111|1716934054140|%M10|1 112|1716934054140|%M11|1 113|1716934054140|%MW0|386 114|1716934054140|%MW1|193 115|1716934054140|%MW2|128 116|1716934054140|%MW3|96 117|1716934054140|%MW4|77 118|1716934055418|%M1|1 119|1716934055418|%M4|0 120|1716934055418|%M12|1 121|1716934055418|%MW0|399 122|1716934055418|%MW1|199 123|1716934055418|%MW2|133 124|1716934055418|%MW3|99 125|1716934055418|%MW4|79 126|1716934056564|%M0|1 127|1716934056564|%M1|0 128|1716934056564|%M13|1 129|1716934056564|%M14|1 130|1716934056564|%M15|1 131|1716934056564|%MW0|410 132|1716934056564|%MW1|205 133|1716934056564|%MW2|136 134|1716934056564|%MW3|102 135|1716934056564|%MW4|82 136|1716934057944|%M0|0 137|1716934057944|%M4|1 138|1716934057944|%M10|0 139|1716934057944|%M11|0 140|1716934057944|%M12|0 141|1716934057944|%M13|0 142|1716934057944|%M14|0 143|1716934057944|%M15|0 144|1716934057944|%MW0|424 145|1716934057944|%MW1|212 146|1716934057944|%MW2|141 147|1716934057944|%MW3|106 148|1716934057944|%MW4|84
__•___••••••••______ [748, 374, 249, 187, 149]Ainsi, les seuls messages qui resteront visibles dans le terminal seront ceux qui indiquent les changements d'états manqués, grâce à la vérification mise en place à la fin de cette étape.
struct Event { utc_ms: u64, coil: bool, address: u16, state: u16, }
fn store_events( db: &rusqlite::Connection, events: Vec<Event>, ) -> Result<(), Box<dyn std::error::Error>> { let mut insert_event = db.prepare( "INSERT INTO event (utc_ms, address, state) VALUES (?1, ?2, ?3)", )?; let transaction = db.unchecked_transaction()?; for event in events { let address = format!( "{}{}", if event.coil { "%M" } else { "%MW" }, event.address ); insert_event.execute((event.utc_ms, address, event.state))?; } transaction.commit()?; Ok(()) }
rustup doc --std 'std::mem::take'.
fn store_events( db: rusqlite::Connection, channel_receiver: std::sync::mpsc::Receiver<Vec<Event>>, ) -> Result<(), Box<dyn std::error::Error>> { // ... Ok(()) }