-
Notifications
You must be signed in to change notification settings - Fork 0
/
devicecreate.go
82 lines (71 loc) · 2.29 KB
/
devicecreate.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
/**
* Created by GoLand.
* User: 姜伟
* Date: 2020/2/2 0002
* Time: 9:29
*/
package device
import (
"regexp"
"github.com/a07061625/gompf/mpf"
"github.com/a07061625/gompf/mpf/api/mpiot"
"github.com/a07061625/gompf/mpf/mpconstant/errorcode"
"github.com/a07061625/gompf/mpf/mpconstant/project"
"github.com/a07061625/gompf/mpf/mperr"
"github.com/valyala/fasthttp"
)
// 创建单个设备
type deviceCreate struct {
mpiot.BaseBaiDu
deviceName string // 设备名称
deviceDesc string // 描述
schemaId string // 物模型ID
}
func (dc *deviceCreate) SetDeviceName(deviceName string) {
match, _ := regexp.MatchString(project.RegexDigitAlpha, deviceName)
if match {
dc.deviceName = deviceName
} else {
panic(mperr.NewIotBaiDu(errorcode.IotBaiDuParam, "设备名称不合法", nil))
}
}
func (dc *deviceCreate) SetDeviceDesc(deviceDesc string) {
if len(deviceDesc) > 0 {
dc.deviceDesc = deviceDesc
} else {
panic(mperr.NewIotBaiDu(errorcode.IotBaiDuParam, "描述不合法", nil))
}
}
func (dc *deviceCreate) SetSchemaId(schemaId string) {
if len(schemaId) > 0 {
dc.schemaId = schemaId
} else {
panic(mperr.NewIotBaiDu(errorcode.IotBaiDuParam, "物模型ID不合法", nil))
}
}
func (dc *deviceCreate) CheckData() (*fasthttp.Client, *fasthttp.Request) {
if len(dc.deviceName) == 0 {
panic(mperr.NewIotBaiDu(errorcode.IotBaiDuParam, "设备名称不能为空", nil))
}
if len(dc.deviceDesc) == 0 {
panic(mperr.NewIotBaiDu(errorcode.IotBaiDuParam, "描述不能为空", nil))
}
if len(dc.schemaId) == 0 {
panic(mperr.NewIotBaiDu(errorcode.IotBaiDuParam, "物模型ID不能为空", nil))
}
dc.ExtendData["deviceName"] = dc.deviceName
dc.ExtendData["description"] = dc.deviceDesc
dc.ExtendData["schemaId"] = dc.schemaId
dc.ReqURI = dc.GetServiceUrl()
reqBody := mpf.JSONMarshal(dc.ExtendData)
client, req := dc.GetRequest()
req.SetBody([]byte(reqBody))
return client, req
}
func NewDeviceCreate() *deviceCreate {
dc := &deviceCreate{mpiot.NewBaseBaiDu(), "", "", ""}
dc.ServiceUri = "/v3/iot/management/device"
dc.ReqContentType = project.HTTPContentTypeJSON
dc.ReqMethod = fasthttp.MethodPost
return dc
}