gh-6 the update/populate functions now work on the struct they're a member of, instead of on a copy. Added /api/v1/status route
This commit is contained in:
parent
960c29fbf4
commit
9fefef8a98
|
@ -17,7 +17,7 @@ import (
|
||||||
var static embed.FS
|
var static embed.FS
|
||||||
|
|
||||||
var (
|
var (
|
||||||
version = "0.0.3"
|
version = "0.0.4"
|
||||||
pingvin pingvinKL.PingvinKL
|
pingvin pingvinKL.PingvinKL
|
||||||
DEBUG = false
|
DEBUG = false
|
||||||
)
|
)
|
||||||
|
@ -38,10 +38,16 @@ func registers(w http.ResponseWriter, r *http.Request) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func status(w http.ResponseWriter, r *http.Request) {
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
json.NewEncoder(w).Encode(pingvin.Status)
|
||||||
|
}
|
||||||
|
|
||||||
func listen() {
|
func listen() {
|
||||||
log.Println("Starting pingvinAPI...")
|
log.Println("Starting pingvinAPI...")
|
||||||
http.HandleFunc("/api/v1/coils/", coils)
|
http.HandleFunc("/api/v1/coils/", coils)
|
||||||
http.HandleFunc("/api/v1/registers/", registers)
|
http.HandleFunc("/api/v1/registers/", registers)
|
||||||
|
http.HandleFunc("/api/v1/status", status)
|
||||||
html, err := fs.Sub(static, "static/html")
|
html, err := fs.Sub(static, "static/html")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
|
|
|
@ -2,6 +2,7 @@ package pingvinKL
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
"bufio"
|
||||||
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
|
@ -44,15 +45,15 @@ type pingvinRegister struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
type pingvinVentInfo struct {
|
type pingvinVentInfo struct {
|
||||||
supplyHeated int `json:"supply_heated"`
|
SupplyHeated int `json:"supply_heated"`
|
||||||
supplyHrc int `json:"supply_hrc"`
|
SupplyHrc int `json:"supply_hrc"`
|
||||||
supplyIntake int `json:"supply_intake"`
|
SupplyIntake int `json:"supply_intake"`
|
||||||
supplyIntake24h int `json:"supply_intake_24h"`
|
SupplyIntake24h int `json:"supply_intake_24h"`
|
||||||
supplyHum int `json:"supply_hum"`
|
SupplyHum int `json:"supply_hum"`
|
||||||
extractIntake int `json:"extract_intake"`
|
ExtractIntake int `json:"extract_intake"`
|
||||||
extractHrc int `json:"extract_hrc"`
|
ExtractHrc int `json:"extract_hrc"`
|
||||||
extractHum int `json:"extract_hum"`
|
ExtractHum int `json:"extract_hum"`
|
||||||
extractHum48h int `json:"extract_hum_48h"`
|
ExtractHum48h int `json:"extract_hum_48h"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type pingvinStatus struct {
|
type pingvinStatus struct {
|
||||||
|
@ -82,11 +83,14 @@ func newCoil(address string, symbol string, description string) pingvinCoil {
|
||||||
func newRegister(address, symbol, typ, multiplier, description string) pingvinRegister {
|
func newRegister(address, symbol, typ, multiplier, description string) pingvinRegister {
|
||||||
addr, err := strconv.Atoi(address)
|
addr, err := strconv.Atoi(address)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal("newRegister: Atio: ")
|
log.Fatal("newRegister: Atoi(address): ", err)
|
||||||
}
|
}
|
||||||
multipl, err := strconv.Atoi(multiplier)
|
multipl := 1
|
||||||
if err != nil {
|
if len(multiplier) > 0 {
|
||||||
log.Fatal("newRegister: Atio: ")
|
multipl, err = strconv.Atoi(multiplier)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal("newRegister: Atoi(multiplier): ", err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
reserved := symbol == "Reserved" && description == "Reserved"
|
reserved := symbol == "Reserved" && description == "Reserved"
|
||||||
register := pingvinRegister{addr, symbol, 0, "00000000", typ, description, reserved, multipl}
|
register := pingvinRegister{addr, symbol, 0, "00000000", typ, description, reserved, multipl}
|
||||||
|
@ -126,7 +130,7 @@ func (p PingvinKL) getHandler() *modbus.RTUClientHandler {
|
||||||
return handler
|
return handler
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p PingvinKL) updateCoils() {
|
func (p *PingvinKL) updateCoils() {
|
||||||
handler := p.getHandler()
|
handler := p.getHandler()
|
||||||
p.buslock.Lock()
|
p.buslock.Lock()
|
||||||
err := handler.Connect()
|
err := handler.Connect()
|
||||||
|
@ -159,7 +163,7 @@ func (p PingvinKL) updateCoils() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p PingvinKL) updateRegisters() {
|
func (p *PingvinKL) updateRegisters() {
|
||||||
handler := p.getHandler()
|
handler := p.getHandler()
|
||||||
p.buslock.Lock()
|
p.buslock.Lock()
|
||||||
err := handler.Connect()
|
err := handler.Connect()
|
||||||
|
@ -214,9 +218,10 @@ func (p PingvinKL) updateRegisters() {
|
||||||
p.buslock.Unlock()
|
p.buslock.Unlock()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p PingvinKL) Update() {
|
func (p *PingvinKL) Update() {
|
||||||
p.updateCoils()
|
p.updateCoils()
|
||||||
p.updateRegisters()
|
p.updateRegisters()
|
||||||
|
p.populateStatus()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p PingvinKL) ReadCoil(n uint16) []byte {
|
func (p PingvinKL) ReadCoil(n uint16) []byte {
|
||||||
|
@ -237,6 +242,19 @@ func (p PingvinKL) ReadCoil(n uint16) []byte {
|
||||||
return results
|
return results
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (p *PingvinKL) populateStatus() {
|
||||||
|
hpct := p.Registers[49].Value / p.Registers[49].Multiplier
|
||||||
|
log.Println(hpct)
|
||||||
|
if hpct > 100 {
|
||||||
|
p.Status.HeaterPct = hpct - 100
|
||||||
|
p.Status.HrcPct = 100
|
||||||
|
} else {
|
||||||
|
p.Status.HeaterPct = 0
|
||||||
|
p.Status.HrcPct = hpct
|
||||||
|
}
|
||||||
|
json.NewEncoder(log.Writer()).Encode(p.Status)
|
||||||
|
}
|
||||||
|
|
||||||
// create a PingvinKL struct, read coils and registers from CSVs
|
// create a PingvinKL struct, read coils and registers from CSVs
|
||||||
func New() PingvinKL {
|
func New() PingvinKL {
|
||||||
pingvin := PingvinKL{}
|
pingvin := PingvinKL{}
|
||||||
|
|
Loading…
Reference in New Issue