|
| 1 | +--- |
| 2 | +related: |
| 3 | + - title: Send a Custom Notification Event |
| 4 | + url: send-a-custom-notification-event.md |
| 5 | + - title: Send Messages to SciLog |
| 6 | + url: send-messages-to-scilog.md |
| 7 | + - title: Send Messages to Signal |
| 8 | + url: send-messages-to-signal.md |
| 9 | + - title: Core Services |
| 10 | + url: ../../learn/system-architecture/overview/core-services.md |
| 11 | +--- |
| 12 | + |
| 13 | +# Set up auto notifications |
| 14 | + |
| 15 | +!!! Info "Overview" |
| 16 | + Configure BEC to send automatic notifications for scan and alarm events through a messaging service such as SciLog or Signal. |
| 17 | + |
| 18 | +## Prerequisites |
| 19 | + |
| 20 | +- You have a running BEC IPython client session. |
| 21 | +- The messaging service you want to use is enabled for your deployment or session. |
| 22 | +- You know which target scope to use for that service, for example a SciLog logbook or a Signal group scope. |
| 23 | + |
| 24 | +## Notification interface and event types |
| 25 | + |
| 26 | +Auto notifications are configured per messaging service from the BEC client with: |
| 27 | + |
| 28 | +```py |
| 29 | +bec.messaging.<service>.set_auto_notifications(event_type, enabled=True, scopes=...) |
| 30 | +``` |
| 31 | + |
| 32 | +Supported event types are: |
| 33 | + |
| 34 | +<table> |
| 35 | + <colgroup> |
| 36 | + <col style="width: 28%;"> |
| 37 | + <col style="width: 72%;"> |
| 38 | + </colgroup> |
| 39 | + <thead> |
| 40 | + <tr> |
| 41 | + <th>Event type</th> |
| 42 | + <th>Emitted when</th> |
| 43 | + </tr> |
| 44 | + </thead> |
| 45 | + <tbody> |
| 46 | + <tr> |
| 47 | + <td><code>"new_scan"</code></td> |
| 48 | + <td>A scan enters the <code>open</code> state, meaning BEC has started the scan.</td> |
| 49 | + </tr> |
| 50 | + <tr> |
| 51 | + <td><code>"scan_completed"</code></td> |
| 52 | + <td>A scan finishes with the status <code>closed</code> or <code>user_completed</code>.</td> |
| 53 | + </tr> |
| 54 | + <tr> |
| 55 | + <td><code>"alarm_warning"</code></td> |
| 56 | + <td>BEC raises an alarm with warning severity.</td> |
| 57 | + </tr> |
| 58 | + <tr> |
| 59 | + <td><code>"alarm_minor"</code></td> |
| 60 | + <td>BEC raises an alarm with minor severity.</td> |
| 61 | + </tr> |
| 62 | + <tr> |
| 63 | + <td><code>"alarm_major"</code></td> |
| 64 | + <td>BEC raises an alarm with major severity.</td> |
| 65 | + </tr> |
| 66 | + <tr> |
| 67 | + <td><code>"scan_interlock"</code></td> |
| 68 | + <td>The scan interlock is triggered because beamline states do not match, and again when that interlock is cleared.</td> |
| 69 | + </tr> |
| 70 | + </tbody> |
| 71 | +</table> |
| 72 | + |
| 73 | +!!! tip "Custom event names" |
| 74 | + |
| 75 | + You can also use custom event names. BEC supports any event string, as long as something in your deployment publishes notifications with the same event name. |
| 76 | + |
| 77 | + To learn how to publish one of these events from custom BEC code, see [Send a Custom Notification Event](send-a-custom-notification-event.md){ data-preview }. |
| 78 | + |
| 79 | + For example, if a custom service or plugin publishes a `beamline_ready` notification, you can route it like this: |
| 80 | + |
| 81 | + ```py |
| 82 | + bec.messaging.scilog.set_auto_notifications( |
| 83 | + "beamline_ready", |
| 84 | + enabled=True, |
| 85 | + scopes="default", |
| 86 | + ) |
| 87 | + ``` |
| 88 | + |
| 89 | +## 1. Enable an automatic notification |
| 90 | + |
| 91 | +Enable a notification by choosing the service, event, and target scope. |
| 92 | + |
| 93 | +For example, route new scan notifications to SciLog: |
| 94 | + |
| 95 | +```py |
| 96 | +bec.messaging.scilog.set_auto_notifications( |
| 97 | + "new_scan", |
| 98 | + enabled=True, |
| 99 | + scopes="default", |
| 100 | +) |
| 101 | +``` |
| 102 | + |
| 103 | +This stores a routing rule for the `new_scan` event. When a new scan starts, BEC forwards the notification to the configured SciLog scope. |
| 104 | + |
| 105 | +You can do the same for Signal when your deployment exposes a named Signal scope: |
| 106 | + |
| 107 | +```py |
| 108 | +bec.messaging.signal.set_auto_notifications( |
| 109 | + "alarm_major", |
| 110 | + enabled=True, |
| 111 | + scopes="beamline-ops", |
| 112 | +) |
| 113 | +``` |
| 114 | + |
| 115 | +## 2. Add more event routes |
| 116 | + |
| 117 | +You can enable more than one event for the same service: |
| 118 | + |
| 119 | +```py |
| 120 | +bec.messaging.scilog.set_auto_notifications( |
| 121 | + "scan_completed", |
| 122 | + enabled=True, |
| 123 | + scopes="default", |
| 124 | +) |
| 125 | + |
| 126 | +bec.messaging.scilog.set_auto_notifications( |
| 127 | + "scan_interlock", |
| 128 | + enabled=True, |
| 129 | + scopes="default", |
| 130 | +) |
| 131 | +``` |
| 132 | + |
| 133 | +You can also route the same event to more than one service. For example, keep SciLog for the permanent record and send major alarms to Signal as well: |
| 134 | + |
| 135 | +```py |
| 136 | +bec.messaging.scilog.set_auto_notifications( |
| 137 | + "alarm_major", |
| 138 | + enabled=True, |
| 139 | + scopes="default", |
| 140 | +) |
| 141 | + |
| 142 | +bec.messaging.signal.set_auto_notifications( |
| 143 | + "alarm_major", |
| 144 | + enabled=True, |
| 145 | + scopes="beamline-ops", |
| 146 | +) |
| 147 | +``` |
| 148 | + |
| 149 | +## 3. Use the default scope |
| 150 | + |
| 151 | +If your messaging service already has a suitable default scope, set it once and omit `scopes=` afterwards: |
| 152 | + |
| 153 | +```py |
| 154 | +bec.messaging.scilog.set_default_scope("default") |
| 155 | + |
| 156 | +bec.messaging.scilog.set_auto_notifications( |
| 157 | + "new_scan", |
| 158 | + enabled=True, |
| 159 | +) |
| 160 | +``` |
| 161 | + |
| 162 | +This is useful when most notifications should go to the same logbook or group. |
| 163 | + |
| 164 | +## 4. Disable an automatic notification |
| 165 | + |
| 166 | +Disable a route by setting `enabled=False` for the same event and scope: |
| 167 | + |
| 168 | +```py |
| 169 | +bec.messaging.scilog.set_auto_notifications( |
| 170 | + "new_scan", |
| 171 | + enabled=False, |
| 172 | + scopes="default", |
| 173 | +) |
| 174 | +``` |
| 175 | + |
| 176 | +If that was the only route for this service and event, BEC removes it from the notification configuration. |
| 177 | + |
| 178 | +!!! success "Congratulations!" |
| 179 | + |
| 180 | + You have configured automatic notifications in BEC. You can now route scan and alarm events to your messaging services without sending each message manually. |
| 181 | + |
| 182 | +## Common pitfalls |
| 183 | + |
| 184 | +- `set_auto_notifications(...)` raises an error if the messaging service is not enabled for the current deployment or session. |
| 185 | +- Use the supported event strings exactly as shown above. |
| 186 | +- For services with configured scopes such as SciLog, the scope must exist before you enable auto notifications. |
| 187 | +- Signal auto notifications are typically routed to a configured Signal scope such as a group, not to an arbitrary phone number. |
0 commit comments