2024-09-12 01:02:57 +00:00
package main
import (
"context"
"flag"
"net/http"
"os"
"os/signal"
2024-11-15 22:49:26 +00:00
"time"
2024-09-12 01:02:57 +00:00
2024-11-15 22:49:26 +00:00
"github.com/simonvetter/modbus"
2024-09-12 01:02:57 +00:00
"k8s.io/klog"
)
var (
2024-10-04 21:13:43 +00:00
flagFake bool
flagListenHTTP string
flagPressureThresholdRough = ScientificNotationValue ( 1e-1 )
2024-10-05 17:44:55 +00:00
flagPressureThresholdRoughHysteresis = ScientificNotationValue ( 3e-2 )
2024-10-04 21:13:43 +00:00
flagPressureThresholdHigh = ScientificNotationValue ( 1e-4 )
2024-10-04 21:24:21 +00:00
flagPressureThresholdHighHysteresis = ScientificNotationValue ( 2e-5 )
2024-09-12 01:02:57 +00:00
)
func main ( ) {
flag . BoolVar ( & flagFake , "fake" , false , "Enable fake mode which allows to run succd for tests outside the succbone" )
flag . StringVar ( & flagListenHTTP , "listen_http" , ":8080" , "Address at which to listen for HTTP requests" )
2024-09-27 00:11:04 +00:00
flag . TextVar ( & flagPressureThresholdRough , "pressure_threshold_rough" , & flagPressureThresholdRough , "Threshold for opening up diffusion pump (mbar)" )
2024-10-04 21:13:43 +00:00
flag . TextVar ( & flagPressureThresholdRoughHysteresis , "pressure_threshold_rough_hysteresis" , & flagPressureThresholdRoughHysteresis , "+-Hysteresis for rough threshold (mbar)" )
2024-09-27 00:11:04 +00:00
flag . TextVar ( & flagPressureThresholdHigh , "pressure_threshold_high" , & flagPressureThresholdHigh , "Threshold for enabling high voltage circuits (mbar)" )
2024-10-04 21:13:43 +00:00
flag . TextVar ( & flagPressureThresholdHighHysteresis , "pressure_threshold_high_hysteresis" , & flagPressureThresholdHighHysteresis , "+-Hysteresis for high threshold (mbar)" )
2024-09-12 01:02:57 +00:00
flag . Parse ( )
ctx , _ := signal . NotifyContext ( context . Background ( ) , os . Interrupt )
2024-09-28 06:10:33 +00:00
d := daemon { }
d . daemonState . rpOn = true
2024-09-28 07:35:41 +00:00
d . daemonState . piraniVolts3 . limit = 3
d . daemonState . piraniVolts100 . limit = 100
2024-09-28 06:10:33 +00:00
2024-11-10 00:22:18 +00:00
d . tempDPBottom = 420.6
d . tempDPTop = 69.0
d . tempDPInlet = 42.0
d . tempSEM = 42.5
d . humiditySEM = 66.6
2024-09-27 00:11:04 +00:00
d . aboveRough . threshold = float64 ( flagPressureThresholdRough )
2024-10-04 21:13:43 +00:00
d . aboveRough . hysteresis = float64 ( flagPressureThresholdRoughHysteresis )
2024-09-27 00:11:04 +00:00
d . aboveHigh . threshold = float64 ( flagPressureThresholdHigh )
2024-10-04 21:13:43 +00:00
d . aboveHigh . hysteresis = float64 ( flagPressureThresholdHighHysteresis )
2024-09-12 01:02:57 +00:00
if flagFake {
2024-09-25 21:28:57 +00:00
klog . Infof ( "Starting with fake peripherals" )
2024-09-12 01:02:57 +00:00
d . adcPirani = & fakeADC { }
} else {
adc , err := newBBADC ( 0 )
if err != nil {
klog . Exitf ( "Failed to setup Pirani ADC: %v" , err )
}
d . adcPirani = adc
2024-09-25 21:28:57 +00:00
2024-11-15 22:49:26 +00:00
// Setup modbus client
d . modbusClient , err = modbus . NewClient ( & modbus . ClientConfiguration {
URL : "tcp://10.250.241.20:8887" ,
Timeout : 1 * time . Second ,
} )
2024-11-10 00:47:01 +00:00
if err != nil {
2024-11-15 22:49:26 +00:00
klog . Exitf ( "Failed to setup modbus client %v" , err )
}
// Connect to modbus client
err = d . modbusClient . Open ( )
if err != nil {
klog . Warningf ( "Failed to connect to modbus TCP %v" , err )
2024-11-10 00:47:01 +00:00
}
2024-09-12 01:02:57 +00:00
}
2024-09-28 07:19:14 +00:00
web := webServer {
2024-09-28 06:10:33 +00:00
d : & d ,
}
2024-09-28 07:19:14 +00:00
web . setupViews ( )
2024-09-12 01:02:57 +00:00
klog . Infof ( "Listening for HTTP at %s" , flagListenHTTP )
go func ( ) {
if err := http . ListenAndServe ( flagListenHTTP , nil ) ; err != nil {
klog . Errorf ( "HTTP listen failed: %v" , err )
}
} ( )
go d . process ( ctx )
2024-11-10 00:47:01 +00:00
if ! flagFake {
go d . modbusProcess ( ctx )
}
2024-09-12 01:02:57 +00:00
<- ctx . Done ( )
}