예제 #1
0
// Update PvData
func updatePvData(client *http.Client,
	initiateData *dataproviders.InitiateData,
	pv *dataproviders.PvData) error {

	log.Debug("Fetching update ...")
	url := fmt.Sprintf(MonthUrl, initiateData.PlantNo, time.Now().Format(requestDateFormat))
	resp, err := client.Get(url)
	if err != nil {
		log.Infof("Error in fetching from %s:%s", url, err.Error())
		return err
	}
	defer resp.Body.Close()
	if resp.StatusCode != 200 {
		err = fmt.Errorf("Dataprovider suntrol fail. Received http status %d from server", resp.StatusCode)
		log.Infof("%s", err.Error())
		return err
	}
	b, _ := ioutil.ReadAll(resp.Body)
	log.Tracef("Received body from server: %s", b)

	chartData := chartData{}
	err = json.Unmarshal(b, &chartData)
	if err != nil {
		log.Infof("Error in umashalling json %s", err.Error())
		return err
	}

	log.Tracef("Unmashaled charData is %s", chartData)

	pv.EnergyToday = uint16(chartData.DataPart[time.Now().Day()-1].Value * 1000)

	log.Tracef("pv is now %s", pv)

	return nil
}
예제 #2
0
func etoday(sid string, client *http.Client,
	initiateData *dataproviders.InitiateData, pv *dataproviders.PvData, resp *[]byte) error {

	reg, err := regexp.Compile(etodayRegEx)
	if err != nil {
		log.Fail(err.Error())
		return err
	}
	foundPart1 := reg.Find(*resp)
	if len(foundPart1) < 6 {
		err := fmt.Errorf("Could not find etoday in response from inverter")
		return err
	}

	reg, err = regexp.Compile(numberRegEx)
	if err != nil {
		log.Fail(err.Error())
	}
	foundPart2 := reg.Find(foundPart1)
	if len(foundPart2) < 2 {
		err = fmt.Errorf("Could not find etoday in response from inverter")
		return err
	}

	etoday := string(foundPart2[1:])
	log.Debugf("Current etoday is %s", etoday)
	etodayfloat, err := strconv.ParseFloat(etoday, 64)

	// Are the value in kW or W?
	factor := 1.0
	if string(foundPart1[len(foundPart1)-1:len(foundPart1)]) == "k" {
		factor = 1000.0
	}
	pv.EnergyToday = uint16(etodayfloat * factor)
	return nil
}