hytos / DTI_PID / DTI_PID / HMBTable.py @ 1c0fc022
이력 | 보기 | 이력해설 | 다운로드 (6.12 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 |
from AppDocData import AppDocData |
167 |
|
168 |
try:
|
169 |
if self._hmbs is not None: |
170 |
appDocData = AppDocData.instance() |
171 |
# Creates or opens a file called mydb with a SQLite3 DB
|
172 |
dbPath = os.path.join(appDocData.getCurrentProject().getDbFilePath(), "ITI_PID.db")
|
173 |
conn = sqlite3.connect(dbPath) |
174 |
# Get a cursor object
|
175 |
cursor = conn.cursor() |
176 |
|
177 |
for data in self._hmbs: |
178 |
if data.isDeleted == False: |
179 |
if data.uid is None: |
180 |
sql = "insert or replace into HMB values(lower(hex(randomblob(16))),?,?,?,?)"
|
181 |
param = (data.streamNo, data.name, data.unit, data.value) |
182 |
cursor.execute(sql, param) |
183 |
else:
|
184 |
sql = "update HMB set STREAM_NO=?,NAME=?,UNIT=?,VALUE=? where UID=?"
|
185 |
param = (data.streamNo, data.name, data.unit, data.value, data.uid) |
186 |
cursor.execute(sql, param) |
187 |
else:
|
188 |
sql = "delete from HMB where uid=?"
|
189 |
param = (data.uid,) |
190 |
cursor.execute(sql, param) |
191 |
|
192 |
conn.commit() |
193 |
# Catch the exception
|
194 |
except Exception as ex: |
195 |
# Roll back any change if something goes wrong
|
196 |
conn.rollback() |
197 |
print('error occured({}) in {}:{}'.format(ex, sys.exc_info()[-1].tb_frame.f_code.co_filename, sys.exc_info()[-1].tb_lineno)) |
198 |
finally:
|
199 |
# Close the db connection
|
200 |
conn.close() |
201 |
|
202 |
'''
|
203 |
@brief return stream no collection which are not duplicated
|
204 |
@author humkyung
|
205 |
@date 2018.07.12
|
206 |
'''
|
207 |
def streamNos(self): |
208 |
return set([hmb.streamNo for hmb in self._hmbs if hmb.isDeleted == False]) if self._hmbs is not None else {} |
209 |
|
210 |
'''
|
211 |
@brief return given index's data
|
212 |
@author humkyung
|
213 |
@date 2018.07.12
|
214 |
'''
|
215 |
def dataOfStreamNo(self, streamNo): |
216 |
return [hmb for hmb in self._hmbs if hmb.streamNo == streamNo and hmb.isDeleted == False] if self._hmbs is not None else None |
217 |
|
218 |
'''
|
219 |
@brief append hmb data
|
220 |
@author humkyung
|
221 |
@date 2018.07.12
|
222 |
'''
|
223 |
def append(self, hmbData): |
224 |
if self._hmbs is None: |
225 |
self._hmbs = []
|
226 |
|
227 |
self._hmbs.append(hmbData)
|
228 |
|
229 |
'''
|
230 |
@brief reset
|
231 |
@author humkyung
|
232 |
@date 2018.07.12
|
233 |
'''
|
234 |
def reset(self): |
235 |
if self._hmbs is not None: |
236 |
del self._hmbs |
237 |
|
238 |
self._hmbs = None |