forked from eclipse-ditto/ditto-clients-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworking_with_features.py
78 lines (55 loc) · 2.69 KB
/
working_with_features.py
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
# Copyright (c) 2022 Contributors to the Eclipse Foundation
#
# See the NOTICE file(s) distributed with this work for additional
# information regarding copyright ownership.
#
# This program and the accompanying materials are made available under the
# terms of the Eclipse Public License 2.0 which is available at
# http://www.eclipse.org/legal/epl-2.0
#
# SPDX-License-Identifier: EPL-2.0
from ditto.client import Client
from ditto.model.definition_id import DefinitionID
from ditto.model.feature import Feature
from ditto.model.namespaced_id import NamespacedID
from ditto.protocol.things.commands import Command
thing_id = NamespacedID().from_string("test.ns:test-name")
feature_id = "MyFeatureID"
property_id = "myProperty"
definition_id = DefinitionID().from_string("my.model.namespace:FeatureModel:1.0.0")
def create_or_modify_feature(client):
# Define the feature to be created or how you want it to be after modification
# You can provide a semantic definition of your feature
my_feature = Feature().with_definition(definition_id).with_property(property_id, "myValue")
# Create your Ditto command. Modify acts as an upsert - it either updates or creates features.
command = Command(thing_id).feature(feature_id).twin().modify(my_feature)
# Send the Ditto command.
envelope = command.envelope(response_required=False)
client.send(envelope)
def create_or_modify_feature_property(client):
# Create your Ditto command. Modify acts as an upsert - it either updates or creates feature properties.
command = Command(thing_id).feature_property(feature_id, property_id).twin().modify("myModifiedValue")
# Send the Ditto command.
envelope = command.envelope(response_required=False)
client.send(envelope)
def delete_feature(client):
# Create your Ditto command. Delete can be used to delete either a feature's properties or the feature itself.
command = Command(thing_id).feature(feature_id).twin().delete()
# Send the Ditto command.
envelope = command.envelope(response_required=False)
client.send(envelope)
def delete_feature_property(client):
# Create your Ditto command. Delete can be used to delete either a feature's properties or the feature itself.
command = Command(thing_id).feature_property(feature_id, property_id).twin().delete()
# Send the Ditto command.
envelope = command.envelope(response_required=False)
client.send(envelope)
def on_connect(cl: Client):
create_or_modify_feature(cl)
create_or_modify_feature_property(cl)
delete_feature_property(cl)
delete_feature(cl)
# Test feature modification actions
ditto_client = Client(on_connect=on_connect)
ditto_client.connect("localhost")
ditto_client.disconnect()