-
Notifications
You must be signed in to change notification settings - Fork 82
/
actions.go
120 lines (105 loc) · 3.24 KB
/
actions.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package main
import (
"encoding/hex"
"errors"
"github.com/PandoCloud/pando-cloud/pkg/models"
"github.com/PandoCloud/pando-cloud/pkg/rpcs"
"github.com/PandoCloud/pando-cloud/pkg/server"
"github.com/PandoCloud/pando-cloud/pkg/token"
"github.com/martini-contrib/render"
"math/rand"
"net/http"
)
const (
ErrOK = 0
ErrSystemFault = 10001
ErrDeviceNotFound = 10002
ErrWrongSecret = 10003
ErrProtocolNotSuported = 10004
)
func renderError(code int, err error) Common {
result := Common{}
result.Code = code
result.Message = err.Error()
server.Log.Error(err.Error())
return result
}
// device register args
type DeviceRegisterArgs struct {
ProductKey string `json:"product_key" binding:"required"`
DeviceCode string `json:"device_code" binding:"required"`
Version string `json:"version" binding:"required"`
}
// device authentication args
type DeviceAuthArgs struct {
DeviceId int64 `json:"device_id" binding:"required"`
DeviceSecret string `json:"device_secret" binding:"required"`
Protocol string `json:"protocol" binding:"required"`
}
func RegisterDevice(args DeviceRegisterArgs, r render.Render) {
server.Log.Printf("ACTION RegisterDevice, args:: %v ", args)
rpcargs := &rpcs.ArgsDeviceRegister{
ProductKey: args.ProductKey,
DeviceCode: args.DeviceCode,
DeviceVersion: args.Version,
}
device := &models.Device{}
err := server.RPCCallByName("registry", "Registry.RegisterDevice", rpcargs, device)
if err != nil {
r.JSON(http.StatusOK, renderError(ErrSystemFault, err))
return
}
server.Log.Infof("register device success: %v", device)
result := DeviceRegisterResponse{}
result.Data = DeviceRegisterData{
DeviceId: device.ID,
DeviceSecret: device.DeviceSecret,
DeviceKey: device.DeviceKey,
DeviceIdentifier: device.DeviceIdentifier,
}
r.JSON(http.StatusOK, result)
return
}
func AuthDevice(args DeviceAuthArgs, r render.Render) {
server.Log.Printf("ACTION AuthDevice, args:: %v", args)
device := &models.Device{}
err := server.RPCCallByName("registry", "Registry.FindDeviceById", int64(args.DeviceId), device)
if err != nil {
r.JSON(http.StatusOK, renderError(ErrDeviceNotFound, err))
return
}
if device.DeviceSecret != args.DeviceSecret {
// device secret is wrong.
r.JSON(http.StatusOK, renderError(ErrWrongSecret, errors.New("wrong device secret.")))
return
}
hepler := token.NewHelper(*confRedisHost)
token, err := hepler.GenerateToken(uint64(device.ID))
if err != nil {
r.JSON(http.StatusOK, renderError(ErrSystemFault, err))
return
}
var hosts []string
switch args.Protocol {
case "http":
hosts, err = server.GetServerHosts(args.Protocol+"access", "httphost")
case "mqtt":
hosts, err = server.GetServerHosts(args.Protocol+"access", "tcphost")
default:
err = errors.New("unsuported protocol: " + args.Protocol)
}
if err != nil {
r.JSON(http.StatusOK, renderError(ErrProtocolNotSuported, err))
return
}
// just get a random host
host := hosts[rand.Intn(len(hosts))]
result := DeviceAuthResponse{}
result.Data = DeviceAuthData{
AccessToken: hex.EncodeToString(token),
AccessAddr: host,
}
server.Log.Infof("auth device success: %v, token: %x, access: %v", device, token, host)
r.JSON(http.StatusOK, result)
return
}