Skip to content

Commit

Permalink
fix #1, #2, errors, change presense to presence
Browse files Browse the repository at this point in the history
  • Loading branch information
AdriDevelopsThings committed Jan 2, 2023
1 parent 9cef742 commit e101712
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 19 deletions.
8 changes: 4 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
[package]
name = "iceportal_rich_presense"
version = "1.0.2"
name = "iceportal_rich_presence"
version = "1.0.3"
edition = "2021"
authors = ["AdriDevelopsThings"]
readme = "README.md"
repository = "https://github.com/AdriDevelopsThings/iceportal_rich_presense"
repository = "https://github.com/AdriDevelopsThings/iceportal_rich_presence"
license = "AGPL-3.0"
description = "Show your current ride in discord via the ICE portal api"


[dependencies]
discord-rich-presence = "0.2.3"
iceportal = "2.0.1"
iceportal = "2.0.2"
inquire = "0.5.2"
tokio = { version = "1", features = ["full"] }
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# iceportal rich presense
# iceportal rich presence
Show your current ride in discord via the ICE portal api.

## Installation
Expand All @@ -10,4 +10,4 @@ Download the binary for your operating system from the current release or build
It will fetch the current trip info from the ICE portal api every 30 seconds (so you have to be on the WIFIonICE) and publish it to discord. Also the right building series will be shown as an image.

You will be asked at which station you entered the train to show the time you need to arrival in discord.
![discord rich presense logo](./screenshots/discord.png)
![discord rich presence logo](./screenshots/discord.png)
16 changes: 11 additions & 5 deletions src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,27 @@ use std::fmt::Debug;

use iceportal::errors::ICEPortalError;

pub enum ICEPortalRichPresenseError {
pub enum ICEPortalRichPresenceError {
ICEPortalError(ICEPortalError)
}

impl From<ICEPortalError> for ICEPortalRichPresenseError {
impl From<ICEPortalError> for ICEPortalRichPresenceError {
fn from(e: ICEPortalError) -> Self {
Self::ICEPortalError(e)
}
}

impl Debug for ICEPortalRichPresenseError {
impl Debug for ICEPortalRichPresenceError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::ICEPortalError(_) =>
write!(f, "Error while connecting to ICE portal: Please check if you are connected to the Bahn WiFi")
Self::ICEPortalError(e) => {
match e {
ICEPortalError::RequestError(_) =>
write!(f, "It seems like a connection error to te ICE Portal happend: {:?}", e),
ICEPortalError::NotConnectedToICEPortal =>
write!(f, "Error while connecting to ICE portal: Please check if you are connected to the Bahn WiFi")
}
}
}
}
}
11 changes: 7 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
use std::{time::Duration, process::exit};

use discord_rich_presence::{DiscordIpcClient, DiscordIpc, activity::{self, Activity, Timestamps, Assets, Button}};
use errors::ICEPortalRichPresenseError;
use errors::ICEPortalRichPresenceError;
use iceportal::{ICEPortal, trip_info::TripInfo, global_models::{PositionStatus, Stop}};
use inquire::Select;
use series::translate_series;
use tokio::{select, time::sleep};

mod errors;
mod series;

fn cancel_activity(client: &mut DiscordIpcClient) {
client.clear_activity().expect("Error while clearing activity");
client.close().expect("Error while closing discord socket");
}

fn update_activity(client: &mut DiscordIpcClient, trip: TripInfo, to: &str, building_series: &str) {
Expand All @@ -25,7 +28,7 @@ fn update_activity(client: &mut DiscordIpcClient, trip: TripInfo, to: &str, buil

let timestamps = Timestamps::new()
.end(end_stop.timetable.actual_arrival_time.unwrap().timestamp());
let assets = Assets::new().large_image(building_series);
let assets = Assets::new().large_image(translate_series(building_series));
let watch_button_url = format!("https://regenbogen-ice.de/trip/{}/{}", trip.train_type, trip.vzn);
let watch_button = Button::new("Watch",
watch_button_url.as_str());
Expand All @@ -43,7 +46,7 @@ fn update_activity(client: &mut DiscordIpcClient, trip: TripInfo, to: &str, buil
}

#[tokio::main]
async fn main() -> Result<(), ICEPortalRichPresenseError>{
async fn main() -> Result<(), ICEPortalRichPresenceError>{
let trip_info = ICEPortal::fetch_trip_info().await?;
let status_info = ICEPortal::fetch_status().await?;
let available_stops = trip_info.trip.stops.iter()
Expand Down Expand Up @@ -78,7 +81,7 @@ async fn main() -> Result<(), ICEPortalRichPresenseError>{
client.set_activity(payload)
.expect("Error while setting activity");

println!("Connected! ICEPortal rich presense is running. Stop it by pressing Ctrl + C");
println!("Connected! ICEPortal rich presence is running. Stop it by pressing Ctrl + C");

loop {
select! {
Expand Down
18 changes: 18 additions & 0 deletions src/series.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
pub fn translate_series(series: &str) -> &str {
let series_as_int = series.parse::<u16>();
match series_as_int {
Ok(s) => {
if (801..804).contains(&s) {
"401"
} else if (805..808).contains(&s) {
"402"
} else if s == 812 {
"412"
} else {
series
}
},
Err(_) => series
}

}

0 comments on commit e101712

Please sign in to comment.