The project providers a python package to develop drivers which running on Link IoT Edge and helping things connect to it.
The HelloThing
sample demonstrates you the procedure that connecting things to Link IoT Edge.
- Copy
examples/HelloThing
folder to your workspace. - Zip up the content of
HelloThing
folder so that theindex.py
is on the top of the zip file structure. - Go to Link IoT Edge console, Edge Management, Driver Management and then Create Driver.
- Choose the programming language as python3.
- Set the driver name
HelloThing
and upload the previous zip file. - Create a product, which owns an property named
temperature
(type of int32), and an event namedhigh_temperature
(type of int32 and has a input parameter namedtemperature
whose type is int32). - Create a device of the product created last step, with name
HelloThing
. - Create a new instance and add the Link IoT Edge gateway device into it.
- Go to Thing Driver tab and add
HelloThing
driver into that instance. - Add the
HelloThing
device into the instance. ChooseHelloThing
as its driver. - Add a Message Router with the folowing configuration:
- Source:
HelloThing
device - TopicFilter: Properties.
- Target: IoT Hub
- Deploy. A message from
HelloThing
device should be published to IoT Hub every 2 seconds. You can check this by going to the Device Running State page on Link IoT Edge console.
First build a Link IoT Edge development environment.
Then connect things to Link IoT Edge. The most common use is as follows:
# -*- coding: utf-8 -*-
import logging
import time
import lethingaccesssdk
from threading import Timer
# Base on device, User need to implement the getProperties, setProperties and callService function.
class Temperature_device(lethingaccesssdk.ThingCallback):
def __init__(self):
self.temperature = 41
self.humidity = 80
def getProperties(self, input_value):
'''
Get properties from the physical thing and return the result.
:param input_value:
:return:
'''
retDict = {
"temperature": 41,
"humidity": 80
}
return 0, retDict
def setProperties(self, input_value):
'''
Set properties to the physical thing and return the result.
:param input_value:
:return:
'''
return 0, {}
def callService(self, name, input_value):
'''
Call services on the physical thing and return the result.
:param name:
:param input_value:
:return:
'''
return 0, {}
def thing_behavior(client, app_callback):
while True:
properties = {"temperature": app_callback.temperature,
"humidity": app_callback.humidity}
client.reportProperties(properties)
client.reportEvent("high_temperature", {"temperature": 41})
time.sleep(2)
try:
infos = lethingaccesssdk.Config().getThingInfos()
for info in infos:
app_callback = Temperature_device()
client = lethingaccesssdk.ThingAccessClient(info)
client.registerAndOnline(app_callback)
t = Timer(2, thing_behavior, (client, app_callback))
t.start()
except Exception as e:
logging.error(e)
# don't remove this function
def handler(event, context):
return 'hello world'
Next follow the Getting Started to upload and test the function.
The main API references are as follows.
- getConfig()
- ThingCallback()
- ThingCallback#setProperties()
- ThingCallback#getProperties()
- ThingCallback#callService()
- ThingAccessClient()
- ThingAccessClient#registerAndOnline()
- ThingAccessClient#reportEvent()
- ThingAccessClient#reportProperties()
- ThingAccessClient#getTsl()
- ThingAccessClient#online()
- ThingAccessClient#offline()
- ThingAccessClient#unregister()
- ThingAccessClient#cleanup()
- Config()
- Config#getThingInfos()
return config information under the driver
Base on device,user need to implement a class inherits from ThingCallback. and user need to implement the setProperties、getProperties and callService in this class.
set device property.
- input_value
dict
: the values of Property. eg:{"property1": 'xxx', "property2": 'yyy', ...}. - return
- code
int
: if success it will return LEDA_SUCCESS, or return error code. - output
dict
: customer data,if no data, it will return {}.
- code
get device property.
- input_value
list
:the Property. eg:['key1', 'key2']. - return:
- code
int
: if success it will return LEDA_SUCCESS, or return error code. - output
dict
: output values. eg:{'property1':xxx, 'property2':yyy, ...}.
- code
call device function.
- parameter:
- name
str
: function name. - input_value
dict
:the parameter of funtion,eg: {"args1": 'xxx', "args2": 'yyy', ...}.
- name
- return:
- code
int
: if success it will return LEDA_SUCCESS, or return error code. - output
dict
: output values. eg:{"key1": 'xxx', "key2": 'yyy', ...}.
- code
Constructs a ThingAccessClient with the specified config.
- config
dict
: include productKey and deviceName allocated by Link IoT Edge, eg: {"productKey": "xxx", "deviceName": "yyy"}.
Registers thing to Link IoT Edge platform and informs it that thing is connected. When register, DEVICE_NAME will be used first if it exists, or LOCAL_NAME is used.
- ThingCallback
object
: ThingCallback object.
Reports a event to Link IoT Edge platform.
- eventName
str
: Event name. - args
dict
: Event information. eg: {"key1": 'xxx', "key2": 'yyy', ...}.
Reports the property values to Link IoT Edge platform.
- properties
dict
: property values. eg:{"property1": 'xxx', "property2": 'yyy', ...}.
Returns the TSL(Thing Specification Language) stringstr
.
Informs Link IoT Edge platform that thing is connected.
Informs Link IoT Edge platform that thing is disconnected.
Removes the binding relationship between thing and Link IoT Edge platform. You usually don't call this function.
Removes the binding relationship between thing and Link IoT Edge platform. You usually don't call this function.
base on current config return config object.
return ThingInfoList
。
ThingInfo include:
- productKey
str
: productKey。 - deviceName
str
: deviceName - custom
dict
: custom config
Copyright (c) 2017-present Alibaba instance Holding Ltd.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.