2025-09-24 18:07:05 +02:00
|
|
|
use reqwest::Client;
|
|
|
|
|
use serde::Deserialize;
|
|
|
|
|
use serial::prelude::*;
|
2025-09-23 17:13:23 +02:00
|
|
|
use std::io;
|
|
|
|
|
use std::str::from_utf8;
|
|
|
|
|
use std::time::Duration;
|
|
|
|
|
|
2025-09-24 18:07:05 +02:00
|
|
|
struct BookData {
|
|
|
|
|
title: String,
|
|
|
|
|
authors: Vec<String>,
|
|
|
|
|
thumbnail: String,
|
|
|
|
|
description: String,
|
|
|
|
|
}
|
|
|
|
|
#[allow(unused)]
|
|
|
|
|
#[derive(Debug, Deserialize)]
|
|
|
|
|
struct BookRoot {
|
|
|
|
|
items: Vec<Item>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[allow(unused)]
|
|
|
|
|
#[derive(Debug, Deserialize)]
|
|
|
|
|
struct Item {
|
|
|
|
|
volumeinfo: VolumeInfo,
|
|
|
|
|
}
|
2025-09-23 17:13:23 +02:00
|
|
|
|
2025-09-24 18:07:05 +02:00
|
|
|
#[allow(unused)]
|
|
|
|
|
#[derive(Debug, Deserialize)]
|
|
|
|
|
struct VolumeInfo {
|
|
|
|
|
title: String,
|
|
|
|
|
authors: Option<Vec<String>>,
|
|
|
|
|
description: Option<String>,
|
|
|
|
|
imagelinks: Option<ImageLinks>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[allow(unused)]
|
|
|
|
|
#[derive(Debug, Deserialize)]
|
|
|
|
|
struct ImageLinks {
|
|
|
|
|
thumbnail: Option<String>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[tokio::main]
|
|
|
|
|
async fn main() {
|
2025-09-23 17:13:23 +02:00
|
|
|
let mut port = serial::open("/dev/ttyACM0").unwrap();
|
2025-09-24 18:07:05 +02:00
|
|
|
interact(&mut port).await.unwrap();
|
2025-09-23 17:13:23 +02:00
|
|
|
}
|
|
|
|
|
|
2025-09-24 18:07:05 +02:00
|
|
|
async fn interact<T: SerialPort>(port: &mut T) -> io::Result<()> {
|
2025-09-23 17:13:23 +02:00
|
|
|
port.reconfigure(&|settings| {
|
|
|
|
|
settings.set_baud_rate(serial::Baud9600)?;
|
|
|
|
|
settings.set_char_size(serial::Bits8);
|
|
|
|
|
settings.set_parity(serial::ParityNone);
|
|
|
|
|
settings.set_stop_bits(serial::Stop1);
|
|
|
|
|
settings.set_flow_control(serial::FlowNone);
|
|
|
|
|
Ok(())
|
|
|
|
|
})?;
|
|
|
|
|
|
|
|
|
|
port.set_timeout(Duration::from_millis(1000000000))?;
|
|
|
|
|
loop {
|
2025-09-24 18:07:05 +02:00
|
|
|
let buf: &mut [u8; 14] = &mut [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
|
|
|
|
|
port.read_exact(buf).unwrap();
|
|
|
|
|
let barcode_id = from_utf8(buf).unwrap();
|
|
|
|
|
println!("{}", &barcode_id);
|
|
|
|
|
if barcode_id.starts_with("978") {
|
|
|
|
|
get_book_data(barcode_id.to_string()).await;
|
|
|
|
|
}
|
2025-09-23 17:13:23 +02:00
|
|
|
}
|
|
|
|
|
}
|
2025-09-24 18:07:05 +02:00
|
|
|
|
|
|
|
|
async fn get_book_data(isbn: String) {
|
|
|
|
|
let lookup_url = format!(
|
|
|
|
|
"https://www.googleapis.com/books/v1/volumes?q=isbn:{}",
|
|
|
|
|
isbn
|
|
|
|
|
);
|
|
|
|
|
let cleint = Client::new();
|
|
|
|
|
let book_data = cleint
|
|
|
|
|
.get(lookup_url)
|
|
|
|
|
.send()
|
|
|
|
|
.await
|
|
|
|
|
.unwrap()
|
|
|
|
|
.json::<BookRoot>()
|
|
|
|
|
.await
|
|
|
|
|
.unwrap();
|
|
|
|
|
println!("{:?}", book_data);
|
|
|
|
|
}
|