|
1 | 1 | package thingDescription |
2 | 2 |
|
3 | 3 | import ( |
4 | | - "net/url" |
5 | 4 | "reflect" |
6 | 5 | "sync/atomic" |
7 | 6 |
|
8 | | - "github.com/fredbi/uri" |
9 | 7 | "github.com/google/uuid" |
10 | 8 | "github.com/plgd-dev/device/v2/bridge/net" |
11 | 9 | "github.com/plgd-dev/device/v2/bridge/resources" |
12 | 10 | "github.com/plgd-dev/device/v2/pkg/eventloop" |
13 | 11 | "github.com/plgd-dev/device/v2/schema" |
14 | | - "github.com/plgd-dev/go-coap/v3/message" |
15 | 12 | "github.com/plgd-dev/go-coap/v3/message/pool" |
16 | 13 | "github.com/plgd-dev/go-coap/v3/pkg/sync" |
17 | 14 | "github.com/web-of-things-open-source/thingdescription-go/thingDescription" |
18 | 15 | ) |
19 | 16 |
|
20 | | -var ( |
21 | | - SecurityNoSec = "nosec_sc" |
22 | | - SecurityDefinitions = map[string]thingDescription.SecurityScheme{ |
23 | | - SecurityNoSec: { |
24 | | - Scheme: "nosec", |
25 | | - }, |
26 | | - } |
27 | | - HTTPSWWWW3Org2022WotTdV11 = thingDescription.HTTPSWWWW3Org2022WotTdV11 |
28 | | - Context = thingDescription.ThingContext{ |
29 | | - Enum: &HTTPSWWWW3Org2022WotTdV11, |
30 | | - } |
31 | | -) |
32 | | - |
33 | 17 | // Resource to avoid import cycle also it is same as in Device package to avoid wrapping it |
34 | 18 | type Resource = interface { |
35 | 19 | Close() |
@@ -117,115 +101,3 @@ func (t *Manager) NotifySubscriptions(td thingDescription.ThingDescription) { |
117 | 101 | default: |
118 | 102 | } |
119 | 103 | } |
120 | | - |
121 | | -func supportedOperationToTDOperation(ops resources.SupportedOperation) []string { |
122 | | - tdOps := make([]string, 0, 3) |
123 | | - if ops.HasOperation(resources.SupportedOperationRead) { |
124 | | - tdOps = append(tdOps, string(thingDescription.Readproperty)) |
125 | | - } |
126 | | - if ops.HasOperation(resources.SupportedOperationWrite) { |
127 | | - tdOps = append(tdOps, string(thingDescription.Writeproperty)) |
128 | | - } |
129 | | - if ops.HasOperation(resources.SupportedOperationObserve) { |
130 | | - tdOps = append(tdOps, string(thingDescription.Observeproperty), string(thingDescription.Unobserveproperty)) |
131 | | - } |
132 | | - if len(tdOps) == 0 { |
133 | | - return nil |
134 | | - } |
135 | | - return tdOps |
136 | | -} |
137 | | - |
138 | | -func boolToPtr(v bool) *bool { |
139 | | - if !v { |
140 | | - return nil |
141 | | - } |
142 | | - return &v |
143 | | -} |
144 | | - |
145 | | -func stringToPtr(v string) *string { |
146 | | - if v == "" { |
147 | | - return nil |
148 | | - } |
149 | | - return &v |
150 | | -} |
151 | | - |
152 | | -func createForms(deviceID uuid.UUID, href string, supportedOperations resources.SupportedOperation, setForm bool) []thingDescription.FormElementProperty { |
153 | | - if !setForm { |
154 | | - return nil |
155 | | - } |
156 | | - ops := supportedOperationToTDOperation(supportedOperations) |
157 | | - if len(ops) > 0 { |
158 | | - hrefStr := href |
159 | | - if deviceID != uuid.Nil { |
160 | | - hrefStr += "?di=" + deviceID.String() |
161 | | - } |
162 | | - href, err := url.Parse(hrefStr) |
163 | | - if err == nil { |
164 | | - return []thingDescription.FormElementProperty{ |
165 | | - { |
166 | | - ContentType: stringToPtr(message.AppCBOR.String()), |
167 | | - Op: &thingDescription.FormElementPropertyOp{ |
168 | | - StringArray: ops, |
169 | | - }, |
170 | | - Href: *href, |
171 | | - }, |
172 | | - } |
173 | | - } |
174 | | - } |
175 | | - return nil |
176 | | -} |
177 | | - |
178 | | -func PatchPropertyElement(prop thingDescription.PropertyElement, deviceID uuid.UUID, resource Resource, setForm bool) thingDescription.PropertyElement { |
179 | | - ops := resource.SupportsOperations() |
180 | | - observable := ops.HasOperation(resources.SupportedOperationObserve) |
181 | | - isReadOnly := ops.HasOperation(resources.SupportedOperationRead) && !ops.HasOperation(resources.SupportedOperationWrite) |
182 | | - isWriteOnly := ops.HasOperation(resources.SupportedOperationWrite) && !ops.HasOperation(resources.SupportedOperationRead) |
183 | | - resourceTypes := resource.GetResourceTypes() |
184 | | - |
185 | | - prop.Type = &thingDescription.TypeDeclaration{ |
186 | | - StringArray: resourceTypes, |
187 | | - } |
188 | | - prop.Observable = boolToPtr(observable) |
189 | | - prop.ReadOnly = boolToPtr(isReadOnly) |
190 | | - prop.WriteOnly = boolToPtr(isWriteOnly) |
191 | | - prop.Observable = boolToPtr(observable) |
192 | | - prop.Forms = createForms(deviceID, resource.GetHref(), ops, setForm) |
193 | | - return prop |
194 | | -} |
195 | | - |
196 | | -func PatchThingDescription(td thingDescription.ThingDescription, device Device, endpoint string, getPropertyElement func(resourceHref string, resource Resource) (thingDescription.PropertyElement, bool)) thingDescription.ThingDescription { |
197 | | - if td.Context == nil { |
198 | | - td.Context = &Context |
199 | | - } |
200 | | - id, err := uri.Parse("urn:uuid:" + device.GetID().String()) |
201 | | - if err == nil { |
202 | | - td.ID = id |
203 | | - } |
204 | | - td.Title = device.GetName() |
205 | | - if endpoint != "" { |
206 | | - // base |
207 | | - u, err := url.Parse(endpoint) |
208 | | - if err == nil { |
209 | | - td.Base = *u |
210 | | - } |
211 | | - // security |
212 | | - td.Security = &thingDescription.TypeDeclaration{ |
213 | | - String: &SecurityNoSec, |
214 | | - } |
215 | | - // securityDefinitions |
216 | | - td.SecurityDefinitions = SecurityDefinitions |
217 | | - } |
218 | | - |
219 | | - device.Range(func(resourceHref string, resource Resource) bool { |
220 | | - pe, ok := getPropertyElement(resourceHref, resource) |
221 | | - if !ok { |
222 | | - return true |
223 | | - } |
224 | | - if td.Properties == nil { |
225 | | - td.Properties = make(map[string]thingDescription.PropertyElement) |
226 | | - } |
227 | | - td.Properties[resourceHref] = pe |
228 | | - return true |
229 | | - }) |
230 | | - return td |
231 | | -} |
0 commit comments