succd: split out http server, daemon state, daemon controller
This improves the structure of the code, separating the data/control interface out and then implementing the http interface as a user of this interface.
This commit is contained in:
parent
3ec6fd1d1b
commit
8f7ec7e141
5 changed files with 190 additions and 180 deletions
|
|
@ -15,6 +15,10 @@ import (
|
|||
"k8s.io/klog"
|
||||
)
|
||||
|
||||
type httpServer struct {
|
||||
d daemonController
|
||||
}
|
||||
|
||||
var (
|
||||
//go:embed index.html
|
||||
templateIndexText string
|
||||
|
|
@ -23,7 +27,7 @@ var (
|
|||
favicon []byte
|
||||
)
|
||||
|
||||
func (d *daemon) httpFavicon(w http.ResponseWriter, r *http.Request) {
|
||||
func (s *httpServer) viewFavicon(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "image/png")
|
||||
w.Write(favicon)
|
||||
}
|
||||
|
|
@ -96,12 +100,10 @@ type apiData struct {
|
|||
// apiData returns the user data model for the current state of the system. If
|
||||
// skipSystem is set, the System subset is ignored (saves system load, and is
|
||||
// not being served via websockets).
|
||||
func (d *daemon) apiData(skipSystem bool) *apiData {
|
||||
volts, mbar := d.pirani()
|
||||
rp := d.rpGet()
|
||||
dp := d.dpGet()
|
||||
rough, high := d.vacuumStatusGet()
|
||||
safety := d.safetyStatusGet()
|
||||
func (s *httpServer) apiData(skipSystem bool) *apiData {
|
||||
state := s.d.snapshot()
|
||||
volts, mbar := state.pirani()
|
||||
rough, high := state.vacuumStatus()
|
||||
|
||||
var hostname, load string
|
||||
var err error
|
||||
|
|
@ -120,13 +122,13 @@ func (d *daemon) apiData(skipSystem bool) *apiData {
|
|||
}
|
||||
|
||||
ad := apiData{}
|
||||
ad.Safety.Failsafe = safety.failsafe
|
||||
ad.Safety.HighPressure = safety.highPressure
|
||||
ad.Safety.Failsafe = state.safety.failsafe
|
||||
ad.Safety.HighPressure = state.safety.highPressure
|
||||
ad.Pirani.Volts = formatVolts(volts)
|
||||
ad.Pirani.Mbar = formatMbar(mbar)
|
||||
ad.Pirani.MbarFloat = mbar
|
||||
ad.Pumps.RPOn = rp
|
||||
ad.Pumps.DPOn = dp
|
||||
ad.Pumps.RPOn = state.rpOn
|
||||
ad.Pumps.DPOn = state.dpOn
|
||||
ad.Feedback.RoughReached = rough
|
||||
ad.Feedback.HighReached = high
|
||||
ad.System.Load = load
|
||||
|
|
@ -135,20 +137,20 @@ func (d *daemon) apiData(skipSystem bool) *apiData {
|
|||
}
|
||||
|
||||
// httpIndex is the / view.
|
||||
func (d *daemon) httpIndex(w http.ResponseWriter, r *http.Request) {
|
||||
func (s *httpServer) viewIndex(w http.ResponseWriter, r *http.Request) {
|
||||
if r.URL.Path != "/" {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
data := d.apiData(false)
|
||||
data := s.apiData(false)
|
||||
//data.Pirani.Mbar = html.St
|
||||
templateIndex.Execute(w, data)
|
||||
}
|
||||
|
||||
// httpStream is the websocket clientwards data hose, returning a 10Hz update
|
||||
// stream of pressure/voltage.
|
||||
func (d *daemon) httpStream(w http.ResponseWriter, r *http.Request) {
|
||||
func (s *httpServer) viewStream(w http.ResponseWriter, r *http.Request) {
|
||||
c, err := websocket.Accept(w, r, nil)
|
||||
if err != nil {
|
||||
return
|
||||
|
|
@ -165,7 +167,7 @@ func (d *daemon) httpStream(w http.ResponseWriter, r *http.Request) {
|
|||
c.Close(websocket.StatusNormalClosure, "")
|
||||
return
|
||||
case <-t.C:
|
||||
v := d.apiData(true)
|
||||
v := s.apiData(true)
|
||||
if err := wsjson.Write(ctx, c, v); err != nil {
|
||||
klog.Errorf("Websocket write failed: %v", err)
|
||||
return
|
||||
|
|
@ -175,39 +177,54 @@ func (d *daemon) httpStream(w http.ResponseWriter, r *http.Request) {
|
|||
}
|
||||
|
||||
// httpMetrics serves minimalistic Prometheus-compatible metrics.
|
||||
func (d *daemon) httpMetrics(w http.ResponseWriter, r *http.Request) {
|
||||
func (s *httpServer) viewMetrics(w http.ResponseWriter, r *http.Request) {
|
||||
// TODO(q3k): also serve Go stuff using the actual Prometheus metrics client
|
||||
// library.
|
||||
_, mbar := d.pirani()
|
||||
// TODO(q3k): serve the rest of the data model
|
||||
state := s.d.snapshot()
|
||||
_, mbar := state.pirani()
|
||||
fmt.Fprintf(w, "# HELP sem_pressure_mbar Pressure in the SEM chamber, in millibar\n")
|
||||
fmt.Fprintf(w, "# TYPE sem_pressure_mbar gauge\n")
|
||||
fmt.Fprintf(w, "sem_pressure_mbar %f\n", mbar)
|
||||
}
|
||||
|
||||
func (d *daemon) httpRoughingPumpEnable(w http.ResponseWriter, r *http.Request) {
|
||||
d.rpSet(true)
|
||||
func (s *httpServer) viewRoughingPumpEnable(w http.ResponseWriter, r *http.Request) {
|
||||
s.d.rpSet(true)
|
||||
fmt.Fprintf(w, "succ on\n")
|
||||
}
|
||||
|
||||
func (d *daemon) httpRoughingPumpDisable(w http.ResponseWriter, r *http.Request) {
|
||||
d.rpSet(false)
|
||||
func (s *httpServer) viewRoughingPumpDisable(w http.ResponseWriter, r *http.Request) {
|
||||
s.d.rpSet(false)
|
||||
fmt.Fprintf(w, "succ off\n")
|
||||
}
|
||||
|
||||
func (d *daemon) httpDiffusionPumpEnable(w http.ResponseWriter, r *http.Request) {
|
||||
d.dpSet(true)
|
||||
func (s *httpServer) viewDiffusionPumpEnable(w http.ResponseWriter, r *http.Request) {
|
||||
s.d.dpSet(true)
|
||||
fmt.Fprintf(w, "deep succ on\n")
|
||||
}
|
||||
|
||||
func (d *daemon) httpDiffusionPumpDisable(w http.ResponseWriter, r *http.Request) {
|
||||
d.dpSet(false)
|
||||
func (s *httpServer) viewDiffusionPumpDisable(w http.ResponseWriter, r *http.Request) {
|
||||
s.d.dpSet(false)
|
||||
fmt.Fprintf(w, "deep succ off\n")
|
||||
}
|
||||
|
||||
func (d *daemon) httpButtonPumpDown(w http.ResponseWriter, r *http.Request) {
|
||||
d.pumpDownPress()
|
||||
func (s *httpServer) viewButtonPumpDown(w http.ResponseWriter, r *http.Request) {
|
||||
s.d.pumpDownPress()
|
||||
}
|
||||
|
||||
func (d *daemon) httpButtonVent(w http.ResponseWriter, r *http.Request) {
|
||||
d.ventPress()
|
||||
func (s *httpServer) viewButtonVent(w http.ResponseWriter, r *http.Request) {
|
||||
s.d.ventPress()
|
||||
}
|
||||
|
||||
func (s *httpServer) setupViews() {
|
||||
http.HandleFunc("/", s.viewIndex)
|
||||
http.HandleFunc("/favicon.png", s.viewFavicon)
|
||||
http.HandleFunc("/stream", s.viewStream)
|
||||
http.HandleFunc("/metrics", s.viewMetrics)
|
||||
http.HandleFunc("/button/vent", s.viewButtonVent)
|
||||
http.HandleFunc("/button/pumpdown", s.viewButtonPumpDown)
|
||||
http.HandleFunc("/rp/on", s.viewRoughingPumpEnable)
|
||||
http.HandleFunc("/rp/off", s.viewRoughingPumpDisable)
|
||||
http.HandleFunc("/dp/on", s.viewDiffusionPumpEnable)
|
||||
http.HandleFunc("/dp/off", s.viewDiffusionPumpDisable)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue