scope_operations.py 6.44 KB
Newer Older
wang.wenrong's avatar
wang.wenrong committed
1 2 3 4 5 6 7 8 9
from pot_libs.common.components.responses import Success
from pot_libs.logger import log
from pot_libs.sanic_api import summary, description, examples
from pot_libs.utils.exc_util import BusinessException
from unify_api.modules.anshiu.components.scope_operations_cps import \
    ScopeListReq, ScopeListResp, GetScopeConfigReq, GetScopeConfigResp, \
    SetScopeConfigReq, SetScopeConfigResp, \
    scope_list_req_example, ScopeDetailsResp, ScopeDetailRep, \
    set_scope_config_example, InitScopeConfigReq, FlushScopeEsDataReq, \
wang.wenrong's avatar
wang.wenrong committed
10
    ScopeListDownloadReq, ScopeListDownloadResp, ScopeDetailResp
wang.wenrong's avatar
wang.wenrong committed
11 12 13
from unify_api.modules.anshiu.service.scope_operations_serv import \
    search_scope_service, scope_detail_data, get_scope_config_serv, \
    set_scope_config_serv, init_scope_config, flush_scope_es_data, \
wang.wenrong's avatar
wang.wenrong committed
14
    scope_list_download_data, scope_detail_service
wang.wenrong's avatar
wang.wenrong committed
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49


@summary("识别记录-列表")
@description("列表的时候正常传页码,下载的时候is_download=1")
@examples(scope_list_req_example)
async def post_scope_list(request, body: ScopeListReq) -> ScopeListResp:
    '''
    识别记录
    '''
    cid = body.cid
    page_size = body.page_size
    page_num = body.page_num
    start = body.start
    end = body.end
    scope_g = body.scope_g
    pids = body.pids
    is_download = body.is_download
    # 监测点选中全部
    if pids and pids[0] == -1:
        pids = []
    # 未选中监测点且未选中工厂提示错误
    if len(pids) == 0 and not cid:
        log.error("post_scope_list_param_error pids:%s cid:%s" % (pids, cid))
        return ScopeListResp.error_param()
    if page_num * page_size > 30000:
        log.error(f"post_scope_list_param_error page_too_large page_num"
                  f":{page_num},page_size:{page_size}")
        raise BusinessException(
            message='只能查询前%s条数据,建议缩小查询范围' % 30000)
    # 下载(限制最大10000条)
    if is_download == 1:
        page_num, page_size = 1, 10000
    # 替换scope_g
    if scope_g:
        scope_g = ['200ms' if i == '0.2s' else i for i in scope_g]
wang.wenrong's avatar
wang.wenrong committed
50 51 52
    rows, total = await search_scope_service(pids, cid,
                                             (page_num - 1) * page_size,
                                             page_size,
wang.wenrong's avatar
wang.wenrong committed
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
                                             start, end, scope_g)
    return ScopeListResp(rows=rows, total=total, page_num=page_num)


@summary("2s录波数据下载")
async def post_scope_list_download(request,
                                   body: ScopeListDownloadReq) -> ScopeListDownloadResp:
    start = body.start
    end = body.end
    pids = body.pids
    if len(pids) > 1:
        raise BusinessException(message="只允许下载一个监测点的数据")
    datas = await scope_list_download_data(pids, start, end)
    return ScopeListDownloadResp(rows=datas)


wang.wenrong's avatar
wang.wenrong committed
69 70 71 72 73 74
@summary('数据统计-录波查询-录波详情')
async def post_scope_detail(req, body: ScopeDetailRep) -> ScopeDetailResp:
    # 1,获取参数
    event_id = body.id

    # 2,获取信息
wang.wenrong's avatar
wang.wenrong committed
75 76
    data, u_list, i_list, residual_current, power = await scope_detail_service(
        event_id)
wang.wenrong's avatar
wang.wenrong committed
77 78 79

    # 3,返回信息
    return ScopeDetailResp(
wang.wenrong's avatar
wang.wenrong committed
80
        point=data.get("name"),
wang.wenrong's avatar
wang.wenrong committed
81
        ctnum=data.get("ctnum"),
wang.wenrong's avatar
wang.wenrong committed
82 83 84
        check_dt=data.get("event_datetime"),
        contin_time=data.get("duration"),
        item=data.get("item"),
wang.wenrong's avatar
wang.wenrong committed
85
        scope_g=data.get("scope_g"),
wang.wenrong's avatar
wang.wenrong committed
86 87 88 89 90 91 92
        type=data.get("type"),
        v=u_list,
        location=data.get("location"),
        i=i_list,
        residual_current=residual_current,
        p=power,

wang.wenrong's avatar
wang.wenrong committed
93 94 95
    )


wang.wenrong's avatar
wang.wenrong committed
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
@summary("识别设置-获取配置信息")
async def post_get_scope_config(request,
                                body: GetScopeConfigReq) -> GetScopeConfigResp:
    '''
    识别设置-配置展示
    '''
    pid = body.pid
    try:
        data = await get_scope_config_serv(pid)
    except Exception as e:
        log.error('post_get_scope_config error:' + str(e))
        return GetScopeConfigResp.server_error()
    return GetScopeConfigResp(pid=pid, rows=data)


@summary("识别设置-设置配置信息")
@examples(set_scope_config_example)
async def post_set_scope_config(request,
                                body: SetScopeConfigReq) -> SetScopeConfigResp:
    '''
    识别设置-配置设置
    '''
    pid = body.pid
    type = body.type
    scope_type = body.scope_type
    # 每一种类型需要的字段
    fields = {'state': ['state'],
              'i': ['imax', 'igap'],
              'v': ['umax', 'umin', 'ugap'],
              'residual_current': ['lcmax', 'lcgap'],
              'power': ['pttlmax', 'pttlgap'],
              'time': ['one_time', 'two_time', 'three_time']}
    args = {}
    for field in fields.get(type):
        args[field] = getattr(body, field)
    try:
        await set_scope_config_serv(pid, type, scope_type, args)
    except Exception as e:
        log.error('post_set_scope_config' + str(e))
        return SetScopeConfigResp(success=0, message=str(e))
    return SetScopeConfigResp(success=1, message='操作成功')


@summary("识别设置-初始化设备配置信息(开发专用!)")
async def post_init_scope_config(request,
                                 body: InitScopeConfigReq) -> Success:
    pids = body.pids
    # user_id = request.ctx.user_id
    # if user_id not in ['100653']:
    #     return Success(success=0, message='无此操作权限')
    error_list = []
    for pid in pids:
        try:
            await init_scope_config(pid)
        except Exception as e:
            log.error(f'{pid}:post_init_scope_config error {str(e)}')
            error_list.append(str(e))
            continue
    if error_list:
        log.error(f"post_init_scope_config error:{','.join(error_list)}")
    else:
        log.info(
            f"post_init_scope_config success total_success_count:"
            f"{str(len(pids))}")
    return Success(success=1, message=','.join(error_list))


@summary("刷新es录波数据(开发专用!)")
async def post_flush_scope_es_data(request, body: FlushScopeEsDataReq) -> \
        Success:
    scope_type_list = body.scope_type_list
    start_time = body.start_time
    end_time = body.end_time
    try:
        for scope_g in scope_type_list:
            if scope_g not in ['200ms', '2s', '0.25ms']:
                continue
            await flush_scope_es_data(scope_g, start_time, end_time)
    except Exception as e:
        log.error(f'post_flush_scope_es_data error {str(e)}')
        return Success(success=0, message=str(e))
    return Success(success=1, message='操作成功')