-
Notifications
You must be signed in to change notification settings - Fork 4
/
thing.go
674 lines (593 loc) · 17.5 KB
/
thing.go
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
package webthing
import (
"encoding/json"
"errors"
"fmt"
"path/filepath"
"strings"
"github.com/gorilla/websocket"
"github.com/xeipuuv/gojsonschema"
)
// Thing A Web Thing struct.
type Thing struct {
id string
context string
atType []string
title string
description string
properties map[string]*Property
availableActions map[string]*AvailableAction
availableEvents map[string]*AvailableEvent
actions map[string][]*Action
events []*Event
subscribers map[string]*websocket.Conn
hrefPrefix string
uiHref string
}
// ThingMember thingmember
type ThingMember struct {
ID string `json:"id"`
Context string `json:"@context"`
AtType []string `json:"@type"`
Title string `json:"title"`
Description string `json:"description,omitempty"`
Properties json.RawMessage `json:"properties,omitempty"`
Actions map[string]json.RawMessage `json:"actions,omitempty"`
Events map[string]json.RawMessage `json:"events,omitempty"`
Links []Link `json:"links"`
}
func NewThingMember(thing *Thing) *ThingMember {
th := &ThingMember{
ID: thing.id,
Title: thing.title,
Context: thing.context,
AtType: thing.atType,
Description: thing.description,
Properties: json.RawMessage{},
Actions: make(map[string]json.RawMessage),
Events: make(map[string]json.RawMessage),
}
return th
}
// NewThing create a thing.
func NewThing(id, title string, atType []string, description string) *Thing {
thing := &Thing{}
thing.id = id
thing.title = title
thing.context = "https://webthings.io/schemas"
thing.atType = atType
thing.description = description
thing.properties = make(map[string]*Property)
thing.availableActions = make(map[string]*AvailableAction)
thing.availableEvents = make(map[string]*AvailableEvent)
thing.actions = make(map[string][]*Action)
thing.events = []*Event{}
thing.subscribers = map[string]*websocket.Conn{}
thing.hrefPrefix = ""
thing.uiHref = ""
return thing
}
// Link base link struct
type Link struct {
Href string `json:"href,omitempty"`
Rel string `json:"rel,omitempty"`
MediaType string `json:"mediaType,omitempty"`
}
func (th *ThingMember) availableActionsDesc(thing *Thing) {
for name := range thing.availableActions {
meta := thing.availableActions[name].Metadata()
var m map[string]interface{}
json.Unmarshal(meta, &m)
m["links"] = []Link{{
Rel: "action",
Href: filepath.Clean(fmt.Sprintf("/%s/actions/%s", thing.Href(), name)),
}}
obj, _ := json.Marshal(m)
th.Actions[name] = obj
}
}
func (th *ThingMember) availableEventsDesc(thing *Thing) {
for name := range thing.availableEvents {
meta, _ := thing.availableEvents[name].Metadata().MarshalJSON()
var m map[string]interface{}
json.Unmarshal(meta, &m)
m["links"] = []Link{{
Rel: "events",
Href: filepath.Clean(fmt.Sprintf("/%s/events/%s", thing.Href(), name)),
}}
obj, _ := json.Marshal(m)
th.Events[name] = obj
}
}
func (th *ThingMember) links(thing *Thing) {
for _, name := range []string{"properties", "actions", "events"} {
th.Links = append(th.Links, Link{
Rel: name,
Href: fmt.Sprintf("%s/%s", thing.hrefPrefix, name),
})
}
if thing.UIHref() != "" {
th.Links = append(th.Links, Link{
Rel: "alternate",
MediaType: "text/html",
Href: filepath.Clean(thing.UIHref()),
})
}
}
// AsThingDescription retrun []byte data of thing struct.
// Return the thing state as a Thing Description.
// @returns {Object} Current thing state
func (thing *Thing) AsThingDescription() []byte {
th := NewThingMember(thing)
th.Properties = []byte(thing.PropertyDescriptions())
th.availableActionsDesc(thing)
th.availableEventsDesc(thing)
th.links(thing)
thingDescription, err := json.Marshal(th)
if err != nil {
fmt.Println(err.Error())
}
return thingDescription
}
// Href Get this thing's href.
//
// @returns {String} The href.
func (thing *Thing) Href() string {
if thing.hrefPrefix != "" {
return thing.hrefPrefix
}
return "/"
}
// UIHref Get this thing's UI href.
//
// @returns {String|null} The href.
func (thing *Thing) UIHref() string {
return thing.uiHref
}
// SetHrefPrefix Set the prefix of any hrefs associated with this thing.
//
// @param {String} prefix The prefix
func (thing *Thing) SetHrefPrefix(prefix string) {
thing.hrefPrefix = prefix
for name := range thing.properties {
thing.properties[name].SetHrefPrefix(prefix)
}
for name := range thing.actions {
for key := range thing.actions[name] {
thing.actions[name][key].SetHrefPrefix(prefix)
}
}
}
// SetUIHref Set the href of this thing's custom UI.
//
// @param {String} href The href
func (thing *Thing) SetUIHref(href string) {
thing.uiHref = href
}
// ID Get the ID of the thing.
//
// @returns {String} The ID.
func (thing *Thing) ID() string {
return thing.id
}
// Title Get the title of the thing.
//
// @returns {String} The title.
func (thing *Thing) Title() string {
return thing.title
}
// Context Get the type context of the thing.
//
// @returns {String} The contexthing.
func (thing *Thing) Context() string {
return thing.context
}
// Type Get the type(s) of the thing.
//
// @returns {String[]} The type(s).
func (thing *Thing) Type() []string {
return thing.atType
}
// Description Get the description of the thing.
//
// @returns {String} The description.
func (thing *Thing) Description() string {
return thing.description
}
// PropertyDescriptions Get the thing's properties as an object.
//
// @returns {Object} Properties, i.e. name -> description
func (thing *Thing) PropertyDescriptions() string {
descriptions := make(map[string]json.RawMessage)
for name, property := range thing.properties {
descriptions[name] = []byte(property.AsPropertyDescription())
}
str, _ := json.Marshal(descriptions)
return string(str)
}
// ActionDescriptions Get the thing's actions as an array.
//
// @param {String?} actionName Optional action name to get descriptions for
// @returns {Object} Action descriptions.
func (thing *Thing) ActionDescriptions(actionName string) (descriptions []json.RawMessage) {
if actionName != "" {
return actionsDescription(thing, descriptions, actionName)
}
for name := range thing.actions {
descriptions = actionsDescription(thing, descriptions, name)
}
return descriptions
}
func actionsDescription(thing *Thing, descriptions []json.RawMessage, actionName string) []json.RawMessage {
if actions, ok := thing.actions[actionName]; ok {
for _, action := range actions {
if action != nil {
descriptions = append(descriptions, []byte(action.AsActionDescription()))
}
}
}
return descriptions
}
// EventDescriptions Get the thing's events as an array.
//
//@param {String?} eventName Optional event name to get descriptions for
//
//@returns {Object} Event descriptions.
func (thing *Thing) EventDescriptions(eventName string) []byte {
var descriptions []json.RawMessage
if len(thing.events) == 0 {
return []byte(`{}`)
}
for _, event := range thing.events {
if eventName == "" || strings.EqualFold(event.Name(), eventName) {
descriptions = append(descriptions, event.AsEventDescription())
}
}
content, _ := json.Marshal(descriptions)
return content
}
// AddProperty Add a property to this thing.
//
// @param property Property to add.
func (thing *Thing) AddProperty(property *Property) {
property.SetHrefPrefix(thing.hrefPrefix)
thing.properties[property.Name()] = property
}
// RemoveProperty Remove a property from this thing.
//
// @param property Property to remove.
func (thing *Thing) RemoveProperty(property Property) {
if p, ok := thing.properties[property.Name()]; ok {
delete(thing.properties, p.Name())
}
}
// Find a property by name.
//
// @param propertyName Name of the property to find
// @return Property if found, else null.
func (thing *Thing) findProperty(propertyName string) (*Property, bool) {
if p, ok := thing.properties[propertyName]; ok {
return p, true
}
return &Property{}, false
}
// Property Get a property's value.
//
// @param propertyName Name of the property to get the value of
// @param <T> Type of the property value
// @return Current property value if found, else null.
func (thing *Thing) Property(propertyName string) *Value {
if prop, ok := thing.findProperty(propertyName); ok {
return prop.Value()
}
return &Value{}
}
// Properties et a mapping of all properties and their values.
//
// @return JSON object of propertyName -> value.
func (thing *Thing) Properties() map[string]interface{} {
properties := make(map[string]interface{})
for name, property := range thing.properties {
properties[name] = property.Value().Get()
}
return properties
}
// Determine whether or not this thing has a given property.
//
// @param propertyName The property to look for
// @return Indication of property presence.
func (thing *Thing) HasProperty(propertyName string) bool {
if _, ok := thing.properties[propertyName]; ok {
return true
}
return false
}
// SetProperty Set a property value.
//
// @param propertyName Name of the property to set
// @param value Value to set
// @param <T> Type of the property value
// @throws PropertyError If value could not be set.
func (thing *Thing) SetProperty(propertyName string, value *Value) error {
if _, ok := thing.findProperty(propertyName); !ok {
return errors.New(`"General property error"`)
}
property := thing.properties[propertyName]
return property.SetValue(value)
}
// Action Get an action.
//
// @param actionName Name of the action
// @param actionId ID of the action
// @return The requested action if found, else null.
func (thing *Thing) Action(actionName, actionID string) (action *Action) {
if _, ok := thing.actions[actionName]; !ok {
return nil
}
for _, ac := range thing.actions[actionName] {
// Each newly created action must contain a new uuid,
// otherwise a random action with the same uuid will be found and returned.
if ac != nil && ac.ID() == actionID {
action = ac
}
}
return action
}
// AddEvent Add a new event and notify subscribers.
//
// @param event The event that occurred.
func (thing *Thing) AddEvent(event *Event) {
thing.events = append(thing.events, event)
thing.EventNotify(event)
}
// AddAvailableEvent Add an available event.
//
// @param name Name of the event
// @param metadata Event metadata, i.e. type, description, etc., as a
// JSONObject
func (thing *Thing) AddAvailableEvent(name string, metadata json.RawMessage) {
thing.availableEvents[name] = NewAvailableEvent(metadata)
}
// PerformAction Perform an action on the thing.
//
// @param actionName Name of the action
// @param input Any action inputs
// @return The action that was created.
func (thing *Thing) PerformAction(actionName string, input *json.RawMessage) (*Action, error) {
if _, ok := thing.availableActions[actionName]; !ok {
fmt.Print("Not found action: ", actionName)
return nil, errors.New("Not found action: " + actionName)
}
actionType := thing.availableActions[actionName]
schemaLoader := gojsonschema.NewGoLoader(actionType.schema)
documentLoader := gojsonschema.NewGoLoader(input)
result, err := gojsonschema.Validate(schemaLoader, documentLoader)
if err != nil {
str1, _ := schemaLoader.LoadJSON()
fmt.Println(str1)
str2, _ := documentLoader.LoadJSON()
fmt.Println(str2)
fmt.Println(err)
return nil, err
}
// if result.Valid() {
// fmt.Printf("The document is valid\n")
// }
if !result.Valid() {
fmt.Printf("The document is not valid. see errors :\n")
for _, desc := range result.Errors() {
fmt.Printf("- %s\n", desc)
}
return nil, err
}
// if !actionType.ValidateActionInput(input) {
// return nil
// }
cls := actionType.getCls()
// The Generator is called to create an action.
action := cls.Generator(thing)
action.SetInput(input)
action.SetHrefPrefix(thing.hrefPrefix)
thing.ActionNotify(action)
thing.actions[actionName] = append(thing.actions[actionName], action)
return action, nil
}
// RemoveAction Remove an existing action.
//
// @param actionName name of the action
// @param actionId ID of the action
// @return Boolean indicating the presence of the action.
func (thing *Thing) RemoveAction(actionName, actionID string) bool {
action := thing.Action(actionName, actionID)
if action.ID() == "" {
return false
}
defer action.Cancel() // Cancel action after delete from origin.
actions := thing.actions[actionName]
for k, ac := range actions {
if ac != nil && ac.ID() == actionID {
actions[k] = nil
}
}
return true
}
// AddAvailableAction Add an available action.
//
// @param name Name of the action
// @param metadata Action metadata, i.e. type, description, etc., as a
// JSONObject
// @param action Instantiate for this action
func (thing *Thing) AddAvailableAction(name string, metadata json.RawMessage, action Actioner) {
thing.availableActions[name] = NewAvailableAction(metadata, action)
thing.actions[name] = []*Action{}
}
// AddSubscriber Add a new websocket subscriber.
//
// @param ws The websocket
func (thing *Thing) AddSubscriber(wsID string, ws *websocket.Conn) {
thing.subscribers[wsID] = ws
}
// RemoveSubscriber Remove a websocket subscriber.
//
// @param ws The websocket
func (thing *Thing) RemoveSubscriber(name string, ws *websocket.Conn) {
delete(thing.subscribers, name)
for name := range thing.availableEvents {
thing.RemoveEventSubscriber(name, ws)
}
}
// AddEventSubscriber Add a new websocket subscriber to an event.
//
// @param name Name of the event
// @param ws The websocket
func (thing *Thing) AddEventSubscriber() {}
// RemoveEventSubscriber Remove a websocket subscriber from an event.
//
// @param name Name of the event
// @param ws The websocket
func (thing *Thing) RemoveEventSubscriber(name string, ws *websocket.Conn) error {
delete(thing.availableEvents, name)
if _, ok := thing.availableEvents[name]; ok {
for _, eventWS := range thing.availableEvents[name].subscribers {
if err := eventWS.Close(); err != nil {
return err
}
}
}
return nil
}
type message struct {
MessageType string `json:"messageType"`
Data json.RawMessage `json:"data"`
}
// PropertyNotify Notify all subscribers of a property change.
//
// @param property The property that changed
func (thing *Thing) PropertyNotify(property Property) error {
str := message{
MessageType: "propertyStatus",
Data: property.AsPropertyDescription(),
}
msg, err := json.Marshal(str)
if err != nil {
return err
}
for _, sub := range thing.subscribers {
if err := sub.WriteJSON(msg); err != nil {
return err
}
}
return nil
}
// ActionNotify Notify all subscribers of an action status change.
//
// @param action The action whose status changed
func (thing *Thing) ActionNotify(action *Action) error {
str := message{
MessageType: "actionStatus",
Data: action.AsActionDescription(),
}
msg, err := json.Marshal(str)
if err != nil {
return err
}
for _, sub := range thing.subscribers {
if err := sub.WriteJSON(msg); err != nil {
return err
}
}
return nil
}
// EventNotify Notify all subscribers of an event.
//
// @param event The event that occurred
func (thing *Thing) EventNotify(event *Event) error {
eventName := event.Name()
if _, ok := thing.availableEvents[eventName]; !ok {
return errors.New("Event not found. ")
}
str := message{
MessageType: "event",
Data: event.AsEventDescription(),
}
msg, err := json.Marshal(str)
if err != nil {
return err
}
for _, sub := range thing.subscribers {
if err := sub.WriteJSON(msg); err != nil {
return err
}
}
return nil
}
// AvailableEvent Class to describe an event available for subscription.
type AvailableEvent struct {
metadata json.RawMessage
subscribers map[string]*websocket.Conn
}
// NewAvailableEvent Initialize the object.
//
// @param metadata The event metadata
func NewAvailableEvent(metadata json.RawMessage) *AvailableEvent {
return &AvailableEvent{metadata: metadata, subscribers: make(map[string]*websocket.Conn)}
}
// Metadata Get the event metadata.
//
// @return The metadata.
func (ae *AvailableEvent) Metadata() json.RawMessage {
return ae.metadata
}
// AvailableAction Class to describe an action available to be taken.
type AvailableAction struct {
metadata json.RawMessage
action *Action
schema interface{}
cls Actioner
}
// NewAvailableAction Initialize the object.
//
// @param metadata The action metadata
// @param action Instance for the action
func NewAvailableAction(metadata json.RawMessage, cls Actioner) *AvailableAction {
ac := &AvailableAction{}
ac.metadata = metadata
ac.cls = cls
// Creating the map for input
m := map[string]json.RawMessage{}
json.Unmarshal(metadata, &m)
if _, ok := m["input"]; ok {
ac.schema = m["input"]
}
return ac
}
// Get the class to instantiate for the action.
//
// @return The class.
func (ac *AvailableAction) getCls() Actioner {
return ac.cls
}
// Metadata Get the action metadata.
//
// @return The metadata.
func (ac *AvailableAction) Metadata() []byte {
metaData, _ := json.Marshal(ac.metadata)
return metaData
}
// Action Get the class to instantiate for the action.
//
// @return The class.
func (ac *AvailableAction) Action() *Action {
return ac.action
}
// ValidateActionInput Validate the input for a new action.
//
// @param actionInput The input to validate
// @return Boolean indicating validation success.
func (ac *AvailableAction) ValidateActionInput(actionInput interface{}) bool {
if ac.schema == nil {
return true
}
_, err := json.Marshal(actionInput)
return err == nil
}