Class: Backend::SensorsController

Inherits:
BaseController show all
Defined in:
app/controllers/backend/sensors_controller.rb

Instance Attribute Summary

Attributes inherited from ApplicationController

#current_theme

Instance Method Summary collapse

Methods inherited from BaseController

account_lettering_states_crit, accounts_range_crit, amount_range_crit, crit_params, journal_entries_states_crit, journal_letter_crit, journal_period_crit, journals_crit, search_conditions

Methods inherited from ApplicationController

#after_sign_in_path_for, #authorized?, human_action_name, #human_action_name, #session_controller?

Instance Method Details

#detailObject

Updates details and settings in form


66
67
68
69
70
71
72
73
74
75
# File 'app/controllers/backend/sensors_controller.rb', line 66

def detail
  @equipment = ActiveSensor::Equipment.find!(params[:vendor_euid], params[:model_euid])

  # Load existing resource for edit
  @sensor = Sensor.find(params[:id]) if params[:id].present?

  respond_to do |format|
    format.js
  end
end

#last_locationsObject


85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'app/controllers/backend/sensors_controller.rb', line 85

def last_locations
  @geolocations = Sensor
                  .includes(:analyses)
                  .find_each
                  .map do |sensor|
    last_analysis = sensor.analyses.last
    if last_analysis.present? && (geoloc = last_analysis.geolocation)
      [sensor.id, geoloc && JSON(geoloc.to_json)]
    end
  end
                  .compact
                  .to_h
  render json: @geolocations.to_json
end

#modelsObject


52
53
54
55
56
57
58
59
60
61
62
63
# File 'app/controllers/backend/sensors_controller.rb', line 52

def models
  vendor_euid = params[:vendor_euid]
  models = []
  if vendor_euid
    models = ActiveSensor::Equipment.equipments_of(vendor_euid).collect do |equipment|
      [equipment.label, equipment.model]
    end
  end
  respond_to do |format|
    format.json { render json: models }
  end
end

#retrieveObject


77
78
79
80
81
82
83
# File 'app/controllers/backend/sensors_controller.rb', line 77

def retrieve
  @sensor = find_and_check
  return unless @sensor
  # SensorReadingJob.perform_later(id: @sensor.id, started_at: Time.now, stopped_at: Time.now)
  @sensor.retrieve(started_at: Time.zone.now - 1.hour, stopped_at: Time.zone.now)
  redirect_to params[:redirect] || { action: :show, id: params[:id] }
end