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

Async Refactor WIP #75

Merged
merged 58 commits into from
Oct 14, 2023
Merged
Show file tree
Hide file tree
Changes from 56 commits
Commits
Show all changes
58 commits
Select commit Hold shift + click to select a range
0e7f8e1
Refactor main: keep old semantic
horenso Aug 23, 2023
3e50b6f
Add scripts to emulate device.
horenso Aug 23, 2023
022fe0b
impl MOD-IR-TMP & Weatherstation
fel115 Aug 28, 2023
d9b14e5
exec - cagro fmt
fel115 Aug 29, 2023
9f38a18
WIP
horenso Aug 30, 2023
84c29a0
WIP: Async
horenso Aug 30, 2023
814a60a
More refactoring work: Split up functionality
horenso Aug 31, 2023
096c159
Update reqwest to 0.11
horenso Sep 1, 2023
04693c9
Refactor nextcloud module.
horenso Sep 1, 2023
5bddb7c
Changes in env_loop, weatherstation_loop, modir_loop, sensors_loop; i…
fel115 Sep 1, 2023
1aa694e
Merge branch 'refactor_main' of https://github.com/horenso/opensesame…
fel115 Sep 1, 2023
853e7c7
WIP move config out of loop
fel115 Sep 1, 2023
a1dbe33
More async refactoring.
horenso Sep 2, 2023
35207ad
compiling worked
fel115 Sep 4, 2023
dbf0dd5
Add join_all in main such that main waits for async modules.
horenso Sep 4, 2023
6c3edbd
ssh async; ErrorType
fel115 Sep 4, 2023
adca7d0
Buttons+Validator, Garage, Sensors, ModIR, Env, Nextcloud working
fel115 Sep 5, 2023
d913bb6
Merge branch 'refactor_main_update_async_loops' into refactor_main
fel115 Sep 5, 2023
a25a50e
Update gatage_loop after merge
fel115 Sep 5, 2023
6419f35
Add serde_json for json deserialization.
horenso Sep 6, 2023
dc6aae5
Add commands chat token to spec.
horenso Sep 6, 2023
238b2b4
Add beginning of chat commands; Move Nextcloud loops to nextcloud.rs
horenso Sep 6, 2023
77e5232
Rename OpensesameError to ModuleError.
horenso Sep 6, 2023
99dc4e9
Remove tokio-experiments and todo.
horenso Sep 10, 2023
bbc6c83
Implement first version of Nextcloud commands.
horenso Sep 10, 2023
cdebc5e
Move env, garage, sensor and bar loops into their files.
horenso Sep 10, 2023
a611ac4
Refactor get_background_task into modules and add lib.rs.
horenso Sep 10, 2023
e21b535
Make Weatherstation and watchdog async.
horenso Sep 11, 2023
8186192
Clippy auto fixes.
horenso Sep 11, 2023
d918a61
Make do_reset async.
horenso Sep 11, 2023
f29ac51
exec cargo fmt; change do_reset
fel115 Sep 12, 2023
9b61496
add LocalSet
fel115 Sep 12, 2023
5f76813
ideas of signals
fel115 Sep 12, 2023
c5a9333
created signals + ping; Added new command to buttons
fel115 Sep 13, 2023
0ffbcbd
Start refactor audio into own background task.
horenso Sep 13, 2023
48911d2
finished signals & cargo fmt
fel115 Sep 14, 2023
8637bf6
signals check for environment enabled
fel115 Sep 14, 2023
5fc9c9d
remove debug line form Sensors; add Testreport
fel115 Sep 14, 2023
b2958ad
Make button use audio sender to play audio file.
horenso Sep 15, 2023
7f46542
Merge branch 'audio' into refactor_main
horenso Sep 15, 2023
afcb03d
Add forgotten .await
horenso Sep 17, 2023
7941f0a
Implement Send for Config and Clima US. Use audio_sender in env.
horenso Sep 18, 2023
271868e
Use clippy suggestions.
horenso Sep 18, 2023
500257f
env implementation of mutex(remember/restore-baseline); sensor impl s…
fel115 Sep 18, 2023
711f147
exec cargo fmt
fel115 Sep 18, 2023
958a6b4
small changes
fel115 Sep 25, 2023
9cfd75a
Cancel previous audio signal when new Alarm/Bell signal comes in.
horenso Sep 27, 2023
c269875
Implementation of Issue #87
fel115 Sep 27, 2023
b8dc9bc
implementation of bat.rs + cargo fmt
fel115 Sep 27, 2023
e423420
changes in button.rs to handle do_reset; update bat.rs to only trigge…
fel115 Sep 27, 2023
77f004a
added test function for watchdog
fel115 Sep 27, 2023
9cec7d5
update specs; bat.rs reordering;
fel115 Sep 28, 2023
4a8d992
audio.rs logging to Ping Chat
fel115 Sep 29, 2023
4244064
rm state.set;
fel115 Oct 2, 2023
e4cac3c
some requested changes
fel115 Oct 3, 2023
4c7aed4
remove prints
fel115 Oct 12, 2023
6e7599a
changed requested changes
fel115 Oct 14, 2023
5619c48
Merge branch 'master' into refactor_main
fel115 Oct 14, 2023
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
1,762 changes: 958 additions & 804 deletions Cargo.lock

Large diffs are not rendered by default.

15 changes: 10 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
[package]
name = "opensesame"
version = "0.7.9"
version = "0.8.0"
authors = ["Opensesame Contributors"]
edition = "2018"
edition = "2021"
readme = "README.md"
license = "BSD"
description = "Awesome home automation"
Expand Down Expand Up @@ -44,25 +44,30 @@ i2cdev = "0.5.1"

elektra = { version = "0.11.0", features = ["pkg-config"] }

reqwest = "0.9.24"
reqwest = { version = "0.11", features = ["json"] }

bme280 = "0.3.0"
mlx9061x = "0.2.1"
linux-embedded-hal = { version = "0.3", features = ["gpio_cdev"] }

systemstat = "0.1.11"

ssh2 = "0.9.3"
nix = "0.25"

chrono = "0.4.19"

signal-hook = "0.3.14"
signal-hook-tokio = "0.3.1"

tokio = { version = "1.15.0", features = ["full"] }
tokio-util = "0.6.9"
async-ssh2-tokio = "0.7.1"
futures = "0.3.19"

gettext-rs = "0.7.0"

serde = "1.0.188"
serde_json = "1.0.106"

serial_test = "0.4.0" # TODO, remove when issues.libelektra.org/4466 is fixed

sunrise = "1.0.0"
Expand Down
4 changes: 2 additions & 2 deletions debian/service
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[Unit]
Description=opensesame
Wants=network-online.target
After=network.target network-online.target
Wants=network-online.target nss-lookup.target
After=network.target network-online.target nss-lookup.target

[Service]
Type=simple
Expand Down
122 changes: 122 additions & 0 deletions doc/Testreport_Async_Refactor.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
# Testing Async
This is the testreport of the async refector of opensesame. (`-`) means that the test failed, (`~`) something went wrong but it works and (`+`) means that everything went as expected. Here is a list of the modules which need to be tested.

1. **Siganls**
fel115 marked this conversation as resolved.
Show resolved Hide resolved
2. **Buttons (+Bell)**
3. **Buttons+Garage**
4. **Sensors**
5. **ModIR**
6. **Environment**
7. **Weatherstation**
8. **Battery**
9. **Watchdog**
10. **Ping**


## Signals
### Configuration
```toml
nextcloud.chat = "<chat-token>"
nextcloud.chat.licht = "<chat-token>"
nextcloud.chat.ping = "<chat-token>"
nextcloud.chat.commands = "<chat-token>"
nextcloud.user = "<user>"
nextcloud.pass = "<pass>"
nextcloud.url = "<nextcloud-url>"
nextcloud.format.datetime = "%d.%m.%Y %H:%M:%S"
nextcloud.format.time = "%H:%M:%S"
validator.test1 = "14, 15 ,13 ,15, 11, 15, 7, 15"
validator.test2 = "14, 12, 14, 15, 11, 15"
buttons.enable = 0
bell.enable = 0
garage.enable = 0
sensors.enable = 0
environment.enable = 0
ir.enable = 0
weatherstation.enable = 0
bat.enable = 0
watchdog.enable = 0
ping.enable = 0
```
### (`-`): This module doesn't enter the async-loop, because of problems with `spawn_local`
fel115 marked this conversation as resolved.
Show resolved Hide resolved

## Buttons
### Configuration
```toml
...
buttons.enable = 1
...
```
### (`+`): light button; light switch; light permanent; light time extended; pin validation
### (`~`): First entering of pin caused time `sequence timeout`, the timeout was too fast. Besides this one failure it worked fine. Cloud be a startup issue.
fel115 marked this conversation as resolved.
Show resolved Hide resolved

## Buttons + Bell
### Configuration
```toml
...
buttons.enable = 1
bell.enable = 1
...
```
### (`+`): bell and buttons worked

## Buttons + Garage
### Configuration
```toml
...
buttons.enable = 1
garage.enable = 1
...
```
### (`+`): endposition garage door; TasterTorUnten (no NC message); TasterUnten (no NC message); TasterOben
### (`~`): TasterTorOben switched only the light indoor at the first time, after a retry it switch both

## Sensors
### Configuration
```toml
...
sensors.enable = 1
sensors.device = "/dev/ttyACM0"
sensors."#0".loc = "Wohnzimmer"
sensors."#0".alarm = 50
sensors."#0".chat = 20
sensors."#1".loc = "Badezimmer"
sensors."#1".alarm = 70
sensors."#1".chat = 35
sensors."#2".loc = "Küche"
sensors."#2".alarm = 5
sensors."#2".chat = 3
...
```
For simulating the sensors we used the methode with is described in [DevelopmentSetup](./DevelopmentSetup.md). With the following script:
```bash
COUNTER=0;
while true; do
let COUNTER=COUNTER+1
if [ $COUNTER -eq 20 ]; then
data="23 35 4 0 0 0 0 0 0 0 0 0"
elif [ $COUNTER -eq 100 ]; then
data="55 80 10 0 0 0 0 0 0 0 0 0"
elif [ $COUNTER -eq 130 ]; then
data="1005 2015 1102 0 0 0 0 0 0 0 0 0"
else
data="0 0 0 0 0 0 0 0 0 0 0 0"
fi
echo "$data"
sleep 60
done
```
### (`+`): Chat and Alarm trigger worked as expected
### (`~`): **!!! implementation of state mutex is missing in async function of sensors !!!**
fel115 marked this conversation as resolved.
Show resolved Hide resolved

## Weatherstation
### Configuration
```toml
...
weatherstation.enable = 1
weatherstation.data.interval = 60
weatherstation.opensensemap.id = "64cb602193c69500072a580f"
weatherstation.opensensemap.token = "7bbb014ffbf974255caef2f88525b0512bd0817d9d222f70c7741a4a9cd56c6c"
...
```
### (`+`): Works as expected, sends warning to Nextcloud and updates opensensemap.org
56 changes: 56 additions & 0 deletions doc/modules.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# OpenSesame
fel115 marked this conversation as resolved.
Show resolved Hide resolved

## audio.rs
This module handles audio output for playing fire alarms and bell sounds. This module can receive commands from the Module Buttons (Bell), Environment (FireAlarm), Nextcloud (FireAlarm, Bell), and Signals (FireAlarm, Bell).

## bat.rs
Checks the battery capacity every ten minutes and outputs to Nextcloud if it falls below 50%. If it goes below 50%, the threshold for the next Nextcloud message is set to 40%.

## buttons.rs
This module implements the `do_reset` if an error occurs on the MOD-IO2 modules. In the `async get_background_task`, we implemented how the buttons were handled in the old main, including the validator and command receiver. The command receiver receives commands like `opendoor` (used by Garage, Nextcloud), `ring_bell` (used by Environment, Signals), `switchlights` (used by Garage, Nextcloud), and `RingBellAlarm` (used by Signals).

## clima_sensor_us.rs
This module works independently, sending warnings to Nextcloud and publishing to opensensemap. We needed to implement `Send` to use libmodbus with async functions.

## config.rs
No changes were made to this module.

## environment.rs
Changed the functions `rememberBaseline`, `restoreBaseline`, and added a `Muext` of state. We moved `handle_environment` from the main into the `get_background_task`. This module receives commands (RestoreBaseline, RememberBaseline) from Signals.
fel115 marked this conversation as resolved.
Show resolved Hide resolved

## garage.rs
This module is checked at intervals of 10 milliseconds, triggering Nextcloud Chat or Buttons commands if the button is pressed. Future changes will involve removing the interval and implementing trigger-oriented events because GPIO pins from the Olimex board are used, along with interrupts.
fel115 marked this conversation as resolved.
Show resolved Hide resolved

## mod_ir_temp.rs
This module is triggered at given intervals and warns by sending Nextcloud messages.

## nextcloud.rs
Implements two loops: one for sending (`message_sender_loop`) messages and status to Nextcloud, and the other for receiving (`command_loop`) messages/commands from Nextcloud. Commands can be sent via Nextcloud chat by typing "\opensesame" to open the door, or other commands like "\ring_bell", "\fire_alarm", "\status", and "\switchlights true true".

## ping.rs
This module sends a ping message to Nextcloud if it receives the `SendPing` event. Other functions update `Env`, `EnvStatus`, `EnvError`, and `BatCapacity`, but these commands aren't used yet. They need to be triggered at the right spot in Environment and Battery to keep the ping up to date.

## pwr.rs
No significant changes were made to this module.

## sensors.rs
No major changes, only the `get_background_task` was added with a similar implementation as in the old version.

## signals.rs
This module listens to system signals and executes the same events as in the old version.

## ssh.rs
Changed to an async function.

## types.rs
This module contains the error types defined so far.

## validator.rs
No changes were made to this module.

## watchdog.rs
This background task is triggered every few seconds and writes to the specified file. The `test_get_background_task` function is used to simulate the watchdog stopping writing to the watchdog file.

## main.rs
In this loop, we initialize every module if it is enabled. Additionally, the MPSC (Multiple Producer, Single Consumer) channels are initialized here. To configure each module without encountering issues related to multiple access to one config object, we read the configuration in the main.

3 changes: 3 additions & 0 deletions files/emu_dev/emulate.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash

socat -d -d pty,raw,echo=0,link=../fakettyACM0 exec:./payload.sh
7 changes: 7 additions & 0 deletions files/emu_dev/payload.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/bin/bash

while true; do
data="12 23 45 66 554 34 23 54 12 32 32 43"
echo "$data"
sleep 10
done
24 changes: 23 additions & 1 deletion files/opensesame.spec
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ description = which chat to use for sending ping messages. Note: this chat will
required =
check/length/max = 8

[nextcloud/chat/commands]
description = which chat is used to send commands to Opensesame.
fel115 marked this conversation as resolved.
Show resolved Hide resolved
required =
check/length/max = 8

[nextcloud/format/time]
description=Format to be used for formatting time within Nextcloud messages, e.g. when entry gets prohibited because of time. By default ISO 8601 (Hour-minute-second format). Example is locales time.
see/#0 = nextcloud/format/date
Expand Down Expand Up @@ -116,16 +121,29 @@ type = boolean
example = 1
default = 0

[watchdog/interval]
description=Trigger interval in seconds for Watchdog
default = 10

[watchdog/path]
description=Device path of Watchdog
default = "/dev/watchdog"

[environment/device]
description=Which device to use for the environment sensor. /dev/null means that no environment sensor is connected.
example = "/dev/i2c"
default = "/dev/null"

[environment/data/interval]
description=How often to get new data (default: every 60 seconds, which is the highest interval)
default = 6000
default = 60
type = unsigned_short

[sensors/enable]
fel115 marked this conversation as resolved.
Show resolved Hide resolved
description = If the sensors module is enabled
type = boolean
default = 0

[sensors]
description = a list of up to 12 MQ135 sensors

Expand Down Expand Up @@ -161,6 +179,9 @@ type = unsigned_short
description = maximum value measured during calibration
type = unsigned_short

[sensors/device]
description = path of the sensor device
default = "/dev/ttyACM0"

[weatherstation/enable]
description = enables/disables weatherstation
Expand All @@ -173,6 +194,7 @@ description = Which Opensensemap senseBoxes should be connected to the weather s

[weatherstation/opensensemap/token]
description = Access-Token for Opensensemap senseBoxes, see doc/Opensensemap.md

[ir/enable]
description=enables/disables MOD-IR-TEMP sensor
example=1
Expand Down
Loading