orchestrator 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (58) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE.md +158 -0
  3. data/README.md +13 -0
  4. data/Rakefile +7 -0
  5. data/app/controllers/orchestrator/api/dependencies_controller.rb +109 -0
  6. data/app/controllers/orchestrator/api/modules_controller.rb +183 -0
  7. data/app/controllers/orchestrator/api/systems_controller.rb +294 -0
  8. data/app/controllers/orchestrator/api/zones_controller.rb +62 -0
  9. data/app/controllers/orchestrator/api_controller.rb +13 -0
  10. data/app/controllers/orchestrator/base.rb +59 -0
  11. data/app/controllers/orchestrator/persistence_controller.rb +29 -0
  12. data/app/models/orchestrator/access_log.rb +35 -0
  13. data/app/models/orchestrator/control_system.rb +160 -0
  14. data/app/models/orchestrator/dependency.rb +87 -0
  15. data/app/models/orchestrator/mod/by_dependency/map.js +6 -0
  16. data/app/models/orchestrator/mod/by_module_type/map.js +6 -0
  17. data/app/models/orchestrator/module.rb +127 -0
  18. data/app/models/orchestrator/sys/by_modules/map.js +9 -0
  19. data/app/models/orchestrator/sys/by_zones/map.js +9 -0
  20. data/app/models/orchestrator/zone.rb +47 -0
  21. data/app/models/orchestrator/zone/all/map.js +6 -0
  22. data/config/routes.rb +43 -0
  23. data/lib/generators/module/USAGE +8 -0
  24. data/lib/generators/module/module_generator.rb +52 -0
  25. data/lib/orchestrator.rb +52 -0
  26. data/lib/orchestrator/control.rb +303 -0
  27. data/lib/orchestrator/core/mixin.rb +123 -0
  28. data/lib/orchestrator/core/module_manager.rb +258 -0
  29. data/lib/orchestrator/core/request_proxy.rb +109 -0
  30. data/lib/orchestrator/core/requests_proxy.rb +47 -0
  31. data/lib/orchestrator/core/schedule_proxy.rb +49 -0
  32. data/lib/orchestrator/core/system_proxy.rb +153 -0
  33. data/lib/orchestrator/datagram_server.rb +114 -0
  34. data/lib/orchestrator/dependency_manager.rb +131 -0
  35. data/lib/orchestrator/device/command_queue.rb +213 -0
  36. data/lib/orchestrator/device/manager.rb +83 -0
  37. data/lib/orchestrator/device/mixin.rb +35 -0
  38. data/lib/orchestrator/device/processor.rb +441 -0
  39. data/lib/orchestrator/device/transport_makebreak.rb +221 -0
  40. data/lib/orchestrator/device/transport_tcp.rb +139 -0
  41. data/lib/orchestrator/device/transport_udp.rb +89 -0
  42. data/lib/orchestrator/engine.rb +70 -0
  43. data/lib/orchestrator/errors.rb +23 -0
  44. data/lib/orchestrator/logger.rb +115 -0
  45. data/lib/orchestrator/logic/manager.rb +18 -0
  46. data/lib/orchestrator/logic/mixin.rb +11 -0
  47. data/lib/orchestrator/service/manager.rb +63 -0
  48. data/lib/orchestrator/service/mixin.rb +56 -0
  49. data/lib/orchestrator/service/transport_http.rb +55 -0
  50. data/lib/orchestrator/status.rb +229 -0
  51. data/lib/orchestrator/system.rb +108 -0
  52. data/lib/orchestrator/utilities/constants.rb +41 -0
  53. data/lib/orchestrator/utilities/transcoder.rb +57 -0
  54. data/lib/orchestrator/version.rb +3 -0
  55. data/lib/orchestrator/websocket_manager.rb +425 -0
  56. data/orchestrator.gemspec +35 -0
  57. data/spec/orchestrator/queue_spec.rb +200 -0
  58. metadata +271 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 3c547e97431a1f2d3914169cfe622e0c056f3eac
4
+ data.tar.gz: 41ededd43c60b014dc0cde4ba8999bc65305bff8
5
+ SHA512:
6
+ metadata.gz: 9d20c2c25f3cd0243774a44cce8e27dc5c05c3983735aaa5ff1318452b962a2fc69f07647ea45fa1da2468cc281312402239505c65aade7bd19ff66f283a6d2c
7
+ data.tar.gz: 78fe6dceb19251529a569ac4f173cc6116d53a43a1ac8a8e82e70e61a0895d8c5b5f9d4dc76fea1937b446ebb6dea22bc539dfde3dae1ba310f483d06418cd4c
@@ -0,0 +1,158 @@
1
+
2
+ ## Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International Public License
3
+
4
+ By exercising the Licensed Rights (defined below), You accept and agree to be bound by the terms and conditions of this Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International Public License ("Public License"). To the extent this Public License may be interpreted as a contract, You are granted the Licensed Rights in consideration of Your acceptance of these terms and conditions, and the Licensor grants You such rights in consideration of benefits the Licensor receives from making the Licensed Material available under these terms and conditions.
5
+
6
+ ### Section 1 – Definitions.
7
+
8
+ a. __Adapted Material__ means material subject to Copyright and Similar Rights that is derived from or based upon the Licensed Material and in which the Licensed Material is translated, altered, arranged, transformed, or otherwise modified in a manner requiring permission under the Copyright and Similar Rights held by the Licensor. For purposes of this Public License, where the Licensed Material is a musical work, performance, or sound recording, Adapted Material is always produced where the Licensed Material is synched in timed relation with a moving image.
9
+
10
+ b. __Adapter's License__ means the license You apply to Your Copyright and Similar Rights in Your contributions to Adapted Material in accordance with the terms and conditions of this Public License.
11
+
12
+ c. __BY-NC-SA Compatible License__ means a license listed at [creativecommons.org/compatiblelicenses](http://creativecommons.org/compatiblelicenses), approved by Creative Commons as essentially the equivalent of this Public License.
13
+
14
+ d. __Copyright and Similar Rights__ means copyright and/or similar rights closely related to copyright including, without limitation, performance, broadcast, sound recording, and Sui Generis Database Rights, without regard to how the rights are labeled or categorized. For purposes of this Public License, the rights specified in Section 2(b)(1)-(2) are not Copyright and Similar Rights.
15
+
16
+ e. __Effective Technological Measures__ means those measures that, in the absence of proper authority, may not be circumvented under laws fulfilling obligations under Article 11 of the WIPO Copyright Treaty adopted on December 20, 1996, and/or similar international agreements.
17
+
18
+ f. __Exceptions and Limitations__ means fair use, fair dealing, and/or any other exception or limitation to Copyright and Similar Rights that applies to Your use of the Licensed Material.
19
+
20
+ g. __License Elements__ means the license attributes listed in the name of a Creative Commons Public License. The License Elements of this Public License are Attribution, NonCommercial, and ShareAlike.
21
+
22
+ h. __Licensed Material__ means the artistic or literary work, database, or other material to which the Licensor applied this Public License.
23
+
24
+ i. __Licensed Rights__ means the rights granted to You subject to the terms and conditions of this Public License, which are limited to all Copyright and Similar Rights that apply to Your use of the Licensed Material and that the Licensor has authority to license.
25
+
26
+ h. __Licensor__ means the individual(s) or entity(ies) granting rights under this Public License.
27
+
28
+ i. __NonCommercial__ means not primarily intended for or directed towards commercial advantage or monetary compensation. For purposes of this Public License, the exchange of the Licensed Material for other material subject to Copyright and Similar Rights by digital file-sharing or similar means is NonCommercial provided there is no payment of monetary compensation in connection with the exchange.
29
+
30
+ j. __Share__ means to provide material to the public by any means or process that requires permission under the Licensed Rights, such as reproduction, public display, public performance, distribution, dissemination, communication, or importation, and to make material available to the public including in ways that members of the public may access the material from a place and at a time individually chosen by them.
31
+
32
+ k. __Sui Generis Database Rights__ means rights other than copyright resulting from Directive 96/9/EC of the European Parliament and of the Council of 11 March 1996 on the legal protection of databases, as amended and/or succeeded, as well as other essentially equivalent rights anywhere in the world.
33
+
34
+ l. __You__ means the individual or entity exercising the Licensed Rights under this Public License. Your has a corresponding meaning.
35
+
36
+ ### Section 2 – Scope.
37
+
38
+ a. ___License grant.___
39
+
40
+ 1. Subject to the terms and conditions of this Public License, the Licensor hereby grants You a worldwide, royalty-free, non-sublicensable, non-exclusive, irrevocable license to exercise the Licensed Rights in the Licensed Material to:
41
+
42
+ A. reproduce and Share the Licensed Material, in whole or in part, for NonCommercial purposes only; and
43
+
44
+ B. produce, reproduce, and Share Adapted Material for NonCommercial purposes only.
45
+
46
+ 2. __Exceptions and Limitations.__ For the avoidance of doubt, where Exceptions and Limitations apply to Your use, this Public License does not apply, and You do not need to comply with its terms and conditions.
47
+
48
+ 3. __Term.__ The term of this Public License is specified in Section 6(a).
49
+
50
+ 4. __Media and formats; technical modifications allowed.__ The Licensor authorizes You to exercise the Licensed Rights in all media and formats whether now known or hereafter created, and to make technical modifications necessary to do so. The Licensor waives and/or agrees not to assert any right or authority to forbid You from making technical modifications necessary to exercise the Licensed Rights, including technical modifications necessary to circumvent Effective Technological Measures. For purposes of this Public License, simply making modifications authorized by this Section 2(a)(4) never produces Adapted Material.
51
+
52
+ 5. __Downstream recipients.__
53
+
54
+ A. __Offer from the Licensor – Licensed Material.__ Every recipient of the Licensed Material automatically receives an offer from the Licensor to exercise the Licensed Rights under the terms and conditions of this Public License.
55
+
56
+ B. __Additional offer from the Licensor – Adapted Material.__ Every recipient of Adapted Material from You automatically receives an offer from the Licensor to exercise the Licensed Rights in the Adapted Material under the conditions of the Adapter’s License You apply.
57
+
58
+ C. __No downstream restrictions.__ You may not offer or impose any additional or different terms or conditions on, or apply any Effective Technological Measures to, the Licensed Material if doing so restricts exercise of the Licensed Rights by any recipient of the Licensed Material.
59
+
60
+ 6. __No endorsement.__ Nothing in this Public License constitutes or may be construed as permission to assert or imply that You are, or that Your use of the Licensed Material is, connected with, or sponsored, endorsed, or granted official status by, the Licensor or others designated to receive attribution as provided in Section 3(a)(1)(A)(i).
61
+
62
+ b. ___Other rights.___
63
+
64
+ 1. Moral rights, such as the right of integrity, are not licensed under this Public License, nor are publicity, privacy, and/or other similar personality rights; however, to the extent possible, the Licensor waives and/or agrees not to assert any such rights held by the Licensor to the limited extent necessary to allow You to exercise the Licensed Rights, but not otherwise.
65
+
66
+ 2. Patent and trademark rights are not licensed under this Public License.
67
+
68
+ 3. To the extent possible, the Licensor waives any right to collect royalties from You for the exercise of the Licensed Rights, whether directly or through a collecting society under any voluntary or waivable statutory or compulsory licensing scheme. In all other cases the Licensor expressly reserves any right to collect such royalties, including when the Licensed Material is used other than for NonCommercial purposes.
69
+
70
+ ### Section 3 – License Conditions.
71
+
72
+ Your exercise of the Licensed Rights is expressly made subject to the following conditions.
73
+
74
+ a. ___Attribution.___
75
+
76
+ 1. If You Share the Licensed Material (including in modified form), You must:
77
+
78
+ A. retain the following if it is supplied by the Licensor with the Licensed Material:
79
+
80
+ i. identification of the creator(s) of the Licensed Material and any others designated to receive attribution, in any reasonable manner requested by the Licensor (including by pseudonym if designated);
81
+
82
+ ii. a copyright notice;
83
+
84
+ iii. a notice that refers to this Public License;
85
+
86
+ iv. a notice that refers to the disclaimer of warranties;
87
+
88
+ v. a URI or hyperlink to the Licensed Material to the extent reasonably practicable;
89
+
90
+ B. indicate if You modified the Licensed Material and retain an indication of any previous modifications; and
91
+
92
+ C. indicate the Licensed Material is licensed under this Public License, and include the text of, or the URI or hyperlink to, this Public License.
93
+
94
+ 2. You may satisfy the conditions in Section 3(a)(1) in any reasonable manner based on the medium, means, and context in which You Share the Licensed Material. For example, it may be reasonable to satisfy the conditions by providing a URI or hyperlink to a resource that includes the required information.
95
+
96
+ 3. If requested by the Licensor, You must remove any of the information required by Section 3(a)(1)(A) to the extent reasonably practicable.
97
+
98
+ b. ___ShareAlike.___
99
+
100
+ In addition to the conditions in Section 3(a), if You Share Adapted Material You produce, the following conditions also apply.
101
+
102
+ 1. The Adapter’s License You apply must be a Creative Commons license with the same License Elements, this version or later, or a BY-NC-SA Compatible License.
103
+
104
+ 2. You must include the text of, or the URI or hyperlink to, the Adapter's License You apply. You may satisfy this condition in any reasonable manner based on the medium, means, and context in which You Share Adapted Material.
105
+
106
+ 3. You may not offer or impose any additional or different terms or conditions on, or apply any Effective Technological Measures to, Adapted Material that restrict exercise of the rights granted under the Adapter's License You apply.
107
+
108
+ ### Section 4 – Sui Generis Database Rights.
109
+
110
+ Where the Licensed Rights include Sui Generis Database Rights that apply to Your use of the Licensed Material:
111
+
112
+ a. for the avoidance of doubt, Section 2(a)(1) grants You the right to extract, reuse, reproduce, and Share all or a substantial portion of the contents of the database for NonCommercial purposes only;
113
+
114
+ b. if You include all or a substantial portion of the database contents in a database in which You have Sui Generis Database Rights, then the database in which You have Sui Generis Database Rights (but not its individual contents) is Adapted Material, including for purposes of Section 3(b); and
115
+
116
+ c. You must comply with the conditions in Section 3(a) if You Share all or a substantial portion of the contents of the database.
117
+
118
+ For the avoidance of doubt, this Section 4 supplements and does not replace Your obligations under this Public License where the Licensed Rights include other Copyright and Similar Rights.
119
+
120
+ ### Section 5 – Disclaimer of Warranties and Limitation of Liability.
121
+
122
+ a. __Unless otherwise separately undertaken by the Licensor, to the extent possible, the Licensor offers the Licensed Material as-is and as-available, and makes no representations or warranties of any kind concerning the Licensed Material, whether express, implied, statutory, or other. This includes, without limitation, warranties of title, merchantability, fitness for a particular purpose, non-infringement, absence of latent or other defects, accuracy, or the presence or absence of errors, whether or not known or discoverable. Where disclaimers of warranties are not allowed in full or in part, this disclaimer may not apply to You.__
123
+
124
+ b. __To the extent possible, in no event will the Licensor be liable to You on any legal theory (including, without limitation, negligence) or otherwise for any direct, special, indirect, incidental, consequential, punitive, exemplary, or other losses, costs, expenses, or damages arising out of this Public License or use of the Licensed Material, even if the Licensor has been advised of the possibility of such losses, costs, expenses, or damages. Where a limitation of liability is not allowed in full or in part, this limitation may not apply to You.__
125
+
126
+ c. The disclaimer of warranties and limitation of liability provided above shall be interpreted in a manner that, to the extent possible, most closely approximates an absolute disclaimer and waiver of all liability.
127
+
128
+ ### Section 6 – Term and Termination.
129
+
130
+ a. This Public License applies for the term of the Copyright and Similar Rights licensed here. However, if You fail to comply with this Public License, then Your rights under this Public License terminate automatically.
131
+
132
+ b. Where Your right to use the Licensed Material has terminated under Section 6(a), it reinstates:
133
+
134
+ 1. automatically as of the date the violation is cured, provided it is cured within 30 days of Your discovery of the violation; or
135
+
136
+ 2. automatically as of the date the violation is cured, provided it is cured within 30 days of Your discovery of the violation; or
137
+
138
+ For the avoidance of doubt, this Section 6(b) does not affect any right the Licensor may have to seek remedies for Your violations of this Public License.
139
+
140
+ c. For the avoidance of doubt, the Licensor may also offer the Licensed Material under separate terms or conditions or stop distributing the Licensed Material at any time; however, doing so will not terminate this Public License.
141
+
142
+ d. Sections 1, 5, 6, 7, and 8 survive termination of this Public License.
143
+
144
+ ### Section 7 – Other Terms and Conditions.
145
+
146
+ a. The Licensor shall not be bound by any additional or different terms or conditions communicated by You unless expressly agreed.
147
+
148
+ b. Any arrangements, understandings, or agreements regarding the Licensed Material not stated herein are separate from and independent of the terms and conditions of this Public License.
149
+
150
+ ### Section 8 – Interpretation.
151
+
152
+ a. For the avoidance of doubt, this Public License does not, and shall not be interpreted to, reduce, limit, restrict, or impose conditions on any use of the Licensed Material that could lawfully be made without permission under this Public License.
153
+
154
+ b. To the extent possible, if any provision of this Public License is deemed unenforceable, it shall be automatically reformed to the minimum extent necessary to make it enforceable. If the provision cannot be reformed, it shall be severed from this Public License without affecting the enforceability of the remaining terms and conditions.
155
+
156
+ c. No term or condition of this Public License will be waived and no failure to comply consented to unless expressly agreed to by the Licensor.
157
+
158
+ d. Nothing in this Public License constitutes or may be interpreted as a limitation upon, or waiver of, any privileges and immunities that apply to the Licensor or You, including from the legal processes of any jurisdiction or authority.
@@ -0,0 +1,13 @@
1
+ # ACA Engine
2
+
3
+ Copyright (c) Advanced Control and Acoustics
4
+
5
+ [![License](https://i.creativecommons.org/l/by-nc-sa/4.0/88x31.png)](http://creativecommons.org/licenses/by-nc-sa/4.0/legalcode)
6
+
7
+ Free for non-commercial use.
8
+
9
+
10
+ ## About
11
+
12
+ ACA Engine is an IoT, Internet of Things, manager that lends itself to HTML5 based user interfaces.
13
+
@@ -0,0 +1,7 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+ require 'rspec/core/rake_task'
4
+
5
+ task :default => [:test]
6
+
7
+ RSpec::Core::RakeTask.new(:test)
@@ -0,0 +1,109 @@
1
+
2
+ module Orchestrator
3
+ module Api
4
+ class DependenciesController < ApiController
5
+ respond_to :json
6
+ before_action :check_admin
7
+ before_action :find_dependency, only: [:show, :update, :destroy, :reload]
8
+
9
+
10
+ @@elastic ||= Elastic.new(Dependency)
11
+
12
+
13
+ def index
14
+ role = params.permit(:role)[:role]
15
+ query = @@elastic.query(params)
16
+
17
+ if role && Dependency::ROLES.include?(role.to_sym)
18
+ query.filter({
19
+ role: [role]
20
+ })
21
+ end
22
+
23
+ query.sort = [{name: "asc"}]
24
+
25
+ respond_with @@elastic.search(query)
26
+ end
27
+
28
+ def show
29
+ respond_with @dep
30
+ end
31
+
32
+ def update
33
+ # Must destroy and re-add to change class or module name
34
+ @dep.update_attributes(update_params)
35
+ save_and_respond @dep
36
+ end
37
+
38
+ def create
39
+ dep = Dependency.new(safe_params)
40
+ save_and_respond dep
41
+ end
42
+
43
+ def destroy
44
+ @dep.delete
45
+ render :nothing => true
46
+ end
47
+
48
+
49
+ ##
50
+ # Additional Functions:
51
+ ##
52
+
53
+ def reload
54
+ depman = ::Orchestrator::DependencyManager.instance
55
+ depman.load(@dep, :force).then(proc {
56
+ updated = 0
57
+
58
+ @dep.modules.each do |mod|
59
+ manager = mod.manager
60
+ if manager
61
+ updated += 1
62
+ manager.reloaded(mod)
63
+ end
64
+ end
65
+
66
+ content = {
67
+ message: updated == 1 ? "#{updated} module updated" : "#{updated} modules updated"
68
+ }.to_json
69
+ env['async.callback'].call([200, {
70
+ 'Content-Length' => content.bytesize,
71
+ 'Content-Type' => 'application/json'
72
+ }, [content]])
73
+ }, proc { |err|
74
+ output = err.message
75
+ env['async.callback'].call([500, {
76
+ 'Content-Length' => output.bytesize,
77
+ 'Content-Type' => 'text/plain'
78
+ }, [output]])
79
+ })
80
+ throw :async
81
+ end
82
+
83
+
84
+ protected
85
+
86
+
87
+ DEP_PARAMS = [
88
+ :name, :description, :role,
89
+ :class_name, :module_name,
90
+ :default
91
+ ]
92
+ def safe_params
93
+ settings = params[:settings]
94
+ {
95
+ settings: settings.is_a?(::Hash) ? settings : {}
96
+ }.merge(params.permit(DEP_PARAMS))
97
+ end
98
+
99
+ def update_params
100
+ params.permit(:name, :description, {settings: []})
101
+ end
102
+
103
+ def find_dependency
104
+ # Find will raise a 404 (not found) if there is an error
105
+ @dep = Dependency.find(id)
106
+ end
107
+ end
108
+ end
109
+ end
@@ -0,0 +1,183 @@
1
+ require 'set'
2
+
3
+
4
+ module Orchestrator
5
+ module Api
6
+ class ModulesController < ApiController
7
+ respond_to :json
8
+ before_action :check_admin, except: [:index, :state, :show]
9
+ before_action :check_support, only: [:index, :state, :show]
10
+ before_action :find_module, only: [:show, :update, :destroy]
11
+
12
+
13
+ @@elastic ||= Elastic.new(::Orchestrator::Module)
14
+
15
+ # Constant for performance
16
+ MOD_INCLUDE = {
17
+ include: {
18
+ # Most human readable module data is contained in dependency
19
+ dependency: {only: [:name, :description, :module_name, :settings]},
20
+
21
+ # include control system on logic modules so it is possible
22
+ # to display the inherited settings
23
+ control_system: {
24
+ only: [:name, :settings],
25
+ methods: [:zone_data]
26
+ }
27
+ }
28
+ }
29
+
30
+
31
+ def index
32
+ filters = params.permit(:system_id, :dependency_id, :connected, :no_logic)
33
+
34
+ # if a system id is present we query the database directly
35
+ if filters[:system_id]
36
+ cs = ControlSystem.find(filters[:system_id])
37
+
38
+ results = ::Orchestrator::Module.find_by_id(cs.modules) || [];
39
+ render json: {
40
+ total: results.length,
41
+ results: results
42
+ }
43
+ else # we use elastic search
44
+ query = @@elastic.query(params)
45
+
46
+ if filters[:dependency_id]
47
+ query.filter({
48
+ dependency_id: [filters[:dependency_id]]
49
+ })
50
+ end
51
+
52
+ if filters[:connected]
53
+ connected = filters[:connected] == 'true'
54
+ query.filter({
55
+ connected: [connected]
56
+ })
57
+ end
58
+
59
+ if filters.has_key? :no_logic
60
+ query.filter({
61
+ role: [1, 2]
62
+ })
63
+ end
64
+
65
+ results = @@elastic.search(query)
66
+ respond_with results, MOD_INCLUDE
67
+ end
68
+ end
69
+
70
+ def show
71
+ respond_with @mod, MOD_INCLUDE
72
+ end
73
+
74
+ def update
75
+ para = safe_params
76
+ old_name = @mod.custom_name
77
+
78
+ @mod.update_attributes(para)
79
+ save_and_respond(@mod) do
80
+ # Update the running module
81
+ control.update(id).then do
82
+ # If custom name is changed we need to expire any system caches
83
+ if para[:custom_name] != old_name
84
+ ::Orchestrator::ControlSystem.using_module(id).each do |sys|
85
+ sys.expire_cache(:no_update)
86
+ end
87
+ end
88
+ end
89
+ end
90
+ end
91
+
92
+ def create
93
+ mod = ::Orchestrator::Module.new(safe_params)
94
+ save_and_respond mod
95
+ end
96
+
97
+ def destroy
98
+ @mod.delete
99
+ render nothing: true
100
+ end
101
+
102
+
103
+ ##
104
+ # Additional Functions:
105
+ ##
106
+
107
+ def start
108
+ # It is possible that module class load can fail
109
+ mod = control.loaded? id
110
+ if mod
111
+ start_module(mod)
112
+ else # attempt to load module
113
+ config = ::Orchestrator::Module.find(id)
114
+ control.load(config).then(
115
+ proc { |mod|
116
+ start_module mod
117
+ },
118
+ proc { # Load failed
119
+ env['async.callback'].call([500, {'Content-Length' => 0}, []])
120
+ }
121
+ )
122
+ end
123
+ throw :async
124
+ end
125
+
126
+ def stop
127
+ # Stop will always succeed
128
+ lookup_module do |mod|
129
+ mod.thread.next_tick do
130
+ mod.stop
131
+ end
132
+ render nothing: true
133
+ end
134
+ end
135
+
136
+ def state
137
+ lookup_module do |mod|
138
+ render json: mod.status[params.permit(:lookup)[:lookup].to_sym]
139
+ end
140
+ end
141
+
142
+
143
+ protected
144
+
145
+
146
+ MOD_PARAMS = [
147
+ :dependency_id, :control_system_id,
148
+ :ip, :tls, :udp, :port, :makebreak,
149
+ :uri, :custom_name, :notes
150
+ ]
151
+ def safe_params
152
+ settings = params[:settings]
153
+ {
154
+ settings: settings.is_a?(::Hash) ? settings : {}
155
+ }.merge(params.permit(MOD_PARAMS))
156
+ end
157
+
158
+ def lookup_module
159
+ mod = control.loaded? id
160
+ if mod
161
+ yield mod
162
+ else
163
+ render nothing: true, status: :not_found
164
+ end
165
+ end
166
+
167
+ def find_module
168
+ # Find will raise a 404 (not found) if there is an error
169
+ @mod = ::Orchestrator::Module.find(id)
170
+ end
171
+
172
+ def start_module(mod)
173
+ mod.thread.next_tick do
174
+ if mod.start
175
+ env['async.callback'].call([200, {'Content-Length' => 0}, []])
176
+ else
177
+ env['async.callback'].call([500, {'Content-Length' => 0}, []])
178
+ end
179
+ end
180
+ end
181
+ end
182
+ end
183
+ end