-
Notifications
You must be signed in to change notification settings - Fork 8
/
ros.go
158 lines (135 loc) · 3.45 KB
/
ros.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
package goros
import (
"encoding/json"
"fmt"
"io"
"log"
"sync"
"golang.org/x/net/websocket"
)
var (
messageCount = 0
)
type Base struct {
Op string `json:"op"`
Id string `json:"id"`
}
type Ros struct {
origin string
url string
ws *websocket.Conn
receivedMapMutex sync.Mutex
receivedMap map[string]chan interface{}
}
func NewRos(url string) *Ros {
ros := Ros{url: url, origin: "https://localhost"}
ros.receivedMap = make(map[string]chan interface{})
ros.connect()
go ros.handleIncoming()
return &ros
}
func (ros *Ros) connect() {
ws, err := websocket.Dial(ros.url, "", ros.origin)
if err != nil {
log.Fatal(err)
}
ros.ws = ws
}
func (ros *Ros) getServiceResponse(service *ServiceCall) *ServiceResponse {
response := make(chan interface{})
ros.receivedMapMutex.Lock()
ros.receivedMap[service.Id] = response
ros.receivedMapMutex.Unlock()
err := websocket.JSON.Send(ros.ws, service)
if err != nil {
fmt.Println("Couldn't send msg")
}
serviceResponse := <-response
return serviceResponse.(*ServiceResponse)
}
func (ros *Ros) getTopicResponse(topic *Topic) *interface{} {
response := make(chan interface{})
ros.receivedMapMutex.Lock()
ros.receivedMap[topic.Id] = response
ros.receivedMapMutex.Unlock()
err := websocket.JSON.Send(ros.ws, topic)
if err != nil {
fmt.Println("Couldn't send msg")
}
log.Println(ros.receivedMap)
topicResponse := <-response
return &topicResponse
}
func (ros *Ros) returnToAppropriateChannel(id string, data interface{}) {
ros.receivedMapMutex.Lock()
ros.receivedMap[id] <- data
ros.receivedMapMutex.Unlock()
}
func (ros *Ros) handleIncoming() {
var msg []byte
for {
err := websocket.Message.Receive(ros.ws, &msg)
if err != nil {
if err == io.EOF {
break
}
fmt.Println("Couldn't receive msg " + err.Error())
break
}
/*
opRegex, err := regexp.Compile(`"op"\s*:\s*"[[:alpha:],_]*`)
if err != nil {
log.Println(err)
}
opString := opRegex.FindString(string(msg))
splitOpString := strings.Split(opString, "\"")
operation := splitOpString[len(splitOpString)-1]
*/
var base Base
json.Unmarshal(msg, &base)
log.Println(base)
if base.Op == "service_response" {
var serviceResponse ServiceResponse
json.Unmarshal(msg, &serviceResponse)
ros.receivedMapMutex.Lock()
ros.receivedMap[serviceResponse.Id] <- &serviceResponse
ros.receivedMapMutex.Unlock()
} else if base.Op == "publish" {
log.Println(base)
var topic Topic
json.Unmarshal(msg, &topic)
ros.receivedMapMutex.Lock()
ros.receivedMap[topic.Topic] <- &topic
ros.receivedMapMutex.Unlock()
}
}
}
func (ros *Ros) GetTopics() []string {
response := ros.getServiceResponse(newServiceCall("/rosapi/topics"))
var topics []string
json.Unmarshal(response.Values["topics"], &topics)
return topics
}
func (ros *Ros) GetServices() []string {
response := ros.getServiceResponse(newServiceCall("/rosapi/services"))
var services []string
json.Unmarshal(response.Values["services"], &services)
return services
}
func (ros *Ros) Subscribe(topicName string, callback TopicCallback) {
//topicResponse := ros.getTopicResponse(topic)
topic := NewTopic(topicName)
response := make(chan interface{})
ros.receivedMapMutex.Lock()
ros.receivedMap[topic.Topic] = response
ros.receivedMapMutex.Unlock()
err := websocket.JSON.Send(ros.ws, topic)
if err != nil {
fmt.Println("Couldn't send msg")
}
go func() {
for {
callback(&(<-response).(*Topic).Msg)
}
}()
}