2024-09-12 01:02:57 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
_ "embed"
|
|
|
|
"fmt"
|
|
|
|
"html/template"
|
|
|
|
"net/http"
|
|
|
|
_ "net/http/pprof"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/coder/websocket"
|
|
|
|
"github.com/coder/websocket/wsjson"
|
|
|
|
"k8s.io/klog"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
//go:embed index.html
|
|
|
|
templateIndexText string
|
|
|
|
templateIndex = template.Must(template.New("index").Parse(templateIndexText))
|
2024-09-27 00:10:39 +00:00
|
|
|
//go:embed succd.png
|
|
|
|
favicon []byte
|
2024-09-12 01:02:57 +00:00
|
|
|
)
|
|
|
|
|
2024-09-27 00:10:39 +00:00
|
|
|
func (d *daemon) httpFavicon(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.Header().Set("Content-Type", "image/png")
|
|
|
|
w.Write(favicon)
|
|
|
|
}
|
|
|
|
|
2024-09-12 01:02:57 +00:00
|
|
|
func formatVolts(v float32) string {
|
2024-09-12 01:18:31 +00:00
|
|
|
return fmt.Sprintf("%.4f V", v)
|
2024-09-12 01:02:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// formatMbar formats a millibar value using scientific notation and returns a
|
|
|
|
// HTML fragment (for superscript support).
|
|
|
|
func formatMbar(v float32) template.HTML {
|
|
|
|
exp := 0
|
|
|
|
for v < 1 {
|
|
|
|
v *= 10
|
|
|
|
exp -= 1
|
|
|
|
}
|
|
|
|
for v >= 10 {
|
|
|
|
v /= 10
|
|
|
|
exp += 1
|
|
|
|
}
|
|
|
|
res := fmt.Sprintf("%.3f", v)
|
|
|
|
res += fmt.Sprintf(" x 10<sup>%d</sup>", exp)
|
|
|
|
res += " mbar"
|
|
|
|
return template.HTML(res)
|
|
|
|
}
|
|
|
|
|
2024-09-28 05:49:03 +00:00
|
|
|
// apiData is the data model served to the user via HTTP/WebSockets
|
|
|
|
type apiData struct {
|
|
|
|
// Safety interlocks.
|
|
|
|
Safety struct {
|
|
|
|
// Failsafe mode enabled - pirani gauge seems disconnected.
|
|
|
|
Failsafe bool
|
|
|
|
// HighPressure interlock enabled - pressure too high to run diffusion
|
|
|
|
// pump.
|
|
|
|
HighPressure bool
|
|
|
|
}
|
|
|
|
// Pirani gauge data.
|
|
|
|
Pirani struct {
|
|
|
|
// Volts read from the gauge (0-10).
|
|
|
|
Volts string
|
|
|
|
// Mbar read from the gauge, formatted as HTML.
|
|
|
|
Mbar template.HTML
|
|
|
|
// MbarFloat read from the gauge.
|
|
|
|
MbarFloat float32
|
2024-09-12 01:02:57 +00:00
|
|
|
}
|
2024-09-28 05:49:03 +00:00
|
|
|
// Pump state.
|
|
|
|
Pumps struct {
|
|
|
|
// RPOn means the roughing pump is turned on.
|
|
|
|
RPOn bool
|
|
|
|
// DPOn means the diffusion pump is turned on.
|
|
|
|
DPOn bool
|
|
|
|
}
|
|
|
|
// Pressure feedback into evacuation board.
|
|
|
|
Feedback struct {
|
|
|
|
// RoughReached is true when the system has reached a rough vacuum
|
|
|
|
// stage.
|
|
|
|
RoughReached bool
|
|
|
|
// HighReached is true when the system has reached a high vacuum stage.
|
|
|
|
HighReached bool
|
|
|
|
}
|
|
|
|
// System junk.
|
|
|
|
System struct {
|
|
|
|
// Load of the system.
|
|
|
|
Load string
|
|
|
|
// Hostname of the system.
|
|
|
|
Hostname string
|
|
|
|
}
|
|
|
|
}
|
2024-09-12 01:02:57 +00:00
|
|
|
|
2024-09-28 05:49:03 +00:00
|
|
|
// 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 {
|
2024-09-12 01:02:57 +00:00
|
|
|
volts, mbar := d.pirani()
|
2024-09-25 21:28:57 +00:00
|
|
|
rp := d.rpGet()
|
|
|
|
dp := d.dpGet()
|
2024-09-28 05:49:03 +00:00
|
|
|
rough, high := d.vacuumStatusGet()
|
2024-09-28 05:35:45 +00:00
|
|
|
safety := d.safetyStatusGet()
|
2024-09-12 01:02:57 +00:00
|
|
|
|
2024-09-28 05:49:03 +00:00
|
|
|
var hostname, load string
|
|
|
|
var err error
|
|
|
|
|
|
|
|
if !skipSystem {
|
|
|
|
hostname, err = os.Hostname()
|
|
|
|
if err != nil {
|
|
|
|
hostname = "unknown"
|
|
|
|
}
|
|
|
|
loadB, err := os.ReadFile("/proc/loadavg")
|
|
|
|
load = "unknown"
|
|
|
|
if err == nil {
|
|
|
|
parts := strings.Fields(string(loadB))
|
|
|
|
load = strings.Join(parts[:3], " ")
|
|
|
|
}
|
2024-09-12 01:02:57 +00:00
|
|
|
}
|
|
|
|
|
2024-09-28 05:49:03 +00:00
|
|
|
ad := apiData{}
|
|
|
|
ad.Safety.Failsafe = safety.failsafe
|
|
|
|
ad.Safety.HighPressure = 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.Feedback.RoughReached = rough
|
|
|
|
ad.Feedback.HighReached = high
|
|
|
|
ad.System.Load = load
|
|
|
|
ad.System.Hostname = hostname
|
|
|
|
return &ad
|
|
|
|
}
|
|
|
|
|
|
|
|
// httpIndex is the / view.
|
|
|
|
func (d *daemon) httpIndex(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if r.URL.Path != "/" {
|
|
|
|
http.NotFound(w, r)
|
|
|
|
return
|
2024-09-12 01:02:57 +00:00
|
|
|
}
|
|
|
|
|
2024-09-28 05:49:03 +00:00
|
|
|
data := d.apiData(false)
|
|
|
|
//data.Pirani.Mbar = html.St
|
|
|
|
templateIndex.Execute(w, data)
|
2024-09-12 01:02:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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) {
|
|
|
|
c, err := websocket.Accept(w, r, nil)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer c.CloseNow()
|
|
|
|
|
|
|
|
t := time.NewTicker(time.Second / 10)
|
|
|
|
defer t.Stop()
|
|
|
|
|
|
|
|
ctx := c.CloseRead(r.Context())
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
c.Close(websocket.StatusNormalClosure, "")
|
|
|
|
return
|
|
|
|
case <-t.C:
|
2024-09-28 05:49:03 +00:00
|
|
|
v := d.apiData(true)
|
2024-09-12 01:02:57 +00:00
|
|
|
if err := wsjson.Write(ctx, c, v); err != nil {
|
|
|
|
klog.Errorf("Websocket write failed: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// httpMetrics serves minimalistic Prometheus-compatible metrics.
|
|
|
|
func (d *daemon) httpMetrics(w http.ResponseWriter, r *http.Request) {
|
|
|
|
// TODO(q3k): also serve Go stuff using the actual Prometheus metrics client
|
|
|
|
// library.
|
|
|
|
_, mbar := d.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)
|
|
|
|
}
|
2024-09-25 21:28:57 +00:00
|
|
|
|
|
|
|
func (d *daemon) httpRoughingPumpEnable(w http.ResponseWriter, r *http.Request) {
|
|
|
|
d.rpSet(true)
|
|
|
|
fmt.Fprintf(w, "succ on\n")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *daemon) httpRoughingPumpDisable(w http.ResponseWriter, r *http.Request) {
|
|
|
|
d.rpSet(false)
|
|
|
|
fmt.Fprintf(w, "succ off\n")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *daemon) httpDiffusionPumpEnable(w http.ResponseWriter, r *http.Request) {
|
|
|
|
d.dpSet(true)
|
|
|
|
fmt.Fprintf(w, "deep succ on\n")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *daemon) httpDiffusionPumpDisable(w http.ResponseWriter, r *http.Request) {
|
|
|
|
d.dpSet(false)
|
|
|
|
fmt.Fprintf(w, "deep succ off\n")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *daemon) httpButtonPumpDown(w http.ResponseWriter, r *http.Request) {
|
|
|
|
d.pumpDownPress()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *daemon) httpButtonVent(w http.ResponseWriter, r *http.Request) {
|
|
|
|
d.ventPress()
|
|
|
|
}
|