/
transmit.go
58 lines (45 loc) · 1.27 KB
/
transmit.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
55
56
57
58
package handler
import (
"net/http"
"os/exec"
"unsafe"
"github.com/labstack/echo"
myMw "github.com/k-tahiro/tahiremogon/middleware"
"github.com/k-tahiro/tahiremogon/model"
"github.com/k-tahiro/tahiremogon/util"
)
func TransmitCode(c echo.Context) error {
cc := c.(*myMw.CustomContext)
key := cc.Param("key")
var code string
cc.Session.Select("code").From("codes").Where("key = ?", key).Load(&code)
if code == "" {
return echo.NewHTTPError(http.StatusBadRequest, "Command Undefined")
}
cmd := "sudo /usr/local/bin/bto_ir_cmd -e -t" + " " + code
if _, err := exec.Command("sh", "-c", cmd).Output(); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, err)
}
response := &model.TransmitResponse{
Sucess: true,
Label: -1,
}
predictionModel := cc.PredictionModel
if predictionModel != nil {
label, _ := confirm(predictionModel)
response.Label = label
}
return cc.JSON(http.StatusOK, response)
}
func confirm(predictionModel *myMw.PredictionModel) (int, error) {
cmd := "sudo /usr/local/bin/camera.sh"
filename, err := exec.Command("sh", "-c", cmd).Output()
if err != nil {
return -1, err
}
input, err := util.ReadImage(*(*string)(unsafe.Pointer(&filename)))
if err != nil {
return -1, err
}
return predictionModel.Predict(input)
}