-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.go
144 lines (125 loc) · 3.47 KB
/
server.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
package goweatherflow
import (
"encoding/json"
"fmt"
"net"
"regexp"
"sync"
"time"
)
type TempestServer struct {
conn *net.UDPConn
running bool
serverWaitGroup sync.WaitGroup
ReceivedMessage chan WeatherFlowMessage
}
//NewServer creates a new server that will start listening
//for new Tempest UDP events
func NewServer() (*TempestServer, error) {
serverAddr, err := net.ResolveUDPAddr("udp4", ":50222")
if err != nil {
return nil, err
}
conn, err := net.ListenUDP("udp4", serverAddr)
if err != nil {
return nil, err
}
server := new(TempestServer)
server.conn = conn
server.ReceivedMessage = make(chan WeatherFlowMessage, 50)
return server, nil
}
//Start starts the server by spinning up a goroutine.
//The function returns only after the server is stopped.
func (server *TempestServer) Start() {
if server.running {
return
}
server.running = true
server.serverWaitGroup.Add(1)
go server.startServer()
server.serverWaitGroup.Wait()
}
func (server *TempestServer) startServer() {
buf := make([]byte, 1024)
for server.running {
server.conn.SetReadDeadline(time.Now().Add(time.Second * 10))
n, _, _, addr, err := server.conn.ReadMsgUDP(buf, nil)
if err != nil {
//err was likely a timeout, we'll continue.
continue
}
sliceCopy := make([]byte, n)
copy(sliceCopy, buf[:n])
go server.processData(addr, sliceCopy)
}
server.serverWaitGroup.Done()
}
func (server *TempestServer) processData(addr net.Addr, data []byte) error {
stringData := string(data)
// fmt.Println(stringData)
typeRegex := regexp.MustCompile("\"type\":\\s*\"(.*?)\"")
typeMatches := typeRegex.FindStringSubmatch(stringData)
if len(typeMatches) != 2 {
return fmt.Errorf("Unable to find type in message")
}
messageType := typeMatches[1]
switch messageType {
case "evt_precip":
msg := new(RainStartEvent)
err := json.Unmarshal(data, msg)
if err != nil {
return fmt.Errorf("Unable to deserialize json message message: %s", err)
}
server.ReceivedMessage <- msg
case "evt_strike":
msg := new(LightningStrikeEvent)
err := json.Unmarshal(data, msg)
if err != nil {
return fmt.Errorf("Unable to deserialize json message message: %s", err)
}
server.ReceivedMessage <- msg
case "rapid_wind":
msg := new(RapidWindObservation)
err := json.Unmarshal(data, msg)
if err != nil {
return fmt.Errorf("Unable to deserialize json message message: %s", err)
}
server.ReceivedMessage <- msg
case "obs_st":
msg := new(TempestObservation)
err := json.Unmarshal(data, msg)
if err != nil {
return fmt.Errorf("Unable to deserialize json message message: %s", err)
}
server.ReceivedMessage <- msg
case "device_status":
msg := new(DeviceStatus)
err := json.Unmarshal(data, msg)
if err != nil {
return fmt.Errorf("Unable to deserialize json message message: %s", err)
}
server.ReceivedMessage <- msg
case "hub_status":
msg := new(HubStatus)
err := json.Unmarshal(data, msg)
if err != nil {
return fmt.Errorf("Unable to deserialize json message message: %s", err)
}
server.ReceivedMessage <- msg
default:
return fmt.Errorf("Unknown message received: %s", messageType)
}
return nil
}
//Stop initiates the server stop. This will wait until the server shuts down and
//stops listening for messages.
func (server *TempestServer) Stop() {
server.running = false
server.serverWaitGroup.Wait()
}
//Close closes the server. Once close is called, the server can not
//be reopened.
func (server *TempestServer) Close() error {
return server.conn.Close()
}