目录
前言
ProcessDB实时/时序数据库支持Python语言开发,本文将针对Python操作ProcessDB库的实时数据的相关操作进行介绍
一、实时数据字段介绍
字段 | 注释 |
id | 数据点id |
name | 数据点名 |
value | 实时数据值 |
nTagType | 数据类型 |
nQuality | 数据质量 |
nMilliSecond | 数据时间(毫秒) |
nSecond | 数据时间(秒) |
二、根据点名插入实时数据
示例代码:
# coding:utf8
import ProcessDBDriver as processdbDriver
import numpy as np
import calendar
import time
# 点的数据类型
# float32--0 digital--1 bBool--2 uInt8--3 Int8--4 uInt16--5 Int16--6 nuInt32--7 nInt32--8 nuInt64--9 nInt64--10 fFloat64--11 nTime--12
nSecond = calendar.timegm(time.gmtime())
print(nSecond)
dataList = ['D2.MODEL_RESULT.PIPELINE01', 2022.202]
# 连接
connectName = '127.0.0.1'
port = 8301
processdbDriver.connect(connectName, port)
# 登录
userName = 'root'
password = 'root'
wErrCode = processdbDriver.login(userName, password)
nQuality = 0x1100
# 登录成功
if wErrCode != 0:
print('login failed. errorCode: ', wErrCode)
else:
# 插入实时数据--前置条件:新建点
errCode = processdbDriver.insert_real_time_data_by_name(
'D2.MODEL_RESULT.PIPELINE01', nQuality, dataList[1])
if errCode == 0:
print('insert_realtime_data succeed!')
else:
print('insert_realtime_data failed, errorCode:', errCode)
三、根据点ID插入实时数据
示例代码:
# coding:utf8
import ProcessDBDriver as processdbDriver
import numpy as np
# 点的数据类型
# float32--0 digital--1 bBool--2 uInt8--3 Int8--4 uInt16--5 Int16--6 nuInt32--7 nInt32--8 nuInt64--9 nInt64--10 fFloat64--11 nTime--12
dataList = [1001000200, 0, np.float32(666)]
#连接
connectName = '127.0.0.1'
port = 8301
processdbDriver.connect(connectName, port)
#登录
userName = 'root'
password = 'root'
wErrCode = processdbDriver.login(userName, password)
#登录成功
if wErrCode != 0:
print('login failed. errorCode: ', wErrCode)
else:
nSecond = 1562045480
nMilliSecond = 0
nQuality = 0
#插入实时数据--前置条件:新建点
errCode = processdbDriver.insert_real_time_data(dataList[0], nSecond, nMilliSecond, nQuality, dataList[1], dataList[2])
if errCode == 0:
print('insert_realtime_data succeed!')
else:
print('insert_realtime_data failed, errorCode:',errCode)
四、点名查询实时数据
示例代码:
# coding:utf8
import ProcessDBDriver as processdbDriver
import numpy as np
# 点的数据类型
# float32--0 digital--1 bBool--2 uInt8--3 Int8--4 uInt16--5 Int16--6 nuInt32--7 nInt32--8 nuInt64--9 nInt64--10 fFloat64--11 nTime--12
pointNameList = [
'PUBLIC.SYS.PDB_OS_CPU_USAGE', 'PUBLIC.SAMPLE.SAMPLE_DIGITAL', 'PUBLIC.SAMPLE.SAMPLE_BOOL','PUBLIC.SAMPLE.SAMPLE_UINT8',\
'PUBLIC.SAMPLE.SAMPLE_INT8', 'PUBLIC.SAMPLE.SAMPLE_UINT16','PUBLIC.SAMPLE.SAMPLE_INT16', 'PUBLIC.SAMPLE.SAMPLE_UINT32','PUBLIC.SAMPLE.SAMPLE_INT32',\
'PUBLIC.SAMPLE.SAMPLE_UINT64', 'PUBLIC.SAMPLE.SAMPLE_INT64','PUBLIC.SAMPLE.SAMPLE_FLOAT32','PUBLIC.SAMPLE.SAMPLE_TIME','D1.4_1.35_C_YOUGONG'
]
#连接
connectName = '127.0.0.1'
port = 8301
processdbDriver.connect(connectName, port)
#登录
userName = 'admin'
password = 'admin'
wErrCode = processdbDriver.login(userName, password)
#登录成功
if wErrCode != 0:
print('login failed. errorCode: ', wErrCode)
else:
#点名查询实时数据
for i in range(len(pointNameList)):
count, errCode, resultSet = processdbDriver.query_realTime_data_by_pointName(pointNameList[i])
if errCode == 0:
if count != 0 and count != 'null':
print('query_realTime_data_by_pointName succeed.')
print(' Realtime-data-pointName:',pointNameList[i])
print(' Realtime-data-Second:',resultSet.nSecond)
print(' Realtime-data-MilliSecond :',resultSet.nMilliSecond)
print(' Realtime-data-Quality:',resultSet.nQuality)
print(' Realtime-data-TagType:',resultSet.nTagType)
print(' Realtime-data-value:',resultSet.getValue())
else:
print('pointName:', pointNameList[i], ',','No Realtime data')
else:
print('query_realTime_data_by_pointName failed.errorCode: ', errCode)
五、点ID查询实时数据
示例代码:
# coding:utf8
import ProcessDBDriver as processdbDriver
import numpy as np
# 点的数据类型
# float32--0 digital--1 bBool--2 uInt8--3 Int8--4 uInt16--5 Int16--6 nuInt32--7 nInt32--8 nuInt64--9 nInt64--10 fFloat64--11 nTime--12
pointIdList = [1001000001, 1001000101, 1001000102, 1001000103, 1001000104, 1001000105, 1001000106, 1001000107, 1001000108, 1001000109, 1001000110, 1001000111, 1001000112]
#连接
connectName = '127.0.0.1'
port = 8301
processdbDriver.connect(connectName, port)
#登录
userName = 'admin'
password = 'admin'
wErrCode = processdbDriver.login(userName, password)
#登录成功
if wErrCode != 0:
print('login failed. errorCode: ', wErrCode)
else:
#点id查询实时数据
for i in range(len(pointIdList)) :
count, errCode, resultSet = processdbDriver.query_realTime_data_by_pointId(pointIdList[i])
if errCode == 0:
if count != 0 and count != 'null':
print('query_realTime_data_by_pointId succeed.')
print(' Realtime-data-pointId:',pointIdList[i])
print(' Realtime-data-Second:',resultSet.nSecond)
print(' Realtime-data-MilliSecond:',resultSet.nMilliSecond)
print(' Realtime-data-Quality:',resultSet.nQuality)
print(' Realtime-data-TagType:',resultSet.nTagType)
print(' Realtime-data-value:',resultSet.getValue())
else:
print('pointId:', pointIdList[i], ',','No Realtime data')
else:
print('query_realTime_data_by_pointId failed.errorCode: ', errCode)