points.py 4.08 KB
Newer Older
lcn's avatar
lcn committed
1 2 3 4 5 6 7 8
from collections import defaultdict

from pot_libs.mysql_util.mysql_util import MysqlUtil
from unify_api.modules.common.components.list_points_cps import CommonLocation, \
    CommonPoint, Inline, ListPointResponse
from unify_api.modules.common.dao.common_dao import query_points_by_storey


ZZH's avatar
ZZH committed
9
async def load_compy_points(cids):
lcn's avatar
lcn committed
10 11 12
    sql = "SELECT p.pid,p.cid,p.inlid,vc,ctnum " \
          "FROM `point` p INNER JOIN " \
          "monitor m on m.mtid=p.mtid where p.cid in %s and m.demolished=0;"
lcn's avatar
lcn committed
13 14 15 16
    async with MysqlUtil() as conn:
        points = await conn.fetchall(sql, args=(cids,))
    company_point_map = defaultdict(dict)
    for point in points:
lcn's avatar
lcn committed
17
        company_point_map[point["cid"]][point["pid"]] = point
lcn's avatar
lcn committed
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
    return company_point_map


async def get_points_num(cid):
    sql = "SELECT count(1) doc_count FROM `point` p LEFT JOIN monitor m " \
          "on m.mtid=p.mtid where m.demolished=0 and m.cid=%s"
    async with MysqlUtil() as conn:
        datas = await conn.fetchone(sql, args=(cid,))
    return datas["doc_count"]


async def proxy_points(cid_list):
    """获取代理下的所有监测点"""
    sql = "SELECT COUNT(p.pid) from point p LEFT JOIN monitor m " \
          "on p.mtid=m.mtid where p.cid in %s and m.demolished=0"
    async with MysqlUtil() as conn:
        num = await conn.fetch_value(sql, args=(tuple(cid_list),))
    return num


async def get_meter_by_point(point_id):
    """
        根据point获取设备数据
    """
    async with MysqlUtil() as conn:
        sql = "SELECT sid, meter_no,m.mtid FROM `point` p LEFT JOIN monitor " \
              "m on m.mtid=p.mtid where m.demolished=0 and p.pid=%s"
        meter_info = await conn.fetchone(sql, args=(point_id,))
        return meter_info
wang.wenrong's avatar
wang.wenrong committed
47 48


lcn's avatar
lcn committed
49 50 51 52
async def list_point(cid):
    list_point = []
    points = {}
    groups = {}
wang.wenrong's avatar
wang.wenrong committed
53
    sql = "SELECT pid, name, add_to_company FROM point WHERE cid=%s"
lcn's avatar
lcn committed
54 55 56 57 58
    async with MysqlUtil() as conn:
        result = await conn.fetchall(sql, args=(cid,))
        for res in result:
            pid = res.get("pid")
            points[pid] = res
ZZH's avatar
ZZH committed
59

lcn's avatar
lcn committed
60 61 62 63 64 65 66 67 68
    sql = "SELECT id, `group`, item FROM location WHERE cid=%s and `type` in %s"
    async with MysqlUtil() as conn:
        result = await conn.fetchall(sql, args=(
            cid, ["temperature", "residual_current"]))
        for res in result:
            id = res.get("id")
            group = res.get("group")
            item = res.get("item")
            groups.setdefault(group, []).append((id, item))
ZZH's avatar
ZZH committed
69

lcn's avatar
lcn committed
70 71 72 73 74 75 76 77 78 79 80 81 82
    for pid, point_info in points.items():
        name = point_info.get("name")
        add_to_company = point_info["add_to_company"]
        items = groups.get(name, [])
        locations = []
        for id, item in items:
            # comm_location = CommonLocation(location_id=id, item=item)
            comm_location = {"location_id": id, "item": item}
            locations.append(comm_location)
        # comm_point = CommonPoint(name=name, point_id=pid, locations=locations, add_to_company=add_to_company)
        comm_point = {"name": name, "point_id": pid, "locations": locations,
                      "add_to_company": add_to_company}
        list_point.append(comm_point)
ZZH's avatar
ZZH committed
83

lcn's avatar
lcn committed
84
    async with MysqlUtil() as conn:
wang.wenrong's avatar
wang.wenrong committed
85
        sql = "SELECT inlid, `name` FROM inline WHERE cid=%s"
lcn's avatar
lcn committed
86 87 88 89 90 91 92 93
        inlines = await conn.fetchall(sql, args=(cid,))
        inline_list = [Inline(inline_id=inline["inlid"], name=inline["name"])
                       for inline in inlines]
    return {"points": list_point, "inlines": inline_list}


async def point_to_mid(points):
    """获取所有poin_id和mid对应关系"""
wang.wenrong's avatar
wang.wenrong committed
94 95
    sql = "SELECT pid, mtid FROM point WHERE pid IN %s " \
          "order by pid, create_time"
lcn's avatar
lcn committed
96 97 98
    async with MysqlUtil() as conn:
        change_meter_records = await conn.fetchall(sql, args=(tuple(points),))
    point_mid_map = {
wang.wenrong's avatar
wang.wenrong committed
99
        i["pid"]: i["mtid"] for i in change_meter_records
lcn's avatar
lcn committed
100 101 102 103 104 105 106 107 108
    }
    point_mid = dict(filter(lambda x: x[1] is not None, point_mid_map.items()))
    return point_mid, len(point_mid)


async def points_by_storeys(storeys):
    """根据storey_id查询point_id和room_name"""
    point_list = await query_points_by_storey(storeys)
    return point_list