forked from edgexfoundry/edgex-go
/
device_profile.go
104 lines (95 loc) · 4.21 KB
/
device_profile.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
/*******************************************************************************
* Copyright 2017 Dell Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*******************************************************************************/
package bolt
import (
"github.com/jduranf/edgex-go/pkg/models"
jsoniter "github.com/json-iterator/go"
)
// Internal version of the device service struct
// Use this to handle DB references
type boltDeviceProfile struct {
models.DeviceProfile
}
// Custom marshaling into bolt
func (bdp boltDeviceProfile) MarshalJSON() ([]byte, error) {
// Get the commands from the device profile and turn them into DBRef objects
var commands []string
for _, command := range bdp.DeviceProfile.Commands {
commands = append(commands, command.Id)
}
json := jsoniter.ConfigCompatibleWithStandardLibrary
return json.Marshal(&struct {
models.DescribedObject `json:",inline"`
Id string `json:"id"`
Name string `json:"name"` // Non-database identifier (must be unique)
Manufacturer string `json:"manufacturer"` // Manufacturer of the device
Model string `json:"model"` // Model of the device
Labels []string `json:"labels,omitempty"` // Labels used to search for groups of profiles
DeviceResources []models.DeviceResource `json:"deviceResources,omitempty"`
Resources []models.ProfileResource `json:"resources,omitempty"`
Commands []string `json:"commands"` // List of commands to Get/Put information for devices associated with this profile
}{
DescribedObject: bdp.DescribedObject,
Id: bdp.Id,
Name: bdp.Name,
Manufacturer: bdp.Manufacturer,
Model: bdp.Model,
Labels: bdp.Labels,
DeviceResources: bdp.DeviceResources,
Resources: bdp.Resources,
Commands: commands,
})
}
// Custom unmarshaling out of bolt
func (bdp *boltDeviceProfile) UnmarshalJSON(data []byte) error {
decoded := new(struct {
models.DescribedObject `json:",inline"`
Id string `json:"id"`
Name string `json:"name"` // Non-database identifier (must be unique)
Manufacturer string `json:"manufacturer"` // Manufacturer of the device
Model string `json:"model"` // Model of the device
Labels []string `json:"labels,omitempty"` // Labels used to search for groups of profiles
DeviceResources []models.DeviceResource `json:"deviceResources,omitempty"`
Resources []models.ProfileResource `json:"resources,omitempty"`
Commands []string `json:"commands,omitempty"` // List of commands to Get/Put information for devices associated with this profile
})
json := jsoniter.ConfigCompatibleWithStandardLibrary
if err := json.Unmarshal(data, &decoded); err != nil {
return err
}
// Copy over the fields
bdp.DescribedObject = decoded.DescribedObject
bdp.Id = decoded.Id
bdp.Name = decoded.Name
bdp.Manufacturer = decoded.Manufacturer
bdp.Model = decoded.Model
bdp.Labels = decoded.Labels
bdp.DeviceResources = decoded.DeviceResources
bdp.Resources = decoded.Resources
m, err := getCurrentBoltClient()
if err != nil {
return err
}
// Get all of the commands from the references
var commands []models.Command
for _, cRef := range decoded.Commands {
c, err := m.GetCommandById(cRef)
if err != nil {
return err
}
commands = append(commands, c)
}
bdp.Commands = commands
return nil
}