// NewDiscovergyFromConfig creates a new configurable meter
NewDiscovergyFromConfig creates a new configurable meter
// NewDiscovergyFromConfig creates a new configurable meter
func NewDiscovergyFromConfig(other map[string]interface{}) (api.Meter, error) {
	cc := struct {
		User     string
		Password string
		Meter    string
		Scale    float64
		Cache    time.Duration
	}{
		Cache: time.Second,
	}
	if err := util.DecodeOther(other, &cc); err != nil {
		return nil, err
	}
	basicAuth := transport.BasicAuthHeader(cc.User, cc.Password)
	log := util.NewLogger("discgy").Redact(cc.User, cc.Password, cc.Meter, basicAuth)
	client := request.NewHelper(log)
	client.Transport = transport.BasicAuth(cc.User, cc.Password, client.Transport)
	var meters []discovergy.Meter
	if err := client.GetJSON(fmt.Sprintf("%s/meters", discovergy.API), &meters); err != nil {
		return nil, err
	}
	var meterID string
	if cc.Meter != "" {
		for _, m := range meters {
			if matchesIdentifier(cc.Meter, m) {
				meterID = m.MeterID
				break
			}
		}
	} else if len(meters) == 1 {
		meterID = meters[0].MeterID
	}
	if meterID == "" {
		return nil, fmt.Errorf("could not determine meter id: %v", funk.Map(meters, func(m discovergy.Meter) string {
			return m.FullSerialNumber
		}))
	}
	dataG := provider.NewCached(func() (interface{}, error) {
		uri := fmt.Sprintf("%s/last_reading?meterId=%s", discovergy.API, meterID)
		var res discovergy.Reading
		err := client.GetJSON(uri, &res)
		return res, err
	}, cc.Cache).InterfaceGetter()
	m := &Discovergy{
		dataG: dataG,
		scale: cc.Scale,
	}
	return m, nil
}
func NewDiscovergyFromConfig ( other map [ string ] interface { } ) ( api . Meter , error ) { cc := struct { User string Password string Meter string Scale float64 Cache time . Duration } { Cache : time . Second , } if err := util . DecodeOther ( other , & cc ) ; err != nil { return nil , err } basicAuth := transport . BasicAuthHeader ( cc . User , cc . Password ) log := util . NewLogger ( " " ) . Redact ( cc . User , cc . Password , cc . Meter , basicAuth ) client := request . NewHelper ( log ) client . Transport = transport . BasicAuth ( cc . User , cc . Password , client . Transport ) var meters [ ] discovergy . Meter if err := client . GetJSON ( fmt . Sprintf ( " " , discovergy . API ) , & meters ) ; err != nil { return nil , err } var meterID string if cc . Meter != " " { for _ , m := range meters { if matchesIdentifier ( cc . Meter , m ) { meterID = m . MeterID break } } } else if len ( meters ) == 1 { meterID = meters [ 0 ] . MeterID } if meterID == " " { return nil , fmt . Errorf ( " " , funk . Map ( meters , func ( m discovergy . Meter ) string { return m . FullSerialNumber } ) ) } dataG := provider . NewCached ( func ( ) ( interface { } , error ) { uri := fmt . Sprintf ( " " , discovergy . API , meterID ) var res discovergy . Reading err := client . GetJSON ( uri , & res ) return res , err } , cc . Cache ) . InterfaceGetter ( ) m := & Discovergy { dataG : dataG , scale : cc . Scale , } return m , nil }
NewDiscovergyFromConfig creates a new configurable meter
NewDiscovergyFromConfig creates a new configurable meter