Compare commits

...

8 commits

Author SHA1 Message Date
f
788f63597e campo que no existe localmente 2022-03-03 14:14:53 -03:00
f
3f9d75429b buscar o crear la raspberry por el nombre 2022-03-03 14:14:37 -03:00
f
8de7a8a74c envolver todo en una transacción 2022-03-03 14:14:22 -03:00
f
cf4dfa3aad asignar la raspberry al arduino también 2022-03-03 14:13:59 -03:00
f
6930afabc8 agregar el campo de error 2022-03-03 14:12:51 -03:00
f
4e0a1d2329 type es un campo reservado 2022-03-03 14:12:39 -03:00
f
eb01449dff el id de los arduinos puede ser una string 2022-03-03 14:12:10 -03:00
f
89d1299930 se llaman sensors 2022-03-03 14:11:57 -03:00
5 changed files with 42 additions and 18 deletions

View file

@ -6,23 +6,25 @@ class ReadingsController < ActionController::API
# @see {https://docutopia.tupale.co/sutty:nodemecu:api}
def create
reading = raspberry.readings.build reading_params
reading.id = params[:transaction_uuid]
# TODO: Verificar firma
reading.signature = headers[:'X-Signature']
Reading.transaction do
reading = raspberry.readings.build reading_params
reading.id = params[:transaction_uuid]
# TODO: Verificar firma
reading.signature = headers[:'X-Signature']
params[:arduinos]&.each do |a|
arduino = reading.arduinos.build local_id: a[:id]
params[:arduinos]&.each do |a|
arduino = reading.arduinos.build local_id: a[:id], raspberry: raspberry
a[:sensores]&.each do |s|
arduino.sensors.build(sensor_params s)
a[:sensores]&.each do |s|
arduino.sensors.build(sensor_params s)
end
end
end
if reading.save
render plain: reading.uuid, status: :ok
else
head :bad_request
if reading.save
render plain: reading.id, status: :ok
else
head :bad_request
end
end
end
@ -33,13 +35,12 @@ class ReadingsController < ActionController::API
end
def raspberry
@raspberry ||= Raspberry.find_or_create_by! params[:controller_id]
@raspberry ||= Raspberry.find_or_create_by! name: params[:controller_id]
end
# Procesa la transacción
def reading_params
@reading_params ||= params.permit(:controller_id,
:timestamp,
@reading_params ||= params.permit(:timestamp,
:error_code,
:battery_status,
:sample,
@ -49,7 +50,9 @@ class ReadingsController < ActionController::API
# Procesa los parámetros de un 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

View file

@ -3,5 +3,5 @@
class Arduino < ApplicationRecord
belongs_to :raspberry
belongs_to :reading
has_many :sensores
has_many :sensors
end

View 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

View 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

View file

@ -0,0 +1,7 @@
# frozen_string_literal: true
class AddErrorToSensor < ActiveRecord::Migration[6.1]
def change
add_column :sensors, :error, :integer
end
end