Problem/Motivation

Sensors in farmOS 1.x had the ability to configure an email notification if a named sensor value was greater than/less than a specific amount. We need this for farmOS 2.x!

A limitation of this system in farmOS 1.x was that only one notification could be configured per sensor, limiting notifications to a single condition (greater than/less than) for only one named value. It's pretty reasonable to want two conditions per named value, eg: high and low temperatures.

Proposed resolution

Create an optional data_stream_notification module.

Since each "named value" will be a separate data stream entity in farmOS 2.x this makes it much easier to support notifications for each named value provided by a sensor by associating them with the data stream rather than the sensor asset.

We could make a custom "data_stream_notification" field type with columns for the 3 required values: condition, value threshold and email addresses. Abstracting this into a field type would allow multiple notifications to be configured per data stream entity.

The "condition" could be implemented as a plugin system so contrib could provide other types of conditions that might trigger a notification. Each condition plugin should define which data stream types it supports. Core farmOS can come with a greater/less than condition. One issue with this is that other conditions may need more configuration than a single threshold field - maybe we could let each condition save it's own JSON object of config in the DB?

Similarly, it would be neat if the "email" part of the notification could be abstracted out as a "delivery" plugin system as well. This could allow for a single notification condition to be delivered in one more ways (webhook, push notification, text..) This might have the same issue as conditions where each will need different config though. I imagine it being useful to include a user reference here, but that introduces some kind of entity reference field which might not be best to save in a JSON object.

Last, there will need to be some kind of mechanism to trigger these conditional notifications. Right now that happens when saving sensor data, but it would be nice to be able to trigger the computation at different times, like on a time interval (see #2948932: Offline sensor notification). This could be supported by creating a data stream notification event that could specify a context (data save, cron, etc.) Different condition plugins might only support some of these event contexts. This would be useful for data streams that are saving data outside of farmOS. *Triggering the event on a time interval is complicated and would not be implemented here, just want to provide a framework to support that!*

Remaining tasks

- Optionally implement a notification condition plugin system? Provide greater/less than by default.
- Optionally implement a notification delivery plugin system? Provide email by default.

- Create a notification field type, columns dependent if we implement condition and deliver plugins.

- Create a data stream notification event. The event class can be pretty simple, just needs to identify the data stream entity and support a context to be passed with it (I believe this is how the event system already works - perhaps we only hard code a data_save context for now).

- Dispatch notification event when data is saved for standard data streams.

- Tests

User interface changes

Likely add a details element to hold the notification configuration in the data stream edit form.

API changes

None? Perhaps add a

Data model changes

Add a notification field of a custom notification field type to all data stream bundles.

Comments

paul121 created an issue. See original summary.

m.stenta’s picture

Chatted with @paul121 a bit about this today. We decided to explore a config entity approach instead of a field approach.

Also, we need a migration of old notification config to 2.x. That is the primary reason this is a "beta blocker".

paul121’s picture

Assigned: Unassigned » paul121
Status: Active » Needs work

I've been working on this here: https://github.com/paul121/farmOS/tree/2.x-data_stream_notifications

I think I have most everything implemented. It's coming along well. Some notable things that still need work:
- Adding the migration from 1.x - this is next up! Should be pretty easy!
- Improve the UI for adding/referencing a data stream.
- Perhaps provide the reverse, from a data stream page, provide an "Add notification" button?

I also opened some issues for next steps - I don't want them to block this issue, but I think that they are features we should consider before a 2.x stable release:
- #3230360: Data stream notifications token replacement
- #3230362: Make data stream condition logic more flexible

Another feature we discussed adding since we are using a config entity approach (rather than a field) is support for multiple data streams - eg: multiple data streams could use the same "Freezing" notification.

I've just implemented the the "activation_threshold" and "deactivation_threshold" feature which allows a notification to only be sent once per set of time, rather than for every individual event. This activation and deactivation state is kept using the Drupal State API (a great idea from @m.stenta!) Currently this logic doesn't take the configured data stream into account, but if we introduce support for multiple data streams, this state would need to be kept for each separate data stream. It wouldn't very hard to add this, but just another level of complexity.

paul121’s picture

- Adding the migration from 1.x - this is next up! Should be pretty easy!

Migration is done! It isn't a simple migration, but it didn't require any PHP code which I'm quite happy about!

https://github.com/paul121/farmOS/commit/45fbd200a857b5f86e8d595f1b5c650...

paul121’s picture

I've been working on this here: https://github.com/paul121/farmOS/tree/2.x-data_stream_notifications

I meant to say, this branch is dependent (and rebased) on my 2.x-data_stream_misc branch which I believe @m.stenta is reviewing. That branch should be merged before any data stream notifications. I'll do a final rebase once that is done.

m.stenta’s picture

2.x-data_stream_misc has been merged.

  • m.stenta committed 72b485e on 2.x
    Issue #3215938 by paul121: Data stream email notifications
    
m.stenta’s picture

Status: Needs work » Fixed

Merged! Thanks @paul121!

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.