-
Notifications
You must be signed in to change notification settings - Fork 72
Expand file tree
/
Copy pathreaction_controller.rb
More file actions
302 lines (289 loc) · 10.1 KB
/
reaction_controller.rb
File metadata and controls
302 lines (289 loc) · 10.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
# encoding: ascii-8bit
# Copyright 2022 Ball Aerospace & Technologies Corp.
# All Rights Reserved.
#
# This program is free software; you can modify and/or redistribute it
# under the terms of the GNU Affero General Public License
# as published by the Free Software Foundation; version 3 with
# attribution addendums as found in the LICENSE.txt
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
# Modified by OpenC3, Inc.
# All changes Copyright 2024, OpenC3, Inc.
# All Rights Reserved
#
# This file may also be used under the terms of a commercial license
# if purchased from OpenC3, Inc.
require 'openc3/models/reaction_model'
class ReactionController < ApplicationController
NOT_FOUND = 'not found'
def initialize
super()
@model_class = OpenC3::ReactionModel
end
# Returns an array/list of reactions in json.
#
# scope [String] the scope of the reaction, `TEST`
# @return [String] the array of triggers converted into json format
def index
return unless authorization('system')
begin
triggers = @model_class.all(scope: params[:scope])
ret = []
triggers.each do |_, trigger|
ret << trigger
end
render json: ret
rescue StandardError => e
log_error(e)
render json: { status: 'error', message: e.message, type: e.class, backtrace: e.backtrace }, status: 500
end
end
# Returns an reactions in json.
#
# name [String] the reaction name, `REACT0`
# scope [String] the scope of the reaction, `TEST`
# @return [String] the array of reactions converted into json format.
def show
return unless authorization('system')
begin
model = @model_class.get(name: params[:name], scope: params[:scope])
render json: model.as_json()
rescue OpenC3::ReactionInputError => e
log_error(e)
render json: { status: 'error', message: e.message, type: e.class }, status: 404
rescue OpenC3::ReactionError => e
log_error(e)
render json: { status: 'error', message: e.message, type: e.class }, status: 400
rescue StandardError => e
log_error(e)
render json: { status: 'error', message: e.message, type: e.class, backtrace: e.backtrace }, status: 500
end
end
# Create a new reaction and return the object/hash of the trigger in json.
#
# name [String] the reaction name, `REACT0`
# scope [String] the scope of the trigger, `TEST`
# json [String] The json of the event (see #reaction_model)
# @return [String] the trigger converted into json format
# Request Headers
#```json
# {
# "Authorization": "token/password",
# "Content-Type": "application/json"
# }
#```
# Request Post Body
#```json
# {
# "triggers": [
# {
# "name": "TV0-1234",
# "group": "foo",
# }
# ],
# "trigger_level": 'EDGE',
# "actions": [
# {
# "type": "command",
# "value": "INST CLEAR",
# }
# ],
# "snooze": 300,
# }
#```
def create
return unless authorization('script_run')
begin
hash = params.to_unsafe_h.slice(:triggers, :trigger_level, :actions, :snooze).to_h
name = @model_class.create_unique_name(scope: params[:scope])
hash[:username] = username()
model = @model_class.from_json(hash.symbolize_keys, name: name, scope: params[:scope])
model.create()
model.deploy()
render json: model.as_json(), status: 201
rescue OpenC3::ReactionInputError => e
log_error(e)
render json: { status: 'error', message: e.message, type: e.class }, status: 400
rescue OpenC3::ReactionError => e
log_error(e)
render json: { status: 'error', message: e.message, type: e.class }, status: 418
rescue StandardError => e
log_error(e)
render json: { status: 'error', message: e.message, type: e.class, backtrace: e.backtrace }, status: 500
end
end
# Update and returns an object/hash of a single reaction in json.
#
# name [String] the reaction name, `REACT0`
# scope [String] the scope of the reaction, `TEST`
# json [String] The json of the event (see #reaction_model)
# @return [String] the reaction as a object/hash converted into json format
# Request Headers
#```json
# {
# "Authorization": "token/password",
# "Content-Type": "application/json"
# }
#```
# Request Post Body
#```json
# {}
#```
def update
return unless authorization('script_run')
hash = nil
begin
model = @model_class.get(name: params[:name], scope: params[:scope])
if model.nil?
render json: { status: 'error', message: NOT_FOUND }, status: 404
return
end
hash = params.to_unsafe_h.slice(:snooze, :triggers, :trigger_level, :actions).to_h
model.triggers = hash['triggers'] if hash['triggers']
model.actions = hash['actions'] if hash['actions']
model.trigger_level = hash['trigger_level'] if hash['trigger_level']
model.snooze = hash['snooze'] if hash['snooze']
# Notify the ReactionMicroservice to update the ReactionModel
# We don't update directly here to avoid a race condition between the microservice
# updating state and an asynchronous user updating the reaction
model.notify(kind: 'updated')
render json: model.as_json()
rescue OpenC3::ReactionInputError => e
log_error(e)
render json: { status: 'error', message: e.message, type: e.class }, status: 400
rescue OpenC3::ReactionError => e
log_error(e)
render json: { status: 'error', message: e.message, type: e.class }, status: 418
rescue StandardError => e
log_error(e)
render json: { status: 'error', message: e.message, type: e.class, backtrace: e.backtrace }, status: 500
end
end
# Enable reaction
#
# name [String] the reaction name, `REACT0`
# scope [String] the scope of the reaction, `TEST`
# @return [String] the reaction as a object/hash converted into json format
# Request Headers
#```json
# {
# "Authorization": "token/password",
# "Content-Type": "application/json"
# }
#```
# Request Post Body
#```json
# {}
#```
def enable
return unless authorization('script_run')
begin
model = @model_class.get(name: params[:name], scope: params[:scope])
if model.nil?
render json: { status: 'error', message: NOT_FOUND }, status: 404
return
end
# Notify the ReactionMicroservice to enable the ReactionModel
# We don't update directly here to avoid a race condition between the microservice
# updating state and an asynchronous user enabling the reaction
model.notify_enable
render json: model.as_json()
rescue StandardError => e
log_error(e)
render json: { status: 'error', message: e.message, type: e.class, backtrace: e.backtrace }, status: 500
end
end
# Disable reaction
#
# name [String] the reaction name, `REACT0`
# scope [String] the scope of the reaction, `TEST`
# @return [String] the reaction as a object/hash converted into json format
# Request Headers
#```json
# {
# "Authorization": "token/password",
# "Content-Type": "application/json"
# }
#```
# Request Post Body
#```json
# {}
#```
def disable
return unless authorization('script_run')
begin
model = @model_class.get(name: params[:name], scope: params[:scope])
if model.nil?
render json: { status: 'error', message: NOT_FOUND }, status: 404
return
end
# Notify the ReactionMicroservice to disable the ReactionModel
# We don't update directly here to avoid a race condition between the microservice
# updating state and an asynchronous user disabling the reaction
model.notify_disable
render json: model.as_json()
rescue StandardError => e
log_error(e)
render json: { status: 'error', message: e.message, type: e.class, backtrace: e.backtrace }, status: 500
end
end
# Execute a reaction's actions
def execute
return unless authorization('script_run')
begin
model = @model_class.get(name: params[:name], scope: params[:scope])
if model.nil?
render json: { status: 'error', message: NOT_FOUND }, status: 404
return
end
# Notify the ReactionMicroservice to execute the ReactionModel
# We don't update directly here to avoid a race condition between the microservice
# updating state and an asynchronous user executing the reaction
model.notify_execute
render json: model.as_json()
rescue StandardError => e
log_error(e)
render json: { status: 'error', message: e.message, type: e.class, backtrace: e.backtrace }, status: 500
end
end
# Removes an reaction by name/id.
#
# name [String] the reaction name, `REACT1`
# scope [String] the scope of the reaction, `TEST`
# @return [String] object/hash converted into json format but with a 200 status code
# Request Headers
#```json
# {
# "Authorization": "token/password",
# "Content-Type": "application/json"
# }
#```
def destroy
return unless authorization('script_run')
begin
model = @model_class.get(name: params[:name], scope: params[:scope])
if model.nil?
render json: { status: 'error', message: NOT_FOUND }, status: 404
return
end
# Notify the ReactionMicroservice to delete the ReactionModel
# We don't update directly here to avoid a race condition between the microservice
# updating state and an asynchronous user deleting the reaction
model.notify(kind: 'deleted')
render json: model.as_json()
rescue OpenC3::ReactionInputError => e
log_error(e)
render json: { status: 'error', message: e.message, type: e.class }, status: 404
rescue OpenC3::ReactionError => e
log_error(e)
render json: { status: 'error', message: e.message, type: e.class }, status: 400
rescue StandardError => e
log_error(e)
render json: { status: 'error', message: e.message, type: e.class, backtrace: e.backtrace }, status: 500
end
end
end