Compare commits

..

No commits in common. "788f63597e84ace8c826761783721a44e494c206" and "3034275ce4a3545704899165794bcb31fe51d08e" have entirely different histories.

5 changed files with 18 additions and 42 deletions

View file

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

View file

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

View file

@ -1,7 +0,0 @@
# frozen_string_literal: true
class ChangeLocalIdToString < ActiveRecord::Migration[6.1]
def change
change_column :arduinos, :local_id, :string
end
end

View file

@ -1,7 +0,0 @@
# frozen_string_literal: true
class ChangeType < ActiveRecord::Migration[6.1]
def change
rename_column :sensors, :type, :local_type
end
end

View file

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