329 lines
10 KiB
Python

# -*- coding: utf-8 -*-
from website import errors
from website.handler import APIHandler, authenticated
from website.db.enterprise_entity import enterprise_entity as DB_Entity
from website.db.enterprise_node import enterprise_node as DB_Node
from website.db.enterprise_busi_model import enterprise_busi_model as DB_BusiModel
from website.db.enterprise_busi_model import enterprise_busi_model_node_device as DB_BusiModelNodeDevice
from website.db.enterprise_device import enterprise_device as DB_Device
from website.util import shortuuid
class AddHandler(APIHandler):
"""
- 描述添加企业节点
- 请求方式post
- 请求参数
>- entity_suid, int, 企业id, short uuid
>- name, string, 名称
>- parent, int, 上级节点id
>- addr, string, 地址
>- lola, string, 经纬度longitude and latitude
>- contact, string, 负责人
>- phone, string, 联系方式
>- comment, string, 简介
- 返回值
"""
# @authenticated
def post(self):
entity_id = self.get_int_argument("entity_suid")
entity_suid = self.get_escaped_argument("entity_suid", "")
name = self.get_escaped_argument("name", "")
parent = self.get_int_argument("parent", 0)
addr = self.get_escaped_argument("addr", "")
lola = self.get_escaped_argument("lola", "")
contact = self.get_escaped_argument("contact", "")
phone = self.get_escaped_argument("phone", "")
comment = self.get_escaped_argument("comment", "")
if not entity_id or not name:
raise errors.HTTPAPIError(errors.ERROR_BAD_REQUEST, "参数错误")
db_entity = DB_Entity.EnterpriseEntityRepository()
entity_suid = db_entity.get_entity_suid(entity_id)
db_node = DB_Node.EnterpriseNodeRepository()
db_node.insert_node({
"suid": shortuuid.ShortUUID().random(length=10),
"entity_id": entity_id,
"entity_suid": entity_suid,
"name": name,
"parent": parent,
"addr": addr,
"lola": lola,
"contact": contact,
"phone": phone,
"comment": comment
})
self.finish()
class EditHandler(APIHandler):
"""
- 描述更新企业节点
- 请求方式post
- 请求参数
>- node_id, int, 节点id
>- name, string, 名称
>- parent, int, 上级节点id
>- addr, string, 地址
>- lola, string, 经纬度longitude and latitude
>- contact, string, 负责人
>- phone, string, 联系方式
>- comment, string, 简介
- 返回值
"""
# @authenticated
def post(self):
node_id = self.get_int_argument("node_id", 0)
name = self.get_escaped_argument("name", "")
parent = self.get_int_argument("parent", 0)
addr = self.get_escaped_argument("addr", "")
lola = self.get_escaped_argument("lola", "")
contact = self.get_escaped_argument("contact", "")
phone = self.get_escaped_argument("phone", "")
comment = self.get_escaped_argument("comment", "")
if not node_id or not name:
raise errors.HTTPAPIError(errors.ERROR_BAD_REQUEST, "参数错误")
db_node = DB_Node.EnterpriseNodeRepository()
db_node.update_node({
"node_id": node_id,
"name": name,
"parent": parent,
"addr": addr,
"lola": lola,
"contact": contact,
"phone": phone,
"comment": comment
})
self.finish()
class TreeHandler(APIHandler):
"""
- 描述企业节点树
- 请求方式post
- 请求参数
>- entity_id, int, 企业id
>- name, string, 搜索内容
- 返回值
```
{
"data":[
{
"id": 123,
"name": "xxx",
"children": [
{
"id": 123,
"name": "xxx"
"children": [
{
"id": 123,
"name": "xxx"
},
]
},
...
]
},
...
]
}
```
"""
@authenticated
def post(self):
entity_id = self.get_escaped_argument("entity_id", "")
name = self.get_escaped_argument("name", "")
if not entity_id:
raise errors.HTTPAPIError(errors.ERROR_BAD_REQUEST, "参数错误")
db_node = DB_Node.EnterpriseNodeRepository()
nodes = db_node.select_tree(entity_id, name)
self.finish({
"data": nodes
})
class InfoHandler(APIHandler):
"""
- 描述企业节点信息
- 请求方式post
- 请求参数
>- node_id, int, 节点id
- 返回值
```
{
"name": "xxx",
"parent": "xxx",
"addr": "xxx",
"lola": "xxx",
"contact": "xxx",
"phone": "xxx",
"comment": "xxx",
}
```
"""
@authenticated
def post(self):
node_id = self.get_int_argument("node_id")
if not node_id:
raise errors.HTTPAPIError(errors.ERROR_BAD_REQUEST, "参数错误")
db_node = DB_Node.EnterpriseNodeRepository()
node = db_node.select_node(node_id)
node = node and node or {}
self.finish(node)
class DeleteHandler(APIHandler):
@authenticated
def post(self):
node_id = self.get_int_argument("node_id")
if not node_id:
raise errors.HTTPAPIError(errors.ERROR_BAD_REQUEST, "参数错误")
db_node = DB_Node.EnterpriseNodeRepository()
db_node.delete_node(node_id)
self.finish()
class BusimodelHandler(APIHandler):
"""
- 描述企业节点业务模型部署
- 请求方式post
- 请求参数
> - pageNo
> - pageSize
> - node_id, int, 节点id
- 返回值
```
{
"count": 123,
"data": [
{
"busi_model_id": 123, # 业务模型id
"busi_model_name": "xxx", # 业务模型name
"deployed": 0, # 是否部署
"create_time": "xxx" # 创建时间
},
...
]
}
```
"""
@authenticated
def post(self):
node_id = self.get_int_argument('node_id')
pageNo = self.get_int_argument('pageNo', 1)
pageSize = self.get_int_argument('pageSize', 20)
if not node_id:
raise errors.HTTPAPIError(errors.ERROR_BAD_REQUEST, '企业节点不能为空')
db_model_node = DB_BusiModel.EnterpriseBusiModelNodeRepository()
busi_models = db_model_node.get_busi_model_by_node_id(node_id, pageNo, pageSize)
count = busi_models["count"]
models = busi_models["data"]
db_mode_node_device = DB_BusiModelNodeDevice.EnterpriseBusiModelNodeDeviceRepository()
for busi_model in models:
deployed = db_mode_node_device.check_deployed(node_id, busi_model["busi_model_id"])
busi_model["deployed"] = deployed
self.finish({"count": count, "data": models})
class BusimodelInfoHandler(APIHandler):
"""
- 描述企业节点业务模型部署 -> 业务模型信息
- 请求方式post
- 请求参数
> - node_id, int, 节点id
> - busi_model_id, int业务模型id
- 返回值
```
{
"busi_model_name": "xx", # 业务模型名称
"busi_model_comment": "xxx", # 简介
"base_models": "xxx,xx,xx,xx" # 关联基础模型
"devices": [
{
"device_id": 123,
"device_name": "xxx"
},
...
]
}
```
"""
@authenticated
def post(self):
self.finish()
class BusimodelDeployHandler(APIHandler):
"""
- 描述企业节点节点信息 -> 业务模型部署 -> 业务模型配置
- 请求方式post
- 请求参数
> - node_id, int, 节点id
> - busi_model_id, int业务模型id
> - device_ids, string, 设备id, 多个id逗号分割
- 返回值
"""
@authenticated
def post(self):
node_id = self.get_int_argument('node_id')
busi_model_id = self.get_int_argument('busi_model_id')
device_ids = self.get_escaped_argument('device_ids', '')
if not node_id or not busi_model_id:
raise errors.HTTPAPIError(errors.ERROR_BAD_REQUEST, '参数错误')
db_node = DB_Node.EnterpriseNodeRepository()
node = db_node.get_node_by_id(node_id)
if not node:
raise errors.HTTPAPIError(errors.ERROR_BAD_REQUEST, '节点不存在')
node_suid = node["suid"]
db_busi_model = DB_BusiModel.EnterpriseBusiModelRepository()
busi_model = db_busi_model.get_busi_model_by_id(busi_model_id)
busi_model_suid = busi_model.suid
entity_suid_row = db_node.get_entity_suid_by_node_id(node_id)
if not entity_suid_row:
raise errors.HTTPAPIError(errors.ERROR_BAD_REQUEST, '企业节点不存在')
records = []
for device_id in device_ids.split(','):
device = DB_Device.EnterpriseDeviceRepository().get_device(int(device_id))
if not device:
raise errors.HTTPAPIError(errors.ERROR_BAD_REQUEST, '设备不存在')
records.append({
"suid": shortuuid.ShortUUID.random(length=10),
"entity_suid": entity_suid_row["entity_suid"],
"node_id": node_id,
"node_suid": node_suid,
"busi_model_id": busi_model_id,
"busi_model_suid": busi_model_suid,
"device_id": int(device_id),
"device_suid": device["suid"]
})
DB_BusiModelNodeDevice.EnterpriseBusiModelNodeDeviceRepository().batch_insert_record(records)
self.finish()