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

SumState-Alerting #3

Merged
merged 7 commits into from
Jan 12, 2024
Merged
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
6 changes: 3 additions & 3 deletions openems/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,10 @@
"website": "https://openems.io",
"license": "AGPL-3",
"category": "Specific Industry Applications",
"depends": ["base", "web", "mail", "crm", "resource", "stock", "web_m2x_options"],
"depends": ["base", "web", "mail", "crm", "resource", "stock", "web_m2x_options", "partner_firstname"],
"data": [
"data/ir_config_parameter.xml",
"data/res_partner_category.xml",
"data/ir_attachment_logos.xml",
"security/openems.xml",
"security/ir.model.access.csv",
"report/setup_protocol.xml",
Expand All @@ -24,7 +23,8 @@
"views/setup_protocol.xml",
"views/user.xml",
"views/stock_production_lot_views.xml",
"mail/openems/alerting_generic.xml",
"mail/openems/alerting_offline.xml",
"mail/openems/alerting_sum_state.xml",
"mail/openems/setup_protocol_customer.xml",
"mail/openems/setup_protocol_installer.xml",
"mail/openems/user_registration.xml",
Expand Down
69 changes: 51 additions & 18 deletions openems/controllers/alerting.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
import logging
from datetime import datetime
from enum import Enum

from odoo import http
from odoo.http import request

class SumState(Enum):
FAULT = 0
WARNING = 1

class Message:
sentAt: datetime
edgeId: str
Expand All @@ -13,41 +18,69 @@ def __init__(self, sentAt: datetime, edgeId: str, userIds: list[int]) -> None:
self.sentAt = sentAt
self.edgeId = edgeId
self.userIds = userIds

class SumStateMessage(Message):
state: SumState

def __init__(self, sentAt: datetime, edgeId: str, userIds: list[int], state: SumState) -> None:
super().__init__(sentAt, edgeId, userIds)
self.state = state

class Alerting(http.Controller):
__logger = logging.getLogger("Alerting")

@http.route("/openems_backend/send_alerting_email", type="json", auth="user")
def old(self, ids: list, now: str, edgeId: str = ''):
params = [{"edgeId":edgeId, "recipients":ids}]
self.index(now, params)
@http.route("/openems_backend/mail/alerting_sum_state", type="json", auth="user")
def sum_state_alerting(self, sentAt: str, params: list[dict]):
msgs = self.__get_sum_state_params(sentAt, params)
update_func = lambda role, at: { role.write({"sum_state_last_notification": at})}

if len(msgs) == 0:
self.__logger.error("Scheduled SumState-Alerting-Mail without any recipients!!!")

template = request.env.ref('openems.alerting_sum_state')
for msg in msgs:
self.__send_mails(template, msg, update_func)

return {}

@http.route("/openems_backend/mail/alerting_email", type="json", auth="user")
def index(self, sentAt: str, params: list[dict]):
msgs = self.__get_params(sentAt, params)
@http.route("/openems_backend/mail/alerting_offline", type="json", auth="user")
def offline_alerting(self, sentAt: str, params: list[dict]):
msgs = self.__get_offline_params(sentAt, params)
update_func = lambda role, at: { role.write({"offline_last_notification": at})}

template = request.env.ref("openems.alerting_offline")
for msg in msgs:
template = self.__get_template(msg.edgeId)
self.__send_mails(template, msg)
self.__send_mails(template, msg, update_func)

return {}

def __get_params(self, sentAt, params) -> list[Message]:
def __get_offline_params(self, sentAt, params) -> list[Message]:
msgs = list()
sent = datetime.strptime(sentAt, "%Y-%m-%d %H:%M:%S")
for param in params:
msgs.append(Message(sent, param["edgeId"], param["recipients"]));
edgeId = param["edgeId"]
recipients = param["recipients"]
msgs.append(Message(sent, edgeId, recipients));
return msgs

def __get_sum_state_params(self, sentAt, params) -> list[SumStateMessage]:
msgs = list()
sent = datetime.strptime(sentAt, "%Y-%m-%d %H:%M:%S")
for param in params:
edgeId = param["edgeId"]
recipients = param["recipients"]
state = param["state"]
msgs.append(SumStateMessage(sent, edgeId, recipients, state));
return msgs

def __get_template(self, device_id):
template = request.env.ref("openems.alerting_email_generic")
return template

def __send_mails(self, template, msg: Message):
roles = http.request.env["openems.device_user_role"].search([("id","in",msg.userIds),("device_id","=",msg.edgeId)])
def __send_mails(self, template, msg: Message, update_func):
roles = http.request.env['openems.alerting'].search(
[('user_id','in',msg.userIds),('device_id','=',msg.edgeId)]
)

for role in roles:
try:
template.send_mail(res_id=role.id, force_send=True)
role.write({"last_notification": msg.sentAt})
update_func(role, msg.sentAt)
except Exception as err:
self.__logger.error("[" + str(err) + "] Unable to send template[" + str(template.name) +"] to edgeUser[user=" + str(role.id) + ", edge=" + str(msg.edgeId)+ "]")
13 changes: 0 additions & 13 deletions openems/data/ir_attachment_logos.xml

This file was deleted.

Loading