Initial commit
This commit is contained in:
@@ -3,6 +3,8 @@ package websocket
|
||||
import (
|
||||
"api-service/internal/config"
|
||||
ws "api-service/internal/services/websocket"
|
||||
"context"
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"time"
|
||||
@@ -88,7 +90,7 @@ func (h *WebSocketHandler) TestWebSocketConnection(c *gin.Context) {
|
||||
"user_id": "optional, for user identification",
|
||||
"room": "optional, for room-based messaging",
|
||||
},
|
||||
"example": "ws://meninjar.dev.rssa.id:8070/api/v1/ws?client_id=test_client&room=test_room",
|
||||
// "example": "ws://meninjar.dev.rssa.id:8070/api/v1/ws?client_id=test_client&room=test_room",
|
||||
"timestamp": time.Now().Unix(),
|
||||
})
|
||||
}
|
||||
@@ -134,3 +136,134 @@ func (h *WebSocketHandler) CleanupInactiveClients(c *gin.Context) {
|
||||
"timestamp": time.Now().Unix(),
|
||||
})
|
||||
}
|
||||
|
||||
func (h *WebSocketHandler) BroadcastQris(c *gin.Context) {
|
||||
var req struct {
|
||||
Data map[string]interface{} `json:"data"`
|
||||
}
|
||||
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
dbConn, err := h.hub.GetDatabaseConnection("simrs_backup")
|
||||
if err != nil || dbConn == nil {
|
||||
c.JSON(500, gin.H{"error": "Database connection failed"})
|
||||
return
|
||||
}
|
||||
|
||||
ip, _ := req.Data["ip"].(string)
|
||||
ctx, cancel := context.WithTimeout(c.Request.Context(), 5*time.Second)
|
||||
defer cancel()
|
||||
|
||||
posDevices, err := h.fetchPosDeviceByIP(ctx, dbConn, ip)
|
||||
|
||||
broadcaster := ws.NewBroadcaster(h.hub)
|
||||
|
||||
if err != nil || len(posDevices) == 0 {
|
||||
broadcaster.BroadcastQris("qris_posdevice", map[string]interface{}{
|
||||
"data": req.Data,
|
||||
"posdevice": nil,
|
||||
"message": "No posdevice found for this IP",
|
||||
"timestamp": time.Now().Unix(),
|
||||
})
|
||||
|
||||
c.JSON(404, gin.H{
|
||||
"error": "No posdevice found for this IP",
|
||||
"data": req.Data,
|
||||
"timestamp": time.Now().Unix(),
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
broadcaster.BroadcastQris("qris_posdevice", map[string]interface{}{
|
||||
"data": req.Data,
|
||||
"posdevice": posDevices,
|
||||
"timestamp": time.Now().Unix(),
|
||||
})
|
||||
|
||||
c.JSON(200, gin.H{
|
||||
"status": "broadcast sent",
|
||||
"data": req.Data,
|
||||
"posdevice": posDevices,
|
||||
"timestamp": time.Now().Unix(),
|
||||
})
|
||||
}
|
||||
|
||||
func (h *WebSocketHandler) BroadcastCheck(c *gin.Context) {
|
||||
var req struct {
|
||||
Data map[string]interface{} `json:"data"`
|
||||
}
|
||||
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
dbConn, err := h.hub.GetDatabaseConnection("simrs_backup")
|
||||
if err != nil || dbConn == nil {
|
||||
c.JSON(500, gin.H{"error": "Database connection failed"})
|
||||
return
|
||||
}
|
||||
|
||||
ip, _ := req.Data["ip"].(string)
|
||||
ctx, cancel := context.WithTimeout(c.Request.Context(), 5*time.Second)
|
||||
defer cancel()
|
||||
|
||||
posDevices, err := h.fetchPosDeviceByIP(ctx, dbConn, ip)
|
||||
|
||||
broadcaster := ws.NewBroadcaster(h.hub)
|
||||
|
||||
if err != nil || len(posDevices) == 0 {
|
||||
broadcaster.BroadcastCheck("qris_check", map[string]interface{}{
|
||||
"data": req.Data,
|
||||
"posdevice": nil,
|
||||
"message": "No posdevice found for this IP",
|
||||
"timestamp": time.Now().Unix(),
|
||||
})
|
||||
|
||||
c.JSON(404, gin.H{
|
||||
"error": "No posdevice found for this IP",
|
||||
"data": req.Data,
|
||||
"timestamp": time.Now().Unix(),
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
broadcaster.BroadcastCheck("qris_check", map[string]interface{}{
|
||||
"data": req.Data,
|
||||
"posdevice": posDevices,
|
||||
"timestamp": time.Now().Unix(),
|
||||
})
|
||||
|
||||
c.JSON(200, gin.H{
|
||||
"status": "broadcast sent",
|
||||
"data": req.Data,
|
||||
"posdevice": posDevices,
|
||||
"timestamp": time.Now().Unix(),
|
||||
})
|
||||
}
|
||||
|
||||
type Qris struct {
|
||||
PosDevice string `json:"posdevice"`
|
||||
}
|
||||
|
||||
func (h *WebSocketHandler) fetchPosDeviceByIP(ctx context.Context, db *sql.DB, ip string) ([]string, error) {
|
||||
query := `SELECT posdevice FROM m_deviceqris WHERE ip = $1 AND status = '1'`
|
||||
rows, err := db.QueryContext(ctx, query, ip)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
var posDevices []string
|
||||
for rows.Next() {
|
||||
var posDevice string
|
||||
if err := rows.Scan(&posDevice); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
posDevices = append(posDevices, posDevice)
|
||||
}
|
||||
return posDevices, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user