-
Notifications
You must be signed in to change notification settings - Fork 1
/
navwarn.go
159 lines (137 loc) · 3.95 KB
/
navwarn.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package main
import (
"bytes"
"crypto/tls"
"crypto/x509"
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"net/url"
"os"
)
type NavWarnings struct {
BroadcastWarn []BroadcastWarn `json:"broadcast-warn"`
}
type BroadcastWarn struct {
MsgYear int `json:"msgYear"`
MsgNumber int `json:"msgNumber"`
NavArea string `json:"navArea"`
Subregion string `json:"subregion"`
Text string `json:"text"`
Status string `json:"status"`
IssueDate string `json:"issueDate"`
Authority string `json:"authority"`
CancelDate interface{} `json:"cancelDate"`
CancelNavArea interface{} `json:"cancelNavArea"`
CancelMsgYear interface{} `json:"cancelMsgYear"`
CancelMsgNumber interface{} `json:"cancelMsgNumber"`
Year int `json:"year"`
Area string `json:"area"`
Number int `json:"number"`
}
type FeatureCollection struct {
Type string `json:"type"`
Features []Features `json:"features"`
}
type Properties struct {
Navarea int `json:"Navarea"`
Number int `json:"Number"`
Status string `json:"Status"`
Authority string `json:"Authority"`
IssueDate string `json:"IssueDate"`
MsgYear int `json:"MsgYear"`
Message string `json:"Message"`
}
type Geometry struct {
Coordinates []float64 `json:"coordinates"`
Type string `json:"type"`
}
type Features struct {
Type string `json:"type"`
Properties Properties `json:"properties"`
Geometry Geometry `json:"geometry"`
ID int `json:"id"`
}
func GetNavWarnings() NavWarnings {
uri := "https://msi.om.east.paas.nga.mil/api/publications/broadcast-warn"
//msgYear := "2023"
// Set parameters
values := url.Values{}
//values.Set("msgYear", msgYear)
values.Set("output", "json")
// Load the server's certificate
cert, err := os.ReadFile("./temp/pcf-east-router.om.east.paas.nga.mil.cer")
if err != nil {
log.Fatal(err)
}
// Create a certificate pool and add the server's certificate
pool := x509.NewCertPool()
if ok := pool.AppendCertsFromPEM(cert); !ok {
log.Fatal("failed to append certificate")
}
// Create transport with the certificate pool
transport := &http.Transport{
TLSClientConfig: &tls.Config{
RootCAs: pool,
},
}
// Create http client with the transport
client := &http.Client{
Transport: transport,
}
// Request url with parameters
uri = uri + "?" + values.Encode()
// create a new HTTP request with the JSON parameters
req, err := http.NewRequest("GET", uri, nil)
if err != nil {
fmt.Println("Error creating request:", err)
return NavWarnings{}
}
// set the request header to specify the JSON content type
req.Header.Set("Content-Type", "application/json")
// send the HTTP request
//client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
fmt.Println("Error sending request:", err)
return NavWarnings{}
}
// print the response body
defer func(Body io.ReadCloser) {
err := Body.Close()
if err != nil {
fmt.Println("Error closing response body:", err)
}
}(resp.Body)
if resp.StatusCode != 200 {
// Print the response status
fmt.Println("Response status:", resp.Status)
// Print the response headers
fmt.Println("Response headers:", resp.Header)
// Read the response body
var buf bytes.Buffer
_, err = io.Copy(&buf, resp.Body)
if err != nil {
fmt.Println(err)
return NavWarnings{}
}
// Print the raw response data
fmt.Println(buf.String())
return NavWarnings{}
}
// variable to store the response body
navWarnings := NavWarnings{}
err = json.NewDecoder(resp.Body).Decode(&navWarnings)
if err != nil {
log.Panic("Error decoding response body:", err)
}
//fmt.Println(navWarnings)
return navWarnings
}
// SaveNavWarnings saves the nav warnings to a json file
func SaveNavWarnings(navWarnings NavWarnings) {
file, _ := json.MarshalIndent(navWarnings, "", " ")
_ = os.WriteFile("dataset/navwarnings.json", file, 0644)
}