Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Start to implement Nextcloud commands. #132

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/audio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,15 @@ async fn play_audio_file(
Ok(())
}

#[derive(Debug)]
pub enum AudioEvent {
CancelAll,
Bell,
FireAlarm,
PlayFile(String),
}

#[derive(Debug)]
pub struct Audio {
bell_path: String,
fire_alarm_path: String,
Expand All @@ -68,6 +72,9 @@ impl Audio {
};
maybe_cancellation_token = Option::Some(CancellationToken::new());
match event {
AudioEvent::CancelAll => {
// This cancels the audio since any AudioEvent cancels the previous
}
AudioEvent::Bell => {
nextcloud_sender
.send(NextcloudEvent::Chat(
Expand Down Expand Up @@ -110,6 +117,13 @@ impl Audio {
}
});
}
AudioEvent::PlayFile(path) => {
spawn(play_audio_file(
path,
"",
maybe_cancellation_token.clone().unwrap(),
));
}
}
}
Err(ModuleError::new(String::from(
Expand Down
1 change: 1 addition & 0 deletions src/buttons.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ pub enum StateChange {
LightsOff,
}

#[derive(Debug)]
pub enum CommandToButtons {
OpenDoor,
RingBell(u32, u32), // maybe implement it with interval
Expand Down
22 changes: 22 additions & 0 deletions src/nextcloud/command_list.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
Info commands:
'?' show this message.
'open?' (or 'offen?') get door states.
'lights?' (or 'licht?') get lights states.
'battery?' (or 'batterie?') see battery status.
'weather?' (or 'wetter?') see weather data.
'status?' (or 'status?') see Opensesame state.
'sensors?' (or 'sensoren?') get sensor data.
'indoor climate?' (or 'innenklima?') see indoor environment.
'time?' (or 'uhrzeit?') see current time.
'code?' (or 'pin?') and gets a list of names and codes.
Action commands:
'open!' (or 'öffnen!') open doors.
'lights [in|out]!' (or 'licht [innen|aussen]!') toggle lights.
'play <file_path>!' (or 'abspielen <file_path>!') play audio file from path.
'bell!' (or 'glocke!') play bell sound.
'alarm!' play alarm sound.
'all clear!' (or 'entwarnen!') cancle all playing audio.
'quit!' (or 'beenden!') quit Opensesame (to restart it).
'code add <name> <pin>!' (or pin hinzufügen <name> <pin>') add new code.
'code del <name>!' (or 'pin löschen <name>') remove code.
'code set <name> <pin>!' (or 'pin ändern <name> <pin>!') set code.
138 changes: 138 additions & 0 deletions src/nextcloud/commands.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
use std::process::exit;

use crate::{
audio::{self, AudioEvent},
buttons::CommandToButtons,
};
use chrono::Local;
use tokio::sync::mpsc::Sender;

use super::nextcloud::NextcloudEvent;

#[derive(Debug)]
pub enum CommandType {
Action,
Info,
}

// Commands have this structure:
// [@user](command)(!|?)
#[derive(Debug)]
pub struct Command {
user_prefix: Option<String>, // @user
words: Vec<String>, // the actual command
command_type: CommandType, // ! = Action ? = Info
}

impl Command {
pub fn new(user_input: &str) -> Result<Command, String> {
eprintln!("user_input: {}", user_input);
let input_trimmed = user_input.trim();

let command_type = match input_trimmed.chars().last() {
Some('!') => CommandType::Action,
Some('?') => CommandType::Info,
_ => return Err(String::from("Command must end with either '!' or '?'.")),
};

let mut words = input_trimmed[..input_trimmed.len() - 1]
.split_whitespace()
.map(|word| word.to_lowercase())
.collect::<Vec<String>>();

let user_prefix = match words.get(0) {
None => None,
Some(first_word) => {
if first_word.starts_with("@") {
Some(first_word.clone())
} else {
None
}
}
};

if user_prefix.is_some() {
words.drain(0..1);
}

Ok(Command {
user_prefix,
words,
command_type,
})
}
}

pub async fn run_command(
command: Command,
nextcloud_sender: Sender<NextcloudEvent>,
command_sender: Sender<CommandToButtons>,
audio_sender: Sender<AudioEvent>,
user: &str,
) -> String {
if let Some(user_prefix) = command.user_prefix {
if user_prefix != user {
return "".to_string();
}
}

let words = command
.words
.iter()
.map(|word| word.as_str())
.collect::<Vec<&str>>();

match command.command_type {
CommandType::Info => match words[..] {
[] => include_str!("command_list.txt").to_owned(),
["open"] | ["offen"] => todo!(), // show_door_status(),
["lights"] | ["licht"] => todo!(), // switch_lights(),
["battery"] | ["batterie"] => todo!(), // show_battery_status(),
["weather"] | ["wetter"] => todo!(), // show_weather(),
["indoor", "climate"] | ["innenklima"] => todo!(), // report_indoor_climate(),
["status"] => todo!(), // report_nextcloud_status(),
["sensors"] | ["sensoren"] => todo!(), // report_sensor_data(),
["time"] | ["uhrzeit"] => Local::now().to_string(),
["code"] | ["pin"] => todo!(), // list_codes(),
_ => String::from("Unknown command!"),
},
CommandType::Action => match words[..] {
["open"] | ["öffnen"] => {
command_sender
.send(CommandToButtons::OpenDoor)
.await
.unwrap();
"Sending open door command...".to_owned()
} // open_door(),
["lights", "in"] | ["licht", "innen"] => todo!(), // lights,
["lights", "out"] | ["licht", "aussen"] => todo!(), // lights,
["play", audio_file_path] | ["abspielen", audio_file_path] => {
audio_sender
.send(AudioEvent::PlayFile(audio_file_path.to_owned()))
.await
.unwrap();
format!("Started playing audio file {}", audio_file_path).to_owned()
}
["bell"] | ["glocke"] => {
audio_sender.send(AudioEvent::Bell).await.unwrap();
"Started bell audio...".to_owned()
}
["alarm"] => {
audio_sender.send(AudioEvent::FireAlarm).await.unwrap();
"Started fire alarm sound...".to_owned()
}
["all", "clear"] | ["entwarnen"] => {
audio_sender.send(AudioEvent::CancelAll).await.unwrap();
"Canceled audio events...".to_owned()
}

["quit"] | ["beenden"] => exit(0),
["code", "add", name, pin] | ["pin", "hinzufügen", name, pin] => {
todo!() //add_code(name, pin)
}
["code", "del"] | ["pin", "löschen"] => todo!(), //delete_code(),
["code", "set"] | ["pin", "ändern"] => todo!(), //set_code(),
_ => String::from("Unknown command!"),
},
}
}
4 changes: 4 additions & 0 deletions src/nextcloud/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
pub mod commands;

pub mod nextcloud;
pub use nextcloud::*; // avoid nextcloud::nextcloud input
Loading