|
| 1 | +package goros |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "io" |
| 7 | + "log" |
| 8 | + "regexp" |
| 9 | + "strings" |
| 10 | + "sync" |
| 11 | + |
| 12 | + "golang.org/x/net/websocket" |
| 13 | +) |
| 14 | + |
| 15 | +var ( |
| 16 | + messageCount = 0 |
| 17 | +) |
| 18 | + |
| 19 | +type Ros struct { |
| 20 | + origin string |
| 21 | + url string |
| 22 | + ws *websocket.Conn |
| 23 | + receivedMapMutex sync.Mutex |
| 24 | + receivedMap map[string]chan interface{} |
| 25 | +} |
| 26 | + |
| 27 | +func NewRos(url string) *Ros { |
| 28 | + ros := Ros{url: url, origin: "https://localhost"} |
| 29 | + ros.receivedMap = make(map[string]chan interface{}) |
| 30 | + ros.connect() |
| 31 | + go ros.handleIncoming() |
| 32 | + return &ros |
| 33 | +} |
| 34 | + |
| 35 | +func (ros *Ros) connect() { |
| 36 | + ws, err := websocket.Dial(ros.url, "", ros.origin) |
| 37 | + if err != nil { |
| 38 | + log.Fatal(err) |
| 39 | + } |
| 40 | + |
| 41 | + ros.ws = ws |
| 42 | +} |
| 43 | + |
| 44 | +func (ros *Ros) getServiceResponse(service *ServiceCall) *ServiceResponse { |
| 45 | + response := make(chan interface{}) |
| 46 | + ros.receivedMapMutex.Lock() |
| 47 | + ros.receivedMap[service.Id] = response |
| 48 | + ros.receivedMapMutex.Unlock() |
| 49 | + err := websocket.JSON.Send(ros.ws, service) |
| 50 | + if err != nil { |
| 51 | + fmt.Println("Couldn't send msg") |
| 52 | + } |
| 53 | + |
| 54 | + serviceResponse := <-response |
| 55 | + return serviceResponse.(*ServiceResponse) |
| 56 | +} |
| 57 | + |
| 58 | +func (ros *Ros) returnToAppropriateChannel(id string, data interface{}) { |
| 59 | + ros.receivedMapMutex.Lock() |
| 60 | + ros.receivedMap[id] <- data |
| 61 | + ros.receivedMapMutex.Unlock() |
| 62 | +} |
| 63 | + |
| 64 | +func (ros *Ros) handleIncoming() { |
| 65 | + var msg []byte |
| 66 | + for { |
| 67 | + err := websocket.Message.Receive(ros.ws, &msg) |
| 68 | + if err != nil { |
| 69 | + if err == io.EOF { |
| 70 | + break |
| 71 | + } |
| 72 | + fmt.Println("Couldn't receive msg " + err.Error()) |
| 73 | + break |
| 74 | + } |
| 75 | + |
| 76 | + opRegex, err := regexp.Compile(`"op"\s*:\s*"[[:alpha:],_]*`) |
| 77 | + if err != nil { |
| 78 | + log.Println(err) |
| 79 | + } |
| 80 | + opString := opRegex.FindString(string(msg)) |
| 81 | + splitOpString := strings.Split(opString, "\"") |
| 82 | + operation := splitOpString[len(splitOpString)-1] |
| 83 | + |
| 84 | + //log.Println(operation) |
| 85 | + |
| 86 | + /* |
| 87 | + var data map[string]interface{} |
| 88 | + jsonErr := json.Unmarshal(msg, &data) |
| 89 | + //fmt.Printf("Received from server: %s\n", data) |
| 90 | + if jsonErr != nil { |
| 91 | + panic(jsonErr) |
| 92 | + } |
| 93 | +
|
| 94 | + ros.receivedMapMutex.Lock() |
| 95 | + ros.receivedMap[data["id"].(string)] <- data |
| 96 | + ros.receivedMapMutex.Unlock() |
| 97 | + */ |
| 98 | + if operation == "service_response" { |
| 99 | + var serviceResponse ServiceResponse |
| 100 | + json.Unmarshal(msg, &serviceResponse) |
| 101 | + ros.receivedMapMutex.Lock() |
| 102 | + ros.receivedMap[serviceResponse.Id] <- &serviceResponse |
| 103 | + ros.receivedMapMutex.Unlock() |
| 104 | + } |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +func (ros *Ros) GetTopics() []string { |
| 109 | + response := ros.getServiceResponse(newServiceCall("/rosapi/topics")) |
| 110 | + var topics []string |
| 111 | + json.Unmarshal(response.Values["topics"], &topics) |
| 112 | + return topics |
| 113 | +} |
| 114 | + |
| 115 | +func (ros *Ros) Subscribe(topic *Topic) { |
| 116 | + |
| 117 | +} |
0 commit comments