Commit 4d1c8095 by 이병복

테스트완료

parent 7647216c
...@@ -72,10 +72,20 @@ func loadConfig() error { ...@@ -72,10 +72,20 @@ func loadConfig() error {
return err return err
} }
return yaml.Unmarshal(data, &config) err = yaml.Unmarshal(data, &config)
if err != nil {
return err
}
return nil
} }
func main() { func main() {
fmt.Println("--------------------------------------")
fmt.Println(" LTE Alarm System : Version 1.0.2")
fmt.Println("--------------------------------------")
if err := loadConfig(); err != nil { if err := loadConfig(); err != nil {
fmt.Printf("Error loading config: %v\n", err) fmt.Printf("Error loading config: %v\n", err)
os.Exit(1) os.Exit(1)
...@@ -83,6 +93,15 @@ func main() { ...@@ -83,6 +93,15 @@ func main() {
fmt.Println(config) fmt.Println(config)
// HTTP 서버 설정
http.HandleFunc("/update-phone", updatePhoneNumberHandler)
go func() {
fmt.Println("Starting HTTP server on :5119")
if err := http.ListenAndServe(":5119", nil); err != nil {
fmt.Printf("HTTP server error: %v\n", err)
}
}()
// 장치 정보 가져오기 및 네트워크 설정 // 장치 정보 가져오기 및 네트워크 설정
maxRetries := 5 maxRetries := 5
for i := 0; i < maxRetries; i++ { for i := 0; i < maxRetries; i++ {
...@@ -223,8 +242,6 @@ func runClient() error { ...@@ -223,8 +242,6 @@ func runClient() error {
fmt.Println("Connected to server") fmt.Println("Connected to server")
setReciverConnectGPIO()
for { for {
if err := handleConnection(conn); err != nil { if err := handleConnection(conn); err != nil {
resetReciverConnectGPIO() resetReciverConnectGPIO()
...@@ -234,6 +251,9 @@ func runClient() error { ...@@ -234,6 +251,9 @@ func runClient() error {
} }
func handleConnection(conn net.Conn) error { func handleConnection(conn net.Conn) error {
setReciverConnectGPIO()
for { for {
conn.SetReadDeadline(time.Now().Add(time.Duration(config.ReadTimeout) * time.Second)) conn.SetReadDeadline(time.Now().Add(time.Duration(config.ReadTimeout) * time.Second))
...@@ -270,8 +290,6 @@ func handleConnection(conn net.Conn) error { ...@@ -270,8 +290,6 @@ func handleConnection(conn net.Conn) error {
return fmt.Errorf("JSON conversion error: %v", err) return fmt.Errorf("JSON conversion error: %v", err)
} }
fmt.Printf("formData data: %s\n", formData)
if err := sendToRESTAPIForm(formData); err != nil { if err := sendToRESTAPIForm(formData); err != nil {
return fmt.Errorf("REST API error: %v", err) return fmt.Errorf("REST API error: %v", err)
} }
...@@ -294,19 +312,6 @@ func Deserialize(data []byte) (*LogEntry, error) { ...@@ -294,19 +312,6 @@ func Deserialize(data []byte) (*LogEntry, error) {
copy(le.Area[:], data[16:40]) copy(le.Area[:], data[16:40])
copy(le.Device[:], data[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)
// deviceStr, _ := decodeEUCKR(bytes.TrimRight(le.Device[:], "\x00"))
// fmt.Printf(" Log_Device: %s\n", deviceStr)
return le, nil return le, nil
} }
...@@ -318,7 +323,9 @@ func decodeEUCKR(b []byte) (string, error) { ...@@ -318,7 +323,9 @@ func decodeEUCKR(b []byte) (string, error) {
} }
return string(result), nil return string(result), nil
} }
func convertToFormData(le *LogEntry) (url.Values, error) { func convertToFormData(le *LogEntry) (url.Values, error) {
area, err := decodeEUCKR(bytes.TrimRight(le.Area[:], "\x00")) area, err := decodeEUCKR(bytes.TrimRight(le.Area[:], "\x00"))
if err != nil { if err != nil {
return nil, err return nil, err
...@@ -334,43 +341,58 @@ func convertToFormData(le *LogEntry) (url.Values, error) { ...@@ -334,43 +341,58 @@ func convertToFormData(le *LogEntry) (url.Values, error) {
switch le.Type { switch le.Type {
case 1: case 1:
le.Type = 6 // 화재 le.Type = 6 // 화재
status = "동작"
case 2: case 2:
le.Type = 2 // 고장 le.Type = 2 // 고장
status = "해제"
case 3: case 3:
le.Type = 3 // 설비 le.Type = 3 // 설비
status = "해제"
case 4: case 4:
le.Type = 4 // 출력 le.Type = 4 // 출력
}
status = "해제" status = "해제"
if le.On == 1 {
status = "동작"
} }
formData := url.Values{} data := url.Values{}
formData.Set("callNo", config.CallNo) data.Set("callNo", config.CallNo)
formData.Set("type", fmt.Sprintf("%d", le.Type)) data.Set("type", fmt.Sprintf("%d", le.Type))
formData.Set("dateTime", le.DateTime) data.Set("dateTime", le.DateTime)
formData.Set("status", status) data.Set("status", status)
formData.Set("addr", fmt.Sprintf("%d%d-%d-%d%d%d-%d", data.Set("addr", fmt.Sprintf("%d%d-%d-%d%d%d-%d",
le.Address[0], le.Address[1], le.Address[2], le.Address[0], le.Address[1], le.Address[2],
le.Address[3], le.Address[4], le.Address[5], le.Address[6])) le.Address[3], le.Address[4], le.Address[5], le.Address[6]))
formData.Set("zoneNm", area) data.Set("zoneNm", area)
formData.Set("deviceNm", device) data.Set("deviceNm", device)
return formData, nil return data, nil
} }
func sendToRESTAPIForm(data url.Values) error { func sendToRESTAPIForm(data url.Values) error {
fmt.Printf("REST API Endpoint: %s\n", config.RestAPIEndpoint)
fmt.Printf("Sending data to REST API: %v\n", data)
resp, err := http.PostForm(config.RestAPIEndpoint, data) resp, err := http.PostForm(config.RestAPIEndpoint, data)
if err != nil { if err != nil {
return err return err
} }
defer resp.Body.Close() defer resp.Body.Close()
if resp.StatusCode != http.StatusOK { body, err := ioutil.ReadAll(resp.Body)
return fmt.Errorf("unexpected status code: %d", resp.StatusCode) if err != nil {
fmt.Printf("Error reading body: %v\n", err)
return err
} }
// 결과 출력
fmt.Printf("Response Headers: %v\n", resp.Header)
fmt.Printf("Response Status: %s\n", resp.Status)
fmt.Printf("Response Body: %s\n", string(body))
return nil return nil
} }
...@@ -392,7 +414,7 @@ func resetServerGPIO() { ...@@ -392,7 +414,7 @@ func resetServerGPIO() {
} }
func setReciverConnectGPIO() error { func setReciverConnectGPIO() error {
cmd := exec.Command("sh", "-c", "echo 1 > /sys/class/gpio/gpio269/value") cmd := exec.Command("sh", "-c", "echo 1 > /sys/class/gpio/gpio270/value")
err := cmd.Run() err := cmd.Run()
if err != nil { if err != nil {
fmt.Printf("GPIO 266 설정 실패: %v", err) fmt.Printf("GPIO 266 설정 실패: %v", err)
...@@ -401,9 +423,53 @@ func setReciverConnectGPIO() error { ...@@ -401,9 +423,53 @@ func setReciverConnectGPIO() error {
} }
func resetReciverConnectGPIO() { func resetReciverConnectGPIO() {
cmd := exec.Command("sh", "-c", "echo 0 > /sys/class/gpio/gpio269/value") cmd := exec.Command("sh", "-c", "echo 0 > /sys/class/gpio/gpio270/value")
err := cmd.Run() err := cmd.Run()
if err != nil { if err != nil {
fmt.Printf("GPIO 266 리셋 실패: %v", err) fmt.Printf("GPIO 266 리셋 실패: %v", err)
} }
} }
func updatePhoneNumberHandler(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
http.Error(w, "Only POST method is allowed", http.StatusMethodNotAllowed)
return
}
newPhoneNumber := r.FormValue("phone_number")
if newPhoneNumber == "" {
http.Error(w, "Phone number is required", http.StatusBadRequest)
return
}
// 전화번호 형식 검증 (간단한 예시)
if !strings.HasPrefix(newPhoneNumber, "010-") || len(newPhoneNumber) != 13 {
http.Error(w, "Invalid phone number format", http.StatusBadRequest)
return
}
err := updateConfigFile(newPhoneNumber)
if err != nil {
http.Error(w, fmt.Sprintf("Failed to update config: %v", err), http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, "Phone number updated successfully to %s", newPhoneNumber)
}
func updateConfigFile(newPhoneNumber string) error {
config.CallNo = newPhoneNumber
data, err := yaml.Marshal(&config)
if err != nil {
return fmt.Errorf("failed to marshal config: %v", err)
}
err = ioutil.WriteFile("config.yaml", data, 0644)
if err != nil {
return fmt.Errorf("failed to write config file: %v", err)
}
return nil
}
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