-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
81 lines (68 loc) · 1.86 KB
/
main.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
package main
import (
"encoding/json"
"fmt"
"log"
"math/rand"
"os"
"os/signal"
"syscall"
"time"
mqtt "github.com/eclipse/paho.mqtt.golang"
"nzen-iot-client-test/common"
)
var MQTTBroker string
var MQTTClientID string
var MQTTTopic string
func init() {
MQTTBroker = common.ConfInfo["mqtt.broker.url"]
MQTTClientID = common.ConfInfo["mqtt.producer.client.id"]
MQTTTopic = common.ConfInfo["mqtt.topic"]
}
// AccelerometerData 구조체 정의
type AccelerometerData struct {
X float64 `json:"x"`
Y float64 `json:"y"`
Z float64 `json:"z"`
}
func main() {
// MQTT 클라이언트 옵션 설정
opts := mqtt.NewClientOptions().AddBroker(MQTTBroker).SetClientID(MQTTClientID)
// MQTT 클라이언트 생성 및 연결
client := mqtt.NewClient(opts)
if token := client.Connect(); token.Wait() && token.Error() != nil {
log.Fatalf("Error connecting to MQTT broker: %v", token.Error())
}
// 주기적으로 가속도 데이터를 생성하여 퍼블리시
ticker := time.NewTicker(1 * time.Second)
defer ticker.Stop()
for range ticker.C {
// 가속도 데이터 생성
data := AccelerometerData{
X: rand.Float64() * 10,
Y: rand.Float64() * 10,
Z: rand.Float64() * 10,
}
// JSON 직렬화
payload, err := json.Marshal(data)
if err != nil {
log.Printf("Error marshalling JSON: %v", err)
continue
}
// MQTT 토픽에 퍼블리시
token := client.Publish(MQTTTopic, 0, false, payload)
token.Wait()
if token.Error() != nil {
log.Printf("Error publishing message: %v", token.Error())
} else {
fmt.Printf("Published accelerometer data: X=%.2f, Y=%.2f, Z=%.2f\n", data.X, data.Y, data.Z)
}
}
// 프로그램 종료를 위한 신호 처리 설정
sigc := make(chan os.Signal, 1)
signal.Notify(sigc, syscall.SIGINT, syscall.SIGTERM)
<-sigc
// MQTT 클라이언트 종료
client.Disconnect(250)
log.Println("Program terminated")
}