├── .gitignore ├── Insomnia_API_Routes.json ├── LICENSE ├── README.md └── app.py /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | *$py.class 5 | 6 | # C extensions 7 | *.so 8 | 9 | # Distribution / packaging 10 | .Python 11 | build/ 12 | develop-eggs/ 13 | dist/ 14 | downloads/ 15 | eggs/ 16 | .eggs/ 17 | lib/ 18 | lib64/ 19 | parts/ 20 | sdist/ 21 | var/ 22 | wheels/ 23 | pip-wheel-metadata/ 24 | share/python-wheels/ 25 | *.egg-info/ 26 | .installed.cfg 27 | *.egg 28 | MANIFEST 29 | 30 | # PyInstaller 31 | # Usually these files are written by a python script from a template 32 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 33 | *.manifest 34 | *.spec 35 | 36 | # Installer logs 37 | pip-log.txt 38 | pip-delete-this-directory.txt 39 | 40 | # Unit test / coverage reports 41 | htmlcov/ 42 | .tox/ 43 | .nox/ 44 | .coverage 45 | .coverage.* 46 | .cache 47 | nosetests.xml 48 | coverage.xml 49 | *.cover 50 | *.py,cover 51 | .hypothesis/ 52 | .pytest_cache/ 53 | 54 | # Translations 55 | *.mo 56 | *.pot 57 | 58 | # Django stuff: 59 | *.log 60 | local_settings.py 61 | db.sqlite3 62 | db.sqlite3-journal 63 | 64 | # Flask stuff: 65 | instance/ 66 | .webassets-cache 67 | 68 | # Scrapy stuff: 69 | .scrapy 70 | 71 | # Sphinx documentation 72 | docs/_build/ 73 | 74 | # PyBuilder 75 | target/ 76 | 77 | # Jupyter Notebook 78 | .ipynb_checkpoints 79 | 80 | # IPython 81 | profile_default/ 82 | ipython_config.py 83 | 84 | # pyenv 85 | .python-version 86 | 87 | # pipenv 88 | # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. 89 | # However, in case of collaboration, if having platform-specific dependencies or dependencies 90 | # having no cross-platform support, pipenv may install dependencies that don't work, or not 91 | # install all needed dependencies. 92 | #Pipfile.lock 93 | 94 | # PEP 582; used by e.g. github.com/David-OConnor/pyflow 95 | __pypackages__/ 96 | 97 | # Celery stuff 98 | celerybeat-schedule 99 | celerybeat.pid 100 | 101 | # SageMath parsed files 102 | *.sage.py 103 | 104 | # Environments 105 | .env 106 | .venv 107 | env/ 108 | venv/ 109 | ENV/ 110 | env.bak/ 111 | venv.bak/ 112 | 113 | # Spyder project settings 114 | .spyderproject 115 | .spyproject 116 | 117 | # Rope project settings 118 | .ropeproject 119 | 120 | # mkdocs documentation 121 | /site 122 | 123 | # mypy 124 | .mypy_cache/ 125 | .dmypy.json 126 | dmypy.json 127 | 128 | # Pyre type checker 129 | .pyre/ 130 | 131 | # pylogix-api 132 | plcConfig.py -------------------------------------------------------------------------------- /Insomnia_API_Routes.json: -------------------------------------------------------------------------------- 1 | {"_type":"export","__export_format":4,"__export_date":"2021-03-17T01:59:12.665Z","__export_source":"insomnia.desktop.app:v2020.5.2","resources":[{"_id":"req_b8f9e34cb5a94eb081461948e7c2da9d","parentId":"wrk_6b294e267e5f4306a259701c74b85008","modified":1615945891458,"created":1615468663261,"url":"localhost:5000/pylogix/v1.0/plc/{{ _.plc_ip }}/{{ _.plc_slot }}/tags/BaseDINTArray[10]{10}","name":"getElementsFromArray","description":"","method":"GET","body":{},"parameters":[],"headers":[],"authentication":{},"metaSortKey":-1615468663261,"isPrivate":false,"settingStoreCookies":true,"settingSendCookies":true,"settingDisableRenderRequestBody":false,"settingEncodeUrl":true,"settingRebuildPath":true,"settingFollowRedirects":"global","_type":"request"},{"_id":"wrk_6b294e267e5f4306a259701c74b85008","parentId":null,"modified":1615468360073,"created":1615468360073,"name":"pylogix-api","description":"","scope":null,"_type":"workspace"},{"_id":"req_7b3e2e743fe44fb49a8cda233ba2f1d3","parentId":"wrk_6b294e267e5f4306a259701c74b85008","modified":1615945924817,"created":1615945294929,"url":"localhost:5000/pylogix/v1.0/plc/{{ _.plc_ip }}/{{ _.plc_slot }}/tags/SmallBOOLArray2D[0,0]{64}","name":"getElementsFromArray2D","description":"","method":"GET","body":{},"parameters":[],"headers":[],"authentication":{},"metaSortKey":-1615468631033,"isPrivate":false,"settingStoreCookies":true,"settingSendCookies":true,"settingDisableRenderRequestBody":false,"settingEncodeUrl":true,"settingRebuildPath":true,"settingFollowRedirects":"global","_type":"request"},{"_id":"req_36fdd6377f854353b80cc7613d66ceec","parentId":"wrk_6b294e267e5f4306a259701c74b85008","modified":1615945950145,"created":1615468598805,"url":"localhost:5000/pylogix/v1.0/plc/{{ _.plc_ip }}/{{ _.plc_slot }}/tags/[BaseBOOL; BaseDINT; BaseINT]","name":"getListOfTags","description":"","method":"GET","body":{},"parameters":[],"headers":[],"authentication":{},"metaSortKey":-1615468598805,"isPrivate":false,"settingStoreCookies":true,"settingSendCookies":true,"settingDisableRenderRequestBody":false,"settingEncodeUrl":true,"settingRebuildPath":true,"settingFollowRedirects":"global","_type":"request"},{"_id":"req_f241fed2da4a4d32a173807e4803494d","parentId":"wrk_6b294e267e5f4306a259701c74b85008","modified":1615946063491,"created":1615468522925,"url":"localhost:5000/pylogix/v1.0/plc/{{ _.plc_ip }}/{{ _.plc_slot }}/devices","name":"getDevices","description":"","method":"GET","body":{},"parameters":[],"headers":[],"authentication":{},"metaSortKey":-1615468522925,"isPrivate":false,"settingStoreCookies":true,"settingSendCookies":true,"settingDisableRenderRequestBody":false,"settingEncodeUrl":true,"settingRebuildPath":true,"settingFollowRedirects":"global","_type":"request"},{"_id":"req_6f85d80aa9934f5b99c5625935d77398","parentId":"wrk_6b294e267e5f4306a259701c74b85008","modified":1615946013616,"created":1615468453175,"url":"localhost:5000/pylogix/v1.0/plc/{{ _.plc_ip }}/{{ _.plc_slot }}/tags/BaseBOOL","name":"getTag","description":"","method":"GET","body":{},"parameters":[],"headers":[],"authentication":{},"metaSortKey":-1615468453175,"isPrivate":false,"settingStoreCookies":true,"settingSendCookies":true,"settingDisableRenderRequestBody":false,"settingEncodeUrl":true,"settingRebuildPath":true,"settingFollowRedirects":"global","_type":"request"},{"_id":"req_71378d89427e4f12a59c488177f7e1af","parentId":"wrk_6b294e267e5f4306a259701c74b85008","modified":1615946032747,"created":1615468488413,"url":"localhost:5000/pylogix/v1.0/plc/{{ _.plc_ip }}/{{ _.plc_slot }}/tags/BaseBOOLsdfsdf","name":"getTagFailure","description":"","method":"GET","body":{},"parameters":[],"headers":[],"authentication":{},"metaSortKey":-1615468425189,"isPrivate":false,"settingStoreCookies":true,"settingSendCookies":true,"settingDisableRenderRequestBody":false,"settingEncodeUrl":true,"settingRebuildPath":true,"settingFollowRedirects":"global","_type":"request"},{"_id":"req_ef0dc460e00e46f7a5d1fb5df549e0fb","parentId":"wrk_6b294e267e5f4306a259701c74b85008","modified":1615946045249,"created":1615468397203,"url":"localhost:5000/pylogix/v1.0/plc/{{ _.plc_ip }}/{{ _.plc_slot }}/tags","name":"getTags","description":"","method":"GET","body":{},"parameters":[],"headers":[],"authentication":{},"metaSortKey":-1615468397203,"isPrivate":false,"settingStoreCookies":true,"settingSendCookies":true,"settingDisableRenderRequestBody":false,"settingEncodeUrl":true,"settingRebuildPath":true,"settingFollowRedirects":"global","_type":"request"},{"_id":"env_2a4c2eba68f3c413c9d8958687f7b3689a78a1fc","parentId":"wrk_6b294e267e5f4306a259701c74b85008","modified":1615468360150,"created":1615468360150,"name":"Base Environment","data":{},"dataPropertyOrder":null,"color":null,"isPrivate":false,"metaSortKey":1615468360150,"_type":"environment"},{"_id":"jar_2a4c2eba68f3c413c9d8958687f7b3689a78a1fc","parentId":"wrk_6b294e267e5f4306a259701c74b85008","modified":1615468360156,"created":1615468360156,"name":"Default Jar","cookies":[],"_type":"cookie_jar"},{"_id":"spc_8d028e65e6f346de9dafb213623f8d00","parentId":"wrk_6b294e267e5f4306a259701c74b85008","modified":1615468360083,"created":1615468360083,"fileName":"pylogix-api","contents":"","contentType":"yaml","_type":"api_spec"},{"_id":"env_5eaf476209554e46984d0a23f145f40b","parentId":"env_2a4c2eba68f3c413c9d8958687f7b3689a78a1fc","modified":1615945800563,"created":1615945745451,"name":"pylogix-api-env","data":{"plc_ip":"192.168.56.106","plc_slot":"2"},"dataPropertyOrder":{"&":["plc_ip","plc_slot"]},"color":"#7d69cb","isPrivate":false,"metaSortKey":1615945745451,"_type":"environment"}]} -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # pylogix-api 2 | 3 | Web api for pylogix library 4 | 5 | - master branch contains the api 6 | - html branch contains api + html example [html](https://github.com/TheFern2/pylogix-api/tree/html) 7 | 8 | ## Video Demo 9 | 10 | [![Demo](https://img.youtube.com/vi/JIagCipFybE/0.jpg)](https://www.youtube.com/watch?v=JIagCipFybE) 11 | 12 | ## Dependencies 13 | 14 | - Flask 15 | 16 | ``` 17 | pip install flask 18 | ``` 19 | 20 | ## How to Run 21 | 22 | Linux or Mac (I don't know many Industrial Engineers with Macs, but hey you never know :) ): 23 | 24 | ``` 25 | python app.py 26 | or 27 | python3 app.py 28 | ``` 29 | 30 | Windows: 31 | 32 | ``` 33 | If there is only one python installed: 34 | python app.py 35 | Else 36 | py -3.7 app.py 37 | ``` 38 | 39 | ## Insomnia Setup 40 | 41 | I've exported a json file for insomnia for quick and easy testing, just import as a workspace. 42 | 43 | Preferences > Data > Import Data 44 | 45 | Then setup a pylogix-api-env, those two variables are used within the routes in insomnia: 46 | 47 | ``` 48 | { 49 | "plc_ip": "192.168.56.106", 50 | "plc_slot": "2" 51 | } 52 | ``` 53 | 54 | ## Usage 55 | 56 | In a browser or insomnia you can do the following get requests. 57 | 58 | Routes: 59 | 60 | ``` 61 | /pylogix/v1.0/plc///tags 62 | 63 | /pylogix/v1.0/plc///tags/ 64 | 65 | /pylogix/v1.0/plc/192.168.0.10/0/tags/[someTag1; someTag2; someTag3; someTag4] 66 | 67 | /pylogix/v1.0/plc/192.168.0.10/0/tags/someArrayTag[x]{a} 68 | 69 | /pylogix/v1.0/plc/192.168.0.10/0/tags/someArrayTag[x, y]{a} 70 | 71 | /pylogix/v1.0/plc/192.168.0.10/0/tags/someArrayTag[x,y,z]{a} 72 | 73 | /pylogix/v1.0/plc///devices 74 | ``` 75 | 76 | Example on local machine: 77 | 78 | Get a tag: 79 | 80 | ``` 81 | localhost:5000/pylogix/v1.0/plc/192.168.0.10/0/tags/someTag 82 | 83 | { 84 | "tag": { 85 | "status": "Success", 86 | "tagName": "BaseBOOL", 87 | "value": true 88 | } 89 | } 90 | ``` 91 | 92 | Get a list of tags: 93 | 94 | ``` 95 | localhost:5000/pylogix/v1.0/plc/192.168.1.196/2/tags/[BaseBOOL; BaseDINT; BaseINT] 96 | 97 | { 98 | "tags": [ 99 | { 100 | "status": "Success", 101 | "tagName": "BaseBOOL", 102 | "value": "True" 103 | }, 104 | { 105 | "status": "Success", 106 | "tagName": "BaseDINT", 107 | "value": "-545437484" 108 | }, 109 | { 110 | "status": "Success", 111 | "tagName": "BaseINT", 112 | "value": "6512" 113 | } 114 | ] 115 | } 116 | ``` 117 | 118 | Get a range from a 1 dim array: 119 | 120 | ``` 121 | localhost:5000/pylogix/v1.0/plc/192.168.1.196/2/tags/BaseDINTArray[0]{10} 122 | 123 | { 124 | "tag": { 125 | "status": "Success", 126 | "tagName": "BaseDINTArray[0]", 127 | "value": [ 128 | 1884903867, 129 | 1524930390, 130 | -1030834104, 131 | -1303090544, 132 | 388864688, 133 | 1027141633, 134 | -39957720, 135 | 58739743, 136 | 1392044477, 137 | -234739063 138 | ] 139 | } 140 | } 141 | ``` 142 | 143 | Example running the request from another machine, replace localhost with the machine ip where the server is running: 144 | 145 | ``` 146 | localhost:5000/pylogix/v1.0/plc/192.168.0.10/0/tags/someTag 147 | ``` 148 | 149 | ## Contributors 150 | 151 | Many thanks to: 152 | 153 | - [GitHubDragonFly](https://github.com/GitHubDragonFly) 154 | -------------------------------------------------------------------------------- /app.py: -------------------------------------------------------------------------------- 1 | #!flask/bin/python 2 | from flask import Flask, jsonify 3 | from pylogix import PLC 4 | 5 | app = Flask(__name__) 6 | 7 | @app.route('/pylogix/v1.0/plc///tags', methods=['GET']) 8 | def get_all_tags(ipAddress, slot): 9 | tags = [] 10 | 11 | comm = PLC(ipAddress, slot) 12 | ret = comm.GetTagList() 13 | comm.Close() 14 | 15 | if ret.Value is None: 16 | return jsonify(ret.Status) 17 | 18 | for tag in ret.Value: 19 | tags.append( 20 | {"tagName" : tag.TagName, 21 | "dataType" : tag.DataType 22 | }) 23 | 24 | return jsonify({'tags': tags}) 25 | 26 | @app.route('/pylogix/v1.0/plc///tags/', methods=['GET']) 27 | def get_tag(tag, ipAddress, slot): 28 | comm = PLC(ipAddress, slot) 29 | regularTags = [] 30 | arrayTags = dict() 31 | results = [] 32 | readArray = False 33 | arrayElementCount = 0 34 | showBoolAsOneZero = False 35 | 36 | if tag.startswith('[') and tag.endswith(']'): # array of mixed tags 37 | tags = (tag[1:-1].replace(' ', '')).split(';') 38 | 39 | for t in tags: 40 | if not t == '': 41 | if t.endswith('}') and '{' in t: # 1 or 2 or 3 dimensional array tag 42 | try: 43 | arrayElementCount = int(t[t.index('{') + 1:t.index('}')]) 44 | readArray = True 45 | t = t[:t.index('{')] 46 | arrayTags.update( {t : arrayElementCount} ) 47 | except: 48 | pass 49 | else: 50 | regularTags.append(t) 51 | 52 | if len(regularTags) > 0: 53 | ret = comm.Read(regularTags) 54 | 55 | if ret[0].Value is None: 56 | comm.Close() 57 | return jsonify(ret[0].Status) 58 | 59 | for i in range(0, len(ret)): 60 | if showBoolAsOneZero and (str(ret[i].Value) == 'True' or str(ret[i].Value) == 'False'): 61 | results.append( 62 | {"tagName" : str(ret[i].TagName), 63 | "value" : 1 if str(ret[i].Value) == 'True' else 0, 64 | "status" : str(ret[i].Status) 65 | }) 66 | else: 67 | results.append( 68 | {"tagName" : str(ret[i].TagName), 69 | "value" : str(ret[i].Value), 70 | "status" : str(ret[i].Status) 71 | }) 72 | 73 | if len(arrayTags) > 0: 74 | for tag in arrayTags: 75 | ret = comm.Read(tag, arrayTags[tag]) 76 | 77 | if ret.Value is None: 78 | comm.Close() 79 | return jsonify(ret.Status) 80 | 81 | if showBoolAsOneZero and (str(ret.Value[0]) == 'True' or str(ret.Value[0]) == 'False'): 82 | newBoolArray = [] 83 | for val in range(0, len(ret.Value)): 84 | newBoolArray.append(1 if str(ret.Value[val]) == 'True' else 0) 85 | 86 | results.append( 87 | {"tagName" : ret.TagName, 88 | "value" : newBoolArray, 89 | "status" : ret.Status 90 | }) 91 | else: 92 | results.append( 93 | {"tagName" : ret.TagName, 94 | "value" : ret.Value, 95 | "status" : ret.Status 96 | }) 97 | 98 | comm.Close() 99 | 100 | if len(regularTags) > 0 or len(arrayTags) > 0: 101 | return jsonify({'tags': results}) 102 | else: 103 | return jsonify('Not a valid tag!') 104 | else: 105 | if tag.endswith('}') and '{' in tag: # 1 or 2 or 3 dimensional array 106 | try: 107 | arrayElementCount = int(tag[tag.index('{') + 1:tag.index('}')]) 108 | readArray = True 109 | tag = tag[:tag.index('{')] 110 | except: 111 | pass 112 | else: 113 | pass 114 | 115 | if readArray and arrayElementCount > 0: 116 | ret = comm.Read(tag, arrayElementCount) 117 | 118 | if ret.Value is None: 119 | comm.Close() 120 | return jsonify(ret.Status) 121 | 122 | if showBoolAsOneZero and (str(ret.Value[0]) == 'True' or str(ret.Value[0]) == 'False'): 123 | newBoolArray = [] 124 | for val in range(0, len(ret.Value)): 125 | newBoolArray.append(1 if str(ret.Value[val]) == 'True' else 0) 126 | 127 | results.append( 128 | {"tagName" : ret.TagName, 129 | "value" : newBoolArray, 130 | "status" : ret.Status 131 | }) 132 | else: 133 | results.append( 134 | {"tagName" : ret.TagName, 135 | "value" : ret.Value, 136 | "status" : ret.Status 137 | }) 138 | else: 139 | ret = comm.Read(tag) 140 | 141 | if ret.Value is None: 142 | comm.Close() 143 | return jsonify(ret.Status) 144 | 145 | if showBoolAsOneZero and (str(ret.Value) == 'True' or str(ret.Value) == 'False'): 146 | results.append( 147 | {"tagName" : ret.TagName, 148 | "value" : 1 if str(ret.Value) == 'True' else 0, 149 | "status" : ret.Status 150 | }) 151 | else: 152 | results.append( 153 | {"tagName" : ret.TagName, 154 | "value" : ret.Value, 155 | "status" : ret.Status 156 | }) 157 | 158 | comm.Close() 159 | 160 | return jsonify({'tag': results}) 161 | 162 | @app.route('/pylogix/v1.0/plc///devices', methods=['GET']) 163 | def get_devices(ipAddress, slot): 164 | devices = [] 165 | 166 | comm = PLC(ipAddress, slot) 167 | ret = comm.Discover() 168 | comm.Close() 169 | 170 | if ret.Value == []: 171 | return jsonify('No Devices Discovered') 172 | 173 | for device in ret.Value: 174 | devices.append( 175 | {"productName": device.ProductName, 176 | "revision" : device.Revision 177 | }) 178 | 179 | return jsonify({'devices': devices}) 180 | 181 | if __name__ == '__main__': 182 | app.run(host='0.0.0.0') --------------------------------------------------------------------------------