개정판 0214673b
refs #584: upload HMBTable which is missing
DTI_PID/DTI_PID/HMBTable.py | ||
---|---|---|
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 |
|
|
19 |
''' |
|
20 |
@author humkyung |
|
21 |
@date 2018.07.12 |
|
22 |
''' |
|
23 |
@property |
|
24 |
def uid(self): |
|
25 |
return self._uid |
|
26 |
|
|
27 |
|
|
28 |
''' |
|
29 |
@author humkyung |
|
30 |
@date 2018.07.12 |
|
31 |
''' |
|
32 |
@uid.setter |
|
33 |
def uid(self, value): |
|
34 |
self._uid = value |
|
35 |
|
|
36 |
''' |
|
37 |
@author humkyung |
|
38 |
@date 2018.07.12 |
|
39 |
''' |
|
40 |
@property |
|
41 |
def streamNo(self): |
|
42 |
return self._stream_no |
|
43 |
|
|
44 |
''' |
|
45 |
@author humkyung |
|
46 |
@date 2018.07.12 |
|
47 |
''' |
|
48 |
@streamNo.setter |
|
49 |
def streamNo(self, value): |
|
50 |
self._stream_no = value |
|
51 |
|
|
52 |
''' |
|
53 |
@author humkyung |
|
54 |
@date 2018.07.12 |
|
55 |
''' |
|
56 |
@property |
|
57 |
def name(self): |
|
58 |
return self._name |
|
59 |
|
|
60 |
''' |
|
61 |
@author humkyung |
|
62 |
@date 2018.07.12 |
|
63 |
''' |
|
64 |
@name.setter |
|
65 |
def name(self, value): |
|
66 |
self._name = value |
|
67 |
|
|
68 |
''' |
|
69 |
@author humkyung |
|
70 |
@date 2018.07.12 |
|
71 |
''' |
|
72 |
@property |
|
73 |
def unit(self): |
|
74 |
return self._unit |
|
75 |
|
|
76 |
''' |
|
77 |
@author humkyung |
|
78 |
@date 2018.07.12 |
|
79 |
''' |
|
80 |
@unit.setter |
|
81 |
def unit(self, value): |
|
82 |
self._unit = value |
|
83 |
|
|
84 |
''' |
|
85 |
@author humkyung |
|
86 |
@date 2018.07.12 |
|
87 |
''' |
|
88 |
@property |
|
89 |
def value(self): |
|
90 |
return self._value |
|
91 |
|
|
92 |
''' |
|
93 |
@author humkyung |
|
94 |
@date 2018.07.12 |
|
95 |
''' |
|
96 |
@value.setter |
|
97 |
def value(self, value): |
|
98 |
self._value = value |
|
99 |
|
|
100 |
''' |
|
101 |
@brief create hmb data from database record |
|
102 |
@author humkyung |
|
103 |
@date 2018.07.12 |
|
104 |
''' |
|
105 |
@staticmethod |
|
106 |
def fromRow(row): |
|
107 |
hmb = HMBData() |
|
108 |
hmb._uid = row[0] |
|
109 |
hmb._stream_no = row[1] |
|
110 |
hmb._name = row[2] |
|
111 |
hmb._unit = row[3] |
|
112 |
hmb._value = row[4] |
|
113 |
|
|
114 |
return hmb |
|
115 |
|
|
116 |
class HMBTable: |
|
117 |
''' |
|
118 |
@brief constructor |
|
119 |
@author humkyung |
|
120 |
@date 2018.07.12 |
|
121 |
''' |
|
122 |
def __init__(self): |
|
123 |
self._hmbs = None |
|
124 |
|
|
125 |
''' |
|
126 |
@brief load hmb data from database |
|
127 |
@author humkyung |
|
128 |
@date 2018.07.12 |
|
129 |
''' |
|
130 |
def loadData(self): |
|
131 |
from AppDocData import AppDocData |
|
132 |
|
|
133 |
try: |
|
134 |
if self._hmbs is None: |
|
135 |
self._hmbs = [] |
|
136 |
|
|
137 |
appDocData = AppDocData.instance() |
|
138 |
|
|
139 |
dbPath = os.path.join(appDocData.getCurrentProject().getDbFilePath() , 'ITI_PID.db') |
|
140 |
|
|
141 |
conn = sqlite3.connect(dbPath) |
|
142 |
cursor = conn.cursor() |
|
143 |
sql = 'SELECT UID,STREAM_NO,NAME,UNIT,VALUE FROM HMB ORDER BY STREAM_NO' |
|
144 |
cursor.execute(sql) |
|
145 |
rows = cursor.fetchall() |
|
146 |
for row in rows: |
|
147 |
hmb = HMBData.fromRow(row) |
|
148 |
self._hmbs.append(hmb) |
|
149 |
# Catch the exception |
|
150 |
except Exception as ex: |
|
151 |
# Roll back any change if something goes wrong |
|
152 |
conn.rollback() |
|
153 |
print('error occured({}) in {}:{}'.format(ex, sys.exc_info()[-1].tb_frame.f_code.co_filename, sys.exc_info()[-1].tb_lineno)) |
|
154 |
finally: |
|
155 |
conn.close() |
|
156 |
|
|
157 |
return self._hmbs |
|
158 |
|
|
159 |
''' |
|
160 |
@brief save hmb data |
|
161 |
@author humkyung |
|
162 |
@date 2018.07.12 |
|
163 |
''' |
|
164 |
def saveData(self): |
|
165 |
from AppDocData import AppDocData |
|
166 |
|
|
167 |
try: |
|
168 |
if self._hmbs is not None: |
|
169 |
appDocData = AppDocData.instance() |
|
170 |
# Creates or opens a file called mydb with a SQLite3 DB |
|
171 |
dbPath = os.path.join(appDocData.getCurrentProject().getDbFilePath(), "ITI_PID.db") |
|
172 |
conn = sqlite3.connect(dbPath) |
|
173 |
# Get a cursor object |
|
174 |
cursor = conn.cursor() |
|
175 |
|
|
176 |
sql = 'delete from HMB' |
|
177 |
cursor.execute(sql) |
|
178 |
|
|
179 |
for data in self._hmbs: |
|
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 |
|
|
184 |
conn.commit() |
|
185 |
# Catch the exception |
|
186 |
except Exception as ex: |
|
187 |
# Roll back any change if something goes wrong |
|
188 |
conn.rollback() |
|
189 |
print('error occured({}) in {}:{}'.format(ex, sys.exc_info()[-1].tb_frame.f_code.co_filename, sys.exc_info()[-1].tb_lineno)) |
|
190 |
finally: |
|
191 |
# Close the db connection |
|
192 |
conn.close() |
|
193 |
|
|
194 |
''' |
|
195 |
@brief return stream no collection which are not duplicated |
|
196 |
@author humkyung |
|
197 |
@date 2018.07.12 |
|
198 |
''' |
|
199 |
def streamNos(self): |
|
200 |
return set([hmb.streamNo for hmb in self._hmbs]) if self._hmbs is not None else {} |
|
201 |
|
|
202 |
''' |
|
203 |
@brief return given index's data |
|
204 |
@author humkyung |
|
205 |
@date 2018.07.12 |
|
206 |
''' |
|
207 |
def dataOfStreamNo(self, streamNo): |
|
208 |
return [hmb for hmb in self._hmbs if hmb.streamNo == streamNo] if self._hmbs is not None else None |
|
209 |
|
|
210 |
''' |
|
211 |
@brief append hmb data |
|
212 |
@author humkyung |
|
213 |
@date 2018.07.12 |
|
214 |
''' |
|
215 |
def append(self, hmbData): |
|
216 |
if self._hmbs is None: |
|
217 |
self._hmbs = [] |
|
218 |
|
|
219 |
self._hmbs.append(hmbData) |
|
220 |
|
|
221 |
''' |
|
222 |
@brief reset |
|
223 |
@author humkyung |
|
224 |
@date 2018.07.12 |
|
225 |
''' |
|
226 |
def reset(self): |
|
227 |
if self._hmbs is not None: |
|
228 |
del self._hmbs |
|
229 |
|
|
230 |
self._hmbs = None |
내보내기 Unified diff