staticPlanPreviewHandler returns a plan preview for given parameters
(lp loadpoint.API)
| 54 | |
| 55 | // staticPlanPreviewHandler returns a plan preview for given parameters |
| 56 | func staticPlanPreviewHandler(lp loadpoint.API) http.HandlerFunc { |
| 57 | return func(w http.ResponseWriter, r *http.Request) { |
| 58 | vars := mux.Vars(r) |
| 59 | |
| 60 | planTime, err := time.ParseInLocation(time.RFC3339, vars["time"], nil) |
| 61 | if err != nil { |
| 62 | jsonError(w, http.StatusBadRequest, err) |
| 63 | return |
| 64 | } |
| 65 | |
| 66 | goal, err := strconv.ParseFloat(vars["value"], 64) |
| 67 | if err != nil { |
| 68 | jsonError(w, http.StatusBadRequest, err) |
| 69 | return |
| 70 | } |
| 71 | |
| 72 | switch typ := vars["type"]; typ { |
| 73 | case "soc": |
| 74 | if !lp.SocBasedPlanning() { |
| 75 | jsonError(w, http.StatusBadRequest, errors.New("soc planning only available for vehicles with known soc and capacity")) |
| 76 | return |
| 77 | } |
| 78 | case "energy": |
| 79 | if lp.SocBasedPlanning() { |
| 80 | jsonError(w, http.StatusBadRequest, errors.New("energy planning not available for vehicles with known soc and capacity")) |
| 81 | return |
| 82 | } |
| 83 | default: |
| 84 | jsonError(w, http.StatusBadRequest, fmt.Errorf("invalid plan type: %s", typ)) |
| 85 | return |
| 86 | } |
| 87 | |
| 88 | maxPower := lp.EffectiveMaxPower() |
| 89 | requiredDuration := lp.GetPlanRequiredDuration(goal, maxPower) |
| 90 | strategy := lp.EffectivePlanStrategy() |
| 91 | |
| 92 | plan := lp.GetPlan(planTime, requiredDuration, strategy.Precondition, strategy.Continuous) |
| 93 | |
| 94 | res := PlanPreviewResponse{ |
| 95 | PlanTime: planTime, |
| 96 | Duration: int64(requiredDuration.Seconds()), |
| 97 | Plan: plan, |
| 98 | Power: maxPower, |
| 99 | } |
| 100 | |
| 101 | jsonWrite(w, res) |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | // planEnergyHandler updates plan energy and time |
| 106 | func planEnergyHandler(lp loadpoint.API) http.HandlerFunc { |
no test coverage detected