-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.go
177 lines (128 loc) · 4.81 KB
/
app.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package main
import (
"html/template"
"net/http"
"os"
"path"
"log"
"encoding/json"
//"time"
"db"
//"github.com/influxdb/influxdb/client/v2"
)
func main() {
http.HandleFunc("/PutData",func(w http.ResponseWriter, r *http.Request) {
db.PutData(w,r)
})
http.HandleFunc("/getData",func(w http.ResponseWriter, r *http.Request) {
// res := db.GetData("power")
// json.NewEncoder(w).Encode(res)
query := "SELECT * FROM one_min_data WHERE fields='SMU_Alarm' AND value=1 limit 7"
res := db.GetDataWhr(query)
json.NewEncoder(w).Encode(res)
})
http.HandleFunc("/getNoComm",func(w http.ResponseWriter, r *http.Request) {
query := "SELECT * FROM one_min_data WHERE fields='COMMUNICATION_STATUS' AND value=1 limit 7"
//log.Println(query)
res := db.GetDataWhr(query)
json.NewEncoder(w).Encode(res)
})
http.HandleFunc("/todayGen",func(w http.ResponseWriter, r *http.Request) {
query := "SELECT MAX(value) as todayGen FROM one_min_data WHERE fields='PAC'"
//log.Println(query)
res := db.GetDataWhr(query)
json.NewEncoder(w).Encode(res)
})
http.HandleFunc("/pkPower",func(w http.ResponseWriter, r *http.Request) {
query := "SELECT MAX(value) as pkPwr FROM one_min_data WHERE fields='PAC'"
//log.Println(query)
res := db.GetDataWhr(query)
json.NewEncoder(w).Encode(res)
})
http.HandleFunc("/totGen",func(w http.ResponseWriter, r *http.Request) {
query := "SELECT SUM(value) as totGen FROM one_min_data WHERE fields='EAE'";
res := db.GetDataWhr(query)
json.NewEncoder(w).Encode(res)
})
http.HandleFunc("/impPwr",func(w http.ResponseWriter, r *http.Request) {
query := "SELECT SUM(value) as impPwr FROM one_min_data WHERE fields='EAI'";
res := db.GetDataWhr(query)
json.NewEncoder(w).Encode(res)
})
http.HandleFunc("/noCommDev",func(w http.ResponseWriter, r *http.Request) {
query := "SELECT COUNT(value) FROM one_min_data WHERE fields='COMMUNICATION_STATUS' AND value=1"
res := db.GetDataWhr(query)
json.NewEncoder(w).Encode(res)
})
http.HandleFunc("/alarms",func(w http.ResponseWriter, r *http.Request) {
query := "SELECT COUNT(value) FROM one_min_data WHERE fields='SMU_Alarm' AND value=1"
res := db.GetDataWhr(query)
json.NewEncoder(w).Encode(res)
})
http.HandleFunc("/actvPwr",func(w http.ResponseWriter, r *http.Request) {
query := "SELECT value as actvPwr FROM one_min_data WHERE fields='PAC' AND time=MAX(time)";
res := db.GetDataWhr(query)
json.NewEncoder(w).Encode(res)
})
http.HandleFunc("/slrRdtn",func(w http.ResponseWriter, r *http.Request) {
query := "SELECT value as slrRdtn FROM one_min_data WHERE fields='SOLAR_RADIATION' AND time=MAX(time)";
res := db.GetDataWhr(query)
json.NewEncoder(w).Encode(res)
})
http.HandleFunc("/windSpd",func(w http.ResponseWriter, r *http.Request) {
query := "SELECT value as windSpd FROM one_min_data WHERE fields='WIND_SPEED' AND time=MAX(time)";
res := db.GetDataWhr(query)
json.NewEncoder(w).Encode(res)
})
fs := http.FileServer(http.Dir("static"))
http.Handle("/static/", http.StripPrefix("/static/", fs))
fonts := http.FileServer(http.Dir("fonts"))
http.Handle("/fonts/", http.StripPrefix("/fonts/", fonts))
images := http.FileServer(http.Dir("images"))
http.Handle("/images/", http.StripPrefix("/images/", images))
scripts := http.FileServer(http.Dir("scripts"))
http.Handle("/scripts/", http.StripPrefix("/scripts/", scripts))
styles := http.FileServer(http.Dir("styles"))
http.Handle("/styles/", http.StripPrefix("/styles/", styles))
templates := http.FileServer(http.Dir("templates"))
http.Handle("/templates/", http.StripPrefix("/templates/", templates))
//log.Println(fs)
http.HandleFunc("/", serveTemplate)
log.Println("Listening...")
http.ListenAndServe(":3010", nil)
}
func serveTemplate(w http.ResponseWriter, r *http.Request) {
lp := path.Join("templates", "layout.html")
// fp := path.Join("templates", r.URL.Path)
// Return a 404 if the template doesn't exist
info, err := os.Stat(lp)
if err != nil {
if os.IsNotExist(err) {
http.NotFound(w, r)
return
}
}
// 404 error for the requests that asks for directory
if info.IsDir() {
http.NotFound(w, r)
return
}
//tmpl, err := template.ParseFiles(lp, fp)
tmpl, err := template.ParseFiles(lp)
if err != nil {
// Log the detailed error
log.Println(err.Error())
// Return a generic "Internal Server Error" message
http.Error(w, http.StatusText(500), 500)
return
}
type Person struct {
UserName string
// Age int
}
p := Person{UserName: "{{themeActive}}"}
if err := tmpl.ExecuteTemplate(w, "layout", p); err != nil {
log.Println(err.Error())
http.Error(w, http.StatusText(500), 500)
}
}