/
dtu.go
76 lines (60 loc) · 1.7 KB
/
dtu.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package rest
import (
"log"
"fmt"
"encoding/json"
"io/ioutil"
"dtu_service/tcp/client"
"github.com/ant0ine/go-json-rest/rest"
)
type requestSt struct{
Id string `json:"id"`
Msg string `json:"msg"`
}
type responseSt struct{
Id string `json:"id"`
Msg string `json:"msg"`
Status string `json:"status"`
Info string `json:"info"`
}
func dtu_execrawcmd(w rest.ResponseWriter, req *rest.Request){
response := responseSt{Status:"ok"}
var request requestSt
jsonbytes, _ := ioutil.ReadAll(req.Body)
json.Unmarshal(jsonbytes, &request)
log.Printf("request.Id=%s request=%v json=%s\n",request.Id ,request, jsonbytes)
response.Id = request.Id
dtuid := fmt.Sprintf("%X",request.Id)
recvmsg, ok := client.DownloadDeviceMsg(dtuid, request.Msg)
if !ok{
response.Status = "fail"
response.Info = recvmsg
recvmsg = ""
}
response.Msg = recvmsg
w.WriteJson(response)
}
func dtu_execclose(w rest.ResponseWriter, req *rest.Request){
response := responseSt{Status:"ok"}
var request requestSt
jsonbytes, _ := ioutil.ReadAll(req.Body)
json.Unmarshal(jsonbytes, &request)
response.Id = request.Id
dtuid := fmt.Sprintf("%X",request.Id)
info, ok := client.DelDevice(dtuid)
if !ok{
response.Status = "fail"
}
response.Info = info
w.WriteJson(response)
}
func dtu_execcmd(w rest.ResponseWriter, req *rest.Request){
resp := make(map[string]string)
resp["exec func"] = "dtu_execcmd"
w.WriteJson(resp)
}
func dtu_getstatus(w rest.ResponseWriter, req *rest.Request){
resp := make(map[string]string)
resp["exec func"] = "dtu_getstatus"
w.WriteJson(resp)
}