/
install.go
54 lines (49 loc) · 1.74 KB
/
install.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package api
import (
"encoding/json"
"io/ioutil"
"net/http"
"strings"
"github.com/energieip/common-components-go/pkg/duser"
"github.com/energieip/common-components-go/pkg/tools"
"github.com/energieip/srv200-coreservice-go/internal/core"
"github.com/energieip/srv200-coreservice-go/internal/database"
)
func (api *API) installDriver(w http.ResponseWriter, req *http.Request) {
w.Header().Set("Connection", "close")
defer req.Body.Close()
if api.hasAccessMode(w, req, []string{duser.PriviledgeAdmin, duser.PriviledgeMaintainer}) != nil {
api.sendError(w, APIErrorUnauthorized, "Unauthorized Access", http.StatusUnauthorized)
return
}
body, err := ioutil.ReadAll(req.Body)
if err != nil {
api.sendError(w, APIErrorBodyParsing, "Error reading request body", http.StatusInternalServerError)
return
}
driver := core.InstallDriver{}
err = json.Unmarshal(body, &driver)
if err != nil {
api.sendError(w, APIErrorBodyParsing, "Could not parse input format "+err.Error(), http.StatusInternalServerError)
return
}
driver.Mac = strings.ToUpper(driver.Mac)
driver.Device = strings.ToUpper(driver.Device)
driver.Label = strings.Replace(driver.Label, "-", "_", -1)
savedProject, _ := database.GetProject(api.db, driver.Label)
if savedProject == nil {
api.sendError(w, APIErrorDeviceNotFound, "Unknow label "+driver.Label, http.StatusInternalServerError)
return
}
if savedProject.ModelName != nil {
refModel := tools.Model2Type(*savedProject.ModelName)
if refModel != driver.Device {
api.sendError(w, APIErrorDeviceNotFound, "Unexpected Driver, expected "+refModel, http.StatusInternalServerError)
return
}
}
event := make(map[string]interface{})
event["installDriver"] = driver
api.EventsToBackend <- event
w.Write([]byte("{}"))
}