-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathwebsocketclient.go
More file actions
134 lines (104 loc) · 3.21 KB
/
websocketclient.go
File metadata and controls
134 lines (104 loc) · 3.21 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
package main
import (
"errors"
"log"
"sync"
ucapi "github.com/enbility/eebus-go/usecases/api"
shipapi "github.com/enbility/ship-go/api"
"github.com/gorilla/websocket"
)
type WebsocketClient struct {
websocket *websocket.Conn
mutex sync.Mutex
mutex2 sync.Mutex
}
func (websocketClient *WebsocketClient) sendMessage(msg interface{}) error {
if websocketClient.websocket == nil {
return errors.New("no frontend connected")
}
websocketClient.mutex.Lock()
defer websocketClient.mutex.Unlock()
err := websocketClient.websocket.WriteJSON(msg)
if err != nil {
log.Println(err)
}
return err
}
func (websocketClient *WebsocketClient) sendNotification(ski string, messageType int, uc string) error {
answer := Message{
SKI: ski,
Type: messageType,
UseCase: uc}
return websocketClient.sendMessage(answer)
}
func (websocketClient *WebsocketClient) sendText(messageType int, text string) error {
answer := Message{
Type: messageType,
Text: text}
return websocketClient.sendMessage(answer)
}
func (websocketClient *WebsocketClient) sendValue(ski string, messageType int, useCase string, value float64) error {
answer := Message{
SKI: ski,
Type: messageType,
Value: value,
UseCase: useCase}
return websocketClient.sendMessage(answer)
}
func (websocketClient *WebsocketClient) sendValueArr(ski string, messageType int, useCase string, values []float64) error {
answer := Message{
SKI: ski,
Type: messageType,
Values: values,
UseCase: useCase}
return websocketClient.sendMessage(answer)
}
func (websocketClient *WebsocketClient) sendLimit(ski string, messageType int, useCase string, limit ucapi.LoadLimit) error {
answer := Message{
SKI: ski,
Type: messageType,
Limit: limit,
UseCase: useCase}
return websocketClient.sendMessage(answer)
}
func (websocketClient *WebsocketClient) sendServiceList(messageType int, services []shipapi.RemoteService) error {
answer := Message{
Type: messageType,
ServiceList: services}
return websocketClient.sendMessage(answer)
}
func (websocketClient *WebsocketClient) sendEntityInfo(messageType int, remoteInfos map[string]RemoteInfo) error {
websocketClient.mutex2.Lock()
defer websocketClient.mutex2.Unlock()
entityInfos := []EntityInfo{}
for _, remoteInfo := range remoteInfos {
device := remoteInfo.Device
if device != nil {
for _, entity := range device.Entities() {
features := []string{}
for _, f := range entity.Features() {
features = append(features, f.String()+", "+string(f.Role()))
}
info := EntityInfo{
Address: entity.Address().String(),
Name: string(entity.EntityType()),
SKI: device.Ski(),
Type: string(*device.DeviceType()),
Features: features}
entityInfos = append(entityInfos, info)
}
}
}
answer := Message{
Type: messageType,
EntityInfos: entityInfos}
return websocketClient.sendMessage(answer)
}
func (websocketClient *WebsocketClient) sendUseCaseInfo(messageType int, useCaseInfos map[string][]UseCaseInfo) error {
websocketClient.mutex2.Lock()
defer websocketClient.mutex2.Unlock()
answer := Message{
Type: messageType,
UseCaseInfos: useCaseInfos}
return websocketClient.sendMessage(answer)
}