Python Edge SDK Manual
October 21, 2024
Environment
- Runtime
- Python: 3.6
- Pip: 1.8
- Python Package:
Installation
- Via the command line
pip install EdgeSync360-EdgeHub-Edge-Python-SDK
- Upgrade the package version
pip install EdgeSync360-EdgeHub-Edge-Python-SDK --upgrade
- Check the installed version
pip show EdgeSync360-EdgeHub-Edge-Python-SDK
Name: EdgeSync360_EdgeHub_Edge_Python_SDK
Version: 1.0.0
Summary: EdgeSync360_EdgeHub_Edge_Python_SDK package allows developers to write Python applications which access the EdgeSync360/EdgeHub Platform via MQTT protocol.
Home-page: https://github.com/EdgeHub-Repo/dc-edge-sdk-python-sample
Author: Paul Tseng
Author-email: edgehub.dev@gmail.com
License:
Location: /opt/homebrew/lib/python3.12/site-packages
Requires: paho-mqtt
Required-by:
FAQ
- The system displays the following SSL certificate error messages:
```json
[SSL: CERTIFICATE_VERIFY_FAILED] certificate verification failed (_ssl.c:720
- Windows
- Install Win 32/Win 64 OpenSSL.
- Mac OS
- You have two options:
- Run an install command shipped with Python 3.6.
cd /Applications/Python\ 3.6/
./Install\ Certificates.command
- Install the certification package with
pip install certifi
EdgeAgent
Constructor (EdgeAgentOptions options)
New EdgeAgent object
options = EdgeAgentOptions(
reconnectInterval = 1, # MQTT reconnect interval in seconds.
nodeId = '5095cf13-f005-4c81-b6c9-68cf038e2b87', # Get from portal
deviceId = 'device1', # If the type is Device, deviceId must be input.
type = constant.EdgeType['Gateway'], # Configure the edge as a Gateway or Device. The default setting is Gateway.
heartbeat = 60, # The default is 60 seconds.
dataRecover = True, # Whether to recover data when disconnected
connectType = constant.ConnectType['DCCS'], # Connection type (DCCS, MQTT). The default setting is DCCS.
MQTT = MQTTOptions( # If the connectType is MQTT, the following options must be input:
hostName = '127.0.0.1',
port = 1883,
userName = 'admin',
password = 'admin',
protocolType = constant.Protocol['TCP'] # MQTT protocol (TCP, WebSocket). The default is TCP.
),
DCCS = DCCSOptions(
APIURL = '', # DCCS API URL
credentialKey = '' # Credential key
),
AzureIotHub = AzureIotHub=AzureIotHubOptions(
connectionString='',
),
)
edgeAgent = EdgeAgent(options = options);
Azure IoT Hub
Due to the connection limit of the Azure IoT Hub SDK, the C# SDK can only connect to the Azure IoT Hub using a connection string. Therefore, you can only connect to the Edge Hub using a EdgeLink device.
Event
EdgeAgent has three subscription events.
- Connected: When EdgeAgent is connected to the IoTHub.
- Disconnected: When EdgeAgent is disconnected from the IoTHub.
- MessageReceived: When EdgeAgent receives an MQTT message from the cloud. The message types are as follows:
- WriteValue: Change the tag value from the cloud.
- WriteConfig: Change the config from the cloud.
- TimeSync: Returns the current time from the cloud.
- ConfigAck: Response to config uploads from the edge to the cloud.
edgeAgent.on_connected = edgeAgent_on_connected
edgeAgent.on_disconnected = edgeAgent_on_disconnected
edgeAgent.on_message = edgeAgent_on_message
def edgeAgent_on_connected(agent, isConnected):
print('Connection success')
def edgeAgent_on_disconnected(agent, isDisconnected):
print('Disconnected')
def edgeAgent_on_message(agent, messageReceivedEventArgs):
# messageReceivedEventArgs format: Model.Event.MessageReceivedEventArgs
type = messageReceivedEventArgs.type
message = messageReceivedEventArgs.message
if type == constant.MessageType['WriteValue']:
# message format: Model.Edge.WriteValueCommand
for device in message.deviceList:
print('deviceId: {0}'.format(device.Id))
for tag in device.tagList:
print('tagName: {0}, Value: {1}'.format(tag.name, str(tag.value)))
elif type == constant.MessageType['WriteConfig']:
print('WriteConfig')
elif type == constant.MessageType['TimeSync']:
# message format: Model.Edge.TimeSyncCommand
print(str(message.UTCTime))
elif type == constant.MessageType['ConfigAck']:
# message format: Model.Edge.ConfigAck
print('Upload Config Result: {0}'}.format(str(message.result)))