monitor.py 5.37 KB
Newer Older
wanli's avatar
wanli committed
1 2
'''
Author: your name
wanli's avatar
wanli committed
3
Date: 2021-06-29 19:24:32
wanli's avatar
wanli committed
4
LastEditTime: 2021-07-01 10:01:48
wanli's avatar
wanli committed
5 6
LastEditors: Please set LastEditors
Description: In User Settings Edit
wanli's avatar
wanli committed
7
FilePath: \evm-store\backend\controller\monitor.py
wanli's avatar
wanli committed
8
'''
wanli's avatar
wanli committed
9
from model.monitor import session, System, Lvgl, Evm, Image, Watch, Request
wanli's avatar
wanli committed
10 11 12 13 14 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 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72

class SystemResource(object):
    def get(self):
        result = session.query(System).all()
        print(result)
        return result

    def post(self, params):
        result = System(**params)
        session.add(result)
        return session.commit()
        
    def put(self):
        pass

    def delete(self):
        pass

class LvglResource(object):
    def get(self):
        result = session.query(Lvgl).all()
        print(result)
        return result

    def post(self, params):
        result = Lvgl(**params)
        session.add(result)
        return session.commit()
        
    def put(self):
        pass

    def delete(self):
        pass

class EvmResource(object):
    def get(self):
        result = session.query(Evm).all()
        print(result)
        return result

    def post(self, params):
        result = Evm(**params)
        session.add(result)
        return session.commit()
        
    def put(self):
        pass

    def delete(self):
        pass

class ImageResource(object):
    def get(self):
        result = session.query(Image).all()
        print(result)
        return result

    def post(self, params):
        result = Image(**params)
        session.add(result)
        return session.commit()

wanli's avatar
wanli committed
73
    def post_array(self, array, watch):
wanli's avatar
wanli committed
74 75
        t = []
        for a in array:
wanli's avatar
wanli committed
76
            a.update({ "watch": watch })
wanli's avatar
wanli committed
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
            t.append(Image(**a))
        session.add_all(t)
        return session.commit()
        
    def put(self):
        pass

    def delete(self):
        pass

systemResource = SystemResource()
lvglResource = LvglResource()
evmResource = EvmResource()
imageResource = ImageResource()

def insert_data(msg):
wanli's avatar
wanli committed
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
    # 先判断手表imei是否存在,不存在则先注册手表IMEI
    watch_id = -1
    if msg.get("imei"):
        result = session.query(Watch).filter_by(imei=msg.get("imei")).first()
        if result:
            watch_id = result.id
        else:
            result = Watch(imei=msg.get("imei"))
            session.add(result)
            session.flush()
            session.commit()

            result = session.query(Watch).filter_by(imei=msg.get("imei")).first()
            if result:
                watch_id = result.id

wanli's avatar
wanli committed
109 110 111 112 113 114 115
    if msg.get("request"):
        msg.get("request").update({ "watch": watch_id })
        result = Request(**msg.get("request"))
        session.add(result)
        session.flush()
        session.commit()

wanli's avatar
wanli committed
116
    if msg.get("system"):
wanli's avatar
wanli committed
117
        msg.get("system").update({ "watch": watch_id })
wanli's avatar
wanli committed
118
        systemResource.post(msg.get("system"))
wanli's avatar
wanli committed
119 120

    if msg.get("lvgl"):
wanli's avatar
wanli committed
121
        msg.get("lvgl").update({ "watch": watch_id })
wanli's avatar
wanli committed
122
        lvglResource.post(msg.get("lvgl"))
wanli's avatar
wanli committed
123 124

    if msg.get("evm"):
wanli's avatar
wanli committed
125
        msg.get("evm").update({ "watch": watch_id })
wanli's avatar
wanli committed
126
        evmResource.post(msg.get("evm"))
wanli's avatar
wanli committed
127 128

    if msg.get("image"):
wanli's avatar
wanli committed
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 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
        imageResource.post_array(msg.get("image"), watch_id)

def get_watch_list():
    result = session.query(Watch).all()
    tmp = []
    for item in result:
        tmp.append({
            'id': item.id,
            'imei': item.imei
        })
    return tmp

def evm_data(watch, start, end):
    filters = [Evm.watch==watch]
    if start:
        filters.append(Evm.timestamp >= start)
    if end:
        filters.append(Evm.timestamp <= end)
    result = session.query(Evm).filter(*filters).order_by(Evm.timestamp).all()
    temp = []
    for item in result:
        t = item.to_dict()
        if t.get("timestamp"):
            t.update({ 'timestamp': t.get("timestamp").strftime("%Y-%m-%d %H:%M:%S") })
        temp.append(t)

    return temp

def lvgl_data(watch, start, end):
    filters = [Lvgl.watch==watch]
    if start:
        filters.append(Lvgl.timestamp>=start)
    if end:
        filters.append(Lvgl.timestamp<=end)
    result = session.query(Lvgl).filter(*filters).order_by(Lvgl.timestamp).all()
    temp = []
    for item in result:
        t = item.to_dict()
        if t.get("timestamp"):
            t.update({ 'timestamp': t.get("timestamp").strftime("%Y-%m-%d %H:%M:%S") })
        temp.append(t)

    return temp

def image_data(watch, start, end):
    filters = [Image.watch==watch]
    if start:
        filters.append(Image.timestamp>=start)
    if end:
        filters.append(Image.timestamp<=end)
    result = session.query(Image).filter(*filters).order_by(Image.timestamp).all()
    temp = []
    for item in result:
        t = item.to_dict()
        if t.get("timestamp"):
            t.update({ 'timestamp': t.get("timestamp").strftime("%Y-%m-%d %H:%M:%S") })
        temp.append(t)

    return temp

def get_monitor_list(watch, category, start, end):
    # 判断watch是否存在
    w = session.query(Watch).filter(Watch.id==watch).first()
    if not w:
        return []

    if category == "system":
        return []
    elif category == "image":
        return image_data(watch, start, end)
    elif category == "lvgl":
        return lvgl_data(watch, start, end)
    elif category == "evm":
        return evm_data(watch, start, end)
    else:
        return {
            'evm': evm_data(watch, start, end),
            'lvgl': lvgl_data(watch, start, end),
            'image': image_data(watch, start, end)
        }