gh-10 read/write single coil api method
GET /api/v1/coils/N - read coil N (int) POST /api/v1/coils/N/VAL - set coil N (int) to VAL, VAL can be one of accepted values for strconv.ParseBool (1 or 0 is the simplest)
This commit is contained in:
parent
71e949f760
commit
f76bddbb45
|
@ -9,7 +9,7 @@
|
||||||
8;COIL_CO2_BOOST_EN;CO2 boosting enabled
|
8;COIL_CO2_BOOST_EN;CO2 boosting enabled
|
||||||
9;COIL_RH_BOOST_EN;Relative humidity boosting enabled
|
9;COIL_RH_BOOST_EN;Relative humidity boosting enabled
|
||||||
10;COIL_M_BOOST;Boost the fanspeeds to 100% for a period of time
|
10;COIL_M_BOOST;Boost the fanspeeds to 100% for a period of time
|
||||||
11;COIL_TEMP_BOOST_EN;Temperature boosting enabled
|
11;COIL_TEMP_BOOST_EN;Adaptive circulation fan speed enabled
|
||||||
12;COIL_SNC;Summer night cooling (SNC) function enabled.
|
12;COIL_SNC;Summer night cooling (SNC) function enabled.
|
||||||
13;-;-
|
13;-;-
|
||||||
14;-;-
|
14;-;-
|
||||||
|
|
|
|
@ -6,6 +6,8 @@ import (
|
||||||
"io/fs"
|
"io/fs"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/0ranki/enervent-ctrl/enervent-ctrl-go/pingvinKL"
|
"github.com/0ranki/enervent-ctrl/enervent-ctrl-go/pingvinKL"
|
||||||
)
|
)
|
||||||
|
@ -24,9 +26,41 @@ var (
|
||||||
|
|
||||||
func coils(w http.ResponseWriter, r *http.Request) {
|
func coils(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.Coils)
|
pathparams := strings.Split(strings.TrimPrefix(r.URL.Path, "/api/v1/coils/"), "/")
|
||||||
|
if len(pathparams[0]) == 0 {
|
||||||
|
json.NewEncoder(w).Encode(pingvin.Coils)
|
||||||
|
} else if len(pathparams[0]) > 0 && r.Method == "GET" && len(pathparams) < 2 { // && r.Method == "POST"
|
||||||
|
intaddr, err := strconv.Atoi(pathparams[0])
|
||||||
|
if err != nil {
|
||||||
|
log.Println("ERROR: Could not parse coil address", pathparams[0])
|
||||||
|
log.Println(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
pingvin.ReadCoil(uint16(intaddr))
|
||||||
|
json.NewEncoder(w).Encode(pingvin.Coils[intaddr])
|
||||||
|
} else if len(pathparams[0]) > 0 && r.Method == "POST" && len(pathparams) == 2 {
|
||||||
|
intaddr, err := strconv.Atoi(pathparams[0])
|
||||||
|
if err != nil {
|
||||||
|
log.Println("ERROR: Could not parse coil address", pathparams[0])
|
||||||
|
log.Println(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
boolval, err := strconv.ParseBool(pathparams[1])
|
||||||
|
if err != nil {
|
||||||
|
log.Println("ERROR: Could not parse coil value", pathparams[1])
|
||||||
|
log.Println(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
pingvin.WriteCoil(uint16(intaddr), boolval)
|
||||||
|
json.NewEncoder(w).Encode(pingvin.Coils[intaddr])
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// func singlecoil(w http.ResponseWriter, r *http.Request) {
|
||||||
|
// w.Header().Set("Content-Type", "application/json")
|
||||||
|
|
||||||
|
// }
|
||||||
|
|
||||||
func registers(w http.ResponseWriter, r *http.Request) {
|
func registers(w http.ResponseWriter, r *http.Request) {
|
||||||
w.Header().Set("Content-Type", "application/json")
|
w.Header().Set("Content-Type", "application/json")
|
||||||
if DEBUG {
|
if DEBUG {
|
||||||
|
|
|
@ -284,6 +284,7 @@ func (p *PingvinKL) WriteCoil(n uint16, val bool) bool {
|
||||||
return false
|
return false
|
||||||
|
|
||||||
}
|
}
|
||||||
|
p.ReadCoil(n)
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue