Document Version: V1.0
Version | Date | Content |
---|---|---|
1.0 | 2022-02-10 | Document created |
This document guide you how to use TagHub Python SDK on Hello World Application to publish Tag data.
You shall complete pre-request steps:
import os, requests, time, json
from thingspro.edge.tag_v1 import tag
- thingspro.edge.tag_v1 is TagHub Python SDK.
_publisher = tag.Publisher()
- We will use _publisher to update Tag value.
#post /api/v1/hello-world/tag
@app.route('/api/v1/hello-world/tag', methods=['POST'])
def post_tag():
newTag = request.json
if ('prvdName' in newTag) and ('srcName' in newTag) and ('tagName' in newTag) and ('dataType' in newTag):
try:
result = call_API('get', '/tags/list?provider='+newTag['prvdName'], None)
messageJson = json.loads(result["message"])
if "data" in messageJson:
tagList = messageJson["data"]
for tag in tagList:
if (tag['srcName'] == newTag['srcName']) and (tag['tagName'] == newTag['tagName']):
return "OK" # Tag already there, return
# Create Tag
postTag = {
'prvdName': newTag['prvdName'],
'srcName': newTag['srcName'],
'tagName': newTag['tagName'],
'dataType': newTag['dataType'],
'access': 'rw'
}
result = call_API('post', '/tags/virtual', postTag)
return 'OK'
except Exception as e:
return str(e)
else:
return "Bad payload."
- Before update writable tags, this method allow us to create virtual tags by Restful API.
#put /api/v1/hello-world/tag
@app.route('/api/v1/hello-world/tag', methods=['PUT'])
def put_tag():
newTagValue = request.json
if ('prvdName' in newTagValue) and ('srcName' in newTagValue) and ('tagName' in newTagValue) and ('dataValue' in newTagValue) and ('dataType' in newTagValue):
timestamp=int(time.time()*1000000)
try:
tagValue = {
'prvdName': newTagValue['prvdName'],
'srcName': newTagValue['srcName'],
'tagName': newTagValue['tagName'],
'dataValue': newTagValue['value'],
'dataType': newTagValue['dataType'],
'ts': timestamp
}
_publisher.publish(tagValue)
return 'OK'
except Exception as e:
return str(e)
else:
return "Bad payload."
- _publisher.publish() will update Tag's value.
$ wget https://tpe2.azureedge.net/Python3/HelloWorldApp12.tar
Name | Type | Note |
---|---|---|
Dockerfile | File | Same with 1.0 |
app | Folder | Update with TagHub SDK code |
docker-compose.yml | File | Update version to 1.2 |
metadata.yml | File | Update version to 1.2 |
nginx.conf | File | Same with 1.0 |
requirements.tx | File | Update required packages for TagHub SDK |
Follow pre-request step: Build and Run "Hello World" Application, to build hello-world application V1.2
drwxrwxrwx 2 root root 4096 Feb 12 15:28 app
-rwxrwxrwx 1 root root 56 Feb 12 08:46 docker-compose.yml
-rwxrwxrwx 1 root root 851 Feb 12 15:35 Dockerfile
-rw-r--r-- 1 root root 136970240 Feb 12 16:11 hello-world_1.2_armhf.mpkg
-rwxrwxrwx 1 root root 107 Feb 12 08:46 metadata.yml
-rwxrwxrwx 1 root root 281 Feb 12 08:46 nginx.conf
-rwxrwxrwx 1 root root 73 Feb 12 13:07 requirements.txt
Follow pre-request step: Build and Run "Hello World" Application, to deploy hello-world application V1.2
curl -X POST https://127.0.0.1:8443/api/v1/hello-world/tag -H "mx-api-token:$(cat /var/thingspro/data/mx-api-token)" -H "Content-Type:application/json" -k -d '{"prvdName":"virtual","srcName":"hello-world","tagName":"tag01", "dataType":"string"}'
curl -X POST https://127.0.0.1:8443/api/v1/hello-world/tag -H "mx-api-token:$(cat /var/thingspro/data/mx-api-token)" -H "Content-Type:application/json" -k -d '{"prvdName":"virtual","srcName":"hello-world","tagName":"tag02", "dataType":"int32"}'
curl -X PUT https://127.0.0.1:8443/api/v1/hello-world/tag -H "mx-api-token:$(cat /var/thingspro/data/mx-api-token)" -H "Content-Type:application/json" -k -d '{"prvdName":"virtual","srcName":"hello-world","tagName":"tag01", "dataValue":"Hello World", "dataType":"string"}'
curl -X PUT https://127.0.0.1:8443/api/v1/hello-world/tag -H "mx-api-token:$(cat /var/thingspro/data/mx-api-token)" -H "Content-Type:application/json" -k -d '{"prvdName":"virtual","srcName":"hello-world","tagName":"tag02", "dataValue":4040, "dataType":"int32"}'
ThingsPro Edge Web Admin / Tag Management offers a easy way to monitor Tag value changed.
This feature is available on ThingsPro Edge V2.2.1+.