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

Send status to mattermost #17

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
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
4 changes: 4 additions & 0 deletions software/raspberrypi/.cargo/config.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
[env]
#PYO3_PYTHON = "./ve/bin/python"
PYTHONPATH = "./ve/lib/python3.12/site-packages"

[build]
#target = "arm-unknown-linux-musleabihf"
#target = "armv7-unknown-linux-musleabihf"
Expand Down
99 changes: 99 additions & 0 deletions software/raspberrypi/Cargo.lock

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

2 changes: 2 additions & 0 deletions software/raspberrypi/backend/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,5 @@ reqwest = { version = "0.12", default-features = false, features = [
reqwest-retry = "0.7"
reqwest-middleware = "0.4"
secrecy = { version = "0.10", features = ["serde"] }

pyo3 = { version = "0.23", features = ["anyhow", "auto-initialize", "serde"] }
44 changes: 44 additions & 0 deletions software/raspberrypi/backend/python/mattermost.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#!/usr/bin/env python

from mattermostdriver import Driver


def main(*args):
url = args[0]
login_id = args[1]
api_token = args[2]
scheme = args[3]
port = args[4]
state = args[5]

driver = Driver(
{
"url": url,
"login_id": login_id,
"token": api_token,
"scheme": scheme,
"port": int(port),
}
)
driver.login()

team = driver.teams.get_team_by_name("section77")
channel = driver.channels.get_channel_by_name(team["id"], "clubstatus")

message = ""
name = ""
if state == "true":
message = "Die Section77 ist offen"
name = "Status: Offen"
elif state == "false":
message = "Die Section77 ist geschlossen"
name = "Status: Geschlossen"

# send message
_post = driver.posts.create_post({"channel_id": channel["id"], "message": message})
# update room name
driver.channels.patch_channel(
channel["id"], {"id": channel["id"], "display_name": name}
)

driver.logout()
12 changes: 12 additions & 0 deletions software/raspberrypi/backend/src/configuration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,24 @@ pub struct SpaceAPI {
pub password: SecretString,
}

#[derive(Debug, Clone, Deserialize)]
pub struct MatterMost {
pub enable: bool,
pub url: String,
pub loginid: String,
pub apitoken: String,
pub scheme: String,
pub port: u16,
}

#[derive(Debug, Clone, Deserialize)]
pub struct Configuration {
pub server: Server,

pub spaceapi: SpaceAPI,

pub mattermost: MatterMost,

// hardware
pub lockmotor: lock::Configuration,
pub lockswitch: lockswitch::Configuration,
Expand Down
31 changes: 30 additions & 1 deletion software/raspberrypi/backend/src/notifyer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use secrecy::ExposeSecret;
use tokio::sync::mpsc::Receiver;
use tracing::{error, info};

use crate::configuration::{ConfigurationRef, SpaceAPI};
use crate::configuration::{ConfigurationRef, MatterMost, SpaceAPI};

// async fn mqtt(state: bool) {
// info!("MQTT");
Expand Down Expand Up @@ -78,10 +78,39 @@ async fn spaceapi(configuration: &'static SpaceAPI, state: bool) {
}
}

fn mattermost(configuration: &'static MatterMost, state: bool) {
use pyo3::{ffi::c_str, prelude::*, types::PyTuple};

let code = c_str!(std::include_str!("../../backend/python/mattermost.py"));
Python::with_gil(|py| {
let fun = PyModule::from_code(py, code, c_str!("mattermost.py"), c_str!("mattermost"))
.unwrap()
.getattr("main")
.unwrap();

// pass object with Rust tuple of positional arguments
let args = PyTuple::new(
py,
[
configuration.url.as_str(),
configuration.loginid.as_str(),
configuration.apitoken.as_str(),
configuration.scheme.as_str(),
&configuration.port.to_string(),
&state.to_string(),
],
)
.unwrap();

let _ = fun.call1(args).unwrap();
})
}

pub async fn notify(configuration: ConfigurationRef, mut receiver: Receiver<bool>) {
while let Some(state) = receiver.recv().await {
// let mqtt = mqtt(state);
let _spaceapi = spaceapi(&configuration.spaceapi, state).await;
mattermost(&configuration.mattermost, state);
// tokio::join!(mqtt, spaceapi);
}
}