-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsensor_client.go
135 lines (111 loc) · 2.67 KB
/
sensor_client.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
package main
import (
"bytes"
"encoding/json"
"fmt"
"log"
"net/http"
"runtime"
"strconv"
"strings"
"sync"
"time"
"github.com/mingrammer/go-codelab/models"
)
// worker struct has necessary information to run sensor data producing job
type worker struct {
ticker *time.Ticker
sensor models.Sensor
sensorError float64
serverPort int
}
// counter is for counting the number of sending request
type counter struct {
mutex sync.Mutex
n int
}
func (c *counter) count() {
c.mutex.Lock()
c.n++
c.mutex.Unlock()
}
func (c *counter) value() int {
return c.n
}
// sensorWorker will be ran in goroutine to produce (random) sensor data and sends it to server
func sensorWorker(done <-chan struct{}, w worker, c *counter) {
for {
select {
case <-done:
return
case <-w.ticker.C:
sensorData := w.sensor.GenerateSensorData(w.sensorError)
url := getRequestServerURL(w.serverPort)
fmt.Println(sensorData.SendingOutputString())
sendJSONSensorData(url, sensorData)
c.count()
}
}
}
func main() {
numCPUs := runtime.NumCPU()
runtime.GOMAXPROCS(numCPUs)
var sendCounter counter
var wg sync.WaitGroup
const numWorkers = 3
done := make(chan struct{})
wg.Add(numWorkers)
workerList := [numWorkers]worker{
worker{
ticker: time.NewTicker(500 * time.Millisecond),
sensor: models.GyroSensor{},
sensorError: 4.0,
serverPort: 8001,
},
worker{
ticker: time.NewTicker(500 * time.Millisecond),
sensor: models.AccelSensor{},
sensorError: 12.0,
serverPort: 8002,
},
worker{
ticker: time.NewTicker(2 * time.Second),
sensor: models.TempSensor{},
sensorError: 1.5,
serverPort: 8003,
},
}
for _, w := range workerList {
go func(w worker) {
sensorWorker(done, w, &sendCounter)
wg.Done()
}(w)
}
go func() {
for {
if sendCounter.value() > 100 {
close(done)
return
}
}
}()
wg.Wait()
fmt.Printf("\n[Count : %d] Sending is stopped.\n", sendCounter.value())
}
// getRequestServerURL just concatenate the server url and port number to get complete server url
func getRequestServerURL(port int) string {
urlComponents := []string{"http://127.0.0.1", strconv.Itoa(port)}
return strings.Join(urlComponents, ":")
}
// sendJSONSensorData sends the sensor data as JSON type to server
func sendJSONSensorData(url string, sensorValues models.Sensor) {
jsonBytes, err := json.Marshal(sensorValues)
if err != nil {
log.Fatal("Error occurs when mashaling the gyro sensor values")
}
buff := bytes.NewBuffer(jsonBytes)
resp, err := http.Post(url, "application/json", buff)
if err != nil || resp.StatusCode != 200 {
log.Fatal("Error occurs when request the post data")
}
}