使用Python通過(guò)oBIX協(xié)議訪問(wèn)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 對(duì) Niagara 軟件中的數(shù)據(jù)進(jìn)行讀寫和控制,所以寫了一個(gè)該協(xié)議的Python版本包,發(fā)布在這里:https://pypi.org/project/oBIX/
使用 pip 安裝使用即可:
pip install oBIX
本文主要介紹使用 Python 通過(guò) oBIX 協(xié)議對(duì) Niagara 軟件中的點(diǎn)進(jìn)行讀、寫操作。
一、準(zhǔn)備工作
1. 在 Niagara 軟件中配置好 oBIX 協(xié)議,確保已經(jīng)可以正常訪問(wèn);
(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)行訪問(wèn)
3. 安裝python的oBIX包:pip install oBIX
二、快速開(kāi)始
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通過(guò)oBIX協(xié)議訪問(wèn)Niagara數(shù)據(jù)的示例的詳細(xì)內(nèi)容,更多關(guān)于Python通過(guò)oBIX協(xié)議訪問(wèn)Niagara數(shù)據(jù)的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
- Python3.7 pyodbc完美配置訪問(wèn)access數(shù)據(jù)庫(kù)
- 詳解js文件通過(guò)python訪問(wèn)數(shù)據(jù)庫(kù)方法
- 對(duì)Python通過(guò)pypyodbc訪問(wèn)Access數(shù)據(jù)庫(kù)的方法詳解
- Python使用pyodbc訪問(wèn)數(shù)據(jù)庫(kù)操作方法詳解
- Python輕量級(jí)ORM框架Peewee訪問(wèn)sqlite數(shù)據(jù)庫(kù)的方法詳解
- Python的Tornado框架實(shí)現(xiàn)異步非阻塞訪問(wèn)數(shù)據(jù)庫(kù)的示例
- Linux下通過(guò)python訪問(wèn)MySQL、Oracle、SQL Server數(shù)據(jù)庫(kù)的方法
- python訪問(wèn)mysql數(shù)據(jù)庫(kù)的實(shí)現(xiàn)方法(2則示例)
- python使用MySQLdb訪問(wèn)mysql數(shù)據(jù)庫(kù)的方法
- Python訪問(wèn)純真IP數(shù)據(jù)庫(kù)腳本分享
- 在Linux中通過(guò)Python腳本訪問(wèn)mdb數(shù)據(jù)庫(kù)的方法
- Shell、Perl、Python、PHP訪問(wèn) MySQL 數(shù)據(jù)庫(kù)代碼實(shí)例
- python訪問(wèn)純真IP數(shù)據(jù)庫(kù)的代碼
相關(guān)文章
Python利用Gradio與EasyOCR構(gòu)建在線識(shí)別文本的Web應(yīng)用
隨著人工智能的不斷發(fā)展,各種智能算法越來(lái)越普遍,本文就給大家介紹一種通過(guò)訓(xùn)練好的算法進(jìn)行文字識(shí)別的方法,而且是Web頁(yè)面可視化操作,方便調(diào)用,希望大家喜歡2023-04-04
Python3的unicode編碼轉(zhuǎn)換成中文的問(wèn)題及解決方案
這篇文章主要介紹了Python3的unicode編碼轉(zhuǎn)換成中文的問(wèn)題及解決方案,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-12-12
python中類的輸出或類的實(shí)例輸出為<__main__類名 object at xxxx>這種形式的原因
在本篇文章里小編給大家分享了關(guān)于python中類的輸出或類的實(shí)例輸出為何是<__main__類名 object at xxxx>這種形式,需要的朋友們可以參考下。2019-08-08
python錯(cuò)誤提示:Errno?2]?No?such?file?or?directory的解決方法
我相信很多人在學(xué)習(xí)Python的時(shí)候,特別是在open文件的時(shí)候總還碰到,還報(bào)錯(cuò)IOError:[Errno?2]沒(méi)有這樣的文件或目錄:'E://aaa.txt',這篇文章主要給大家介紹了關(guān)于python錯(cuò)誤提示:Errno?2]?No?such?file?or?directory的解決方法,需要的朋友可以參考下2022-02-02
使用Python實(shí)現(xiàn)網(wǎng)絡(luò)設(shè)備配置備份與恢復(fù)
網(wǎng)絡(luò)設(shè)備配置備份與恢復(fù)在網(wǎng)絡(luò)安全管理中起著至關(guān)重要的作用,本文為大家介紹了如何通過(guò)Python實(shí)現(xiàn)網(wǎng)絡(luò)設(shè)備配置備份與恢復(fù),需要的可以參考下2025-03-03

