Class: Sensor

Inherits:
Ekylibre::Record::Base show all
Includes:
Attachable, Customizable
Defined in:
app/models/sensor.rb

Overview

Informations

License

Ekylibre - Simple agricultural ERP Copyright (C) 2008-2009 Brice Texier, Thibaud Merigon Copyright (C) 2010-2012 Brice Texier Copyright (C) 2012-2019 Brice Texier, David Joulin

This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details.

You should have received a copy of the GNU Affero General Public License along with this program. If not, see www.gnu.org/licenses.

Table: sensors

access_parameters    :json
active               :boolean          default(TRUE), not null
battery_level        :decimal(19, 4)
created_at           :datetime         not null
creator_id           :integer
custom_fields        :jsonb
embedded             :boolean          default(FALSE), not null
euid                 :string
host_id              :integer
id                   :integer          not null, primary key
last_transmission_at :datetime
lock_version         :integer          default(0), not null
model_euid           :string
name                 :string           not null
partner_url          :string
product_id           :integer
retrieval_mode       :string           not null
token                :string
updated_at           :datetime         not null
updater_id           :integer
vendor_euid          :string

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Customizable

#custom_value, #set_custom_value, #validate_custom_fields

Methods inherited from Ekylibre::Record::Base

#already_updated?, #check_if_destroyable?, #check_if_updateable?, columns_definition, #customizable?, customizable?, #customized?, #destroyable?, #editable?, has_picture, #human_attribute_name, nomenclature_reflections, #old_record, #others, refers_to, #unsuppress, #updateable?

Methods included from Userstamp::Stampable

included

Methods included from Userstamp::Stamper

included

Class Method Details

.retrieve_all(options = {}) ⇒ Object

Get all sensors and retrieve data


146
147
148
149
150
151
152
153
154
155
# File 'app/models/sensor.rb', line 146

def retrieve_all(options = {})
  attributes = { active: true }
  attributes[:retrieval_mode] = options[:mode] || :listening
  default_interval = 1.hour
  options[:stopped_at] ||= Time.zone.now
  options[:started_at] ||= options[:stopped_at] - default_interval
  where(attributes).find_each do |sensor|
    sensor.retrieve(options)
  end
end

Instance Method Details

#alert_on?(alert_nature) ⇒ Boolean

Returns:

  • (Boolean)

90
91
92
93
# File 'app/models/sensor.rb', line 90

def alert_on?(alert_nature)
  alert = alerts.find_by(nature: alert_nature)
  alert.present? && alert.level > 0
end

#alert_statusObject


84
85
86
87
88
# File 'app/models/sensor.rb', line 84

def alert_status
  return :go if alerts.joins(:phases).all? { |alert| alert.level.zero? }
  return :stop if alerts.joins(:phases).none? { |alert| alert.level.zero? }
  :caution
end

#equipmentObject


80
81
82
# File 'app/models/sensor.rb', line 80

def equipment
  ActiveSensor::Equipment.find(vendor_euid, model_euid)
end

#notify(message, interpolations = {}, options = {}) ⇒ Object

Notify concerned people about issues on sensors For now, administrators are notified.


160
161
162
# File 'app/models/sensor.rb', line 160

def notify(message, interpolations = {}, options = {})
  User.notify_administrators(message, interpolations, options.merge(target: self))
end

#retrieve(options = {}) ⇒ Object

Read sensor indicator and write an analysis


96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'app/models/sensor.rb', line 96

def retrieve(options = {})
  return if listening?
  raise "Unknown equipment: vendor=#{vendor_euid}, model=#{model_euid}" unless equipment

  connection = equipment.connect(access_parameters)

  results = connection.retrieve(options)
  attributes = {
    retrieval_status: results[:status]
  }
  attributes[:nature] = results[:nature] || :sensor_analysis
  if results[:status].to_s == 'ok'
    # Indicators
    values = []
    results[:values].each do |k, v|
      values << { indicator_name: k, value: v } if v.present?
    end
    attributes.update(
      sampled_at: options[:started_at],
      analysed_at: options[:stopped_at],
      stopped_at: options[:stopped_at],
      geolocation: results[:geolocation],
      sampling_temporal_mode: results[:sampling_temporal_mode],
      items_attributes: values
    )
  else
    attributes[:retrieval_message] = results[:message]
  end
  analysis = analyses.create!(attributes)
  if analysis.status_changed? && options[:background]
    if analysis.ok? && analysis.previous
      notify(:sensor_has_been_fixed, { name: name }, level: :success)
    else
      notify(:sensor_is_out_of_service, { name: name, message: attributes[:retrieval_message] }, level: :error)
    end
  end
  analysis
rescue => e
  # save failure
  # NOPE
  raise e
  analysis = analyses.create!(retrieval_status: :internal_error, retrieval_message: e.message, nature: :sensor_analysis, sampled_at: Time.now)
  if options[:background] && analysis.status_changed?
    notify_error(:sensor_reading_failed, { name: name, message: e.message }, level: :error)
  end
  analysis
end