Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
2
2401-0022-lte-fire-alarm
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
이병복
2401-0022-lte-fire-alarm
Commits
4d1c8095
Commit
4d1c8095
authored
Jul 20, 2024
by
이병복
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
테스트완료
parent
7647216c
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
101 additions
and
35 deletions
+101
-35
main.go
cmd/app-lte-alarm/main.go
+101
-35
No files found.
cmd/app-lte-alarm/main.go
View file @
4d1c8095
...
@@ -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
=
"해제"
}
}
formData
:=
url
.
Values
{}
status
=
"해제"
formData
.
Set
(
"callNo"
,
config
.
CallNo
)
formData
.
Set
(
"type"
,
fmt
.
Sprintf
(
"%d"
,
le
.
Type
))
if
le
.
On
==
1
{
formData
.
Set
(
"dateTime"
,
le
.
DateTime
)
status
=
"동작"
formData
.
Set
(
"status"
,
status
)
}
formData
.
Set
(
"addr"
,
fmt
.
Sprintf
(
"%d%d-%d-%d%d%d-%d"
,
data
:=
url
.
Values
{}
data
.
Set
(
"callNo"
,
config
.
CallNo
)
data
.
Set
(
"type"
,
fmt
.
Sprintf
(
"%d"
,
le
.
Type
))
data
.
Set
(
"dateTime"
,
le
.
DateTime
)
data
.
Set
(
"status"
,
status
)
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
]))
formD
ata
.
Set
(
"zoneNm"
,
area
)
d
ata
.
Set
(
"zoneNm"
,
area
)
formD
ata
.
Set
(
"deviceNm"
,
device
)
d
ata
.
Set
(
"deviceNm"
,
device
)
return
formD
ata
,
nil
return
d
ata
,
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/gpio2
69
/value"
)
cmd
:=
exec
.
Command
(
"sh"
,
"-c"
,
"echo 1 > /sys/class/gpio/gpio2
70
/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/gpio2
69
/value"
)
cmd
:=
exec
.
Command
(
"sh"
,
"-c"
,
"echo 0 > /sys/class/gpio/gpio2
70
/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
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment