프로젝트

일반

사용자정보

통계
| 브랜치(Branch): | 개정판:

hytos / DTI_PID / DTI_PID / HMBTable.py @ 4a4e7537

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

1
# coding: utf-8
2

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

    
8
'''
9
    @brief      HMB data
10
'''
11
class HMBData:
12
    def __init__(self, uid=None):
13
        self._uid = uid
14
        self._stream_no = None
15
        self._name = None
16
        self._unit = None
17
        self._value = None
18
        self.isDeleted = False
19

    
20
    '''
21
        @author humkyung
22
        @date   2018.07.12
23
    '''
24
    @property
25
    def uid(self):
26
        return self._uid
27

    
28

    
29
    '''
30
        @author humkyung
31
        @date   2018.07.12
32
    '''
33
    @uid.setter
34
    def uid(self, value):
35
        self._uid = value
36
    
37
    '''
38
        @author humkyung
39
        @date   2018.07.12
40
    '''
41
    @property
42
    def streamNo(self):
43
        return self._stream_no
44

    
45
    '''
46
        @author humkyung
47
        @date   2018.07.12
48
    '''
49
    @streamNo.setter
50
    def streamNo(self, value):
51
        self._stream_no = value
52

    
53
    '''
54
        @author humkyung
55
        @date   2018.07.12
56
    '''
57
    @property
58
    def name(self):
59
        return self._name
60

    
61
    '''
62
        @author humkyung
63
        @date   2018.07.12
64
    '''
65
    @name.setter
66
    def name(self, value):
67
        self._name = value
68

    
69
    '''
70
        @author humkyung
71
        @date   2018.07.12
72
    '''
73
    @property
74
    def unit(self):
75
        return self._unit
76

    
77
    '''
78
        @author humkyung
79
        @date   2018.07.12
80
    '''
81
    @unit.setter
82
    def unit(self, value):
83
        self._unit = value
84

    
85
    '''
86
        @author humkyung
87
        @date   2018.07.12
88
    '''
89
    @property
90
    def value(self):
91
        return self._value
92

    
93
    '''
94
        @author humkyung
95
        @date   2018.07.12
96
    '''
97
    @value.setter
98
    def value(self, value):
99
        self._value = value
100

    
101
    '''
102
        @brief      create hmb data from database record
103
        @author     humkyung
104
        @date       2018.07.12
105
    '''
106
    @staticmethod
107
    def fromRow(row):
108
        hmb = HMBData()
109
        hmb._uid = row[0]
110
        hmb._stream_no = row[1]
111
        hmb._name = row[2]
112
        hmb._unit = row[3]
113
        hmb._value = row[4]
114

    
115
        return hmb
116

    
117
class HMBTable:
118
    '''
119
        @brief  constructor
120
        @author humkyung
121
        @date   2018.07.12
122
    '''
123
    def __init__(self):
124
        self._hmbs = None
125

    
126
    '''
127
        @brief      load hmb data from database
128
        @author     humkyung
129
        @date       2018.07.12
130
    '''
131
    def loadData(self):
132
        from AppDocData import AppDocData
133

    
134
        try:
135
            if self._hmbs is None:
136
                self._hmbs = []
137

    
138
                appDocData = AppDocData.instance()
139

    
140
                dbPath = os.path.join(appDocData.getCurrentProject().getDbFilePath() , 'ITI_PID.db')
141

    
142
                conn = sqlite3.connect(dbPath)
143
                cursor = conn.cursor()
144
                sql = 'SELECT UID,STREAM_NO,NAME,UNIT,VALUE FROM HMB ORDER BY STREAM_NO'
145
                cursor.execute(sql)
146
                rows = cursor.fetchall()
147
                for row in rows:
148
                    hmb = HMBData.fromRow(row)
149
                    self._hmbs.append(hmb)
150
        # Catch the exception
151
        except Exception as ex:
152
            # Roll back any change if something goes wrong
153
            conn.rollback()
154
            print('error occured({}) in {}:{}'.format(ex, sys.exc_info()[-1].tb_frame.f_code.co_filename, sys.exc_info()[-1].tb_lineno))
155
        finally:
156
            conn.close()
157

    
158
        return self._hmbs
159
        
160
    '''
161
        @brief      save hmb data 
162
        @author     humkyung
163
        @date       2018.07.12
164
    '''
165
    def saveData(self):
166
        import uuid
167
        from AppDocData import AppDocData
168

    
169
        try:
170
            if self._hmbs is not None:
171
                appDocData = AppDocData.instance()
172
                # Creates or opens a file called mydb with a SQLite3 DB
173
                dbPath = os.path.join(appDocData.getCurrentProject().getDbFilePath(), "ITI_PID.db")
174
                conn = sqlite3.connect(dbPath)
175
                # Get a cursor object
176
                cursor = conn.cursor()
177

    
178
                for data in self._hmbs:
179
                    if data.isDeleted == False:
180
                        if data.uid is None:
181
                            sql = "insert or replace into HMB values(?,?,?,?,?)"
182
                            param = (str(uuid.uuid4()), data.streamNo, data.name, data.unit, data.value)
183
                            cursor.execute(sql, param)
184
                        else:
185
                            sql = "update HMB set STREAM_NO=?,NAME=?,UNIT=?,VALUE=? where UID=?"
186
                            param = (data.streamNo, data.name, data.unit, data.value, data.uid)
187
                            cursor.execute(sql, param)
188
                    else:
189
                        sql = "delete from HMB where uid=?"
190
                        param = (data.uid,)
191
                        cursor.execute(sql, param)
192

    
193
                conn.commit()
194
        # Catch the exception
195
        except Exception as ex:
196
            # Roll back any change if something goes wrong
197
            conn.rollback()
198
            print('error occured({}) in {}:{}'.format(ex, sys.exc_info()[-1].tb_frame.f_code.co_filename, sys.exc_info()[-1].tb_lineno))
199
        finally:
200
            # Close the db connection
201
            conn.close()
202

    
203
    '''
204
        @brief      return stream no collection which are not duplicated 
205
        @author     humkyung
206
        @date       2018.07.12
207
    '''
208
    def streamNos(self):
209
        return set([hmb.streamNo for hmb in self._hmbs if hmb.isDeleted == False]) if self._hmbs is not None else {}
210

    
211
    '''
212
        @brief      return given index's data
213
        @author     humkyung
214
        @date       2018.07.12
215
    '''
216
    def dataOfStreamNo(self, streamNo):
217
        return [hmb for hmb in self._hmbs if hmb.streamNo == streamNo and hmb.isDeleted == False] if self._hmbs is not None else None
218

    
219
    '''
220
        @brief      append hmb data
221
        @author     humkyung
222
        @date       2018.07.12
223
    '''
224
    def append(self, hmbData):
225
        if self._hmbs is None:
226
            self._hmbs = []
227

    
228
        self._hmbs.append(hmbData)
229

    
230
    '''
231
        @brief      reset
232
        @author     humkyung
233
        @date       2018.07.12
234
    '''
235
    def reset(self):
236
        if self._hmbs is not None:
237
            del self._hmbs
238

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