{ "info": { "author": "lihu.yang", "author_email": "lihu.yang@envision-digital.com", "bugtrack_url": null, "classifiers": [ "Programming Language :: Python :: 2.7", "Programming Language :: Python :: 3.5", "Programming Language :: Python :: 3.6" ], "description": "# Using EnOS Device SDK for MQTT for Python (Preview Edition)\n\nThis repo contains the preview edition of EnOS Device SDK for MQTT for Python. This article instructs how to prepare your development environment to use the EnOS Device SDK for MQTT for Python.\n\n* [Installing Python](#python)\n* [Obtaining EnOS Device SDK for MQTT for Python](#obtaining)\n* [Key Features](#keyfeatures)\n* [Sample Code](#samplecode)\n\n\n## Installing Python\nTo use the EnOS Device SDK for MQTT for Python, you will need Python 2.7.13+ or 3.5.3+, and `pip` is required.\n\n\n## Obtaining EnOS Device SDK for MQTT for Python\nYou can obtain the SDK through the following methods:\n\n- Install from pip\n- Download the source code by cloning this repo and build on your machine\n\n### Installing from PIP\n\nUse the following command to install EnOS Device SDK for MQTT for Python from PIP.\n\n```bash \npip install enos-mqtt-sdk-python\n```\n\n### Building from Source Code\n1. Obtain the EnOS Device SDK for MQTT for Python source code.\n - From GitHub:\n ```\n git clone https://github.com/EnvisionIot/enos-mqtt-sdk-python.git\n ```\n - From EnOS SDK Center. Click **SDK Center** from the left navigation of EnOS Console, and obtain the SDK source code.\n\n2. From the directory where the source code is stored, run the following command:\n\n ```\n python setup.py install\n ```\n\n## Key Features\n\nAs the preview edition, the EnOS Device SDK for MQTT for Python currently contains only partial of the EnOS connection features as listed below:\n\n- MQTT over TCP based on username and password and MQTT over TLS 1.2 with X.509 certificate-based mutual authentication supported\n- Create, read, update and delete (CRUD) gateway device topology\n- Publish measure point data\n\n\n## Sample Code\n\n### Preparation (Cloud Configuration and Certificate Generation)\n1. Create device models (including gateway and sub-device), products, devices, and applications in the EnOS Console, and get the corresponding product key-secret and device key-secret pair. For more information, see [Cloud Configuration](https://docs.envisioniot.com/docs/device-connection/en/latest/cloud/index.html) in EnOS Documentation Center.\n\n2. Get the CA root certificate, and generate the local RSA private key and csr file, and apply a certificate from EnOS via the EnOS cert tool (available in EnOS SDK Center).\n\n3. Update the CA root certificate.\n - Enter python shell, input the codes and run:\n ```python\n >>>import certifi\n >>>certifi.where()\n ```\n And then one path will be printed on the console, copy it.\n - Quit the python shell, enter the directory where the CA file in step.2 was generated.\n - Run the following command:\n ```\n cat edge_ca.pem {the path generated in previous step} > all_ca.pem\n ```\n We will use the all_ca.pem in the next steps.\n### Establishing Connection\n\n1. Initialize the MQTT client and load the certificate files.\n ```python\n client = MqttClient(enos_mqtt_url, gateway_product_key, gateway_device_key, gateway_device_secret)\n client.getProfile().setSSLContext(ca_file, cer_file, key_file, key_file_password)\n client.getProfile().setAutoReconnect(True) # if connection interrupted, the client can automaticlly reconnect\n ```\n\n2. Set the online and offline callback method.\n ```python\n client.onOnline = onOnline\n client.onOffline = onOffine\n client.onConnectFailed = onConnectFailed\n ```\n\n3. Connect in synchronous mode.\n ```python\n client.connect()\n ```\n\n### Register a handle_msg to handle the downstream received measurepoint\n ```python\n client.onMessage(handle_msg)\n ```\n\n### Sending Telemetries\n1. Add topology for the sub-devices\n ```python\n topo_req = TopoAddRequest.builder().addSubDevice(SubDeviceInfo(sub_product_key, sub_device_key, sub_device_secret)).build()\n topo_res = client.publish(topo_req)\n ```\n\n2. Login the sub-devices\n ```python\n login_req = SubDeviceLoginRequest.builder().setSubDeviceInfo(sub_product_key, sub_device_key, sub_device_secret).build()\n login_res = client.publish(login_req)\n ```\n\n3. Send telemetries from the MQTT client\n ```python\n meapt_req = MeasurepointPostRequest.builder() \\\n .setProductKey(sub_product_key).setDeviceKey(sub_device_key) \\\n .addMeasurePoint('MeasurePoint1', value1) \\ # the measure point identity\n .addMeasurePoint('MeasurePoint2', value2) \\\n .setTimestamp(timestamp) \\\n .build()\n meapt_res = client.publish(meapt_req)\n ```\n\n### End-to-End Sample\n\n```python\nfrom core.MqttClient import MqttClient\nfrom message.downstream.ota.OtaUpgradeCommand import OtaUpgradeCommand\nfrom message.upstream.status.SubDeviceLoginRequest import SubDeviceLoginRequest\nfrom message.upstream.topo.TopoAddRequest import TopoAddRequest\nfrom message.upstream.topo.SubDeviceInfo import SubDeviceInfo\nfrom message.upstream.ota.OtaVersionReportRequest import OtaVersionReportRequest\nfrom message.upstream.ota.OtaProgressReportRequest import OtaProgressReportRequest\nfrom message.upstream.topo.TopoGetRequest import TopoGetRequest\nfrom message.upstream.topo.TopoDeleteRequest import TopoDeleteRequest\nfrom message.downstream.tsl.MeasurepointSetCommand import MeasurepointSetCommand\nfrom message.upstream.tsl.MeasurepointPostRequest import MeasurepointPostRequest\nimport random\nimport time\n\n# mqtt broker url\nenos_mqtt_url = \"tcp://{HOST}:11883\" # for tcp connection\n# enos_mqtt_url = \"ssl://{HOST}:18883\" # for ssl connection\n\n# gateway parameters\ngateway_product_key = \"GATEWAY_PRODUCT_KEY\"\ngateway_product_secret = 'GATEWAY_PRODUCT_SECRET'\ngateway_device_key = \"GATEWAY_DEVICE_KEY\"\ngateway_device_secret = \"GATEWAY_DEVICE_SECRET\"\n\n# sub-device parameters\nsub_product_key = 'SUB_PRODUCT_KEY'\nsub_device_key = \"SUB_DEVICE_KEY\"\nsub_device_secret = \"SUB_DEVICE_SECRET\"\n\n# these file are generated by get_cert.py via EnOS cert tool, for tls connection\nca_file = 'edge_ca.pem'\nkey_file = 'edge.key'\ncer_file = 'edge.pem'\nkey_file_password = 'PRIVATE_KEY_PASSWORD'\n\n\ndef onConnectFailed():\n\tprint('connect failed...')\n\ttime.sleep(10)\n\tclient.connect()\n\ndef onOnline():\n\tlogin_sub_device(client) # login the sub-device if exists sub-device\n\tprint('connected...')\n\ndef onOffine():\n\tprint('disconnected...')\n\ndef get_topo(mqtt_client):\n\ttopo_get_req = TopoGetRequest.builder().build()\n\ttopo_get_res = mqtt_client.publish(topo_get_req)\n\tif topo_get_res:\n\t\tprint('topo_response: code: %s' % topo_get_res.getCode())\n\t\tprint(topo_get_res.getData())\n\n\ndef add_topo(mqtt_client):\n\ttopo_req = TopoAddRequest.builder().addSubDevice(SubDeviceInfo(sub_product_key, sub_device_key, sub_device_secret)).build()\n\ttopo_res = mqtt_client.publish(topo_req)\n\tif topo_res:\n\t\tprint('topo_response: code: %s' % topo_res.getCode())\n\t\tprint('topo_response: message: %s' % topo_res.getMessage())\n\n\ndef delete_topo(mqtt_client):\n\ttopo_del_req = TopoDeleteRequest.builder().addSubDevice(sub_product_key, sub_device_key).build()\n\ttopo_del_res = mqtt_client.publish(topo_del_req)\n\tif topo_del_res:\n\t\tprint('topo_delete_response: %s' % topo_del_res.getCode())\n\n\ndef login_sub_device(mqtt_client):\n\tlogin_req = SubDeviceLoginRequest.builder().setSubDeviceInfo(sub_product_key, sub_device_key, sub_device_secret).build()\n\tlogin_res = mqtt_client.publish(login_req)\n\tif login_res:\n\t\tprint('login_response: code: %s' % login_res.getCode())\n\t\tprint('login_response: message: %s' % login_res.getMessage())\n\n\n# post measure points data via MQTT\ndef post_measure_points(mqtt_client, timestamp):\n\tmeapt_req = MeasurepointPostRequest.builder() \\\n\t\t.setProductKey(sub_product_key).setDeviceKey(sub_device_key) \\\n\t\t.addMeasurePoint('MeasurePoint1', random.randint(100, 200)) \\\n\t\t.addMeasurePoint('MeasurePoint2', random.randint(100, 200)) \\\n\t\t.setTimestamp(timestamp) \\\n\t\t.build()\n\tmeapt_res = mqtt_client.publish(meapt_req)\n\tif meapt_res:\n\t\tprint('measurepointPost_response: %s' % meapt_res.getCode())\n\n# handle the received downstream message and implement your logic\ndef handle_msg(arrivedMessage, replyHandler):\n\t'''\n\t:param arrivedMessage: \n\t:param replyHandler: \n\t'''\n\t# handle logic\n\tsuccess = 1\n\tprint(arrivedMessage.params)\n\t# set reply payload\n\tif success:\n\t\tcode = 200\n\t\tmessage = 'test'\n\t\tdata = 0\n\telse:\n\t\tcode = 2000\n\t\tmessage = 'test'\n\t\tdata = 1\n\treplyHandler.reply_with_payload(code=code, message=message, data=data)\n\ndef report_version(mqtt_client, version):\n\tmeapt_req = OtaVersionReportRequest.builder() \\\n\t\t.setProductKey(gateway_product_key).setDeviceKey(gateway_device_key) \\\n\t\t.setVersion(version) \\\n\t\t.build()\n\tmeapt_res = mqtt_client.publish(meapt_req)\n\tif meapt_res:\n\t\tprint('OtaVersionReport_response: %s' % meapt_res.getCode())\n\ndef report_progress(mqtt_client, progress, desc):\n\tmeapt_req = OtaProgressReportRequest.builder() \\\n\t\t.setProductKey(gateway_product_key).setDeviceKey(gateway_device_key) \\\n\t\t.setStep(progress) \\\n\t\t.setDesc(desc) \\\n\t\t.build()\n\tmeapt_res = mqtt_client.publish(meapt_req)\n\tif meapt_res:\n\t\tprint('OtaProgressReport_response: %s' % meapt_res.getCode())\n\ndef upgrade_firmware(arrivedMessage, replyHandler):\n\tOtaUpgradeCommand = arrivedMessage\n\tfirmware = OtaUpgradeCommand.getFirmwareInfo()\n\tprint(\"receive command: \", firmware.fileUrl, firmware.version)\n\n\t# TODO: download firmware from firmware.fileUrl\n\n\t# mock reporting progress\n\treport_progress(client, '35', 'downloading firmware finished')\n\n\treport_progress(client, '70', 'decompressing firmware finished')\n\n\treport_progress(client, '90', 'running firmware finished')\n\n\treport_progress(client, '100', 'upgrading firmware finished')\n\n\t# firmware upgrade success, report new version\n\treport_version(client, firmware.version)\n\nif __name__ == \"__main__\":\n\n\tclient = MqttClient(enos_mqtt_url, gateway_product_key, gateway_device_key, gateway_device_secret)\n\tclient.getProfile().setAutoReconnect(True) # if connection interrupted, the client can automaticlly reconnect\n\t# set the certificate files for bi-directional certification\n\tclient.getProfile().setSSLContext(ca_file, cer_file, key_file, key_file_password)\n\tclient.setupBasicLogger('INFO')\n\tclient.onOnline = onOnline\n\tclient.onOffline = onOffine\n\tclient.onConnectFailed = onConnectFailed\n\n\tclient.connect() # connect in sync\n\n\t# register a handle_msg to handle the downstream received measurepoint\n\tclient.onMessage(MeasurepointSetCommand().getClass(), handle_msg)\n\t# register a handle_msg to implement the ota function, e.g: upgrade firmware\n\tclient.onMessage(OtaUpgradeCommand().getClass(), upgrade_firmware)\n\tadd_topo(client) # add the device to the gateway as sub-device if exists sub-device\n\treport_version(client, '1.0.0') # report the version number before upgrading firmware\n\twhile True:\n\t\ttimestamp = int(time.time() * 1000) # timestamp in milliseconds\n\t\tpost_measure_points(client, timestamp) # publish measure points data\n\t\ttime.sleep(10)\n\n```\n\n\n\n", "description_content_type": "text/markdown", "docs_url": null, "download_url": "", "downloads": { "last_day": -1, "last_month": -1, "last_week": -1 }, "home_page": "https://github.com/EnvisionIot/enos-mqtt-sdk-python.git", "keywords": "", "license": "", "maintainer": "", "maintainer_email": "", "name": "enos-mqtt-sdk-python", "package_url": "https://pypi.org/project/enos-mqtt-sdk-python/", "platform": "", "project_url": "https://pypi.org/project/enos-mqtt-sdk-python/", "project_urls": { "Homepage": "https://github.com/EnvisionIot/enos-mqtt-sdk-python.git" }, "release_url": "https://pypi.org/project/enos-mqtt-sdk-python/0.0.6/", "requires_dist": [ "atomic (>=0.7.3)", "futures (>=3.1.1)", "paho-mqtt (>=1.4.0)", "certifi" ], "requires_python": "", "summary": "EnOS MQTT SDK for Python", "version": "0.0.6" }, "last_serial": 5848651, "releases": { "0.0.1": [ { "comment_text": "", "digests": { "md5": "34a0ddb6185e7f5ba46d8dec205d815b", "sha256": "67f6851ec8c6193fae976335608e3d1f49b6f12d10580c8698cf69b90d7681b9" }, "downloads": -1, "filename": "enos_mqtt_sdk_python-0.0.1-py2-none-any.whl", "has_sig": false, "md5_digest": "34a0ddb6185e7f5ba46d8dec205d815b", "packagetype": "bdist_wheel", "python_version": "py2", "requires_python": null, "size": 59992, "upload_time": "2018-12-04T08:43:27", "url": "https://files.pythonhosted.org/packages/6c/32/79d73714fb90a75092871735495d01d61d562388a863696d8c9090006e8b/enos_mqtt_sdk_python-0.0.1-py2-none-any.whl" }, { "comment_text": "", "digests": { "md5": "d6f77833a077bc0a0d23586d3d982863", "sha256": "e1fbf2cd54294e78b17e4630856b763f0d55d32f433eaa7172c292e4e62d7042" }, "downloads": -1, "filename": "enos-mqtt-sdk-python-0.0.1.tar.gz", "has_sig": false, "md5_digest": "d6f77833a077bc0a0d23586d3d982863", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 40265, "upload_time": "2018-12-04T08:43:30", "url": "https://files.pythonhosted.org/packages/28/b3/29c26751b1774b66f1f6d9c9372ad0fb99c02a4f9f279de705c424a12db1/enos-mqtt-sdk-python-0.0.1.tar.gz" } ], "0.0.2": [ { "comment_text": "", "digests": { "md5": "6a2649fb3568b8d7d88025d396dfeae5", "sha256": "d37bbc1f993a9ef976f0224b694338928c6684ae232bad5d9ed876c4c6fef281" }, "downloads": -1, "filename": "enos_mqtt_sdk_python-0.0.2-py2-none-any.whl", "has_sig": false, "md5_digest": "6a2649fb3568b8d7d88025d396dfeae5", "packagetype": "bdist_wheel", "python_version": "py2", "requires_python": null, "size": 59990, "upload_time": "2018-12-04T10:46:07", "url": "https://files.pythonhosted.org/packages/0c/62/a228930440c0b716c1cfe734f189f02bfb1aa35255381658f6205d20c1e9/enos_mqtt_sdk_python-0.0.2-py2-none-any.whl" }, { "comment_text": "", "digests": { "md5": "8ba368cd32ae7435e205ece21a6c52e5", "sha256": "42b5b78b7e1f09f3635c4c32fd40dd3eb8e78316fb544876b402c0955e2fd08e" }, "downloads": -1, "filename": "enos-mqtt-sdk-python-0.0.2.tar.gz", "has_sig": false, "md5_digest": "8ba368cd32ae7435e205ece21a6c52e5", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 40388, "upload_time": "2018-12-04T10:46:10", "url": "https://files.pythonhosted.org/packages/39/0c/52cf72f0204f2e1a239132d0f572d389ad1c6ca808ccd271e34d92b23deb/enos-mqtt-sdk-python-0.0.2.tar.gz" } ], "0.0.3": [ { "comment_text": "", "digests": { "md5": "31628b0b114ec28f74568b0e74abe936", "sha256": "cc98746aa593e8cb4d82fd25d9b6c707f8a23d334b9eafab867a5be3dbc2355e" }, "downloads": -1, "filename": "enos_mqtt_sdk_python-0.0.3-py2-none-any.whl", "has_sig": false, "md5_digest": "31628b0b114ec28f74568b0e74abe936", "packagetype": "bdist_wheel", "python_version": "py2", "requires_python": null, "size": 63529, "upload_time": "2019-01-29T03:31:21", "url": "https://files.pythonhosted.org/packages/b5/28/02e30b834f6c176e3b2594e9e19b2e226f750dd0e49c1215e6961aadae18/enos_mqtt_sdk_python-0.0.3-py2-none-any.whl" }, { "comment_text": "", "digests": { "md5": "797409d76267600d24b0c53fcebe92d5", "sha256": "92e9d6de7ec0d9df666294c580bcf5e56022fd0ceaefa9bf98b858697bf3782e" }, "downloads": -1, "filename": "enos-mqtt-sdk-python-0.0.3.tar.gz", "has_sig": false, "md5_digest": "797409d76267600d24b0c53fcebe92d5", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 39590, "upload_time": "2019-01-29T03:31:22", "url": "https://files.pythonhosted.org/packages/54/b1/5e1cee0b2ae48230d35c5369e0d2b51a3124fb58487e9e2bf4099fc84221/enos-mqtt-sdk-python-0.0.3.tar.gz" } ], "0.0.5": [ { "comment_text": "", "digests": { "md5": "f56c03a624b7cfa9698b124431018aed", "sha256": "b8289cda71ee9c36a104fe34780be5c154a9f96b6b7446d37f845468924ce8cb" }, "downloads": -1, "filename": "enos_mqtt_sdk_python-0.0.5-py2-none-any.whl", "has_sig": false, "md5_digest": "f56c03a624b7cfa9698b124431018aed", "packagetype": "bdist_wheel", "python_version": "py2", "requires_python": null, "size": 63551, "upload_time": "2019-03-19T10:53:31", "url": "https://files.pythonhosted.org/packages/ee/69/161199f4bb57bd541efeb204096b53e3ddf78fc20edb7159da6790d2bb01/enos_mqtt_sdk_python-0.0.5-py2-none-any.whl" }, { "comment_text": "", "digests": { "md5": "73eaca504db43937c313eb3edd07cb2a", "sha256": "fdf1f92b1ba75998fa06db1c5d74b209fb659a489ad206c619edeccad799e714" }, "downloads": -1, "filename": "enos-mqtt-sdk-python-0.0.5.tar.gz", "has_sig": false, "md5_digest": "73eaca504db43937c313eb3edd07cb2a", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 40147, "upload_time": "2019-03-19T10:53:33", "url": "https://files.pythonhosted.org/packages/1e/59/d1d2eced86c44d02c804e4f5b6daea1bfecb04d947fb3e442caaabcd3cdf/enos-mqtt-sdk-python-0.0.5.tar.gz" } ], "0.0.6": [ { "comment_text": "", "digests": { "md5": "44afc12846d2ae612e62bab4acd33435", "sha256": "b89fdbbd54cd098ace9adc03d266f7d0de773dff31a6a0fb9d91ef2f5c387c7f" }, "downloads": -1, "filename": "enos_mqtt_sdk_python-0.0.6-py2-none-any.whl", "has_sig": false, "md5_digest": "44afc12846d2ae612e62bab4acd33435", "packagetype": "bdist_wheel", "python_version": "py2", "requires_python": null, "size": 67513, "upload_time": "2019-09-18T08:30:26", "url": "https://files.pythonhosted.org/packages/93/6c/19e934df637fee5c2a3fe622fcf16a3271f6981041d454142ed00aae30a7/enos_mqtt_sdk_python-0.0.6-py2-none-any.whl" }, { "comment_text": "", "digests": { "md5": "11e36da5fa25975b8b42e273db50fc82", "sha256": "b39d7e31664724b9d8369504b819d56a428f9fd66e7e0920e0ec38b6b29aab1b" }, "downloads": -1, "filename": "enos-mqtt-sdk-python-0.0.6.tar.gz", "has_sig": false, "md5_digest": "11e36da5fa25975b8b42e273db50fc82", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 42118, "upload_time": "2019-09-18T08:30:29", "url": "https://files.pythonhosted.org/packages/a4/30/3566fb45fc7731255f837646b5f60acfde858c6e1748469fd296113b665e/enos-mqtt-sdk-python-0.0.6.tar.gz" } ] }, "urls": [ { "comment_text": "", "digests": { "md5": "44afc12846d2ae612e62bab4acd33435", "sha256": "b89fdbbd54cd098ace9adc03d266f7d0de773dff31a6a0fb9d91ef2f5c387c7f" }, "downloads": -1, "filename": "enos_mqtt_sdk_python-0.0.6-py2-none-any.whl", "has_sig": false, "md5_digest": "44afc12846d2ae612e62bab4acd33435", "packagetype": "bdist_wheel", "python_version": "py2", "requires_python": null, "size": 67513, "upload_time": "2019-09-18T08:30:26", "url": "https://files.pythonhosted.org/packages/93/6c/19e934df637fee5c2a3fe622fcf16a3271f6981041d454142ed00aae30a7/enos_mqtt_sdk_python-0.0.6-py2-none-any.whl" }, { "comment_text": "", "digests": { "md5": "11e36da5fa25975b8b42e273db50fc82", "sha256": "b39d7e31664724b9d8369504b819d56a428f9fd66e7e0920e0ec38b6b29aab1b" }, "downloads": -1, "filename": "enos-mqtt-sdk-python-0.0.6.tar.gz", "has_sig": false, "md5_digest": "11e36da5fa25975b8b42e273db50fc82", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 42118, "upload_time": "2019-09-18T08:30:29", "url": "https://files.pythonhosted.org/packages/a4/30/3566fb45fc7731255f837646b5f60acfde858c6e1748469fd296113b665e/enos-mqtt-sdk-python-0.0.6.tar.gz" } ] }