package logger import ( "encoding/json" "fmt" "io" "os" "time" "github.com/volker-raschek/flucky/pkg/types" ) type FileLogger struct { LogFiles map[LogValue][]string `json:"logfiles"` writer io.Writer } func (fl *FileLogger) AddLogFile(logfile string, logValue LogValue) error { for _, logfiles := range fl.LogFiles { for _, lf := range logfiles { if lf == logfile { return fmt.Errorf("Logfile %v already configured", logfile) } } } fl.LogFiles[logValue] = append(fl.LogFiles[logValue], logfile) return nil } func (fl *FileLogger) GetHumidities(fromDate *time.Time, tillDate *time.Time, sensorNameOrUUID *[]string) ([]*types.Humidity, error) { return nil, nil } func (fl *FileLogger) GetTemperatures(from *time.Time, till *time.Time, sensorIDs []string) ([]*types.Temperature, error) { cachedTemperatures := []*types.Temperature{} // iterate over all configured temperature logfiles for _, logfile := range fl.LogFiles[LogTemperature] { // skip logfile if not exists if _, err := os.Stat(logfile); os.IsNotExist(err) { return nil, fmt.Errorf("Logfile %v not exists", logfile) } loggedTemperatures := []*types.Temperature{} f, err := os.Open(logfile) if err != nil { return nil, fmt.Errorf("Can not open %v: %v", logfile, err) } defer f.Close() decoder := json.NewDecoder(f) err = decoder.Decode(&loggedTemperatures) if err != nil { return nil, fmt.Errorf("Can not decode temperatures from logfile %v: %v", logfile, err) } cachedTemperatures = append(cachedTemperatures, loggedTemperatures...) // // sort all redundant values out // for _, loggedTemperature := range loggedTemperatures { // found := false // for _, cachedTemperature := range cachedTemperatures { // if cachedTemperature.TemperatureID == loggedTemperature.TemperatureID { // found = true // break // } // } // if !found { // cachedTemperatures = append(cachedTemperatures, loggedTemperature) // } // } } // // sort out all temperatures which are not mesured by sensor id // for i, cachedTemperature := range cachedTemperatures { // found := false // for _, sensorID := range sensorIDs { // if cachedTemperature.SensorID == sensorID { // found = true // break // } // } // if !found { // cachedTemperatures = append(cachedTemperatures[:i], cachedTemperatures[i+1:]...) // } // } // // sort out all temperatures which are before from date // if from != nil { // for i, cachedTemperature := range cachedTemperatures { // if cachedTemperature.TemperatureDate.Before(*from) { // cachedTemperatures = append(cachedTemperatures[:i], cachedTemperatures[i+1:]...) // } // } // } // // sort out all temperatures which are after till date // if till != nil { // for i, cachedTemperature := range cachedTemperatures { // if cachedTemperature.TemperatureDate.After(*till) { // cachedTemperatures = append(cachedTemperatures[:i], cachedTemperatures[i+1:]...) // } // } // } return cachedTemperatures, nil } // LogHumidities write humidities to all configured humidity logfiles func (fl *FileLogger) LogHumidities(humidities []*types.Humidity) error { for _, logfile := range fl.LogFiles[LogHumidity] { logHumidities := []*types.Humidity{} // if logfile already exists, read humidities from logfile if _, err := os.Stat(logfile); err == nil { f, err := os.Open(logfile) if err != nil { return fmt.Errorf("Can not open file %v: %v", logfile, err) } decoder := json.NewDecoder(f) err = decoder.Decode(&logHumidities) if err != nil { return fmt.Errorf("Can not decode humidities from reader") } f.Close() } // append new humidities with existing humidities from logfile logHumidities = append(logHumidities, humidities...) // overwrite lofgile with new entries f, err := os.Create(logfile) if err != nil { return fmt.Errorf("Can not open file %v: %v", logfile, err) } defer f.Close() // encode humidities encoder := json.NewEncoder(f) encoder.SetIndent("", " ") err = encoder.Encode(logHumidities) if err != nil { return fmt.Errorf("Can not encode humidities for the writer") } } return nil } // LogTemperatures write temperatures to all configured temperature logfiles func (fl *FileLogger) LogTemperatures(temperatures []*types.Temperature) error { for _, logfile := range fl.LogFiles[LogTemperature] { logTemperatures := []*types.Temperature{} // if logfile already exists, read temperatures from logfile if _, err := os.Stat(logfile); err == nil { f, err := os.Open(logfile) if err != nil { return fmt.Errorf("Can not open file %v: %v", logfile, err) } decoder := json.NewDecoder(f) err = decoder.Decode(&logTemperatures) if err != nil { return fmt.Errorf("Can not decode temperatures from reader") } f.Close() } // append new temperatures with existing temperatures from logfile logTemperatures = append(logTemperatures, temperatures...) // overwrite lofgile with new entries f, err := os.Create(logfile) if err != nil { return fmt.Errorf("Can not open file %v: %v", logfile, err) } defer f.Close() // encode temperatures encoder := json.NewEncoder(f) encoder.SetIndent("", " ") err = encoder.Encode(logTemperatures) if err != nil { return fmt.Errorf("Can not encode temperatures for the writer") } } return nil } func (fl *FileLogger) RemoveLogFile(logfile string) error { for i, logfiles := range fl.LogFiles { cachedLogfiles := []string{} for _, lf := range logfiles { if lf == logfile { continue } cachedLogfiles = append(cachedLogfiles, lf) } fl.LogFiles[i] = cachedLogfiles } if _, err := os.Stat(logfile); err == nil { err := os.Remove(logfile) if err != nil { return fmt.Errorf("Can not remote file %v: %v", logfile, err) } } return nil } func NewFileLogger(w io.Writer) *FileLogger { return &FileLogger{ writer: w, } }