package temperature import ( "fmt" "io" "path/filepath" "text/tabwriter" "git.cryptic.systems/fh-trier/go-flucky/pkg/config" "git.cryptic.systems/fh-trier/go-flucky/pkg/sensor" "git.cryptic.systems/fh-trier/go-flucky/pkg/types" ) // AddDS18B20 a new temperature sensor from type ds18b20 func AddDS18B20(sensorID, sensorName, configDir, wirePath string) error { // read cnf file cnf, err := config.Read(configDir) if err != nil { return err } // check if sensor exists if !sensor.Exists(wirePath, sensorID) { return fmt.Errorf("Can not find sensor: %v", filepath.Join(wirePath, sensorID)) } // check if sensor is redundant if sensor.IsRedundant(sensorID, cnf.TemperatureSensors) { return fmt.Errorf("Sensor %v already exists as temperature sensor", sensorID) } temperatureSensor := &types.WireSensor{ ID: sensorID, Name: sensorName, Typ: types.SENSOR_DS18B20, WirePath: wirePath, } // append sensor to list cnf.TemperatureSensors = append(cnf.TemperatureSensors, temperatureSensor) // write cnf file if err := config.Write(cnf, configDir); err != nil { return err } return nil } // Print a list with the given writer w over all temperature sensors func Print(w io.Writer, configDir string, quiet bool) error { // read cnf file cnf, err := config.Read(configDir) if err != nil { return err } // declar tabwriter tw := tabwriter.NewWriter(w, 0, 0, 5, ' ', 0) // headline if !quiet { fmt.Fprint(tw, "id\tname\ttype\tpath\n") } for _, sensor := range cnf.TemperatureSensors { if quiet { fmt.Fprintf(tw, "%v\n", sensor.ID) } else { fmt.Fprintf(tw, "%v\t%v\t%v\t%v\n", sensor.ID, sensor.Name, sensor.Typ, sensor.WirePath) } } tw.Flush() return nil } // Remove a temperature sensor over the given sensor id func Remove(sensorID, configDir string) error { // read cnf file cnf, err := config.Read(configDir) if err != nil { return err } // remove matching sensor ids var j int for _, sensor := range cnf.TemperatureSensors { if sensor.ID == sensorID { cnf.TemperatureSensors = append(cnf.TemperatureSensors[:j], cnf.TemperatureSensors[j+1:]...) if j > 0 { j = j - 1 } continue } j++ } // write cnf file if err := config.Write(cnf, configDir); err != nil { return err } return nil }