feat: implement real-time Pub/Sub alert broadcast system and update docs/GRPC.md
This commit is contained in:
@@ -91,6 +91,31 @@ func AskDownloadFile(ctx context.Context, m protoapi.IoTServiceClient, fileName
|
||||
return buffer, nil
|
||||
}
|
||||
|
||||
func AskSubscribeAlerts(ctx context.Context, m protoapi.IoTServiceClient, clientId string, topic string) {
|
||||
stream, err := m.SubscribeAlerts(ctx, &protoapi.AlertSubscription{
|
||||
ClientId: clientId,
|
||||
Topic: topic,
|
||||
})
|
||||
if err != nil {
|
||||
fmt.Println("Failed to subscribe alerts:", err)
|
||||
return
|
||||
}
|
||||
|
||||
for {
|
||||
alert, err := stream.Recv()
|
||||
if err == io.EOF {
|
||||
fmt.Println("Alert subscription stream closed by server.")
|
||||
break
|
||||
}
|
||||
if err != nil {
|
||||
break
|
||||
}
|
||||
fmt.Printf("\n🔔 [ALERT RECEIVED] ID: %s | Device: %s | Msg: %s | Time: %s\n\n",
|
||||
alert.GetAlertId(), alert.GetDeviceId(), alert.GetMessage(),
|
||||
time.Unix(alert.GetTimestamp(), 0).Format("15:04:05"))
|
||||
}
|
||||
}
|
||||
|
||||
func ClientRun(addr string) {
|
||||
conn, err := grpc.NewClient(addr, grpc.WithTransportCredentials(insecure.NewCredentials()))
|
||||
if err != nil {
|
||||
@@ -99,6 +124,14 @@ func ClientRun(addr string) {
|
||||
}
|
||||
|
||||
client := protoapi.NewIoTServiceClient(conn)
|
||||
|
||||
// 백그라운드에서 실시간 경보 구독 기동
|
||||
alertCtx, alertCancel := context.WithCancel(context.Background())
|
||||
defer alertCancel()
|
||||
go AskSubscribeAlerts(alertCtx, client, "client-app-01", "temperature_warnings")
|
||||
// 구독 리시버 채널 등록을 위해 50ms 슬립
|
||||
time.Sleep(50 * time.Millisecond)
|
||||
|
||||
r, err := AskingDateTime(context.Background(), client)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
@@ -171,4 +204,15 @@ func ClientRun(addr string) {
|
||||
}
|
||||
}
|
||||
fmt.Printf("Data Integrity Checked (Upload vs Download matches?): %t\n", isMatch)
|
||||
|
||||
// 7단계: 임계값 초과 온습도 전송을 통한 Pub/Sub 실시간 알림 유발 시뮬레이션
|
||||
fmt.Println("Sending abnormal high-temperature sensing data (45.8°C)...")
|
||||
alertRes, err := AskUpdateSensingData(context.Background(), client, "sensor-room-01", 45.8, 60.1)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
fmt.Println("Abnormal Sensing Update Success:", alertRes.Success)
|
||||
// 알림 이벤트가 비동기로 화면에 출력될 시간을 확보하기 위해 100ms 대기
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user