// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
-use serde::Serialize;
+use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::net::UdpSocket;
use std::time::{Duration, SystemTime, UNIX_EPOCH};
seq: u16,
}
-#[derive(Debug, Serialize)]
+#[derive(Debug, Deserialize, Serialize)]
struct Message {
epoch: u32,
device: u16,
}
}
+fn replay_log() -> HashMap<MessageKey, Range> {
+ let mut presses = HashMap::new();
+ if std::path::Path::new(LOGFILENAME).exists() {
+ let mut log = csv::Reader::from_path(LOGFILENAME).expect("Couldn't open log for replay");
+ for message in log.deserialize() {
+ merge_message(
+ &mut presses,
+ message.expect("Error reading log during replay"),
+ );
+ }
+ }
+ presses
+}
+
fn open_log_for_writing() -> csv::Writer<std::fs::File> {
let log_file_exists = std::path::Path::new(LOGFILENAME).exists();
let logfile = std::fs::OpenOptions::new()
fn main() {
let socket = UdpSocket::bind("0.0.0.0:29803").expect("couldn't bind to address");
- let mut presses = HashMap::<MessageKey, Range>::new();
+ let mut presses = replay_log();
let mut log = open_log_for_writing();
loop {
let mut buf = [0; MESSAGE_SIZE];