使用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, DataTypefrom oBIX import Clientif __name__ == ’__main__’: # ip, userName, password # 可選項(xiàng): # port: 端口號,如: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)
四、高級應(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, DataTypefrom oBIX import Clientdef 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: 端口號,如: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)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章:
1. Python 實(shí)現(xiàn)勞拉游戲的實(shí)例代碼(四連環(huán)、重力四子棋)2. Python+unittest+requests 接口自動(dòng)化測試框架搭建教程3. Java GZip 基于內(nèi)存實(shí)現(xiàn)壓縮和解壓的方法4. jsp+servlet簡單實(shí)現(xiàn)上傳文件功能(保存目錄改進(jìn))5. JavaScript數(shù)據(jù)結(jié)構(gòu)之雙向鏈表6. 利用CSS制作3D動(dòng)畫7. 一款功能強(qiáng)大的markdown編輯器tui.editor使用示例詳解8. 存儲(chǔ)于xml中需要的HTML轉(zhuǎn)義代碼9. SpringBoot+TestNG單元測試的實(shí)現(xiàn)10. .Net加密神器Eazfuscator.NET?2023.2?最新版使用教程
