From d4d3f03f3a3fa066eee8b40d31dec878b3227205 Mon Sep 17 00:00:00 2001 From: Andreas Neuhaus Date: Tue, 29 Oct 2024 11:12:55 +0100 Subject: [PATCH] Make articleid a String, like Vereinsflieger defines it --- firmware/config-example.json | 2 +- firmware/src/article.rs | 15 ++++++++++----- firmware/src/ui.rs | 6 +++--- firmware/src/vereinsflieger/proto_articles.rs | 6 +++--- 4 files changed, 17 insertions(+), 12 deletions(-) diff --git a/firmware/config-example.json b/firmware/config-example.json index 5d1ab5c..13afff0 100644 --- a/firmware/config-example.json +++ b/firmware/config-example.json @@ -15,5 +15,5 @@ "vf-cid": 0, // Vereinsflieger article id to use for purchases - "vf-article-id": 0 + "vf-article-id": "1234" } diff --git a/firmware/src/article.rs b/firmware/src/article.rs index c98d0c7..3241489 100644 --- a/firmware/src/article.rs +++ b/firmware/src/article.rs @@ -3,7 +3,7 @@ use alloc::string::String; /// Article id /// Equivalent to the Vereinsflieger `articleid` attribute #[allow(clippy::module_name_repetitions)] -pub type ArticleId = u32; +pub type ArticleId = String; /// Article information #[derive(Debug, Clone, PartialEq)] @@ -38,8 +38,8 @@ impl Articles { } /// Update article with given article id. Ignores article ids not in list. - pub fn update(&mut self, id: ArticleId, name: String, price: f32) { - if let Some(idx) = self.ids.iter().position(|i| *i == id) { + pub fn update(&mut self, id: &ArticleId, name: String, price: f32) { + if let Some(idx) = self.find(id) { self.articles[idx] = Some(Article { name, price }); } } @@ -49,10 +49,15 @@ impl Articles { self.articles.iter().filter(|a| a.is_some()).count() } + /// Find index of article with given id + pub fn find(&self, id: &ArticleId) -> Option { + self.ids.iter().position(|i| i == id) + } + /// Look up id of article at given index #[allow(dead_code)] - pub fn id(&self, index: usize) -> Option { - self.ids.get(index).copied() + pub fn id(&self, index: usize) -> Option<&ArticleId> { + self.ids.get(index) } /// Look up article information at given index diff --git a/firmware/src/ui.rs b/firmware/src/ui.rs index eb25a3a..1a08e64 100644 --- a/firmware/src/ui.rs +++ b/firmware/src/ui.rs @@ -186,11 +186,11 @@ impl<'a, I2C: I2c, IRQ: Wait> Ui<'a, I2C, IRQ> { let _user = self.users.get(userid); // Ask for number of drinks let num_drinks = self.get_number_of_drinks().await?; - // Get article price - let price = self.articles.get(0).ok_or(Error::ArticleNotFound)?.price; + // Get article information + let article = self.articles.get(0).ok_or(Error::ArticleNotFound)?; // Calculate total price. It's ok to cast num_drinks to f32 as it's always a small number. #[allow(clippy::cast_precision_loss)] - let total_price = price * num_drinks as f32; + let total_price = article.price * num_drinks as f32; // Show total price and ask for confirmation self.confirm_purchase(num_drinks, total_price).await?; diff --git a/firmware/src/vereinsflieger/proto_articles.rs b/firmware/src/vereinsflieger/proto_articles.rs index 6f9e595..b125fbe 100644 --- a/firmware/src/vereinsflieger/proto_articles.rs +++ b/firmware/src/vereinsflieger/proto_articles.rs @@ -57,7 +57,7 @@ impl FromJsonObject for ArticleListResponse { // articles directly to the article lookup table and only keeps the articles // needed, which heavily reduces memory consumption. let mut articles = context.borrow_mut(); - articles.update(article.articleid, article.designation.clone(), price); + articles.update(&article.articleid, article.designation, price); } else { warn!( "Ignoring article with no valid price ({}): {}", @@ -74,7 +74,7 @@ impl FromJsonObject for ArticleListResponse { /// Article #[derive(Debug, Default)] pub struct Article { - pub articleid: u32, + pub articleid: String, pub designation: String, pub unittype: String, pub prices: Vec, @@ -90,7 +90,7 @@ impl FromJsonObject for Article { _context: &Self::Context<'_>, ) -> Result<(), json::Error> { match &*key { - "articleid" => self.articleid = json.read_any().await?.try_into()?, + "articleid" => self.articleid = json.read().await?, "designation" => self.designation = json.read().await?, "unittype" => self.unittype = json.read().await?, "prices" => self.prices = json.read().await?,