-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Scheduling of control loops, chart improvements
- Loading branch information
Showing
32 changed files
with
327 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,4 +12,5 @@ | |
* | ||
*= require_tree . | ||
*= require_self | ||
*= require amcharts/dist/amcharts/plugins/export/export.css | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,5 @@ | ||
class ApplicationJob < ActiveJob::Base | ||
#before_perform do |job| | ||
# ActiveRecord::Base.clear_active_connections! | ||
#end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,26 @@ | ||
class ControlLoopJob < ApplicationJob | ||
queue_as :control_loops | ||
queue_as :run_control_loops | ||
|
||
def perform | ||
puts "TESTE" | ||
Rails.logger.debug "*********** DEBUG: I'm here" | ||
end | ||
def perform(control_loop_id) | ||
require Rails.root.join('lib', 'cervejator_mqtt').to_s | ||
|
||
def do_perform(control_loop_id) | ||
require 'mqtt' | ||
controller = ControlLoop.find(control_loop_id) | ||
action = controller.run | ||
|
||
return if action == :inactive | ||
|
||
MQTT::Client.connect(host: ENV['MQTT_BROKER_HOST'], | ||
username: 'mosquitto', | ||
port: ENV['MQTT_BROKER_PORT']) do |client| | ||
# parameters are: topic, payload, retain flag | ||
# the retain flag means that the broker will store the last message for | ||
# clients that subscribe later. Those will receive immediatelly the last | ||
# retained message. | ||
client.publish("actuators/#{controller.actuator.write_key}", action, true, 1) | ||
end | ||
cli = Cervejator::MQTT.instance | ||
|
||
# parameters are: topic, payload, retain flag | ||
# the retain flag means that the broker will store the last message for | ||
# clients that subscribe later. Those will receive immediatelly the last | ||
# retained message. | ||
# IDEALLY WE WOULD SEND RETAINED MESSAGES, BUT UNTIL I FIND A WAY FOR THE | ||
# MOSQUITTO BROKER TO ONLY RETAIN THE LAST MESSAGE PER TOPIC, I WILL REFRAIN | ||
# FROM USING THIS, BECAUSE IT GENERATES A HUGE BACKLOG OF MESSAGES WHEN THE | ||
# SUBSCRIBER IS OFFLINE | ||
cli.publish("actuator/#{controller.actuator.write_key}", action, false, 1) | ||
|
||
controller.update_attribute(:next_run, Time.now + controller.sampling_rate.seconds) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
class ScheduleControlLoopsJob < ApplicationJob | ||
queue_as :control_loops | ||
|
||
def perform | ||
ControlLoop.all.each do |cl| | ||
ActiveJob::Base.logger.debug "Checking #{cl.name} for run" | ||
if Time.now >= cl.next_run | ||
# enqueue assynchronous action, we don't want to hang around | ||
# here for too long | ||
ControlLoopJob.perform_later(cl.id) | ||
ActiveJob::Base.logger.info "Controle Loop ##{cl.id}:#{cl.name} enqueued to run" | ||
end | ||
|
||
# Let's test for missed deadlines | ||
if Time.now >= cl.next_run + cl.sampling_rate.seconds | ||
# this means the ControlLoopJob is not keeping up | ||
ActiveJob::Base.logger.error "Controle Loop ##{cl.id}:#{cl.name} is missing deadlines!" | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
class UpdateActuatorJob < ApplicationJob | ||
queue_as :default | ||
|
||
def perform(actuator_id, output) | ||
if actuator = Actuator.find(actuator_id) | ||
actuator.update_attribute(:output, output) if Actuator.outputs.keys.include?(output) | ||
else | ||
Rails.logger.error "UpdateActuatorJob#perform could not find Actuator with id #{actuator_id}" | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,9 @@ | ||
class Datum < ApplicationRecord | ||
belongs_to :sensor | ||
|
||
#serialize :measured_at, FormatedDateTime | ||
|
||
def measured_at_formatted | ||
self[:measured_at].strftime('%Y-%m-%d %H:%M:%S') | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
json.extract! sensor, :id, :name, :created_at, :updated_at | ||
json.extract! sensor, :id, :name, :measured_at, :created_at, :updated_at | ||
json.url sensor_url(sensor, format: :json) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
json.extract! datum, :created_at, :value | ||
json.extract! datum, :measured_at_formatted, :value | ||
#json.url sensor_data_url(datum, format: :json) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,5 @@ | ||
json.array! @data, partial: 'sensors/data/datum', as: :datum | ||
#json.array! @data, partial: 'sensors/data/datum', as: :datum | ||
json.array! @data do |datum| | ||
json.measured_at_formatted datum.measured_at_formatted | ||
json.value datum.value | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.