-
Notifications
You must be signed in to change notification settings - Fork 4
/
egfCreateEndpoint-07.py
208 lines (173 loc) · 8.61 KB
/
egfCreateEndpoint-07.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
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#!/usr/bin/python
##############################################################################################################
# ______ __ _ __ __ _ __
# / ____/___/ /___ ____ | |/ / ____/ /__ _ __(_)_______ _____________ ____ _/ /_____ _____
# / __/ / __ / __ `/ _ \| / / __ / _ \ | / / / ___/ _ \ / ___/ ___/ _ \/ __ `/ __/ __ \/ ___/
# / /___/ /_/ / /_/ / __/ | / /_/ / __/ |/ / / /__/ __/ / /__/ / / __/ /_/ / /_/ /_/ / /
# /_____/\__,_/\__, /\___/_/|_| \__,_/\___/|___/_/\___/\___/ \___/_/ \___/\__,_/\__/\____/_/
# /____/
##############################################################################################################
# Name; egfCreateEndpoint-07.py
# Version: 0.7
# Author: Jonas Werner
##############################################################################################################
import requests, json, sys, re, time, os, warnings, argparse
from requests_toolbelt.multipart.encoder import MultipartEncoder
from datetime import datetime
warnings.filterwarnings("ignore")
# Gather information from arguments
parser=argparse.ArgumentParser(description="Python script for creating a new device from scratch in EdgeX Foundry")
parser.add_argument('-ip',help='EdgeX Foundry IP address', required=True)
parser.add_argument('-version',help='EdgeX Foundry version (delhi or edinburgh)', required=True)
args=vars(parser.parse_args())
edgex_ip=args["ip"]
edgexVer=args["version"]
# Make two calls to Core Metadata, to create the Addressable for the Device Service:
def createAddressables():
# Create the addressable for the Device Service:
url = 'http://%s:48081/api/v1/addressable' % edgex_ip
payload = {
"name": "humidity and temp address 1",
"protocol": "HTTP",
"address": "172.17.0.1",
"port": 49999,
"path": "/hum_and_temp_cluster-01",
"publisher": "none",
"user": "none",
"password": "none",
"topic": "none"
}
headers = {'content-type': 'application/json'}
response = requests.post(url, data=json.dumps(payload), headers=headers, verify=False)
print("Result of create addressables: %s with message %s" % (response, response.text))
def createValueDescriptors():
url = 'http://%s:48080/api/v1/valuedescriptor' % edgex_ip
payload = {
"name": "Humidity",
"description": "Ambient humidity in percent",
"max": "100",
"min": "0",
"type": "I",
"uomLabel": "count",
"defaultValue": "0",
"formatting": "%s",
"labels": [
"humidity",
"percent"
]
}
headers = {'content-type': 'application/json'}
response = requests.post(url, data=json.dumps(payload), headers=headers, verify=False)
print("Result of creating value descriptor for humidity: %s with message %s" % (response, response.text))
payload = {
"name": "Temperature in C",
"description": "Ambient temperature in Celcius",
"min": "-10",
"max": "100",
"type": "I",
"uomLabel": "count",
"defaultValue": "0",
"formatting": "%s",
"labels": [
"temp",
"celcius"
]
}
headers = {'content-type': 'application/json'}
response = requests.post(url, data=json.dumps(payload), headers=headers, verify=False)
print("Result creating value descriptor for temperature: %s with message %s" % (response, response.text))
def uploadDeviceProfile():
multipart_data = MultipartEncoder(
fields={
# a file upload field
'file': ('file.py', open('EdgeX_TempHumidity_MonitorProfile.yml', 'rb'), 'text/plain')
}
)
url = 'http://%s:48081/api/v1/deviceprofile/uploadfile' % edgex_ip
response = requests.post(url, data=multipart_data,
headers={'Content-Type': multipart_data.content_type})
print("Result of uploading device profile: %s with message %s" % (response, response.text))
def createDeviceService():
url = 'http://%s:48081/api/v1/deviceservice' % edgex_ip
payload = {
"name": "sensor cluster control device service",
"description": "Manage sensor clusters delivering humidity and temperature readings",
"labels": [
"Raspberry Pi",
"Sensor cluster"
],
"adminState": "unlocked",
"operatingState": "enabled",
"addressable": {
"name": "humidity and temp address 1"
}
}
headers = {'content-type': 'application/json'}
response = requests.post(url, data=json.dumps(payload), headers=headers, verify=False)
print("Result of create device service: %s with message %s" % (response, response.text))
def addNewDevice(edgexVer):
url = 'http://%s:48081/api/v1/device' % edgex_ip
if edgexVer == "delhi":
payload = {
"name": "Temp_and_Humidity_sensor_cluster_01",
"description": "Raspberry Pi sensor cluster",
"adminState": "unlocked",
"operatingState": "enabled",
"addressable": {
"name": "humidity and temp address 1"
},
"labels": [
"Raspberry Pi",
"Sensor cluster"
],
"location": "",
"service": {
"name": "sensor cluster control device service"
},
"profile": {
"name": "Temp and Humidity sensor cluster monitor profile - 01"
}
}
elif edgexVer == "edinburgh":
payload = {
"name": "Temp_and_Humidity_sensor_cluster_01",
"description": "Raspberry Pi sensor cluster",
"adminState": "unlocked",
"operatingState": "enabled",
"protocols": {
"example": {
"host": "localhost",
"port": "1234",
"unitID": "1"
}
},
"addressable": {
"name": "humidity and temp address 1"
},
"labels": [
"Raspberry Pi",
"Sensor cluster"
],
"location": "",
"service": {
"name": "sensor cluster control device service"
},
"profile": {
"name": "Temp and Humidity sensor cluster monitor profile - 01"
}
}
headers = {'content-type': 'application/json'}
response = requests.post(url, data=json.dumps(payload), headers=headers, verify=False)
print("Result of creating the device: %s with message %s" % (response, response.text))
if __name__ == "__main__":
# Sanity check
if not edgexVer == "delhi" and not edgexVer == "edinburgh":
print("Supported versions are either delhi or edinburgh")
sys.exit()
else:
print("EdgeX Foundry version is: %s" % edgexVer)
createAddressables()
createValueDescriptors()
uploadDeviceProfile()
createDeviceService()
addNewDevice(edgexVer)