Compare commits
8 commits
3034275ce4
...
788f63597e
Author | SHA1 | Date | |
---|---|---|---|
|
788f63597e | ||
|
3f9d75429b | ||
|
8de7a8a74c | ||
|
cf4dfa3aad | ||
|
6930afabc8 | ||
|
4e0a1d2329 | ||
|
eb01449dff | ||
|
89d1299930 |
5 changed files with 42 additions and 18 deletions
|
@ -6,23 +6,25 @@ class ReadingsController < ActionController::API
|
||||||
|
|
||||||
# @see {https://docutopia.tupale.co/sutty:nodemecu:api}
|
# @see {https://docutopia.tupale.co/sutty:nodemecu:api}
|
||||||
def create
|
def create
|
||||||
reading = raspberry.readings.build reading_params
|
Reading.transaction do
|
||||||
reading.id = params[:transaction_uuid]
|
reading = raspberry.readings.build reading_params
|
||||||
# TODO: Verificar firma
|
reading.id = params[:transaction_uuid]
|
||||||
reading.signature = headers[:'X-Signature']
|
# TODO: Verificar firma
|
||||||
|
reading.signature = headers[:'X-Signature']
|
||||||
|
|
||||||
params[:arduinos]&.each do |a|
|
params[:arduinos]&.each do |a|
|
||||||
arduino = reading.arduinos.build local_id: a[:id]
|
arduino = reading.arduinos.build local_id: a[:id], raspberry: raspberry
|
||||||
|
|
||||||
a[:sensores]&.each do |s|
|
a[:sensores]&.each do |s|
|
||||||
arduino.sensors.build(sensor_params s)
|
arduino.sensors.build(sensor_params s)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
|
||||||
|
|
||||||
if reading.save
|
if reading.save
|
||||||
render plain: reading.uuid, status: :ok
|
render plain: reading.id, status: :ok
|
||||||
else
|
else
|
||||||
head :bad_request
|
head :bad_request
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -33,13 +35,12 @@ class ReadingsController < ActionController::API
|
||||||
end
|
end
|
||||||
|
|
||||||
def raspberry
|
def raspberry
|
||||||
@raspberry ||= Raspberry.find_or_create_by! params[:controller_id]
|
@raspberry ||= Raspberry.find_or_create_by! name: params[:controller_id]
|
||||||
end
|
end
|
||||||
|
|
||||||
# Procesa la transacción
|
# Procesa la transacción
|
||||||
def reading_params
|
def reading_params
|
||||||
@reading_params ||= params.permit(:controller_id,
|
@reading_params ||= params.permit(:timestamp,
|
||||||
:timestamp,
|
|
||||||
:error_code,
|
:error_code,
|
||||||
:battery_status,
|
:battery_status,
|
||||||
:sample,
|
:sample,
|
||||||
|
@ -49,7 +50,9 @@ class ReadingsController < ActionController::API
|
||||||
|
|
||||||
# Procesa los parámetros de un sensor
|
# Procesa los parámetros de un sensor
|
||||||
def sensor_params(sensor)
|
def sensor_params(sensor)
|
||||||
sensor.permit(:timestamp, :type, :value, :unit, :error)
|
sensor.permit(:timestamp, :type, :value, :unit, :error).tap do |p|
|
||||||
|
p[:local_type] = p.delete :type
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -3,5 +3,5 @@
|
||||||
class Arduino < ApplicationRecord
|
class Arduino < ApplicationRecord
|
||||||
belongs_to :raspberry
|
belongs_to :raspberry
|
||||||
belongs_to :reading
|
belongs_to :reading
|
||||||
has_many :sensores
|
has_many :sensors
|
||||||
end
|
end
|
||||||
|
|
7
db/migrate/20220303170429_change_local_id_to_string.rb
Normal file
7
db/migrate/20220303170429_change_local_id_to_string.rb
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
class ChangeLocalIdToString < ActiveRecord::Migration[6.1]
|
||||||
|
def change
|
||||||
|
change_column :arduinos, :local_id, :string
|
||||||
|
end
|
||||||
|
end
|
7
db/migrate/20220303170706_change_type.rb
Normal file
7
db/migrate/20220303170706_change_type.rb
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
class ChangeType < ActiveRecord::Migration[6.1]
|
||||||
|
def change
|
||||||
|
rename_column :sensors, :type, :local_type
|
||||||
|
end
|
||||||
|
end
|
7
db/migrate/20220303170801_add_error_to_sensor.rb
Normal file
7
db/migrate/20220303170801_add_error_to_sensor.rb
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
class AddErrorToSensor < ActiveRecord::Migration[6.1]
|
||||||
|
def change
|
||||||
|
add_column :sensors, :error, :integer
|
||||||
|
end
|
||||||
|
end
|
Loading…
Reference in a new issue