Commit bc8c58c3cb4d5bb90dd756de019d96c733174005
Committed by
Andrew Shvayka
1 parent
1b391847
ResourceValue is serializable
Showing
7 changed files
with
190 additions
and
9 deletions
... | ... | @@ -29,7 +29,6 @@ import org.eclipse.leshan.core.node.codec.LwM2mValueConverter; |
29 | 29 | import org.eclipse.leshan.core.request.ContentFormat; |
30 | 30 | import org.eclipse.leshan.server.model.LwM2mModelProvider; |
31 | 31 | import org.eclipse.leshan.server.registration.Registration; |
32 | -import org.eclipse.leshan.server.security.SecurityInfo; | |
33 | 32 | import org.thingsboard.server.common.data.Device; |
34 | 33 | import org.thingsboard.server.common.data.DeviceProfile; |
35 | 34 | import org.thingsboard.server.common.data.device.data.Lwm2mDeviceTransportConfiguration; |
... | ... | @@ -39,6 +38,8 @@ import org.thingsboard.server.common.transport.auth.ValidateDeviceCredentialsRes |
39 | 38 | import org.thingsboard.server.gen.transport.TransportProtos.SessionInfoProto; |
40 | 39 | import org.thingsboard.server.gen.transport.TransportProtos.TsKvProto; |
41 | 40 | |
41 | +import java.io.IOException; | |
42 | +import java.io.ObjectInputStream; | |
42 | 43 | import java.io.Serializable; |
43 | 44 | import java.util.Collection; |
44 | 45 | import java.util.Map; |
... | ... | @@ -66,10 +67,10 @@ public class LwM2mClient implements Serializable { |
66 | 67 | @Getter |
67 | 68 | private final String endpoint; |
68 | 69 | |
69 | - private transient final Lock lock = new ReentrantLock(); | |
70 | - //TODO: define custom serialization of those fields. | |
70 | + private transient Lock lock; | |
71 | + | |
71 | 72 | @Getter |
72 | - private transient final Map<String, ResourceValue> resources; | |
73 | + private final Map<String, ResourceValue> resources; | |
73 | 74 | @Getter |
74 | 75 | private final Map<String, TsKvProto> sharedAttributes; |
75 | 76 | |
... | ... | @@ -100,6 +101,7 @@ public class LwM2mClient implements Serializable { |
100 | 101 | this.sharedAttributes = new ConcurrentHashMap<>(); |
101 | 102 | this.resources = new ConcurrentHashMap<>(); |
102 | 103 | this.state = LwM2MClientState.CREATED; |
104 | + this.lock = new ReentrantLock(); | |
103 | 105 | } |
104 | 106 | |
105 | 107 | public void init(ValidateDeviceCredentialsResponse credentials, UUID sessionId) { |
... | ... | @@ -353,5 +355,10 @@ public class LwM2mClient implements Serializable { |
353 | 355 | } |
354 | 356 | } |
355 | 357 | |
358 | + private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException { | |
359 | + in.defaultReadObject(); | |
360 | + this.lock = new ReentrantLock(); | |
361 | + } | |
362 | + | |
356 | 363 | } |
357 | 364 | ... | ... |
... | ... | @@ -18,14 +18,46 @@ package org.thingsboard.server.transport.lwm2m.server.client; |
18 | 18 | import lombok.Data; |
19 | 19 | import org.eclipse.leshan.core.model.ResourceModel; |
20 | 20 | import org.eclipse.leshan.core.node.LwM2mResource; |
21 | +import org.eclipse.leshan.core.node.LwM2mResourceInstance; | |
22 | + | |
23 | +import java.io.Serializable; | |
21 | 24 | |
22 | 25 | @Data |
23 | -public class ResourceValue { | |
24 | - private LwM2mResource lwM2mResource; | |
25 | - private ResourceModel resourceModel; | |
26 | +public class ResourceValue implements Serializable { | |
27 | + | |
28 | + private static final long serialVersionUID = -228268906779089402L; | |
29 | + | |
30 | + private TbLwM2MResource lwM2mResource; | |
31 | + private TbResourceModel resourceModel; | |
26 | 32 | |
27 | 33 | public ResourceValue(LwM2mResource lwM2mResource, ResourceModel resourceModel) { |
28 | - this.lwM2mResource = lwM2mResource; | |
29 | - this.resourceModel = resourceModel; | |
34 | + this.lwM2mResource = toTbLwM2MResource(lwM2mResource); | |
35 | + this.resourceModel = toTbResourceModel(resourceModel); | |
36 | + } | |
37 | + | |
38 | + public void setLwM2mResource(LwM2mResource lwM2mResource) { | |
39 | + this.lwM2mResource = toTbLwM2MResource(lwM2mResource); | |
40 | + } | |
41 | + | |
42 | + public void setResourceModel(ResourceModel resourceModel) { | |
43 | + this.resourceModel = toTbResourceModel(resourceModel); | |
44 | + } | |
45 | + | |
46 | + private static TbLwM2MResource toTbLwM2MResource(LwM2mResource lwM2mResource) { | |
47 | + if (lwM2mResource.isMultiInstances()) { | |
48 | + TbLwM2MResourceInstance[] instances = (TbLwM2MResourceInstance[]) lwM2mResource.getInstances().values().stream().map(ResourceValue::toTbLwM2MResourceInstance).toArray(); | |
49 | + return new TbLwM2MMultipleResource(lwM2mResource.getId(), lwM2mResource.getType(), instances); | |
50 | + } else { | |
51 | + return new TbLwM2MSingleResource(lwM2mResource.getId(), lwM2mResource.getValue(), lwM2mResource.getType()); | |
52 | + } | |
53 | + } | |
54 | + | |
55 | + private static TbLwM2MResourceInstance toTbLwM2MResourceInstance(LwM2mResourceInstance instance) { | |
56 | + return new TbLwM2MResourceInstance(instance.getId(), instance.getValue(), instance.getType()); | |
57 | + } | |
58 | + | |
59 | + private static TbResourceModel toTbResourceModel(ResourceModel resourceModel) { | |
60 | + return new TbResourceModel(resourceModel.id, resourceModel.name, resourceModel.operations, resourceModel.multiple, | |
61 | + resourceModel.mandatory, resourceModel.type, resourceModel.rangeEnumeration, resourceModel.units, resourceModel.description); | |
30 | 62 | } |
31 | 63 | } | ... | ... |
1 | +/** | |
2 | + * Copyright © 2016-2021 The Thingsboard Authors | |
3 | + * | |
4 | + * Licensed under the Apache License, Version 2.0 (the "License"); | |
5 | + * you may not use this file except in compliance with the License. | |
6 | + * You may obtain a copy of the License at | |
7 | + * | |
8 | + * http://www.apache.org/licenses/LICENSE-2.0 | |
9 | + * | |
10 | + * Unless required by applicable law or agreed to in writing, software | |
11 | + * distributed under the License is distributed on an "AS IS" BASIS, | |
12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
13 | + * See the License for the specific language governing permissions and | |
14 | + * limitations under the License. | |
15 | + */ | |
16 | +package org.thingsboard.server.transport.lwm2m.server.client; | |
17 | + | |
18 | +import org.eclipse.leshan.core.model.ResourceModel; | |
19 | +import org.eclipse.leshan.core.node.LwM2mMultipleResource; | |
20 | +import org.eclipse.leshan.core.node.LwM2mResourceInstance; | |
21 | + | |
22 | +import java.io.Serializable; | |
23 | +import java.util.Collection; | |
24 | + | |
25 | +public class TbLwM2MMultipleResource extends LwM2mMultipleResource implements TbLwM2MResource, Serializable { | |
26 | + | |
27 | + private static final long serialVersionUID = 4658477128628087186L; | |
28 | + | |
29 | + public TbLwM2MMultipleResource(int id, ResourceModel.Type type, TbLwM2MResourceInstance... instances) { | |
30 | + super(id, type, instances); | |
31 | + } | |
32 | +} | ... | ... |
1 | +/** | |
2 | + * Copyright © 2016-2021 The Thingsboard Authors | |
3 | + * | |
4 | + * Licensed under the Apache License, Version 2.0 (the "License"); | |
5 | + * you may not use this file except in compliance with the License. | |
6 | + * You may obtain a copy of the License at | |
7 | + * | |
8 | + * http://www.apache.org/licenses/LICENSE-2.0 | |
9 | + * | |
10 | + * Unless required by applicable law or agreed to in writing, software | |
11 | + * distributed under the License is distributed on an "AS IS" BASIS, | |
12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
13 | + * See the License for the specific language governing permissions and | |
14 | + * limitations under the License. | |
15 | + */ | |
16 | +package org.thingsboard.server.transport.lwm2m.server.client; | |
17 | + | |
18 | +import org.eclipse.leshan.core.node.LwM2mResource; | |
19 | + | |
20 | +public interface TbLwM2MResource extends LwM2mResource { | |
21 | +} | ... | ... |
1 | +/** | |
2 | + * Copyright © 2016-2021 The Thingsboard Authors | |
3 | + * | |
4 | + * Licensed under the Apache License, Version 2.0 (the "License"); | |
5 | + * you may not use this file except in compliance with the License. | |
6 | + * You may obtain a copy of the License at | |
7 | + * | |
8 | + * http://www.apache.org/licenses/LICENSE-2.0 | |
9 | + * | |
10 | + * Unless required by applicable law or agreed to in writing, software | |
11 | + * distributed under the License is distributed on an "AS IS" BASIS, | |
12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
13 | + * See the License for the specific language governing permissions and | |
14 | + * limitations under the License. | |
15 | + */ | |
16 | +package org.thingsboard.server.transport.lwm2m.server.client; | |
17 | + | |
18 | +import org.eclipse.leshan.core.model.ResourceModel; | |
19 | +import org.eclipse.leshan.core.node.LwM2mResourceInstance; | |
20 | + | |
21 | +import java.io.Serializable; | |
22 | + | |
23 | +public class TbLwM2MResourceInstance extends LwM2mResourceInstance implements Serializable { | |
24 | + | |
25 | + private static final long serialVersionUID = -8322290426892538345L; | |
26 | + | |
27 | + protected TbLwM2MResourceInstance(int id, Object value, ResourceModel.Type type) { | |
28 | + super(id, value, type); | |
29 | + } | |
30 | +} | ... | ... |
1 | +/** | |
2 | + * Copyright © 2016-2021 The Thingsboard Authors | |
3 | + * | |
4 | + * Licensed under the Apache License, Version 2.0 (the "License"); | |
5 | + * you may not use this file except in compliance with the License. | |
6 | + * You may obtain a copy of the License at | |
7 | + * | |
8 | + * http://www.apache.org/licenses/LICENSE-2.0 | |
9 | + * | |
10 | + * Unless required by applicable law or agreed to in writing, software | |
11 | + * distributed under the License is distributed on an "AS IS" BASIS, | |
12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
13 | + * See the License for the specific language governing permissions and | |
14 | + * limitations under the License. | |
15 | + */ | |
16 | +package org.thingsboard.server.transport.lwm2m.server.client; | |
17 | + | |
18 | +import org.eclipse.leshan.core.model.ResourceModel; | |
19 | +import org.eclipse.leshan.core.node.LwM2mSingleResource; | |
20 | + | |
21 | +import java.io.Serializable; | |
22 | + | |
23 | +public class TbLwM2MSingleResource extends LwM2mSingleResource implements TbLwM2MResource, Serializable { | |
24 | + | |
25 | + private static final long serialVersionUID = -878078368245340809L; | |
26 | + | |
27 | + public TbLwM2MSingleResource(int id, Object value, ResourceModel.Type type) { | |
28 | + super(id, value, type); | |
29 | + } | |
30 | +} | ... | ... |
1 | +/** | |
2 | + * Copyright © 2016-2021 The Thingsboard Authors | |
3 | + * | |
4 | + * Licensed under the Apache License, Version 2.0 (the "License"); | |
5 | + * you may not use this file except in compliance with the License. | |
6 | + * You may obtain a copy of the License at | |
7 | + * | |
8 | + * http://www.apache.org/licenses/LICENSE-2.0 | |
9 | + * | |
10 | + * Unless required by applicable law or agreed to in writing, software | |
11 | + * distributed under the License is distributed on an "AS IS" BASIS, | |
12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
13 | + * See the License for the specific language governing permissions and | |
14 | + * limitations under the License. | |
15 | + */ | |
16 | +package org.thingsboard.server.transport.lwm2m.server.client; | |
17 | + | |
18 | +import org.eclipse.leshan.core.model.ResourceModel; | |
19 | + | |
20 | +import java.io.Serializable; | |
21 | + | |
22 | +public class TbResourceModel extends ResourceModel implements Serializable { | |
23 | + | |
24 | + private static final long serialVersionUID = -2082846558899793932L; | |
25 | + | |
26 | + public TbResourceModel(Integer id, String name, Operations operations, Boolean multiple, Boolean mandatory, Type type, String rangeEnumeration, String units, String description) { | |
27 | + super(id, name, operations, multiple, mandatory, type, rangeEnumeration, units, description); | |
28 | + } | |
29 | +} | ... | ... |