Renamed pingvin variable -> device

This commit is contained in:
Jarno Rankinen 2023-03-15 21:35:23 +02:00
parent 140e68aa13
commit cb1b818b65
1 changed files with 25 additions and 25 deletions

50
main.go
View File

@ -31,7 +31,7 @@ var static embed.FS
var ( var (
version = "0.0.23" version = "0.0.23"
pingvin pingvinKL.PingvinKL device pingvin.PingvinKL
config Conf config Conf
usernamehash [32]byte usernamehash [32]byte
passwordhash [32]byte passwordhash [32]byte
@ -102,7 +102,7 @@ func coils(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json") w.Header().Set("Content-Type", "application/json")
pathparams := strings.Split(strings.TrimPrefix(r.URL.Path, "/api/v1/coils/"), "/") pathparams := strings.Split(strings.TrimPrefix(r.URL.Path, "/api/v1/coils/"), "/")
if len(pathparams[0]) == 0 { if len(pathparams[0]) == 0 {
json.NewEncoder(w).Encode(pingvin.Coils) json.NewEncoder(w).Encode(device.Coils)
} else if len(pathparams[0]) > 0 && r.Method == "GET" && len(pathparams) < 2 { // && r.Method == "POST" } else if len(pathparams[0]) > 0 && r.Method == "GET" && len(pathparams) < 2 { // && r.Method == "POST"
intaddr, err := strconv.Atoi(pathparams[0]) intaddr, err := strconv.Atoi(pathparams[0])
if err != nil { if err != nil {
@ -110,8 +110,8 @@ func coils(w http.ResponseWriter, r *http.Request) {
log.Println(err) log.Println(err)
return return
} }
pingvin.ReadCoil(uint16(intaddr)) device.ReadCoil(uint16(intaddr))
json.NewEncoder(w).Encode(pingvin.Coils[intaddr]) json.NewEncoder(w).Encode(device.Coils[intaddr])
} else if len(pathparams[0]) > 0 && r.Method == "POST" && len(pathparams) == 2 { } else if len(pathparams[0]) > 0 && r.Method == "POST" && len(pathparams) == 2 {
intaddr, err := strconv.Atoi(pathparams[0]) intaddr, err := strconv.Atoi(pathparams[0])
if err != nil { if err != nil {
@ -125,8 +125,8 @@ func coils(w http.ResponseWriter, r *http.Request) {
log.Println(err) log.Println(err)
return return
} }
pingvin.WriteCoil(uint16(intaddr), boolval) device.WriteCoil(uint16(intaddr), boolval)
json.NewEncoder(w).Encode(pingvin.Coils[intaddr]) json.NewEncoder(w).Encode(device.Coils[intaddr])
} else if len(pathparams[0]) > 0 && r.Method == "POST" && len(pathparams) == 1 { } else if len(pathparams[0]) > 0 && r.Method == "POST" && len(pathparams) == 1 {
intaddr, err := strconv.Atoi(pathparams[0]) intaddr, err := strconv.Atoi(pathparams[0])
if err != nil { if err != nil {
@ -134,8 +134,8 @@ func coils(w http.ResponseWriter, r *http.Request) {
log.Println(err) log.Println(err)
return return
} }
pingvin.WriteCoil(uint16(intaddr), !pingvin.Coils[intaddr].Value) device.WriteCoil(uint16(intaddr), !device.Coils[intaddr].Value)
json.NewEncoder(w).Encode(pingvin.Coils[intaddr]) json.NewEncoder(w).Encode(device.Coils[intaddr])
} }
} }
@ -144,7 +144,7 @@ func registers(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json") w.Header().Set("Content-Type", "application/json")
pathparams := strings.Split(strings.TrimPrefix(r.URL.Path, "/api/v1/registers/"), "/") pathparams := strings.Split(strings.TrimPrefix(r.URL.Path, "/api/v1/registers/"), "/")
if len(pathparams[0]) == 0 { if len(pathparams[0]) == 0 {
json.NewEncoder(w).Encode(pingvin.Registers) json.NewEncoder(w).Encode(device.Registers)
} else if len(pathparams[0]) > 0 && r.Method == "GET" && len(pathparams) < 2 { // && r.Method == "POST" } else if len(pathparams[0]) > 0 && r.Method == "GET" && len(pathparams) < 2 { // && r.Method == "POST"
intaddr, err := strconv.Atoi(pathparams[0]) intaddr, err := strconv.Atoi(pathparams[0])
if err != nil { if err != nil {
@ -152,8 +152,8 @@ func registers(w http.ResponseWriter, r *http.Request) {
log.Println(err) log.Println(err)
return return
} }
pingvin.ReadRegister(uint16(intaddr)) device.ReadRegister(uint16(intaddr))
json.NewEncoder(w).Encode(pingvin.Registers[intaddr]) json.NewEncoder(w).Encode(device.Registers[intaddr])
} else if len(pathparams[0]) > 0 && r.Method == "POST" && len(pathparams) == 2 { } else if len(pathparams[0]) > 0 && r.Method == "POST" && len(pathparams) == 2 {
intaddr, err := strconv.Atoi(pathparams[0]) intaddr, err := strconv.Atoi(pathparams[0])
if err != nil { if err != nil {
@ -167,18 +167,18 @@ func registers(w http.ResponseWriter, r *http.Request) {
log.Println(err) log.Println(err)
return return
} }
_, err = pingvin.WriteRegister(uint16(intaddr), uint16(intval)) _, err = device.WriteRegister(uint16(intaddr), uint16(intval))
if err != nil { if err != nil {
log.Println(err) log.Println(err)
} }
json.NewEncoder(w).Encode(pingvin.Registers[intaddr]) json.NewEncoder(w).Encode(device.Registers[intaddr])
} }
} }
// \/status endpoint // \/status endpoint
func status(w http.ResponseWriter, r *http.Request) { func status(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json") w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(pingvin.Status) json.NewEncoder(w).Encode(device.Status)
} }
// \/api/v1/temperature endpoint // \/api/v1/temperature endpoint
@ -186,8 +186,8 @@ func temperature(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json") w.Header().Set("Content-Type", "application/json")
pathparams := strings.Split(strings.TrimPrefix(r.URL.Path, "/api/v1/temperature/"), "/") pathparams := strings.Split(strings.TrimPrefix(r.URL.Path, "/api/v1/temperature/"), "/")
if len(pathparams[0]) > 0 && r.Method == "POST" && len(pathparams) == 1 { if len(pathparams[0]) > 0 && r.Method == "POST" && len(pathparams) == 1 {
pingvin.Temperature(pathparams[0]) device.Temperature(pathparams[0])
json.NewEncoder(w).Encode(pingvin.Registers[135]) json.NewEncoder(w).Encode(device.Registers[135])
} else { } else {
return return
} }
@ -195,7 +195,7 @@ func temperature(w http.ResponseWriter, r *http.Request) {
// Start the HTTP server // Start the HTTP server
func serve(cert, key *string) { func serve(cert, key *string) {
log.Println("Starting pingvinAPI...") log.Println("Starting deviceAPI...")
http.HandleFunc("/api/v1/coils/", authHandlerFunc(coils)) http.HandleFunc("/api/v1/coils/", authHandlerFunc(coils))
http.HandleFunc("/api/v1/status", authHandlerFunc(status)) http.HandleFunc("/api/v1/status", authHandlerFunc(status))
http.HandleFunc("/api/v1/registers/", authHandlerFunc(registers)) http.HandleFunc("/api/v1/registers/", authHandlerFunc(registers))
@ -232,8 +232,8 @@ func generateCertificate(cert, key string) {
if err != nil { if err != nil {
log.Fatal("Error generating SSL certificate: ", err) log.Fatal("Error generating SSL certificate: ", err)
} }
pingvin.Debug.Println("Certificate:\n", string(pub)) device.Debug.Println("Certificate:\n", string(pub))
pingvin.Debug.Println("Key:\n", string(priv)) device.Debug.Println("Key:\n", string(priv))
if err := os.WriteFile(key, priv, 0600); err != nil { if err := os.WriteFile(key, priv, 0600); err != nil {
log.Fatal("Error writing private key ", key, ": ", err) log.Fatal("Error writing private key ", key, ": ", err)
} }
@ -279,7 +279,7 @@ func initDefaultConfig(confpath string) {
8888, 8888,
confpath + "/certificate.pem", confpath + "/certificate.pem",
confpath + "/privatekey.pem", confpath + "/privatekey.pem",
"pingvin", "device",
"enervent", "enervent",
4, 4,
false, false,
@ -347,16 +347,16 @@ func configure() {
log.Println("Update interval set to", config.Interval, "seconds") log.Println("Update interval set to", config.Interval, "seconds")
if config.EnableMetrics { if config.EnableMetrics {
log.Println("Prometheus exporter enabled (/metrics)") log.Println("Prometheus exporter enabled (/metrics)")
prometheus.MustRegister(&pingvin) prometheus.MustRegister(&device)
} }
} }
func main() { func main() {
log.Println("enervent-ctrl version", version) log.Println("enervent-ctrl version", version)
configure() configure()
pingvin = pingvinKL.New(config.Debug) device = pingvin.New(config.Debug)
pingvin.Update() device.Update()
go pingvin.Monitor(config.Interval) go device.Monitor(config.Interval)
serve(&config.SslCertificate, &config.SslPrivatekey) serve(&config.SslCertificate, &config.SslPrivatekey)
pingvin.Quit() device.Quit()
} }