/
sensors_controller.rb
99 lines (89 loc) · 2.97 KB
/
sensors_controller.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# = Informations
#
# == License
#
# Ekylibre - Simple agricultural ERP
# Copyright (C) 2008-2009 Brice Texier, Thibaud Merigon
# Copyright (C) 2010-2012 Brice Texier
# Copyright (C) 2012-2015 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 http://www.gnu.org/licenses.
#
module Backend
class SensorsController < Backend::BaseController
manage_restfully
unroll
list do |t|
t.action :edit
t.action :destroy
t.column :active
t.column :name, url: true
t.column :retrieval_mode
t.column :vendor_euid
t.column :model_euid
t.column :product, url: true
t.column :embedded
t.column :host, url: true
end
list :analyses, model: :analysis, conditions: { sensor_id: 'params[:id]'.c } do |t|
t.column :number, url: true
t.column :nature
t.column :analysed_at
t.column :retrieval_status
t.column :retrieval_message
t.column :sampling_temporal_mode
end
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
# Updates details and settings in form
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
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
def last_locations
@geolocations = Sensor
.includes(:analyses)
.find_each
.map do |sensor|
if (geoloc = sensor.analyses.last.geolocation)
[sensor.id, geoloc && JSON(geoloc.to_json)]
end
end
.compact
.to_h
render json: @geolocations.to_json
end
end
end