使用Python通過oBIX協(xié)議訪問Niagara數(shù)據(jù)的示例
oBIX 全稱是 Open Building Information Exchange,它是基于 RESTful Web Service 的接口的標(biāo)準(zhǔn),用于構(gòu)建控制系統(tǒng)。oBIX是在專為樓宇自動(dòng)化設(shè)計(jì)的框架內(nèi),使用XML和URI在設(shè)備網(wǎng)絡(luò)上讀寫數(shù)據(jù)的。
因項(xiàng)目需要使用 Python 對 Niagara 軟件中的數(shù)據(jù)進(jìn)行讀寫和控制,所以寫了一個(gè)該協(xié)議的Python版本包,發(fā)布在這里:https://pypi.org/project/oBIX/
使用 pip 安裝使用即可:
pip install oBIX
本文主要介紹使用 Python 通過 oBIX 協(xié)議對 Niagara 軟件中的點(diǎn)進(jìn)行讀、寫操作。
一、準(zhǔn)備工作
1. 在 Niagara 軟件中配置好 oBIX 協(xié)議,確保已經(jīng)可以正常訪問;
(1)Palette 搜 oBIX, 添加一個(gè) ObixNetwork 到 Drivers中
(2)Palette 搜 baja, 將 AuthenticationSchemes/WebServicesSchemes/的 HTTPBasicScheme 拖拽到 Services/AuthenticationService/Authentication Schemes/
(3)UserServices 右鍵 View, AX User Manager下新建一個(gè)用戶,配置如下:
* 用戶名:oBIX
* 密碼:oBIX.12345
* Authentication Schemes Name 選:HTTPBasicScheme
* Admin 權(quán)限
2. Niagara 中新建一個(gè)數(shù)值類型的可讀寫的點(diǎn),命名為:temp1,完整路徑是:/config/AHU/temp1/,后面以此為例進(jìn)行訪問
3. 安裝python的oBIX包:pip install oBIX
二、快速開始
from oBIX.common import Point, DataType from oBIX import Client if __name__ == '__main__': # ip, userName, password # 可選項(xiàng): # port: 端口號(hào),如:8080 # https: 是否使用 https,默認(rèn):True client = Client("127.0.0.1", "oBIX", "oBIX.12345") # 點(diǎn)的路徑 point_path = "/config/AHU/temp1/" # 讀取一個(gè)點(diǎn)的值 point_value = client.read_point_value(point_path) print("point value is {0}".format(point_value))
三、基本實(shí)例
3.1 讀取點(diǎn)
# 點(diǎn)的路徑 point_path = "/config/AHU/temp1/" # 讀取一個(gè)點(diǎn)的值 point_value = client.read_point_value(point_path) print("point value is {0}".format(point_value)) # 讀取一個(gè)點(diǎn)實(shí)例 # 然后就能獲取到這個(gè)點(diǎn)所包含的常用屬性 # 例如:name, val, status, display, href, in1, in2 ... in16, fallback, out point_obj = client.read_point(point_path) print("name is {0}".format(point_obj.name)) print("fallback is {0}".format(point_obj.fallback)) print("in10 is {0}".format(point_obj.in10)) # 也可以使用下面代碼直接獲取 point_in10_value = client.read_point_slot(point_path, "in10") print("in10 is {0}".format(point_in10_value))
3.2 寫入點(diǎn)
# 點(diǎn)的路徑 point_path = "/config/AHU/temp1/" # set 一個(gè)點(diǎn)的值 client.write_point(point_path, 15.2, DataType.real) # set point auto client.set_point_auto(point_path, DataType.real) # override a point client.override_point(point_path, 14, DataType.real) # emergency override a point client.emergency_override_point(point_path, 15, DataType.real) # set a point emergency auto client.set_point_emergency_auto(point_path, DataType.real)
四、高級(jí)應(yīng)用
4.1 讀取歷史數(shù)據(jù)
# 起始時(shí)間 start_time = datetime.now(tz=timezone(timedelta(hours=8))) - timedelta(minutes=10) # 結(jié)束時(shí)間 end_time = datetime.now(tz=timezone(timedelta(hours=8))) # 讀取該斷時(shí)間內(nèi)的歷史數(shù)據(jù) history = client.read_history("Station01", "OutDoorTemp", start_time, end_time) # 取起始時(shí)間往后指定個(gè)數(shù)的歷史數(shù)據(jù) limit_num = 1 history = client.read_history("Station01", "OutDoorTemp", start_time=start_time, limit=limit_num)
4.2 讀取報(bào)警數(shù)據(jù)
# 起始時(shí)間 start_time = datetime.now(tz=timezone(timedelta(hours=8))) - timedelta(minutes=10) # 結(jié)束時(shí)間 end_time = datetime.now(tz=timezone(timedelta(hours=8))) # 讀取該段時(shí)間內(nèi)的報(bào)警數(shù)據(jù) alarms = client.read_alarms("Station01", "OutDoorTemp", start_time, end_time) # 取起始時(shí)間往后指定個(gè)數(shù)的報(bào)警數(shù)據(jù) limit_num = 1 alarms = client.read_alarms("Station01", "OutDoorTemp", start_time=start_time, limit=limit_num)
4.3 監(jiān)控點(diǎn)的數(shù)據(jù)變化
監(jiān)控點(diǎn)的數(shù)據(jù)變化時(shí) oBIX 協(xié)議的一部分。添加想要監(jiān)控的點(diǎn),然后當(dāng) Niagara 中點(diǎn)的值發(fā)生變化后,會(huì)自動(dòng)觸發(fā)相應(yīng)的函數(shù)。
from oBIX.common import Point, DataType from oBIX import Client def init_watch(): global client, point_path # 添加監(jiān)控 point_path_list = [point_path] # 這里可以是多個(gè)點(diǎn) result = client.add_watch_points(point_path_list) client.watch_changed_handler.on_change += on_watch_changed # Niagara 里改點(diǎn)的值發(fā)生變化時(shí),會(huì)自動(dòng)觸發(fā)改函數(shù) def on_watch_changed(points: [Point]): for point in points: val = point.val print(f"on_watch_changed: {val}") if __name__ == '__main__': # ip, userName, password # 可選項(xiàng): # port: 端口號(hào),如:8080 # https: 是否使用 https,默認(rèn):True client = Client("127.0.0.1", "oBIX", "oBIX.12345") # 點(diǎn)的路徑 point_path = "/config/AHU/temp1/" init_watch() client.start_watch() while True: i = 0
4.4 導(dǎo)出所有點(diǎn)的信息
如果一個(gè)項(xiàng)目中有大量的目錄和點(diǎn),手動(dòng)挨個(gè)去寫比較麻煩,所以這里提供了一個(gè)導(dǎo)出點(diǎn)信息的函數(shù)。將點(diǎn)的信息保存文件后,再直接從文件中讀取點(diǎn)的信息就會(huì)方便很多。
# 導(dǎo)出所有點(diǎn)的信息 export_result = client.export_points() # folder_path [optional]: 想要導(dǎo)出的目錄,如: "/config/xxx/",默認(rèn)會(huì)導(dǎo)出所有點(diǎn)的信息 # export_file_name [optional]: 導(dǎo)出文件的名稱,默認(rèn): "all_points.json" # export_type [optional]: # 0: JSON格式,嵌套格式并保留目錄信息 # 1: JSON格式, 只保留點(diǎn)的信息,不保留目錄信息 # 2: 字符串列表格式, 只輸出點(diǎn)的路徑信息 export_result = client.export_points(folder_path="/config/AHU/", export_file_name="output.json", export_type=1)
以上就是使用Python通過oBIX協(xié)議訪問Niagara數(shù)據(jù)的示例的詳細(xì)內(nèi)容,更多關(guān)于Python通過oBIX協(xié)議訪問Niagara數(shù)據(jù)的資料請關(guān)注腳本之家其它相關(guān)文章!
- Python3.7 pyodbc完美配置訪問access數(shù)據(jù)庫
- 詳解js文件通過python訪問數(shù)據(jù)庫方法
- 對Python通過pypyodbc訪問Access數(shù)據(jù)庫的方法詳解
- Python使用pyodbc訪問數(shù)據(jù)庫操作方法詳解
- Python輕量級(jí)ORM框架Peewee訪問sqlite數(shù)據(jù)庫的方法詳解
- Python的Tornado框架實(shí)現(xiàn)異步非阻塞訪問數(shù)據(jù)庫的示例
- Linux下通過python訪問MySQL、Oracle、SQL Server數(shù)據(jù)庫的方法
- python訪問mysql數(shù)據(jù)庫的實(shí)現(xiàn)方法(2則示例)
- python使用MySQLdb訪問mysql數(shù)據(jù)庫的方法
- Python訪問純真IP數(shù)據(jù)庫腳本分享
- 在Linux中通過Python腳本訪問mdb數(shù)據(jù)庫的方法
- Shell、Perl、Python、PHP訪問 MySQL 數(shù)據(jù)庫代碼實(shí)例
- python訪問純真IP數(shù)據(jù)庫的代碼
相關(guān)文章
Django用戶注冊并自動(dòng)關(guān)聯(lián)到某數(shù)據(jù)表?xiàng)l目的實(shí)現(xiàn)步驟
當(dāng)一個(gè)新用戶注冊并且你想要自動(dòng)關(guān)聯(lián)到特定的Box條目(假設(shè)其ID為1)時(shí),下面給大家分享完整實(shí)現(xiàn)流程和步驟,對Django關(guān)聯(lián)數(shù)據(jù)表?xiàng)l目實(shí)現(xiàn)代碼感興趣的朋友跟隨小編一起看看吧2017-04-04Python JSON格式數(shù)據(jù)的提取和保存的實(shí)現(xiàn)
這篇文章主要介紹了Python JSON格式數(shù)據(jù)的提取和保存的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-03-03python題解LeetCode303區(qū)域和檢索示例詳解
這篇文章主要為大家介紹了python題解LeetCode303區(qū)域和檢索示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12Python如何提取csv數(shù)據(jù)并篩選指定條件數(shù)據(jù)詳解
在學(xué)習(xí)python過程中常遇到一種情況,要讀取.csv文件的數(shù)據(jù),然后取出其中某個(gè)字段,下面這篇文章主要給大家介紹了關(guān)于Python如何提取csv數(shù)據(jù)并篩選指定條件數(shù)據(jù)的相關(guān)資料,需要的朋友可以參考下2022-08-08Pandas告警UserWarning:pandas?only?supports?SQLAlchemy?conn
這篇文章主要給大家介紹了關(guān)于Pandas告警UserWarning:pandas only supports SQLAlchemy connectable的處理方式,文中還分享了pandas還有哪些userwarning,對大家學(xué)習(xí)或者工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2024-02-02