package logs import ( "encoding/json" "fmt" "io" "io/ioutil" "os" "path/filepath" "text/tabwriter" stypes "git.cryptic.systems/fh-trier/go-flucky-server/pkg/types" "git.cryptic.systems/fh-trier/go-flucky/pkg/config" "git.cryptic.systems/fh-trier/go-flucky/pkg/types" ) // AppendTemperature write a given array of temperatures to a logfile func AppendTemperature(temperatures []*stypes.Temperature, cnf *types.Config) error { tLog, err := ReadTemperatures(cnf) if err != nil { return err } tLog = append(tLog, temperatures...) err = WriteTemperatures(tLog, cnf) if err != nil { return err } return nil } // FlushTemperatures ... func FlushTemperatures(cnf *types.Config) error { if err := ioutil.WriteFile(cnf.TemperatureLogfile, []byte(""), 0644); err != nil { return err } return nil } // ListTemperatures ... func ListTemperatures(configDir string, w io.Writer) error { cnf, err := config.Read(configDir) if err != nil { return err } temperatures, err := ReadTemperatures(cnf) if err != nil { return err } // tabwriter tw := tabwriter.NewWriter(w, 0, 0, 5, ' ', 0) sensors := []*stypes.Sensor{} // find all sensors which matched with one of the configuration file for _, temperature := range temperatures { for _, cnfSensor := range cnf.Sensors { if temperature.SensorID == cnfSensor.SensorID { var found bool for _, sensor := range sensors { if temperature.SensorID == sensor.SensorID { found = true } } if !found { sensors = append(sensors, cnfSensor) } } } } // sort temperature values to every sensor orderedTemperatures := make(map[string][]*stypes.Temperature) for _, temperature := range temperatures { orderedTemperatures[temperature.SensorID] = append(orderedTemperatures[temperature.SensorID], temperature) } // print headline fmt.Fprint(tw, "\t") for _, sensor := range sensors { fmt.Fprintf(tw, "%v\t", sensor.ReadableSensorName()) } if len(sensors) > 0 { fmt.Fprint(tw, "\n") } // find max length maxLength := 0 for _, orderedTemperature := range orderedTemperatures { if len(orderedTemperature) > maxLength { maxLength = len(orderedTemperature) } } // print body for i := 0; i < maxLength; i++ { fmt.Fprintf(tw, "%v\t", i) for _, sensor := range sensors { if len(orderedTemperatures[sensor.SensorID]) > i { fmt.Fprintf(tw, "%3.3f (%v)\t", orderedTemperatures[sensor.SensorID][i].TemperatureValue, orderedTemperatures[sensor.SensorID][i].TemperatureDate.Format("2006-01-02 15:04:05")) } else { fmt.Fprint(tw, "\t") } } fmt.Fprint(tw, "\n") } tw.Flush() return nil } // ReadTemperatures ... func ReadTemperatures(cnf *types.Config) ([]*stypes.Temperature, error) { logPath := filepath.Dir(cnf.TemperatureLogfile) // create log dir if not exist if _, err := os.Stat(cnf.TemperatureLogfile); os.IsNotExist(err) { if _, err := os.Stat(logPath); os.IsNotExist(err) { err := os.MkdirAll(logPath, os.ModePerm) if err != nil { return nil, fmt.Errorf("Can not create directory: %v", err) } } if err := FlushTemperatures(cnf); err != nil { return nil, err } } // open config file jsonFile, err := os.Open(cnf.TemperatureLogfile) if err != nil { return nil, fmt.Errorf("Can not open file %v: %v", cnf.TemperatureLogfile, err) } defer jsonFile.Close() bytes, err := ioutil.ReadAll(jsonFile) if err != nil { return nil, fmt.Errorf("Can not read jsonfile %v: %v", cnf.TemperatureLogfile, err) } temperatures := []*stypes.Temperature{} json.Unmarshal(bytes, &temperatures) return temperatures, nil } // WriteTemperatures ... func WriteTemperatures(temperatures []*stypes.Temperature, cnf *types.Config) error { bytes, err := json.Marshal(temperatures) if err != nil { return err } if err := ioutil.WriteFile(cnf.TemperatureLogfile, bytes, 0644); err != nil { return err } return nil }