class PlaceOS::Api::Modules

Included Modules

Defined in:

placeos-rest-api/controllers/modules.cr

Constant Summary

AFTER = {} of Nil => Nil
AROUND = {} of Nil => Nil
BEFORE = {set_request_id => {nil, nil}, authorize! => {nil, [root, mqtt_user, mqtt_access]}, set_user_id => {nil, [root, mqtt_user, mqtt_access]}, can_read => {[index, show], nil}, can_write => {[create, update, destroy, remove, update_alt], nil}, check_admin => {nil, [index, state, show, ping]}, check_support => {[index, state, show, ping], nil}, ensure_json => {[create, update, update_alt, execute], nil}, current_module => {[show, update, update_alt, destroy, ping, state], nil}, body => {[create, execute, update, update_alt], nil}} of Nil => Nil
DRIVER_ATTRIBUTES = ["name", "description"] of ::String
FORCE = {} of Nil => Nil
LOCAL_AFTER = {} of Nil => Nil

function => options

LOCAL_AROUND = {} of Nil => Nil

function => options

LOCAL_BEFORE = {can_read => {[index, show], nil}, can_write => {[create, update, destroy, remove, update_alt], nil}, check_admin => {nil, [index, state, show, ping]}, check_support => {[index, state, show, ping], nil}, ensure_json => {[create, update, update_alt, execute], nil}, current_module => {[show, update, update_alt, destroy, ping, state], nil}, body => {[create, execute, update, update_alt], nil}} of Nil => Nil

function => options

LOCAL_FORCE = {} of Nil => Nil

function => options

LOCAL_RESCUE = {} of Nil => Nil

function => options

LOCAL_ROUTES = {update_alt => {"put", "/:id", nil, do update end, false}, settings => {"get", "/:id/settings", nil, do render(json: Api::Settings.collated_settings(current_user, current_module)) end, false}, start => {"post", "/:id/start", nil, do if current_module.running == true head(:ok) end current_module.update_fields(running: true) if current_module.running_changed? Log.error do {controller: "Modules", action: "start", module_id: current_module.id, event: "failed"} end head(:internal_server_error) else head(:ok) end end, false}, stop => {"post", "/:id/stop", nil, do if current_module.running else head(:ok) end current_module.update_fields(running: false) if current_module.running_changed? Log.error do {controller: "Modules", action: "stop", module_id: current_module.id, event: "failed"} end head(:internal_server_error) else head(:ok) end end, false}, execute => {"post", "/:id/exec/:method", nil, do begin sys_id = current_module.control_system_id || "" args = Array(JSON::Any).from_json(self.body) result, status_code = Driver::Proxy::RemoteDriver.new(module_id: module_id, sys_id: sys_id, module_name: current_module.name, discovery: self.class.core_discovery, user_id: current_user.id).exec(security: driver_clearance(user_token), function: method, args: args, request_id: request_id) response.content_type = "application/json" render(text: result, status: status_code) rescue e : Driver::Proxy::RemoteDriver::Error handle_execute_error(e) rescue e Log.error(exception: e) do {message: "core execute request failed", sys_id: sys_id, module_id: module_id, module_name: current_module.name, method: method} end if Api.production? render_error(HTTP::Status::INTERNAL_SERVER_ERROR, e.message) else render_error(HTTP::Status::INTERNAL_SERVER_ERROR, e.message, backtrace: e.backtrace?) end end end, false}, state => {"get", "/:id/state", nil, do render(json: self.class.module_state(current_module)) end, false}, state_lookup => {"get", "/:id/state/:key", nil, do render(json: self.class.module_state(current_module, key)) end, false}, ping => {"post", "/:id/ping", nil, do if current_module.role.logic? Log.debug do {controller: "Modules", action: "ping", module_id: current_module.id, role: current_module.role.to_s} end head(:not_acceptable) else pinger = Pinger.new(current_module.hostname.as(String), count: 3) pinger.ping render(json: {host: pinger.ip.to_s, pingable: pinger.pingable, warning: pinger.warning, exception: pinger.exception}) end end, false}, load => {"post", "/:id/load", nil, do render(json: Api::Systems.core_for(module_id, request_id, &.load(module_id))) end, false}} of Nil => Nil

function => options

LOCAL_SKIP = {} of Nil => Nil

function => options

Log = ::PlaceOS::Api::Log.for(self)
NAMESPACE = ["/api/engine/v2/modules/"]

default namespace based on class

RESCUE = {Error::NoBody => {error_no_body, do |_error| message = "missing request body" Log.debug do message end return render_error(HTTP::Status::BAD_REQUEST, message) end}, JSON::SerializableError => {json_serializable_error, do |error| message = "Missing or extraneous properties in client JSON" Log.debug(exception: error) do message end if Api.production? return render_error(HTTP::Status::BAD_REQUEST, message) else return render_error(HTTP::Status::BAD_REQUEST, error.message, backtrace: error.backtrace?) end end}, JSON::ParseException => {json_parse_exception, do |error| message = "Failed to parse client JSON" Log.debug(exception: error) do message end if Api.production? return render_error(HTTP::Status::BAD_REQUEST, message) else return render_error(HTTP::Status::BAD_REQUEST, error.message, backtrace: error.backtrace?) end end}, Error::Unauthorized => {error_unauthorized, do |error| Log.debug do error.message end head(:unauthorized) end}, Error::Forbidden => {error_forbidden, do |error| Log.debug do error.message end head(:forbidden) end}, RethinkORM::Error::DocumentNotFound => {rethink_orm_error_document_not_found, do |error| Log.debug do error.message end head(:not_found) end}, RethinkORM::Error::DocumentInvalid => {rethink_orm_error_document_invalid, do |error| Log.debug do error.message end return render_error(HTTP::Status::UNPROCESSABLE_ENTITY, error.message) end}, Error::InvalidParams => {error_invalid_params, do |error| model_errors = error.params.errors.map(&.to_s) Log.debug(exception: error) do {message: "Invalid params", model_errors: model_errors} end return render_error(HTTP::Status::BAD_REQUEST, "Invalid params: #{model_errors.join(", ")}") end}} of Nil => Nil
ROUTE_RESOURCE = "modules"
ROUTES = {update_alt => {"put", "/:id", nil, do update end, false}, settings => {"get", "/:id/settings", nil, do render(json: Api::Settings.collated_settings(current_user, current_module)) end, false}, start => {"post", "/:id/start", nil, do if current_module.running == true head(:ok) end current_module.update_fields(running: true) if current_module.running_changed? Log.error do {controller: "Modules", action: "start", module_id: current_module.id, event: "failed"} end head(:internal_server_error) else head(:ok) end end, false}, stop => {"post", "/:id/stop", nil, do if current_module.running else head(:ok) end current_module.update_fields(running: false) if current_module.running_changed? Log.error do {controller: "Modules", action: "stop", module_id: current_module.id, event: "failed"} end head(:internal_server_error) else head(:ok) end end, false}, execute => {"post", "/:id/exec/:method", nil, do begin sys_id = current_module.control_system_id || "" args = Array(JSON::Any).from_json(self.body) result, status_code = Driver::Proxy::RemoteDriver.new(module_id: module_id, sys_id: sys_id, module_name: current_module.name, discovery: self.class.core_discovery, user_id: current_user.id).exec(security: driver_clearance(user_token), function: method, args: args, request_id: request_id) response.content_type = "application/json" render(text: result, status: status_code) rescue e : Driver::Proxy::RemoteDriver::Error handle_execute_error(e) rescue e Log.error(exception: e) do {message: "core execute request failed", sys_id: sys_id, module_id: module_id, module_name: current_module.name, method: method} end if Api.production? render_error(HTTP::Status::INTERNAL_SERVER_ERROR, e.message) else render_error(HTTP::Status::INTERNAL_SERVER_ERROR, e.message, backtrace: e.backtrace?) end end end, false}, state => {"get", "/:id/state", nil, do render(json: self.class.module_state(current_module)) end, false}, state_lookup => {"get", "/:id/state/:key", nil, do render(json: self.class.module_state(current_module, key)) end, false}, ping => {"post", "/:id/ping", nil, do if current_module.role.logic? Log.debug do {controller: "Modules", action: "ping", module_id: current_module.id, role: current_module.role.to_s} end head(:not_acceptable) else pinger = Pinger.new(current_module.hostname.as(String), count: 3) pinger.ping render(json: {host: pinger.ip.to_s, pingable: pinger.pingable, warning: pinger.warning, exception: pinger.exception}) end end, false}, load => {"post", "/:id/load", nil, do render(json: Api::Systems.core_for(module_id, request_id, &.load(module_id))) end, false}, index => {"get", "/", nil, nil, false}, show => {"get", "/:id", nil, nil, false}, update => {"patch", "/:id", nil, nil, false}, create => {"post", "/", nil, nil, false}, destroy => {"delete", "/:id", nil, nil, false}} of Nil => Nil
SKIP = {} of Nil => Nil

Class Method Summary

Instance Method Summary

Macro Summary

Instance methods inherited from module PlaceOS::Api::Utils::CoreHelper

driver_clearance(user : Model::User | Model::UserJWT) driver_clearance

Instance methods inherited from class PlaceOS::Api::Application

body : IO body, boolean_param(key : String, default : Bool = false, allow_empty : Bool = false) : Bool boolean_param, paginate_results(elastic, query, route = base_route) paginate_results, request_id : String request_id, set_collection_headers(size : Int32, content_type : String) set_collection_headers, set_request_id set_request_id, set_user_id set_user_id

Class methods inherited from class PlaceOS::Api::Application

context(method : String, route : String, route_params : Hash(String, String) | Nil = nil, headers : Hash(String, String) | Nil = nil, body : String | Bytes | IO | Nil = nil, &) context, core_discovery : Discovery::Core core_discovery

Class methods inherited from module PlaceOS::Api::Utils::Scopes

can_scope_access?(user_token : UserJWT, scope : String, access : Access) can_scope_access?, can_scopes_access!(user_token : UserJWT, scopes : Enumerable(String), access : Access) can_scopes_access!

Instance methods inherited from module PlaceOS::Api::Utils::CurrentUser

authorize! : Model::UserJWT authorize!, check_admin check_admin, check_oauth_scope check_oauth_scope, check_support check_support, current_authority : Model::Authority | Nil current_authority, current_user : Model::User current_user, is_admin? is_admin?, is_support? is_support?, user_token : Model::UserJWT user_token

Instance methods inherited from module PlaceOS::Api::Utils::Responders

handle_execute_error(error : Driver::Proxy::RemoteDriver::Error, respond : Bool = true) handle_execute_error, render_error(status : HTTP::Status, message : String | Nil, **additional) render_error, restrict_attributes(model, only : Array(String) | Nil = nil, except : Array(String) | Nil = nil, fields : Hash | Nil = nil) : Hash restrict_attributes, save_and_respond(resource, &)
save_and_respond(resource)
save_and_respond
, save_and_status(resource) save_and_status, with_fields(model, fields) : Hash with_fields

Class Method Detail

def self.__init_routes__(router) #

Routes call the functions generated above


def self.__route_list__ #

Create draw_routes function

Create instance of controller class init with context, params and logger protocol checks (https etc) controller instance created begin exception helpers inline the around filters inline the before filters inline the action inline the after filters rescue exception handlers


def self.base_route #

Helper for obtaining base route


def self.context(method : String, route : String, route_params : Hash(String, String) | Nil = nil, headers : Hash(String, String) | Nil = nil, body : String | Bytes | IO | Nil = nil, &) #

[View source]
def self.create(hash_parts : Hash(String | Symbol, Nil | Bool | Int32 | Int64 | Float32 | Float64 | String | Symbol) | Nil = nil, **tuple_parts) #

Create draw_routes function

Create instance of controller class init with context, params and logger protocol checks (https etc) controller instance created begin exception helpers inline the around filters inline the before filters inline the action inline the after filters rescue exception handlers


def self.delete__api_engine_v2_modules___id(context, head_request) #

Create draw_routes function

Create instance of controller class init with context, params and logger protocol checks (https etc) controller instance created begin exception helpers inline the around filters inline the before filters inline the action inline the after filters rescue exception handlers


def self.destroy(hash_parts : Hash(String | Symbol, Nil | Bool | Int32 | Int64 | Float32 | Float64 | String | Symbol) | Nil = nil, **tuple_parts) #

Create draw_routes function

Create instance of controller class init with context, params and logger protocol checks (https etc) controller instance created begin exception helpers inline the around filters inline the before filters inline the action inline the after filters rescue exception handlers


def self.driver_compiled?(mod : Model::Module, request_id : String) #

[View source]
def self.execute(hash_parts : Hash(String | Symbol, Nil | Bool | Int32 | Int64 | Float32 | Float64 | String | Symbol) | Nil = nil, **tuple_parts) #

Create draw_routes function

Create instance of controller class init with context, params and logger protocol checks (https etc) controller instance created begin exception helpers inline the around filters inline the before filters inline the action inline the after filters rescue exception handlers


def self.get__api_engine_v2_modules__(context, head_request) #

Create draw_routes function

Create instance of controller class init with context, params and logger protocol checks (https etc) controller instance created begin exception helpers inline the around filters inline the before filters inline the action inline the after filters rescue exception handlers


def self.get__api_engine_v2_modules___id(context, head_request) #

Create draw_routes function

Create instance of controller class init with context, params and logger protocol checks (https etc) controller instance created begin exception helpers inline the around filters inline the before filters inline the action inline the after filters rescue exception handlers


def self.get__api_engine_v2_modules___id_settings(context, head_request) #

Create draw_routes function

Create instance of controller class init with context, params and logger protocol checks (https etc) controller instance created begin exception helpers inline the around filters inline the before filters inline the action inline the after filters rescue exception handlers


def self.get__api_engine_v2_modules___id_state(context, head_request) #

Create draw_routes function

Create instance of controller class init with context, params and logger protocol checks (https etc) controller instance created begin exception helpers inline the around filters inline the before filters inline the action inline the after filters rescue exception handlers


def self.get__api_engine_v2_modules___id_state__key(context, head_request) #

Create draw_routes function

Create instance of controller class init with context, params and logger protocol checks (https etc) controller instance created begin exception helpers inline the around filters inline the before filters inline the action inline the after filters rescue exception handlers


def self.index(hash_parts : Hash(String | Symbol, Nil | Bool | Int32 | Int64 | Float32 | Float64 | String | Symbol) | Nil = nil, **tuple_parts) #

Create draw_routes function

Create instance of controller class init with context, params and logger protocol checks (https etc) controller instance created begin exception helpers inline the around filters inline the before filters inline the action inline the after filters rescue exception handlers


def self.load(hash_parts : Hash(String | Symbol, Nil | Bool | Int32 | Int64 | Float32 | Float64 | String | Symbol) | Nil = nil, **tuple_parts) #

Create draw_routes function

Create instance of controller class init with context, params and logger protocol checks (https etc) controller instance created begin exception helpers inline the around filters inline the before filters inline the action inline the after filters rescue exception handlers


def self.module_state(mod : Model::Module | String, key : String | Nil = nil) #

[View source]
def self.patch__api_engine_v2_modules___id(context, head_request) #

Create draw_routes function

Create instance of controller class init with context, params and logger protocol checks (https etc) controller instance created begin exception helpers inline the around filters inline the before filters inline the action inline the after filters rescue exception handlers


def self.ping(hash_parts : Hash(String | Symbol, Nil | Bool | Int32 | Int64 | Float32 | Float64 | String | Symbol) | Nil = nil, **tuple_parts) #

Create draw_routes function

Create instance of controller class init with context, params and logger protocol checks (https etc) controller instance created begin exception helpers inline the around filters inline the before filters inline the action inline the after filters rescue exception handlers


def self.post__api_engine_v2_modules__(context, head_request) #

Create draw_routes function

Create instance of controller class init with context, params and logger protocol checks (https etc) controller instance created begin exception helpers inline the around filters inline the before filters inline the action inline the after filters rescue exception handlers


def self.post__api_engine_v2_modules___id_exec__method(context, head_request) #

Create draw_routes function

Create instance of controller class init with context, params and logger protocol checks (https etc) controller instance created begin exception helpers inline the around filters inline the before filters inline the action inline the after filters rescue exception handlers


def self.post__api_engine_v2_modules___id_load(context, head_request) #

Create draw_routes function

Create instance of controller class init with context, params and logger protocol checks (https etc) controller instance created begin exception helpers inline the around filters inline the before filters inline the action inline the after filters rescue exception handlers


def self.post__api_engine_v2_modules___id_ping(context, head_request) #

Create draw_routes function

Create instance of controller class init with context, params and logger protocol checks (https etc) controller instance created begin exception helpers inline the around filters inline the before filters inline the action inline the after filters rescue exception handlers


def self.post__api_engine_v2_modules___id_start(context, head_request) #

Create draw_routes function

Create instance of controller class init with context, params and logger protocol checks (https etc) controller instance created begin exception helpers inline the around filters inline the before filters inline the action inline the after filters rescue exception handlers


def self.post__api_engine_v2_modules___id_stop(context, head_request) #

Create draw_routes function

Create instance of controller class init with context, params and logger protocol checks (https etc) controller instance created begin exception helpers inline the around filters inline the before filters inline the action inline the after filters rescue exception handlers


def self.put__api_engine_v2_modules___id(context, head_request) #

Generate functions for each route


def self.settings(hash_parts : Hash(String | Symbol, Nil | Bool | Int32 | Int64 | Float32 | Float64 | String | Symbol) | Nil = nil, **tuple_parts) #

Create draw_routes function

Create instance of controller class init with context, params and logger protocol checks (https etc) controller instance created begin exception helpers inline the around filters inline the before filters inline the action inline the after filters rescue exception handlers


def self.show(hash_parts : Hash(String | Symbol, Nil | Bool | Int32 | Int64 | Float32 | Float64 | String | Symbol) | Nil = nil, **tuple_parts) #

Create draw_routes function

Create instance of controller class init with context, params and logger protocol checks (https etc) controller instance created begin exception helpers inline the around filters inline the before filters inline the action inline the after filters rescue exception handlers


def self.start(hash_parts : Hash(String | Symbol, Nil | Bool | Int32 | Int64 | Float32 | Float64 | String | Symbol) | Nil = nil, **tuple_parts) #

Create draw_routes function

Create instance of controller class init with context, params and logger protocol checks (https etc) controller instance created begin exception helpers inline the around filters inline the before filters inline the action inline the after filters rescue exception handlers


def self.state(hash_parts : Hash(String | Symbol, Nil | Bool | Int32 | Int64 | Float32 | Float64 | String | Symbol) | Nil = nil, **tuple_parts) #

Create draw_routes function

Create instance of controller class init with context, params and logger protocol checks (https etc) controller instance created begin exception helpers inline the around filters inline the before filters inline the action inline the after filters rescue exception handlers


def self.state_lookup(hash_parts : Hash(String | Symbol, Nil | Bool | Int32 | Int64 | Float32 | Float64 | String | Symbol) | Nil = nil, **tuple_parts) #

Create draw_routes function

Create instance of controller class init with context, params and logger protocol checks (https etc) controller instance created begin exception helpers inline the around filters inline the before filters inline the action inline the after filters rescue exception handlers


def self.stop(hash_parts : Hash(String | Symbol, Nil | Bool | Int32 | Int64 | Float32 | Float64 | String | Symbol) | Nil = nil, **tuple_parts) #

Create draw_routes function

Create instance of controller class init with context, params and logger protocol checks (https etc) controller instance created begin exception helpers inline the around filters inline the before filters inline the action inline the after filters rescue exception handlers


def self.update(hash_parts : Hash(String | Symbol, Nil | Bool | Int32 | Int64 | Float32 | Float64 | String | Symbol) | Nil = nil, **tuple_parts) #

Create draw_routes function

Create instance of controller class init with context, params and logger protocol checks (https etc) controller instance created begin exception helpers inline the around filters inline the before filters inline the action inline the after filters rescue exception handlers


def self.update_alt(hash_parts : Hash(String | Symbol, Nil | Bool | Int32 | Int64 | Float32 | Float64 | String | Symbol) | Nil = nil, **tuple_parts) #

Helper methods for performing redirect_to calls


Instance Method Detail

def base_route(*args, **options) #

Helper for obtaining base route


def base_route(*args, **options, &) #

Helper for obtaining base route


def create #

[View source]
def current_module : Model::Module #

[View source]
def destroy #

[View source]
def error_forbidden(error) #

def error_invalid_params(error) #

def error_no_body(_error) #

Create functions as required for errors Skip the generating methods for existing handlers


def error_unauthorized(error) #

def execute #

Executes a command on a module


[View source]
def index #

[View source]
def json_parse_exception(error) #

def json_serializable_error(error) #

def key : String #

[View source]
def load #

[View source]
def method : String #

[View source]
def module_id : String #

[View source]
def ping #

[View source]
def rethink_orm_error_document_invalid(error) #

def rethink_orm_error_document_not_found(error) #

def settings #

Receive the collated settings for a module


[View source]
def show #

[View source]
def start #

Starts a module


[View source]
def state #

Dumps the complete status state of the module


[View source]
def state_lookup #

Returns the value of the requested status variable


[View source]
def stop #

Stops a module


[View source]
def update #

[View source]
def update_alt #

Macro Detail

macro __inherit_after_filters__ #

macro __inherit_around_filters__ #

macro __inherit_before_filters__ #

macro __inherit_force_filters__ #

macro __inherit_rescue_filters__ #

macro __inherit_routes_filters__ #

macro __inherit_skip_filters__ #

macro generate_scope_check(*scopes) #