Commit 49b573dd by 이병복

수신기 통신 완료

parent bbb2f516
server_address: 127.0.0.1:12345 server_address: 118.46.137.10:20000
max_retries: 3 max_retries: 3
reconnect_interval: 5 reconnect_interval: 5
read_timeout: 60 read_timeout: 60
......
...@@ -2,9 +2,9 @@ package main ...@@ -2,9 +2,9 @@ package main
import ( import (
"bytes" "bytes"
"encoding/binary"
"encoding/json" "encoding/json"
"fmt" "fmt"
"io"
"io/ioutil" "io/ioutil"
"net" "net"
"net/http" "net/http"
...@@ -29,7 +29,7 @@ type Config struct { ...@@ -29,7 +29,7 @@ type Config struct {
type LogEntry struct { type LogEntry struct {
Size byte Size byte
Type uint8 Type uint8
DateTime time.Time DateTime string
On uint8 On uint8
Address [7]byte Address [7]byte
Area [24]byte Area [24]byte
...@@ -38,12 +38,12 @@ type LogEntry struct { ...@@ -38,12 +38,12 @@ type LogEntry struct {
// JSONLogEntry 구조체 정의 (이전과 동일) // JSONLogEntry 구조체 정의 (이전과 동일)
type JSONLogEntry struct { type JSONLogEntry struct {
Type uint8 `json:"type"` Type uint8 `json:"type"`
DateTime time.Time `json:"dateTime"` DateTime string `json:"dateTime"`
On uint8 `json:"on"` On uint8 `json:"on"`
Address string `json:"address"` Address string `json:"address"`
Area string `json:"area"` Area string `json:"area"`
Device string `json:"device"` Device string `json:"device"`
} }
var config Config var config Config
...@@ -63,6 +63,8 @@ func main() { ...@@ -63,6 +63,8 @@ func main() {
os.Exit(1) os.Exit(1)
} }
fmt.Print(config)
for { for {
if err := runClient(); err != nil { if err := runClient(); err != nil {
fmt.Printf("Error: %v\n", err) fmt.Printf("Error: %v\n", err)
...@@ -99,46 +101,92 @@ func runClient() error { ...@@ -99,46 +101,92 @@ func runClient() error {
} }
func handleConnection(conn net.Conn) error { func handleConnection(conn net.Conn) error {
conn.SetReadDeadline(time.Now().Add(time.Duration(config.ReadTimeout) * time.Second)) for {
// 읽기 타임아웃 설정
conn.SetReadDeadline(time.Now().Add(time.Duration(config.ReadTimeout) * time.Second))
// 첫 번째 바이트 (데이터 길이) 읽기
sizeBuf := make([]byte, 1)
_, err := conn.Read(sizeBuf)
if err != nil {
return fmt.Errorf("error reading size: %v", err)
}
buf := make([]byte, 64) size := int(sizeBuf[0])
_, err := conn.Read(buf)
if err != nil {
return fmt.Errorf("read error: %v", err)
}
logEntry, err := Deserialize(buf) // size가 0인 경우 처리
if err != nil { if size == 0 {
return fmt.Errorf("deserialization error: %v", err) // fmt.Println("Received packet with size 0, skipping...")
} continue
}
jsonData, err := convertToJSON(logEntry) fmt.Printf("Received size: %d\n", size)
if err != nil { // 나머지 데이터 읽기
return fmt.Errorf("JSON conversion error: %v", err) buf := make([]byte, size-1)
} _, err = io.ReadFull(conn, buf)
if err != nil {
return fmt.Errorf("error reading data: %v", err)
}
if err := sendToRESTAPI(jsonData); err != nil { fmt.Printf("Received data: %v\n", buf)
return fmt.Errorf("REST API error: %v", err) fmt.Printf("Received size: %v\n", len(buf))
} // 전체 데이터 (크기 바이트 포함)
fullData := append(sizeBuf, buf...)
return nil logEntry, err := Deserialize(fullData)
if err != nil {
return fmt.Errorf("deserialization error: %v", err)
}
jsonData, err := convertToJSON(logEntry)
if err != nil {
return fmt.Errorf("JSON conversion error: %v", err)
}
fmt.Printf("JSON data: %s\n", jsonData)
if err := sendToRESTAPI(jsonData); err != nil {
return fmt.Errorf("REST API error: %v", err)
}
}
} }
func Deserialize(data []byte) (*LogEntry, error) { func Deserialize(data []byte) (*LogEntry, error) {
if len(data) != 64 { // if len(data) != 64 {
return nil, fmt.Errorf("invalid data length: expected 64, got %d", len(data)) // return nil, fmt.Errorf("invalid data length: expected 64, got %d", len(data))
} // }
fmt.Printf("Data: %v\n", data)
le := &LogEntry{} le := &LogEntry{}
le.Size = data[0] le.Size = data[0]
le.Type = data[1] if le.Size == 0 {
year := binary.LittleEndian.Uint16(data[2:4]) return nil, fmt.Errorf("invalid data length: expected , got %d", le.Size)
le.DateTime = time.Date(int(year), time.Month(data[4]), int(data[5]), int(data[6]), int(data[7]), int(data[8]), 0, time.UTC) }
le.On = data[9]
copy(le.Address[:], data[10:17])
copy(le.Area[:], data[17:41])
copy(le.Device[:], data[41:])
le.Type = data[1]
le.DateTime = fmt.Sprintf("%02d-%02d-%02d %02d:%02d:%02d", (int(data[2]) + 2000), data[3], data[4], data[5], data[6], data[7])
le.On = data[8]
copy(le.Address[:], data[9:16])
copy(le.Area[:], data[16:40])
copy(le.Device[:], data[40:])
// 로그 출력
fmt.Printf(" Deserialized Packet:\n")
fmt.Printf(" Log_size: %d\n", le.Size)
fmt.Printf(" Log_type: %d\n", le.Type)
fmt.Printf(" Log_datetime: %s\n", le.DateTime)
fmt.Printf(" Log_On: %d\n", le.On)
fmt.Printf(" Log_Address: %d%d-%d-%d%d%d-%d\n", le.Address[0], le.Address[1], le.Address[2], le.Address[3], le.Address[4], le.Address[5], le.Address[6])
areaStr, _ := decodeEUCKR(bytes.TrimRight(le.Area[:], "\x00"))
fmt.Printf(" Log_Area: %s\n", areaStr)
//50 195 254 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
deviceStr, _ := decodeEUCKR(bytes.TrimRight(le.Device[:], "\x00"))
fmt.Printf(" Log_Device: %s\n", deviceStr)
//191 161 186 241 0 132 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
return le, nil return le, nil
} }
...@@ -148,6 +196,7 @@ func convertToJSON(le *LogEntry) ([]byte, error) { ...@@ -148,6 +196,7 @@ func convertToJSON(le *LogEntry) ([]byte, error) {
return nil, err return nil, err
} }
fmt.Printf("%d\n", len(le.Device[:]))
device, err := decodeEUCKR(bytes.TrimRight(le.Device[:], "\x00")) device, err := decodeEUCKR(bytes.TrimRight(le.Device[:], "\x00"))
if err != nil { if err != nil {
return nil, err return nil, err
...@@ -157,7 +206,7 @@ func convertToJSON(le *LogEntry) ([]byte, error) { ...@@ -157,7 +206,7 @@ func convertToJSON(le *LogEntry) ([]byte, error) {
Type: le.Type, Type: le.Type,
DateTime: le.DateTime, DateTime: le.DateTime,
On: le.On, On: le.On,
Address: string(bytes.TrimRight(le.Address[:], "\x00")), Address: fmt.Sprintf("%d%d-%d-%d%d%d-%d", le.Address[0], le.Address[1], le.Address[2], le.Address[3], le.Address[4], le.Address[5], le.Address[6]),
Area: area, Area: area,
Device: device, Device: device,
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment