Reputation: 357
I am doing a project of Internet of things. I got a raspberry pi with various sensors, this raspberry write data on a SQL Server Database every minute. I have a platform in ruby on rails to users see the data. I want to create a new feature, users can create multiple alarms. I got a form in a page of application to users define alarms.
Example: User set maximum temperature in Friday to 23º. If temperature up to 23º in Friday the user receive a message (this is a easy alarm can be more difficult).
How can I do this?
I don't want to create triggers in SQL sever cause the querys can be hard (I dont have experience on SQL Server).
Upvotes: 0
Views: 75
Reputation: 1005
The way I've done it is to use a scheduler, e.g. https://github.com/jmettraux/rufus-scheduler and have it call a static method, that checks if, for each alarm condition being met, and then sends a message.
Your model would be, maybe
AlarmType - an STI with multiple classes - AlarmIfTemperatureTooHigh, AlarmPrecipiations etc.
An Alarm class, which belongs to a User and belongs_to AlarmType, and contains the parameters set up for the user - so AlarmTemperature might have a max temperature, as a variable for instance.
Then from the scheduler it would call a static method every x minutes (whatever you want) that would iterate through the AlarmTypes, which in turn would iterate through the Alarms that belongs to it, and if the conditions are met, message the user (who also belongs to that Alarm). As AlertType is a STI you can have different methods to assess the data for each AlertType, and you can have as many as you like, as simple as complex as you like. I'd start simple. :)
STI is a little bit contreverisal perhaps, but for me, I like having one single:
has_many :alarm_types.
alarm_types.all do |alarm_type|
alarm_type.run
end
Without having to revisit the code every time I add a new alarm type.
Upvotes: 1