refactorización

los modelos no se pueden llamar transaction
This commit is contained in:
f 2022-03-03 13:19:47 -03:00
parent 01416fd7ca
commit 3034275ce4
8 changed files with 88 additions and 83 deletions

View file

@ -1,24 +1,26 @@
# README
# Ectomobile
This README would normally document whatever steps are necessary to get the
application up and running.
Colector de datos.
Things you may want to cover:
## Configuración
* Ruby version
1. Conseguir la _master key_ desde otrx miembrx del proyecto y colocarla
en `config/master.key`.
* System dependencies
* Si no sos parte del proyecto, podés borrar el archivo
`config/credentials.yml.enc` y recrearlo con:
* Configuration
```bash
rm config/credentials.yml.enc
make rails args=credentials:edit
```
* Database creation
Dentro hay que agregar las variables siguientes con contraseñas de
128 caracteres, que se pueden generar con `pwgen -1 128`.
* Database initialization
```yaml
secret_key_base: "contraseña de 128 caracteres"
devise_pepper: "contraseña de 128 caracteres"
```
* How to run the test suite
* Services (job queues, cache servers, search engines, etc.)
* Deployment instructions
* ...
2. Adaptar las variables en el archivo `ansible.yml`.

View file

@ -1,59 +0,0 @@
# frozen_string_literal: true
class ApiController < ActionController::API
# TODO: Habilitar esto de alguna forma.
skip_forgery_protection
rescue_from ActiveRecord::RecordNotFound, with: :bad_request_error
rescue_from ActionController::ParameterMissing, with: :bad_request_error
# @see {https://docutopia.tupale.co/sutty:nodemecu:api}
def transactions
Transaction.transaction do
transaction = raspberry.transactions.build transaction_params
transaction.id = params[:transaction_uuid]
# TODO: Verificar firma
transaction.signature = headers[:'X-Signature']
params[:arduinos]&.each do |a|
arduino = transaction.arduinos.build local_id: a[:id]
a[:sensores]&.each do |s|
arduino.sensors.build(sensor_params s)
end
end
if transaction.save
render plain: transaction.uuid, status: :ok
else
head :bad_request
end
end
end
private
def bad_request_error
head :bad_request
end
def raspberry
@raspberry ||= Raspberry.find_or_create_by! params[:controller_id]
end
# Procesa la transacción
def transaction_params
@transaction_params ||= params.permit(:controller_id,
:timestamp,
:error_code,
:battery_status,
:sample,
:signature,
coordinates: %i[lat lng])
end
# Procesa los parámetros de un sensor
def sensor_params(sensor)
sensor.permit(:timestamp, :type, :value, :unit, :error)
end
end

View file

@ -0,0 +1,55 @@
# frozen_string_literal: true
class ReadingsController < ActionController::API
rescue_from ActiveRecord::RecordNotFound, with: :bad_request_error
rescue_from ActionController::ParameterMissing, with: :bad_request_error
# @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']
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
end
if reading.save
render plain: reading.uuid, status: :ok
else
head :bad_request
end
end
private
def bad_request_error
head :bad_request
end
def raspberry
@raspberry ||= Raspberry.find_or_create_by! params[:controller_id]
end
# Procesa la transacción
def reading_params
@reading_params ||= params.permit(:controller_id,
:timestamp,
:error_code,
:battery_status,
:sample,
:signature,
coordinates: %i[lat lng])
end
# Procesa los parámetros de un sensor
def sensor_params(sensor)
sensor.permit(:timestamp, :type, :value, :unit, :error)
end
end

View file

@ -1,7 +1,7 @@
# frozen_string_literal: true
class Transaction < ApplicationRecord
class Arduino < ApplicationRecord
belongs_to :raspberry
belongs_to :transaction
belongs_to :reading
has_many :sensores
end

View file

@ -1,6 +1,6 @@
# frozen_string_literal: true
class Raspberry < ApplicationRecord
has_many :transactions
has_many :readings
has_many :arduinos
end

View file

@ -1,6 +1,6 @@
# frozen_string_literal: true
class Transaction < ApplicationRecord
class Reading < ApplicationRecord
belongs_to :raspberry
has_many :arduinos
end

View file

@ -1,10 +1,9 @@
Rails.application.routes.draw do
devise_for :users
root 'application#index'
resources :sensors, only: [] do
resources :measurements, only: [:create]
end
mount Blazer::Engine, at: 'blazer'
post :transactions, to: 'api#transactions'
resources :readings, only: %i[create]
end

View file

@ -0,0 +1,8 @@
# frozen_string_literal: true
class RenameTransactionsToReadings < ActiveRecord::Migration[6.1]
def change
rename_table :transactions, :readings
rename_column :arduinos, :transaction_id, :reading_id
end
end