You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

286 lines
8.3 KiB
Python

# -*- coding: utf-8 -*-
12 months ago
import logging
from sqlalchemy import text
12 months ago
from website import db_mysql, errors
from website.handler import APIHandler, authenticated
from website.util import shortuuid
from website.db.enterprise_device import enterprise_device as DB_Device
class DeviceClassificationAddHandler(APIHandler):
"""
- 描述添加设备分类
- 请求方式post
- 请求参数
>- entity_id, int, 企业id
>- name, string, 设备分类名称
- 返回值
```
{
"id": 123
}
```
"""
@authenticated
def post(self):
name = self.get_escaped_argument('name', '')
with self.app_mysql.connect() as conn:
cur = conn.execute(
text("SELECT id FROM device_classification WHERE name=:name"), {"name": name}
)
ex = cur.fetchone()
12 months ago
logging.info("##############################")
logging.info(ex)
logging.info("##############################")
if ex:
raise errors.HTTPAPIError(errors.ERROR_BAD_REQUEST, '设备分类已存在')
conn.execute(
text("INSERT INTO device_classification (name, suid) VALUES (:name, :suid)"),
{"name": name, "suid": shortuuid.ShortUUID().random(10)}
)
12 months ago
conn.commit()
self.finish()
class DeviceClassificationHandler(APIHandler):
"""
- 描述设备分类列表
- 请求方式post
- 请求参数
>-
- 返回值
```
{
"data": [
{
"id": 123,
"suid": "xxx",
"name": "xxx"
},
...
]
}
```
"""
@authenticated
def post(self):
with self.app_mysql.connect() as conn:
cur = conn.execute(
text("SELECT id, suid, name FROM device_classification where del=0 ORDER BY id DESC")
)
12 months ago
res = db_mysql.to_json_list(cur)
res = res and res or []
self.finish({"data": res})
class DeviceClassificationDeleteHandler(APIHandler):
"""
- 描述删除设备分类
- 请求方式post
- 请求参数
>- id
- 返回值
"""
@authenticated
def post(self):
did = self.get_int_argument('id')
with self.app_mysql.connect() as conn:
conn.execute(
text("update device_classification set del=1 WHERE id=:id"), {"id": did}
)
12 months ago
conn.commit()
self.finish()
class DeviceAddHandler(APIHandler):
"""
12 months ago
- 描述企业节点添加设备
- 请求方式post
- 请求参数
>- entity_id, int, 企业id
>- node_id, int
>- name, string, 设备名称
>- addr, string, 设备位置
>- classification string, 设备分类
>- device_model, string, 设备型号
>- param, string, 设备参数
>- comment, string, 备注
- 返回值
```
```
"""
@authenticated
def post(self):
12 months ago
entity_id = self.get_int_argument('entity_id')
node_id = self.get_int_argument('node_id')
name = self.get_escaped_argument('name', '')
addr = self.get_escaped_argument('addr', '')
classification = self.get_escaped_argument('classification', '')
device_model = self.get_escaped_argument('device_model', '')
param = self.get_escaped_argument('param', '')
comment = self.get_escaped_argument('comment', '')
if not name:
raise errors.HTTPAPIError(errors.ERROR_BAD_REQUEST, '设备名称不能为空')
if not entity_id or not node_id:
raise errors.HTTPAPIError(errors.ERROR_BAD_REQUEST, '企业节点不能为空')
device_data = {
"entity_id": entity_id,
"node_id": node_id,
"name": name,
"addr": addr,
"classification": classification,
"device_model": device_model,
"param": param,
"comment": comment
}
db_device = DB_Device.EnterpriseDeviceRepository()
12 months ago
db_device.add_device(device_data)
self.finish()
class DeviceEditHandler(APIHandler):
"""
12 months ago
- 描述企业节点编辑设备
- 请求方式post
- 请求参数
>- device_id, int, 设备id
>- name, string, 设备名称
>- addr, string, 设备位置
>- classification string, 设备分类的short uuid
>- device_model, string, 设备型号
>- param, string, 设备参数
>- comment, string, 备注
- 返回值
"""
@authenticated
def post(self):
12 months ago
device_id = self.get_int_argument('device_id')
name = self.get_escaped_argument('name', '')
addr = self.get_escaped_argument('addr', '')
classification = self.get_escaped_argument('classification', '')
device_model = self.get_escaped_argument('device_model', '')
param = self.get_escaped_argument('param', '')
comment = self.get_escaped_argument('comment', '')
if not device_id:
raise errors.HTTPAPIError(errors.ERROR_BAD_REQUEST, '设备id不能为空')
if not name:
raise errors.HTTPAPIError(errors.ERROR_BAD_REQUEST, '设备名称不能为空')
device_data = {
"id": device_id,
"name": name,
"addr": addr,
"classification": classification,
"device_model": device_model,
"param": param,
"comment": comment
}
db_device = DB_Device.EnterpriseDeviceRepository()
12 months ago
db_device.edit_device(device_data)
self.finish()
class DeviceDeleteHandler(APIHandler):
"""
12 months ago
### /enterprise/entity/nodes/device/delete
12 months ago
- 描述企业节点删除设备
- 请求方式post
- 请求参数
>- node_id, int
>- device_id, int, 设备id
- 返回值
"""
@authenticated
def post(self):
# node_id = self.get_int_argument('node_id')
12 months ago
device_id = self.get_int_argument('device_id')
if not device_id:
12 months ago
raise errors.HTTPAPIError(errors.ERROR_BAD_REQUEST, '企业节点或设备不能为空')
db_device = DB_Device.EnterpriseDeviceRepository()
12 months ago
db_device.delete_device(device_id)
self.finish()
class DeviceListHandler(APIHandler):
"""
12 months ago
- 描述企业节点设备列表
- 请求方式post
- 请求参数
> - pageNo
> - pageSize
> - node_id, int, 节点id
- 返回值
```
{
"count": 123,
"data": [
{
"device_id": 123,
"device_name": "xxx",
"device_class": "xxx", 设备类型
"deployed": 0, # 是否部署, 0/未部署, 1/已部署
},
...
]
}
```
"""
@authenticated
def post(self):
12 months ago
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_device = DB_Device.EnterpriseDeviceRepository()
devices = db_device.list_devices(node_id=node_id, pageNo=pageNo, pageSize=pageSize)
logging.info(devices)
self.finish({"count": devices["total_count"], "data": devices["devices"]})
class DeviceInfoHandler(APIHandler):
"""
- 描述企业节点设备信息
- 请求方式post
- 请求参数
> - device_id, int, 设备id
- 返回值
```
{
"name": "xxx",
"addr": "xxx",
"classification": "xxx",
"device_model": "xxx",
"param": "xxx",
"comment": "xxx",
}
```
"""
def post(self):
device_id = self.get_int_argument('device_id')
if not device_id:
raise errors.HTTPAPIError(errors.ERROR_BAD_REQUEST, '企业节点或设备不能为空')
db_device = DB_Device.EnterpriseDeviceRepository()
device = db_device.get_device(device_id=device_id)
logging.info(device)
self.finish(device)