5
5
"fmt"
6
6
"net/http"
7
7
"strconv"
8
+ "strings"
8
9
9
10
"github.com/labstack/echo/v4"
10
11
"github.com/pkg/errors"
@@ -19,6 +20,8 @@ import (
19
20
"github.com/sargassum-world/pslive/internal/clients/presence"
20
21
)
21
22
23
+ const flagChecked = "true"
24
+
22
25
func parseID [ID ~ int64 ](raw string , typeName string ) (ID , error ) {
23
26
const intBase = 10
24
27
const intWidth = 64
@@ -29,6 +32,85 @@ func parseID[ID ~int64](raw string, typeName string) (ID, error) {
29
32
return ID (id ), err
30
33
}
31
34
35
+ // Components (common across cameras & controllers)
36
+
37
+ func handleInstrumentComponentsPost (
38
+ storeAdder func (
39
+ ctx context.Context , iid instruments.InstrumentID , url , protocol string , enabled bool ,
40
+ ) error ,
41
+ ) auth.HTTPHandlerFunc {
42
+ return func (c echo.Context , a auth.Auth ) error {
43
+ // Parse params
44
+ iid , err := parseID [instruments.InstrumentID ](c .Param ("id" ), "instrument" )
45
+ if err != nil {
46
+ return err
47
+ }
48
+ url := c .FormValue ("url" )
49
+ protocol := c .FormValue ("protocol" )
50
+ enabled := strings .ToLower (c .FormValue ("enabled" )) == flagChecked
51
+
52
+ // Run queries
53
+ // FIXME: there needs to be an authorization check to ensure that the user attempting to
54
+ // delete the instrument is an administrator of the instrument!
55
+ if err := storeAdder (c .Request ().Context (), iid , url , protocol , enabled ); err != nil {
56
+ return err
57
+ }
58
+
59
+ // TODO: return turbo stream, broadcast updates
60
+
61
+ // Redirect user
62
+ return c .Redirect (http .StatusSeeOther , fmt .Sprintf ("/instruments/%d" , iid ))
63
+ }
64
+ }
65
+
66
+ func handleInstrumentComponentPost [ComponentID ~ int64 ](
67
+ typeName string ,
68
+ componentUpdater func (
69
+ ctx context.Context , componentID ComponentID , url , protocol string , enabled bool ,
70
+ ) error ,
71
+ componentDeleter func (ctx context.Context , componentID ComponentID ) error ,
72
+ ) auth.HTTPHandlerFunc {
73
+ return func (c echo.Context , a auth.Auth ) error {
74
+ // Parse params
75
+ iid , err := parseID [instruments.InstrumentID ](c .Param ("id" ), "instrument" )
76
+ if err != nil {
77
+ return err
78
+ }
79
+ componentID , err := parseID [ComponentID ](c .Param (typeName + "ID" ), typeName )
80
+ if err != nil {
81
+ return err
82
+ }
83
+ state := c .FormValue ("state" )
84
+
85
+ // Run queries
86
+ ctx := c .Request ().Context ()
87
+ switch state {
88
+ default :
89
+ return echo .NewHTTPError (http .StatusBadRequest , fmt .Sprintf (
90
+ "invalid %s state %s" , typeName , state ,
91
+ ))
92
+ case "updated" :
93
+ protocol := c .FormValue ("protocol" )
94
+ url := c .FormValue ("url" )
95
+ enabled := strings .ToLower (c .FormValue ("enabled" )) == flagChecked
96
+ // FIXME: needs authorization check!
97
+ if err = componentUpdater (ctx , componentID , url , protocol , enabled ); err != nil {
98
+ return err
99
+ }
100
+ // TODO: deal with turbo streams
101
+ case "deleted" :
102
+ // FIXME: needs authorization check!
103
+ if err = componentDeleter (ctx , componentID ); err != nil {
104
+ return err
105
+ }
106
+ // TODO: deal with turbo streams
107
+ }
108
+
109
+ // Redirect user
110
+ return c .Redirect (http .StatusSeeOther , fmt .Sprintf ("/instruments/%d" , iid ))
111
+ }
112
+ }
113
+
32
114
// Instrument
33
115
34
116
type InstrumentViewData struct {
@@ -56,6 +138,9 @@ func getInstrumentViewData(
56
138
vd .ControllerIDs = make ([]instruments.ControllerID , 0 , len (vd .Instrument .Controllers ))
57
139
vd .Controllers = make (map [instruments.ControllerID ]planktoscope.Planktoscope )
58
140
for _ , controller := range vd .Instrument .Controllers {
141
+ if ! controller .Enabled {
142
+ continue
143
+ }
59
144
pc , ok := pco .Get (planktoscope .ClientID (controller .ID ))
60
145
if ! ok {
61
146
return InstrumentViewData {}, errors .Errorf (
@@ -249,126 +334,3 @@ func (h *Handlers) HandleInstrumentDescriptionPost() auth.HTTPHandlerFunc {
249
334
return c .Redirect (http .StatusSeeOther , fmt .Sprintf ("/instruments/%d" , iid ))
250
335
}
251
336
}
252
-
253
- // Components
254
-
255
- func handleInstrumentComponentsPost (
256
- storeAdder func (ctx context.Context , iid instruments.InstrumentID , url , protocol string ) error ,
257
- ) auth.HTTPHandlerFunc {
258
- return func (c echo.Context , a auth.Auth ) error {
259
- // Parse params
260
- iid , err := parseID [instruments.InstrumentID ](c .Param ("id" ), "instrument" )
261
- if err != nil {
262
- return err
263
- }
264
- url := c .FormValue ("url" )
265
- protocol := c .FormValue ("protocol" )
266
-
267
- // Run queries
268
- // FIXME: there needs to be an authorization check to ensure that the user attempting to
269
- // delete the instrument is an administrator of the instrument!
270
- if err := storeAdder (c .Request ().Context (), iid , url , protocol ); err != nil {
271
- return err
272
- }
273
-
274
- // TODO: return turbo stream, broadcast updates
275
-
276
- // Redirect user
277
- return c .Redirect (http .StatusSeeOther , fmt .Sprintf ("/instruments/%d" , iid ))
278
- }
279
- }
280
-
281
- func handleInstrumentComponentPost [ComponentID ~ int64 ](
282
- typeName string ,
283
- componentUpdater func (ctx context.Context , componentID ComponentID , url , protocol string ) error ,
284
- componentDeleter func (ctx context.Context , componentID ComponentID ) error ,
285
- ) auth.HTTPHandlerFunc {
286
- return func (c echo.Context , a auth.Auth ) error {
287
- // Parse params
288
- iid , err := parseID [instruments.InstrumentID ](c .Param ("id" ), "instrument" )
289
- if err != nil {
290
- return err
291
- }
292
- componentID , err := parseID [ComponentID ](c .Param (typeName + "ID" ), typeName )
293
- if err != nil {
294
- return err
295
- }
296
- state := c .FormValue ("state" )
297
-
298
- // Run queries
299
- ctx := c .Request ().Context ()
300
- switch state {
301
- default :
302
- return echo .NewHTTPError (http .StatusBadRequest , fmt .Sprintf (
303
- "invalid %s state %s" , typeName , state ,
304
- ))
305
- case "updated" :
306
- protocol := c .FormValue ("protocol" )
307
- url := c .FormValue ("url" )
308
- // FIXME: needs authorization check!
309
- if err = componentUpdater (ctx , componentID , url , protocol ); err != nil {
310
- return err
311
- }
312
- // TODO: deal with turbo streams
313
- case "deleted" :
314
- // FIXME: needs authorization check!
315
- if err = componentDeleter (ctx , componentID ); err != nil {
316
- return err
317
- }
318
- // TODO: deal with turbo streams
319
- }
320
-
321
- // Redirect user
322
- return c .Redirect (http .StatusSeeOther , fmt .Sprintf ("/instruments/%d" , iid ))
323
- }
324
- }
325
-
326
- // Controllers
327
-
328
- func (h * Handlers ) HandleInstrumentControllersPost () auth.HTTPHandlerFunc {
329
- return handleInstrumentComponentsPost (
330
- func (ctx context.Context , iid instruments.InstrumentID , url , protocol string ) error {
331
- controllerID , err := h .is .AddController (ctx , instruments.Controller {
332
- InstrumentID : iid ,
333
- URL : url ,
334
- Protocol : protocol ,
335
- })
336
- if err != nil {
337
- return err
338
- }
339
- if err := h .pco .Add (planktoscope .ClientID (controllerID ), url ); err != nil {
340
- return err
341
- }
342
- return nil
343
- },
344
- )
345
- }
346
-
347
- func (h * Handlers ) HandleInstrumentControllerPost () auth.HTTPHandlerFunc {
348
- return handleInstrumentComponentPost (
349
- "controller" ,
350
- func (ctx context.Context , controllerID instruments.ControllerID , url , protocol string ) error {
351
- if err := h .is .UpdateController (ctx , instruments.Controller {
352
- ID : controllerID ,
353
- URL : url ,
354
- Protocol : protocol ,
355
- }); err != nil {
356
- return err
357
- }
358
- // Note: when we have other controllers, we'll need to generalize this
359
- if err := h .pco .Update (ctx , planktoscope .ClientID (controllerID ), url ); err != nil {
360
- return err
361
- }
362
- return nil
363
- },
364
- func (ctx context.Context , controllerID instruments.ControllerID ) error {
365
- if err := h .is .DeleteController (ctx , controllerID ); err != nil {
366
- return err
367
- }
368
- if err := h .pco .Remove (ctx , planktoscope .ClientID (controllerID )); err != nil {
369
- return err
370
- }
371
- return nil
372
- },
373
- )
374
- }
0 commit comments