Skip to content

Commit

Permalink
fix response procedure part
Browse files Browse the repository at this point in the history
  • Loading branch information
LaQuay committed Apr 10, 2024
1 parent 0cff567 commit 3436436
Show file tree
Hide file tree
Showing 8 changed files with 107 additions and 25 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ Main repository for an article submitted.

In each folder, there is a README.md with more details.

To run the server, execute the `start.sh` script. To run the: `context_aware_engine`, `lora_client`,
To run the server, execute the `start.sh` script.
To run the: `context_aware_engine`, `lora_client`,
or `raspberry_pi_client`, each folder has to be run in a Python standalone instance.

## Requirements
Expand Down
8 changes: 6 additions & 2 deletions context_aware_engine/src/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
BASE_URL = "http://localhost:5000"

# BASE_URL = "http://localhost:5000"
BASE_URL = "http://localhost:8000/api"

API_ACTUATOR_ENDPOINT = "actuators"
API_ACTUATION_ENDPOINT = "actuations"
Expand All @@ -14,3 +14,7 @@
API_CONTEXT_AWARE_RULES_RESPONSE_PROCEDURES_ENDPOINT = "response-procedures"
API_PROCEDURE_TYPE_NAME_ENDPOINT = "procedure-types"

# SMTP_HOST = "smtp.gmail.com"
# SMTP_USER = "your_user_here"
# SMTP_PASS = "your_password_here"
# SMTP_EMAIL_SENDER = "your_sender_email_here"
34 changes: 32 additions & 2 deletions context_aware_engine/src/actuators.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,35 @@
def send_email():
print("This a dummy function to actually send a email")
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText

from src import SMTP_HOST, SMTP_USER, SMTP_PASS, SMTP_EMAIL_SENDER


def send_email(recipient: str, message: str):
config = {
"hostname": SMTP_HOST,
"port": 2525,
"username": SMTP_USER,
"password": SMTP_PASS,
"use_tls": True,
}
sender = SMTP_EMAIL_SENDER

msg = MIMEMultipart('mixed')
msg['Subject'] = 'Actuation from TOCA'
msg['From'] = sender
msg['To'] = recipient
msg.attach(MIMEText(message, 'plain'))

print(f"Sending email to {recipient}...")

server = smtplib.SMTP(config["hostname"], config["port"])
server.ehlo()
server.starttls()
server.ehlo()
server.login(config["username"], config["password"])
server.sendmail(sender, recipient, msg.as_string())
print("Email sent")


def send_http():
Expand Down
2 changes: 1 addition & 1 deletion context_aware_engine/src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
print("Starting up code...")

execute_context_aware_rules()
schedule.every(1).minute.do(execute_context_aware_rules)
schedule.every(30).seconds.do(execute_context_aware_rules)

while True:
schedule.run_pending()
Expand Down
25 changes: 15 additions & 10 deletions context_aware_engine/src/repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
import requests

from src import BASE_URL, API_SENSORS_ENDPOINT, API_SENSOR_OBSERVATIONS_ENDPOINT, \
API_ACTUATOR_ENDPOINT, API_ACTUATION_ENDPOINT, API_PROCEDURE_TYPE_NAME_ENDPOINT
API_ACTUATOR_ENDPOINT, API_ACTUATION_ENDPOINT, API_PROCEDURE_TYPE_NAME_ENDPOINT, \
API_CONTEXT_AWARE_RULES_RESPONSE_PROCEDURES_ENDPOINT
from src import actuators, core, utils


Expand Down Expand Up @@ -96,32 +97,36 @@ def check_condition_rule(condition_rule: dict) -> bool:

def execute_response_procedures(response_procedures: List):
for response_procedure in response_procedures:
actuator_name = response_procedure["actuator_name"]

url = f"{BASE_URL}{API_ACTUATOR_ENDPOINT}{actuator_name}"
print(f"Send GET to obtain Actuator {actuator_name}: {url}")
response_procedure_name = response_procedure['name']
print(f"Executing response procedure: {response_procedure_name}")
url = f"{BASE_URL}/{API_CONTEXT_AWARE_RULES_RESPONSE_PROCEDURES_ENDPOINT}/{response_procedure_name}/{API_ACTUATOR_ENDPOINT}"
print(f"Send GET to obtain Actuators: {url}")
r = requests.get(url)
actuator = json.loads(r.content)
actuator = json.loads(r.content)[0] # TODO Currently obtains the first Actuator
if not actuator:
return False

url = f"{BASE_URL}{API_ACTUATION_ENDPOINT}"
url = f"{BASE_URL}/{API_ACTUATION_ENDPOINT}/"
print(f"Send POST to create Actuation: {url}")
body = {
"time_start": utils.get_current_datetime_str(),
"actuator_name": actuator_name,
"actuator_name": actuator["name"],
"actuatable_property_name": actuator["actuatable_property_name"],
}
r = requests.post(url, json=body)
actuation = json.loads(r.content)
print(actuation)

procedure_type = response_procedure['procedure_type_name']
url = f"{BASE_URL}{API_PROCEDURE_TYPE_NAME_ENDPOINT}{procedure_type}"
url = f"{BASE_URL}/{API_PROCEDURE_TYPE_NAME_ENDPOINT}/{procedure_type}/"
print(f"Send GET to obtain ProcedureType {procedure_type}: {url}")
r = requests.get(url)
procedure = json.loads(r.content)

if procedure["procedure_type"] == "EMAIL":
actuators.send_email()
message = (f"The ContextAwareRule '{response_procedure['context_aware_rule_name']}' has raised a warning. "
f"The Actuator '{actuator['name']}' has been triggered at '{body['time_start']}'.")
actuators.send_email(recipient=actuator["info"], message=message)
elif procedure["procedure_type"] == "HTTP":
actuators.send_http()
break
Expand Down
1 change: 1 addition & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ services:
volumes:
- ./frontend/grafana/ENV/grafana.ini:/etc/grafana/grafana.ini
depends_on:
- kong
- postgres
networks:
- kong-ext-net
Expand Down
2 changes: 1 addition & 1 deletion frontend/grafana/ENV/grafana.ini
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ allow_embedding = true

[live]
# EXTERNAL_URL
allowed_origins = "http://34.122.80.205:8000"
allowed_origins = http://localhost:8000,http://34.122.80.205:8000
57 changes: 49 additions & 8 deletions frontend/website/src/config.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
console.log('Running in mode: ' + process.env.REACT_APP_MODE)

const dev_url = "http://localhost"
const prod_url = "http://34.122.80.205:8000" // EXTERNAL_URL
const prod_url = "http://localhost:8000"
//const prod_url = "http://34.122.80.205:8000" // EXTERNAL_URL

const dev = {
services: {
Expand Down Expand Up @@ -64,17 +65,57 @@ const dev = {

const prod = {
services: {
api_device: {
url_gateway: prod_url + '/api/gateways/',
url_node: prod_url + '/api/nodes/',
url_sensor: prod_url + '/api/sensors/'
api_actuatable_properties: {
url: prod_url + '/api/actuatable-properties/',
},
api_actuations: {
url: prod_url + '/api/actuations/'
},
api_actuators: {
url: prod_url + '/api/actuators/'
},
api_condition_rules: {
url: prod_url + '/api/condition-rules/'
},
api_context_aware_rules: {
url: prod_url + '/api/context-aware-rules/'
},
api_event_rules: {
url: prod_url + '/api/event-rules/'
},
api_event_rule_types: {
url: prod_url + '/api/event-rule-types/'
},
api_features_of_interest: {
url: prod_url + '/api/features-of-interest/'
},
api_device_type: {
url: prod_url + '/api/device-types/'
api_gateways: {
url: prod_url + '/api/gateways/'
},
api_observation: {
api_groups: {
url: prod_url + '/api/groups/'
},
api_locations: {
url: prod_url + '/api/locations/'
},
api_observable_properties: {
url: prod_url + '/api/observable-properties/'
},
api_observations: {
url: prod_url + '/api/observations/'
},
api_response_procedures: {
url: prod_url + '/api/response-procedures/'
},
api_procedure_types: {
url: prod_url + '/api/procedure-types/'
},
api_sensors: {
url: prod_url + '/api/sensors/'
},
api_things: {
url: prod_url + '/api/things/'
},
api_graph: {
url: prod_url + '/api/graph/'
},
Expand Down

0 comments on commit 3436436

Please sign in to comment.