|
| 1 | +import uuid |
| 2 | +from datetime import datetime |
| 3 | + |
| 4 | +from nisystemlink.clients.alarm import AlarmClient |
| 5 | +from nisystemlink.clients.alarm.models._alarm import Alarm, AlarmSeverityLevel |
| 6 | +from nisystemlink.clients.alarm.models._create_or_update_alarm_request import ( |
| 7 | + CreateOrUpdateAlarmRequest, |
| 8 | + SetAlarmTransition, |
| 9 | +) |
| 10 | +from nisystemlink.clients.core import HttpConfiguration |
| 11 | +from nisystemlink.clients.notification import NotificationClient |
| 12 | +from nisystemlink.clients.notification.models import ( |
| 13 | + DynamicNotificationConfiguration, |
| 14 | + DynamicNotificationStrategy, |
| 15 | + DynamicStrategyRequest, |
| 16 | + SmtpAddressFields, |
| 17 | + SmtpAddressGroup, |
| 18 | + SmtpMessageTemplate, |
| 19 | + SmtpMessageTemplateFields, |
| 20 | +) |
| 21 | + |
| 22 | +# Server configuration is not required when used with SystemLink Client or run through Jupyter on SystemLink |
| 23 | +server_configuration: HttpConfiguration | None = None |
| 24 | + |
| 25 | +# To set up the server configuration to point to your instance of SystemLink Enterprise, uncomment |
| 26 | +# the following lines and provide your server URI and API key. |
| 27 | +# server_configuration = HttpConfiguration( |
| 28 | +# server_uri="https://yourserver.yourcompany.com", |
| 29 | +# api_key="", |
| 30 | +# ) |
| 31 | + |
| 32 | + |
| 33 | +# Create request for applying strategy |
| 34 | +def create_notification_request_for_alarm( |
| 35 | + alarm: Alarm, |
| 36 | + address_group: SmtpAddressGroup, |
| 37 | + message_template: SmtpMessageTemplate, |
| 38 | +) -> DynamicStrategyRequest: |
| 39 | + """Creates and returns a dynamic strategy request.""" |
| 40 | + occurred_at = alarm.most_recent_transition_occurred_at |
| 41 | + |
| 42 | + return DynamicStrategyRequest( |
| 43 | + message_template_substitution_fields={ |
| 44 | + "alarm_id": alarm.alarm_id, |
| 45 | + "alarm_condition": alarm.condition, |
| 46 | + "alarm_description": alarm.description, |
| 47 | + "alarm_severity": str(alarm.current_severity_level), |
| 48 | + "alarm_occurred_at": occurred_at.isoformat() if occurred_at else "", |
| 49 | + }, |
| 50 | + notification_strategy=DynamicNotificationStrategy( |
| 51 | + notification_configurations=[ |
| 52 | + DynamicNotificationConfiguration( |
| 53 | + address_group=address_group, |
| 54 | + message_template=message_template, |
| 55 | + ) |
| 56 | + ] |
| 57 | + ), |
| 58 | + ) |
| 59 | + |
| 60 | + |
| 61 | +# Create clients for Notification and Alarm services |
| 62 | +notification_client = NotificationClient(configuration=server_configuration) |
| 63 | +alarm_client = AlarmClient(configuration=server_configuration) |
| 64 | + |
| 65 | +# Create a unique alarm ID for this example |
| 66 | +alarm_id = f"example_alarm_{uuid.uuid1().hex}" |
| 67 | + |
| 68 | +# Create an alarm with a SET transition |
| 69 | +create_alarm_request = CreateOrUpdateAlarmRequest( |
| 70 | + alarm_id=alarm_id, |
| 71 | + transition=SetAlarmTransition( |
| 72 | + occurred_at=datetime.now(), |
| 73 | + severity_level=AlarmSeverityLevel.HIGH, |
| 74 | + value="85", |
| 75 | + condition="Greater than 80", |
| 76 | + short_text="Temperature is high", |
| 77 | + detail_text="Temperature sensor reading is 85°C (higher than the configured threshold of 80°C)", |
| 78 | + ), |
| 79 | + description="Example alarm for notification", |
| 80 | +) |
| 81 | +id = alarm_client.create_or_update_alarm(create_alarm_request) |
| 82 | +print("Alarm created successfully") |
| 83 | + |
| 84 | +# Get the alarm by its instance ID (the unique occurrence identifier) |
| 85 | +retrieved_alarm = alarm_client.get_alarm(instance_id=id) |
| 86 | + |
| 87 | +# Define recipients to notify |
| 88 | +recipients = SmtpAddressFields(toAddresses=["sample1@example.com"]) |
| 89 | + |
| 90 | +# Create address group |
| 91 | +address_group = SmtpAddressGroup( |
| 92 | + display_name="Alarm Notification Recipients", |
| 93 | + properties={"address group": "Alarm"}, |
| 94 | + fields=recipients, |
| 95 | +) |
| 96 | + |
| 97 | +# Create mail template for alarm creation notification |
| 98 | +alarm_creation_template = SmtpMessageTemplate( |
| 99 | + display_name="Alarm Creation Template", |
| 100 | + fields=SmtpMessageTemplateFields( |
| 101 | + subject_template="Alarm Created: <alarm_id>", |
| 102 | + body_template="An alarm with ID <alarm_id> has been created.\n" |
| 103 | + "Condition: <alarm_condition>\n" |
| 104 | + "Description: <alarm_description>\n" |
| 105 | + "Current severity: <alarm_severity>\n" |
| 106 | + "Occurred At: <alarm_occurred_at>", |
| 107 | + ), |
| 108 | +) |
| 109 | + |
| 110 | +# Send notification for alarm creation |
| 111 | +notification_for_alarm_creation = create_notification_request_for_alarm( |
| 112 | + alarm=retrieved_alarm, |
| 113 | + address_group=address_group, |
| 114 | + message_template=alarm_creation_template, |
| 115 | +) |
| 116 | +notification_client.apply_dynamic_notification_strategy( |
| 117 | + request=notification_for_alarm_creation |
| 118 | +) |
| 119 | +print("Notification sent for alarm creation") |
0 commit comments