[UNSTABLE][NEEDS TESTS] Sensor endpoint controller actions
parent
2c4a77a865
commit
1635e146a6
|
@ -4,10 +4,6 @@ module Api
|
|||
render json: current_device.peripherals
|
||||
end
|
||||
|
||||
# Another Oddball endpoint. This controller action might not follow
|
||||
# traditional conventions for REST APIs. Replaces *all* existing peripherals
|
||||
# with a new array of peripherals. Performing patch operations on
|
||||
# collections (where order is signifcant) was too much of a pain.
|
||||
def create
|
||||
mutate Peripherals::Create.run(raw_json, device: current_device)
|
||||
end
|
||||
|
|
|
@ -1,24 +1,25 @@
|
|||
module Api
|
||||
class SensorsController < Api::AbstractController
|
||||
|
||||
def index
|
||||
raise "Not yet implements :("
|
||||
end
|
||||
|
||||
def show
|
||||
raise "Not yet implements :("
|
||||
render json: current_device.sensors
|
||||
end
|
||||
|
||||
def create
|
||||
raise "Not yet implements :("
|
||||
mutate Sensors::Create.run(raw_json, device: current_device)
|
||||
end
|
||||
|
||||
def update
|
||||
raise "Not yet implements :("
|
||||
mutate Sensors::Update.run(raw_json, sensor: sensor, device: current_device)
|
||||
end
|
||||
|
||||
def destroy
|
||||
raise "Not yet implements :("
|
||||
mutate Sensors::Destroy.run(sensor: sensor)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def sensor
|
||||
@sensor ||= current_device.sensors.find(params[:id])
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -0,0 +1,14 @@
|
|||
module Sensors
|
||||
class Create < Mutations::Command
|
||||
required do
|
||||
model :device, class: Device
|
||||
integer :pin
|
||||
string :label
|
||||
integer :mode, in: CeleryScriptSettingsBag::ALLOWED_PIN_MODES
|
||||
end
|
||||
|
||||
def execute
|
||||
Sensor.create!(inputs)
|
||||
end
|
||||
end
|
||||
end
|
|
@ -0,0 +1,26 @@
|
|||
module Sensors
|
||||
class Destroy < Mutations::Command
|
||||
required { model :sensor, class: Peripheral }
|
||||
IN_USE = "Can't delete sensor because the following sequences are still " \
|
||||
"using it: %s"
|
||||
|
||||
def execute
|
||||
sensor.destroy!
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def sequences_using_it
|
||||
raise "UNCLEAR: Are we adding a `sensor_id` arg?"
|
||||
|
||||
@sequences_using_it ||= EdgeNode
|
||||
.where(kind: "sensor_id", value: sensor.id)
|
||||
.pluck(:sequence_id)
|
||||
end
|
||||
|
||||
def not_in_use?
|
||||
names = Sequence.where(id: sequences_using_it).pluck(:name).join(", ")
|
||||
add_error :sensor, :in_use, (IN_USE % [names]) if names.present?
|
||||
end
|
||||
end
|
||||
end
|
|
@ -0,0 +1,19 @@
|
|||
module Sensors
|
||||
class Update < Mutations::Command
|
||||
required do
|
||||
model :peripheral, class: Sensor
|
||||
model :device, class: Device
|
||||
end
|
||||
|
||||
optional do
|
||||
integer :pin
|
||||
string :label
|
||||
integer :mode, in: CeleryScriptSettingsBag::ALLOWED_PIN_MODES
|
||||
end
|
||||
|
||||
def execute
|
||||
peripheral.update_attributes!(inputs.except(:peripheral, :device))
|
||||
peripheral
|
||||
end
|
||||
end
|
||||
end
|
|
@ -1,55 +1,36 @@
|
|||
# DRY up some of the repetitive route configs.
|
||||
module Only
|
||||
CREATE = [:create ]
|
||||
DESTROY = [:destroy]
|
||||
INDEX = [:index ]
|
||||
SHOW = [:show ]
|
||||
UPDATE = [:update ]
|
||||
|
||||
ALL = CREATE + DESTROY + INDEX + SHOW + UPDATE
|
||||
CREATE_SHOW = CREATE + SHOW
|
||||
CREATE_UPDATE = CREATE + UPDATE
|
||||
INDEX_AND_SHOW = INDEX + SHOW
|
||||
INDEX_CREATE_DESTROY = INDEX + CREATE + DESTROY
|
||||
NON_INDEX = ALL - INDEX
|
||||
NON_INDEX_CREATE = ALL - INDEX - CREATE
|
||||
NON_SHOW = ALL - SHOW
|
||||
NON_UPDATE = ALL - UPDATE
|
||||
end
|
||||
|
||||
FarmBot::Application.routes.draw do
|
||||
|
||||
resources :sensors
|
||||
namespace :api, defaults: {format: :json}, constraints: { format: "json" } do
|
||||
# Standard API Resources:
|
||||
{
|
||||
corpuses: Only::INDEX_AND_SHOW,
|
||||
farm_events: Only::NON_SHOW,
|
||||
images: Only::NON_UPDATE,
|
||||
logs: Only::INDEX_CREATE_DESTROY,
|
||||
password_resets: Only::CREATE_UPDATE,
|
||||
peripherals: Only::NON_SHOW,
|
||||
regimens: Only::NON_SHOW,
|
||||
sensor_readings: Only::NON_UPDATE,
|
||||
sequences: Only::ALL,
|
||||
tools: Only::ALL,
|
||||
webcam_feeds: Only::ALL,
|
||||
corpuses: [:index, :show],
|
||||
farm_events: [:create, :destroy, :index, :update],
|
||||
images: [:create, :destroy, :index, :show],
|
||||
logs: [:create, :destroy, :index],
|
||||
password_resets: [:create, :update],
|
||||
peripherals: [:create, :destroy, :index, :update],
|
||||
sensors: [:create, :destroy, :index, :update],
|
||||
regimens: [:create, :destroy, :index, :update],
|
||||
sensor_readings: [:create, :destroy, :index, :show],
|
||||
sequences: [:create, :destroy, :index, :show, :update],
|
||||
tools: [:create, :destroy, :index, :show, :update],
|
||||
webcam_feeds: [:create, :destroy, :index, :show, :update],
|
||||
}.to_a.map{|(name, only)| resources name, only: only}
|
||||
|
||||
# Singular API Resources:
|
||||
{
|
||||
device: Only::NON_INDEX,
|
||||
fbos_config: Only::NON_INDEX_CREATE,
|
||||
firmware_config: Only::NON_INDEX_CREATE,
|
||||
public_key: Only::SHOW,
|
||||
tokens: Only::CREATE_SHOW,
|
||||
web_app_config: Only::NON_INDEX_CREATE
|
||||
device: [:create, :destroy, :show, :update],
|
||||
fbos_config: [:destroy, :show, :update,],
|
||||
firmware_config: [:destroy, :show, :update,],
|
||||
public_key: [:show],
|
||||
tokens: [:create, :show],
|
||||
web_app_config: [:destroy, :show, :update],
|
||||
}.to_a.map{|(name, only)| resource name, only: only}
|
||||
|
||||
resources :points, only: Only::ALL do
|
||||
resources :points, only: [:create, :destroy, :index, :show, :update,] do
|
||||
post :search, on: :collection
|
||||
end
|
||||
resource :users, only: Only::NON_INDEX do
|
||||
|
||||
resource :users, only: [:create, :destroy, :show, :update,] do
|
||||
post :resend_verification, on: :member
|
||||
end
|
||||
|
||||
|
|
Loading…
Reference in New Issue