Skip to content

设备因子

设备因子是设备的数据点配置,用于定义设备的数据采集和控制点。通过因子API,您可以创建、查询、更新和删除设备的因子配置。

获取因子列表

http
GET /api/v2/devices/{device_id}/factors/

接口说明

获取指定设备的所有因子配置。

请求参数

参数类型必填说明
device_idinteger设备ID

响应

返回数据 (200 OK)

json
{
    "success": true,
    "data": [
        {
            "pk": 5855,
            "device": 17644,
            "name": "fe",
            "unit": 1,
            "address": 0,
            "the_type": 200,
            "the_type_detail": {
                "name": "远程开关",
                "unit": "",
                "icon": "https://agri-static.holdingbyte.com/media/icon/200.png"
            },
            "data_endian": ">",
            "data_endian_display": "Big-Endian (ABCD)",
            "data_type": "int8",
            "data_type_display": "8-bit Integer",
            "data_index": 0,
            "modbus_type": 1,
            "modbus_type_display": "Coil",
            "data_factor": 1.0,
            "data_delta": 0.0,
            "info": "",
            "enabled": true,
            "agri_id": "d-1000-qyjngeufmfqf-1-00"
        }
    ],
    "error": null
}

响应参数

字段类型说明
pkinteger因子主键ID
deviceinteger设备ID
namestring因子名称
unitintegerModbus 从站地址
addressinteger寄存器地址
the_typeinteger因子类型,请参考 因子类型定义
the_type_detailobject因子类型详情
data_endianstring数据大小端(big/little)
data_endian_displaystring数据大小端显示名称
data_typestring数据类型(如:int8/int16/float等)
data_type_displaystring数据类型显示名称
data_indexinteger数据读取索引
modbus_typeintegerModbus寄存器类型
modbus_type_displaystringModbus寄存器类型显示名称
data_factorfloat数据因子,用于数据转换
data_deltafloat数据差值
infostring备注信息
enabledboolean是否启用
agri_idstring因子内部ID

代码示例

Python
python
def get_device_factors(device_id):
    """获取设备的所有因子
    
    Args:
        device_id: 设备ID
    """
    headers = {
        "Authorization": f"Bearer {ACCESS_TOKEN}"
    }
    
    url = f"{API_BASE}/devices/{device_id}/factors/"
    response = requests.get(url, headers=headers)
    return response.json()

# 使用示例
device_id = 1
factors = get_device_factors(device_id)
print(f"设备有 {len(factors['data'])} 个因子")
cURL
bash
curl -X GET "https://ums.holdingbyte.com/api/v2/devices/1/factors/" \
     -H "Authorization: Bearer your_access_token"

获取因子详情

http
GET /api/v2/devices/{device_id}/factors/{factor_id}/

接口说明

获取指定设备的指定因子详细配置。

请求参数

参数类型必填说明
device_idinteger设备ID
factor_idinteger因子ID

响应

返回数据 (200 OK)

json
{
    "success": true,
    "data": {
        "pk": 1,
        "device": 1,
        "name": "温度",
        "unit": 1,
        "address": 0,
        "the_type": "temp",
        "the_type_detail": {
            "name": "温度",
            "unit": "℃",
            "icon": "https://ums.holdingbyte.com/media/icon/temp.png"
        },
        "data_endian": "big",
        "data_endian_display": "大端",
        "data_type": "int16",
        "data_type_display": "16位整数",
        "data_index": 0,
        "modbus_type": "holding",
        "modbus_type_display": "保持寄存器",
        "data_factor": 0.1,
        "data_delta": 0,
        "info": "温度传感器",
        "enabled": true,
        "agri_id": "DEVICE001-1-00"
    },
    "error": null
}

代码示例

Python
python
def get_factor_detail(device_id, factor_id):
    """获取因子详情
    
    Args:
        device_id: 设备ID
        factor_id: 因子ID
    """
    headers = {
        "Authorization": f"Bearer {ACCESS_TOKEN}"
    }
    
    url = f"{API_BASE}/devices/{device_id}/factors/{factor_id}/"
    response = requests.get(url, headers=headers)
    return response.json()

# 使用示例
device_id = 1
factor_id = 1
factor = get_factor_detail(device_id, factor_id)
print(f"因子名称: {factor['data']['name']}")
cURL
bash
curl -X GET "https://ums.holdingbyte.com/api/v2/devices/1/factors/1/" \
     -H "Authorization: Bearer your_access_token"

创建因子

http
POST /api/v2/devices/{device_id}/factors/

接口说明

为指定设备创建新的因子配置。

请求参数

参数类型必填说明
namestring因子名称
unitintegerModbus 从站地址
addressinteger寄存器地址
the_typestring设备类型
data_endianstring数据大小端(big/little)
data_typestring数据类型(如:int16/uint16/float等)
data_indexinteger数据读取索引
modbus_typestringModbus寄存器类型(holding/input/coil/discrete)
data_factorfloat数据因子,用于数据转换,默认为1.0
data_deltafloat数据差值,默认为0.0
infostring备注信息
enabledboolean是否启用,默认true

请求示例

请求数据

json
{
    "name": "温度传感器",
    "unit": 1,
    "address": 0,
    "the_type": "temp",
    "data_endian": "big",
    "data_type": "int16",
    "data_index": 0,
    "modbus_type": "holding",
    "data_factor": 0.1,
    "data_delta": 0,
    "info": "温度传感器",
    "enabled": true
}

响应

返回数据 (200 OK)

json
{
    "success": true,
    "data": {
        "pk": 1,
        "device": 1,
        "name": "温度传感器",
        "unit": 1,
        "address": 0,
        "the_type": "temp",
        "the_type_detail": {
            "name": "温度",
            "unit": "℃",
            "icon": "https://ums.holdingbyte.com/media/icon/temp.png"
        },
        "data_endian": "big",
        "data_endian_display": "大端",
        "data_type": "int16",
        "data_type_display": "16位整数",
        "data_index": 0,
        "modbus_type": "holding",
        "modbus_type_display": "保持寄存器",
        "data_factor": 0.1,
        "data_delta": 0,
        "info": "温度传感器",
        "enabled": true,
        "agri_id": "DEVICE001-1-00"
    },
    "error": null
}

代码示例

Python
python
def create_factor(device_id, factor_data):
    """创建新因子
    
    Args:
        device_id: 设备ID
        factor_data: 因子配置数据
    """
    headers = {
        "Authorization": f"Bearer {ACCESS_TOKEN}",
        "Content-Type": "application/json"
    }
    
    url = f"{API_BASE}/devices/{device_id}/factors/"
    response = requests.post(url, headers=headers, json=factor_data)
    return response.json()

# 使用示例
device_id = 1
factor_data = {
    "name": "温度传感器",
    "unit": 1,
    "address": 0,
    "the_type": "temp",
    "data_endian": "big",
    "data_type": "int16",
    "data_index": 0,
    "modbus_type": "holding",
    "data_factor": 0.1,
    "data_delta": 0,
    "info": "温度传感器",
    "enabled": True
}
result = create_factor(device_id, factor_data)
print(f"创建的因子ID: {result['data']['pk']}")
cURL
bash
curl -X POST "https://ums.holdingbyte.com/api/v2/devices/1/factors/" \
     -H "Authorization: Bearer your_access_token" \
     -H "Content-Type: application/json" \
     -d '{
         "name": "温度传感器",
         "unit": 1,
         "address": 0,
         "the_type": "temp",
         "data_endian": "big",
         "data_type": "int16",
         "data_index": 0,
         "modbus_type": "holding",
         "data_factor": 0.1,
         "data_delta": 0,
         "info": "温度传感器",
         "enabled": true
     }'

更新因子

http
PUT /api/v2/devices/{device_id}/factors/{factor_id}/

接口说明

更新指定设备的指定因子配置。

请求参数

参数类型必填说明
namestring因子名称
unitintegerModbus 从站地址
addressinteger寄存器地址
the_typestring设备类型
data_endianstring数据大小端(big/little)
data_typestring数据类型(如:int16/uint16/float等)
data_indexinteger数据读取索引
modbus_typestringModbus寄存器类型(holding/input/coil/discrete)
data_factorfloat数据因子,用于数据转换
data_deltafloat数据差值
infostring备注信息
enabledboolean是否启用

请求示例

请求数据

json
{
    "name": "更新后的温度传感器",
    "info": "更新后的备注信息"
}

响应

返回数据 (200 OK)

json
{
    "success": true,
    "data": {
        "pk": 1,
        "device": 1,
        "name": "更新后的温度传感器",
        // 其他字段...
        "info": "更新后的备注信息",
        "enabled": true,
        "agri_id": "DEVICE001-1-00"
    },
    "error": null
}

代码示例

Python
python
def update_factor(device_id, factor_id, factor_data):
    """更新因子
    
    Args:
        device_id: 设备ID
        factor_id: 因子ID
        factor_data: 要更新的因子数据
    """
    headers = {
        "Authorization": f"Bearer {ACCESS_TOKEN}",
        "Content-Type": "application/json"
    }
    
    url = f"{API_BASE}/devices/{device_id}/factors/{factor_id}/"
    response = requests.put(url, headers=headers, json=factor_data)
    return response.json()

# 使用示例
device_id = 1
factor_id = 1
update_data = {
    "name": "更新后的温度传感器",
    "info": "更新后的备注信息"
}
result = update_factor(device_id, factor_id, update_data)
print(f"更新结果: {result['success']}")
cURL
bash
curl -X PUT "https://ums.holdingbyte.com/api/v2/devices/1/factors/1/" \
     -H "Authorization: Bearer your_access_token" \
     -H "Content-Type: application/json" \
     -d '{
         "name": "更新后的温度传感器",
         "info": "更新后的备注信息"
     }'

删除因子

http
DELETE /api/v2/devices/{device_id}/factors/{factor_id}/

接口说明

删除指定设备的指定因子配置。

请求参数

参数类型必填说明
device_idinteger设备ID
factor_idinteger因子ID

响应

返回数据 (200 OK)

json
{
    "success": true,
    "data": null,
    "error": null
}

代码示例

Python
python
def delete_factor(device_id, factor_id):
    """删除因子
    
    Args:
        device_id: 设备ID
        factor_id: 因子ID
    """
    headers = {
        "Authorization": f"Bearer {ACCESS_TOKEN}"
    }
    
    url = f"{API_BASE}/devices/{device_id}/factors/{factor_id}/"
    response = requests.delete(url, headers=headers)
    return response.json()

# 使用示例
device_id = 1
factor_id = 1
result = delete_factor(device_id, factor_id)
print(f"删除结果: {result['success']}")
cURL
bash
curl -X DELETE "https://ums.holdingbyte.com/api/v2/devices/1/factors/1/" \
     -H "Authorization: Bearer your_access_token"

控制因子

因子为开关类型时,比如IO控制器、智能配电箱,可以控制该因子的开启、关闭。详细信息请参阅控制因子文档

获取因子数据

获取因子的历史数据记录,详细信息请参阅因子数据文档

错误响应

错误响应

因子API可能返回的错误响应遵循统一的格式。有关详细的错误码和处理方法,请参阅API错误响应文档。

错误响应示例

json
{
    "success": false,
    "data": null,
    "error": {
        "code": 404,
        "message": "因子不存在"
    }
}