Class: Orchestrator::Api::ModulesController

Inherits:
Orchestrator::ApiController show all
Defined in:
app/controllers/orchestrator/api/modules_controller.rb

Constant Summary collapse

MOD_INCLUDE =

Constant for performance

{
    include: {
        # Most human readable module data is contained in dependency
        dependency: {only: [:name, :description, :module_name, :settings]},

        # include control system on logic modules so it is possible
        # to display the inherited settings
        control_system: {
            only: [:name, :settings],
            methods: [:zone_data]
        }
    }
}

Instance Method Summary collapse

Methods inherited from Base

#options

Instance Method Details

#createObject



92
93
94
95
# File 'app/controllers/orchestrator/api/modules_controller.rb', line 92

def create
    mod = ::Orchestrator::Module.new(safe_params)
    save_and_respond mod
end

#destroyObject



97
98
99
100
# File 'app/controllers/orchestrator/api/modules_controller.rb', line 97

def destroy
    @mod.delete
    render nothing: true
end

#indexObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'app/controllers/orchestrator/api/modules_controller.rb', line 31

def index
    filters = params.permit(:system_id, :dependency_id, :connected, :no_logic)

    # if a system id is present we query the database directly
    if filters[:system_id]
        cs = ControlSystem.find(filters[:system_id])

        results = ::Orchestrator::Module.find_by_id(cs.modules) || [];
        render json: {
            total: results.length,
            results: results
        }
    else # we use elastic search
        query = @@elastic.query(params)

        if filters[:dependency_id]
            query.filter({
                dependency_id: [filters[:dependency_id]]
            })
        end

        if filters[:connected]
            connected = filters[:connected] == 'true'
            query.filter({
                connected: [connected]
            })
        end

        if filters.has_key? :no_logic
            query.filter({
                role: [1, 2]
            })
        end

        results = @@elastic.search(query)
        respond_with results, MOD_INCLUDE
    end
end

#showObject



70
71
72
# File 'app/controllers/orchestrator/api/modules_controller.rb', line 70

def show
    respond_with @mod, MOD_INCLUDE
end

#startObject

Additional Functions:



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'app/controllers/orchestrator/api/modules_controller.rb', line 107

def start
    # It is possible that module class load can fail
    mod = control.loaded? id
    if mod
        start_module(mod)
    else # attempt to load module
        config = ::Orchestrator::Module.find(id)
        control.load(config).then(
            proc { |mod|
                start_module mod
            },
            proc { # Load failed
                env['async.callback'].call([500, {'Content-Length' => 0}, []])
            }
        )
    end
    throw :async
end

#stateObject



136
137
138
139
140
# File 'app/controllers/orchestrator/api/modules_controller.rb', line 136

def state
    lookup_module do |mod|
        render json: mod.status[params.permit(:lookup)[:lookup].to_sym]
    end
end

#stopObject



126
127
128
129
130
131
132
133
134
# File 'app/controllers/orchestrator/api/modules_controller.rb', line 126

def stop
    # Stop will always succeed
    lookup_module do |mod|
        mod.thread.next_tick do
            mod.stop
        end
        render nothing: true
    end
end

#updateObject



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'app/controllers/orchestrator/api/modules_controller.rb', line 74

def update
    para = safe_params
    old_name = @mod.custom_name

    @mod.update_attributes(para)
    save_and_respond(@mod) do
        # Update the running module
        control.update(id).then do
            # If custom name is changed we need to expire any system caches
            if para[:custom_name] != old_name
                ::Orchestrator::ControlSystem.using_module(id).each do |sys|
                    sys.expire_cache(:no_update)
                end
            end
        end
    end
end