func IdEverything(cfg *Configuration) { idMap := make(map[string]bool) stepLists := cfg.StepLists for i := 0; i < len(stepLists); i++ { stepList := &stepLists[i] if len(stepList.Id) == 0 || idMap[stepList.Id] { stepList.Id = id.RandomId() } idMap[stepList.Id] = true } tanks := cfg.BrewLayout.Tanks for i := 0; i < len(tanks); i++ { tank := &tanks[i] if len(tank.Id) == 0 || idMap[tank.Id] { tank.Id = id.RandomId() } idMap[tank.Id] = true if len(tank.Heater.Id) == 0 || idMap[tank.Heater.Id] { tank.Heater.Id = id.RandomId() } idMap[tank.Heater.Id] = true } pumps := cfg.BrewLayout.Pumps for i := 0; i < len(pumps); i++ { pump := &pumps[i] if len(pump.Id) == 0 || idMap[pump.Id] { pump.Id = id.RandomId() } idMap[pump.Id] = true } }
func createControlPoint(io int32, hasDuty bool) (cp ControlPoint) { cp.Id = id.RandomId() cp.Io = io cp.FullOnAmps = 0 cp.HasDuty = hasDuty // cp.On = false cp.Duty = 0 initControlPointDuty(&cp) return }
func updateForNewConfiguration(newCfg *Configuration, state *State, cfg *Configuration) { // TODO Update target sensors IdEverything(newCfg) oldMap := IoToOwnerIdMap(cfg) newMap := IoToOwnerIdMap(newCfg) if newCfg.Version == cfg.Version { cfg.Version = id.RandomId() } else { cfg.Version = newCfg.Version } state.ConfigurationVersion = cfg.Version cfg.LogMessages = newCfg.LogMessages cfg.BrewLayout = newCfg.BrewLayout cfg.Sensors = newCfg.Sensors cfg.StepLists = newCfg.StepLists modifiedIos := false for k := range oldMap { if newMap[k] != oldMap[k] { modifiedIos = true } } for k := range newMap { if newMap[k] != oldMap[k] { modifiedIos = true } } if modifiedIos { // Moved an IO. Shouldn't happen while ON, if it does, clear everything turnOffSeenControls() state.Steps = state.Steps[:0] SetToStateDefault(*cfg, state) } WriteConfiguration(cfg) }
func StepDefault(cfg Configuration) (step ControlStep) { step.Name = "Manual Step" step.Id = id.RandomId() step.StepTime = 0 step.Active = true tanks := cfg.BrewLayout.Tanks for i := 0; i < len(tanks); i++ { tank := tanks[i] heater := tank.Heater if heater.Io > 0 { cp := createControlPoint(heater.Io, heater.HasDuty) step.ControlPoints = append(step.ControlPoints, cp) } } pumps := cfg.BrewLayout.Pumps for i := 0; i < len(pumps); i++ { pump := pumps[i] cp := createControlPoint(pump.Io, pump.HasDuty) step.ControlPoints = append(step.ControlPoints, cp) } return }
func SelectReadingSensors(cfg *Configuration, state *State, sensorReadings []onewire.TempReading) { configDirty := false // Add to cfg list for i := range sensorReadings { reading := sensorReadings[i] found := false for j := range cfg.Sensors { cSensor := cfg.Sensors[j] if cSensor.Address == reading.Id { found = true break } } if !found { log.Printf("Add Sensor: %v", reading.Id) var cSensor SensorConfig cSensor.Address = reading.Id cfg.Sensors = append(cfg.Sensors, cSensor) configDirty = true } } // Update Tank selected Sensors tanks := cfg.BrewLayout.Tanks for i := range tanks { tank := &tanks[i] if len(tank.Sensor.Address) > 0 { found := false for j := range sensorReadings { reading := sensorReadings[j] if tank.Sensor.Address == reading.Id { found = true break } } if found { // Sensor for this tank is being read. Make sure it is still for this tank for j := range cfg.Sensors { cSensor := cfg.Sensors[j] if cSensor.Address == tank.Sensor.Address { if cSensor.Location != tank.Name { log.Printf("Clear Tank Sensor %v for tank %v", tank.Sensor.Address, tank.Name) tank.Sensor.Address = "" configDirty = true ChangeSpepControlSensorTo(state, tank.Heater.Io, "") } } } } else { tank.Sensor.Address = "" configDirty = true } } if len(tank.Sensor.Address) == 0 { for j := range cfg.Sensors { cSensor := cfg.Sensors[j] if cSensor.Location == tank.Name { for k := range sensorReadings { reading := sensorReadings[k] if cSensor.Address == reading.Id { log.Printf("Select Tank Sensor %v for tank %v", cSensor.Address, tank.Name) tank.Sensor.Address = cSensor.Address // Change Control Points As well ChangeSpepControlSensorTo(state, tank.Heater.Io, cSensor.Address) configDirty = true break } } } } } } if configDirty { cfg.Version = id.RandomId() state.ConfigurationVersion = cfg.Version WriteConfiguration(cfg) } }