프로젝트

일반

사용자정보

통계
| 개정판:

hytos / DTI_PID / DTI_PID / HMBTable.py @ e1bfcd0e

이력 | 보기 | 이력해설 | 다운로드 (4.97 KB)

1
# coding: utf-8
2

    
3
import os
4
import sys
5
import sqlite3
6
from SingletonInstance import SingletonInstance
7

    
8

    
9
class StreamData:
10
    def __init__(self, name, unit='', value='', index=0, phase='', case=''):
11
        self.name = name
12
        self.unit = unit
13
        self.value = value
14
        self.index = index
15
        self.phase = phase
16
        self.case = case
17

    
18

    
19
class HMBData:
20
    """HMB data class"""
21
    def __init__(self, uid=None):
22
        self._uid = uid
23
        self._stream_no = None
24
        self._from = None
25
        self._to = None
26
        self._datas = []
27

    
28
    @property
29
    def datas(self):
30
        """return hmb data"""
31
        return self._datas
32

    
33
    @datas.setter
34
    def datas(self, value):
35
        """hmb data"""
36
        self._datas = value
37

    
38
    @property
39
    def hfrom(self):
40
        """from"""
41
        return self._from
42

    
43
    @hfrom.setter
44
    def hfrom(self, value):
45
        """from"""
46
        self._from = value
47

    
48
    @property
49
    def hto(self):
50
        """to"""
51
        return self._to
52

    
53
    @hto.setter
54
    def hto(self, value):
55
        """to"""
56
        self._to = value
57

    
58
    @property
59
    def uid(self):
60
        """uid"""
61
        return self._uid
62

    
63
    @uid.setter
64
    def uid(self, value):
65
        """uid"""
66
        self._uid = value
67
    
68
    @property
69
    def stream_no(self):
70
        """stream no"""
71
        return self._stream_no
72

    
73
    @stream_no.setter
74
    def stream_no(self, value):
75
        """stream no"""
76
        self._stream_no = value
77

    
78
    @staticmethod
79
    def from_row(rows):
80
        """create hmb data from database record"""
81
        if rows:
82
            pre_no = rows[0]['Stream_No']
83
        else:
84
            return []
85

    
86
        data_list = []
87
        datas = []
88

    
89
        for row in rows:
90
            if row['Stream_No'] == pre_no:
91
                datas.append(row)
92
            else:
93
                pre_no = row['Stream_No']
94
                data_list.append(datas)
95
                datas = [row]
96

    
97
        data_list.append(datas)
98

    
99
        hmbs = []
100
        for datas in data_list:
101
            hmb = HMBData(datas[0]['UID'])
102
            hmb.stream_no = datas[0]['Stream_No']
103
            hmb.hfrom = datas[0]['From']
104
            hmb.hto = datas[0]['To']
105

    
106
            cases = {}
107
            for _data in datas:
108
                case = _data['Case']
109
                if case not in cases:
110
                    cases[case] = [_data]
111
                else:
112
                    cases[case].append(_data)
113

    
114
            hmb_datas = []
115
            keys = sorted(list(cases.keys()))
116
            for key in keys:
117
                hmb_data = []
118
                _datas = cases[key]
119
                for _data in _datas:
120
                    name = _data['Name']
121
                    unit = _data['Unit']
122
                    value = _data['Value']
123
                    index = _data['index']
124
                    phase = _data['Type']
125
                    case = _data['Case']
126
                    hmb_data.append(StreamData(name, unit, value, index, phase, case))
127
                hmb_datas.append(hmb_data)
128

    
129
            hmb.datas = hmb_datas
130
            hmbs.append(hmb)
131

    
132
        return hmbs
133

    
134

    
135
"""
136
class HMBTable:
137
    '''HMB table class'''
138
    def __init__(self):
139
        self._hmbs = None
140

141
    def load_data(self):
142
        '''load hmb data from database'''
143
        from AppDocData import AppDocData
144

145
        try:
146
            if self._hmbs is None:
147
                self._hmbs = []
148

149
                rows = AppDocData.instance().get_hmb_data(None)
150
                for row in rows:
151
                    hmb = HMBData.from_row(row)
152
                    self._hmbs.append(hmb)
153
        # Catch the exception
154
        except Exception as ex:
155
            from App import App
156
            from AppDocData import MessageType
157

158
            message = f"error occurred({repr(ex)}) in {sys.exc_info()[-1].tb_frame.f_code.co_filename}:" \
159
                      f"{sys.exc_info()[-1].tb_lineno}"
160
            App.mainWnd().addMessage.emit(MessageType.Error, message)
161

162
        return self._hmbs
163
        
164
    @property
165
    def names(self):
166
        '''return hmb attribute names'''
167
        return set([hmb.name for hmb in self._hmbs if not hmb.isDeleted]) if self._hmbs is not None else {}
168

169
    def streamNos(self):
170
        '''return stream no collection which are not duplicated'''
171
        return set([hmb.stream_no for hmb in self._hmbs]) if self._hmbs is not None else {}
172

173
    def dataOfStreamNo(self, streamNo):
174
        '''return stream data'''
175
        if self._hmbs is not None:
176
            matches = [hmb.data for hmb in self._hmbs if hmb.stream_no == streamNo]
177
            return matches[0] if matches else None
178
        else:
179
            return None
180

181
    '''
182
        @brief      append hmb data
183
        @author     humkyung
184
        @date       2018.07.12
185
    '''
186
    def append(self, hmbData):
187
        if self._hmbs is None:
188
            self._hmbs = []
189

190
        self._hmbs.append(hmbData)
191

192
    '''
193
        @brief      reset
194
        @author     humkyung
195
        @date       2018.07.12
196
    '''
197
    def reset(self):
198
        if self._hmbs is not None:
199
            del self._hmbs
200

201
        self._hmbs = None
202
"""
클립보드 이미지 추가 (최대 크기: 500 MB)