updateDeviceHandler updates database device's configuration by class
(site site.API, authObject auth.Auth)
| 435 | |
| 436 | // updateDeviceHandler updates database device's configuration by class |
| 437 | func updateDeviceHandler(site site.API, authObject auth.Auth) http.HandlerFunc { |
| 438 | return func(w http.ResponseWriter, r *http.Request) { |
| 439 | vars := mux.Vars(r) |
| 440 | |
| 441 | class, err := templates.ClassString(vars["class"]) |
| 442 | if err != nil { |
| 443 | jsonError(w, http.StatusBadRequest, err) |
| 444 | return |
| 445 | } |
| 446 | |
| 447 | id, err := strconv.Atoi(vars["id"]) |
| 448 | if err != nil { |
| 449 | jsonError(w, http.StatusBadRequest, err) |
| 450 | return |
| 451 | } |
| 452 | |
| 453 | req, err := decodeDeviceConfig(r.Body) |
| 454 | if err != nil { |
| 455 | jsonError(w, http.StatusBadRequest, err) |
| 456 | return |
| 457 | } |
| 458 | |
| 459 | if !requireCriticalConfigAuth(w, r, authObject, req) { |
| 460 | return |
| 461 | } |
| 462 | |
| 463 | ctx, cancel, done := startDeviceTimeout() |
| 464 | |
| 465 | force := r.URL.Query().Get("force") == "true" |
| 466 | |
| 467 | switch class { |
| 468 | case templates.Charger: |
| 469 | err = updateDevice(ctx, id, class, req, charger.NewFromConfig, config.Chargers(), force) |
| 470 | |
| 471 | case templates.Meter: |
| 472 | err = updateDevice(ctx, id, class, req, meter.NewFromConfig, config.Meters(), force) |
| 473 | |
| 474 | case templates.Vehicle: |
| 475 | err = updateDevice(ctx, id, class, req, vehicle.NewFromConfig, config.Vehicles(), force) |
| 476 | |
| 477 | case templates.Circuit: |
| 478 | err = updateDevice(ctx, id, class, req, circuit.NewFromConfig, config.Circuits(), force) |
| 479 | |
| 480 | case templates.Hems: |
| 481 | err = updateDevice(ctx, id, class, req, newHemsFactory(site), config.Hems(), force) |
| 482 | |
| 483 | case templates.Tariff: |
| 484 | err = updateDevice(ctx, id, class, req, tariff.NewFromConfig, config.Tariffs(), force) |
| 485 | |
| 486 | case templates.Messenger: |
| 487 | err = updateDevice(ctx, id, class, req, messenger.NewFromConfig, config.Messengers(), force) |
| 488 | } |
| 489 | |
| 490 | setConfigDirty() |
| 491 | |
| 492 | if err != nil { |
| 493 | cancel() |
| 494 | jsonError(w, http.StatusBadRequest, err) |
no test coverage detected