Skip to content

Commit

Permalink
Add option to control which product(name)s should trigger a notification
Browse files Browse the repository at this point in the history
Signed-off-by: Adam.Dybbroe <a000680@c21856.ad.smhi.se>
  • Loading branch information
Adam.Dybbroe committed Aug 28, 2023
1 parent 21c7a1a commit 79271cd
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 8 deletions.
18 changes: 11 additions & 7 deletions activefires_pp/fire_notifications.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

# Copyright (c) 2021, 2022 Adam Dybbroe
# Copyright (c) 2021 - 2023 Adam Dybbroe

# Author(s):

Expand All @@ -20,8 +20,7 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

"""Creating and sending notifications for detected forest fires.
"""
"""Creating and sending notifications for detected forest fires."""

import socket
from netrc import netrc
Expand Down Expand Up @@ -56,8 +55,10 @@


class RecipientDataStruct(object):
def __init__(self):
"""A data structure to control the list of configured recipients."""

def __init__(self):
"""Initialize the recipient data structure."""
self.recipients_with_attachment = []
self.recipients_without_attachment = []
self.recipients_all = []
Expand Down Expand Up @@ -140,7 +141,6 @@ def _setup_and_start_communication(self):

def _set_options_from_config(self, config):
"""From the configuration on disk set the option dictionary, holding all metadata for processing."""

for item in config:
self.options[item] = config[item]

Expand Down Expand Up @@ -184,6 +184,12 @@ def run(self):
LOG.debug("Message type not supported: %s", str(msg.type))
continue

product_name = msg.data.get('product')
product_list = self.options.get('products')
if product_list and product_name and product_name not in product_list:
LOG.info('Product %s will not generate a notification!', product_name)
continue

output_msg = self.notify_end_users(msg)
if output_msg:
LOG.debug("Sending message: %s", str(output_msg))
Expand Down Expand Up @@ -218,7 +224,6 @@ def notify_end_users(self, msg):

def _send_notifications_with_attachments(self, server, recipients, full_message, filename, platform_name):
"""Send notifications with attachments."""

notification = MIMEMultipart()
notification['From'] = self.sender
if platform_name:
Expand Down Expand Up @@ -252,7 +257,6 @@ def _send_notifications_with_attachments(self, server, recipients, full_message,

def _send_notifications_without_attachments(self, server, recipients, sub_messages, platform_name):
"""Send notifications without attachments."""

for submsg in sub_messages:
notification = MIMEMultipart()
notification['From'] = self.sender
Expand Down
8 changes: 7 additions & 1 deletion activefires_pp/tests/test_fire_notifications.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@
publish_topic: VIIRS/L2/MSB/National
subscribe_topics: VIIRS/L2/Fires/PP/National
products:
- afimg
- afimg_some_other_geoid
smtp_server: smtp.mydomain.se
domain: mydomain.se
Expand Down Expand Up @@ -137,7 +141,9 @@ def test_get_options_national_filtering(self, setup_comm, read_config, gethostna

expected = {'publish_topic': 'VIIRS/L2/MSB/National',
'subscribe_topics': ['VIIRS/L2/Fires/PP/National'],
'smtp_server': 'smtp.mydomain.se', 'domain': 'mydomain.se', 'sender': 'active-fires@mydomain.se',
'products': ['afimg', 'afimg_some_other_geoid'],
'smtp_server': 'smtp.mydomain.se',
'domain': 'mydomain.se', 'sender': 'active-fires@mydomain.se',
'recipients': ['recipient1@recipients.se', 'recipient2@recipients.se', 'recipient3@recipients.se'],
'recipients_attachment': ['recipient1@recipients.se', 'recipient2@recipients.se'],
'subject': 'My subject', 'max_number_of_fires_in_sms': 3,
Expand Down
4 changes: 4 additions & 0 deletions examples/fire_notifier.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
publish_topic: VIIRS/L2/MSB/National
subscribe_topics: VIIRS/L2/Fires/PP/National

products:
- afimg
- afimg_some_other_geoid

smtp_server: smtp.mydomain.se

domain: mydomain.se
Expand Down

0 comments on commit 79271cd

Please sign in to comment.