-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdb.py
300 lines (225 loc) · 8.86 KB
/
db.py
1
2
3
4
5
6
7
8
9
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
# -*- coding: utf-8 -*-
# !! add some test stuff for connect and add row, run with test controller
#import mysql.connector
#from mysql.connector import errorcode
import logging
import pymysql
import os
import time
#import importlib
import sys
sys.path.append( "../rshlib" )
#sys.path.append( "./" )
#import logger
#import parameters
class DBAccess:
"""
connect to db
add data
decide if data is new enough to be added
not general purpose but for the well monitor app
"""
def __init__(self, aController, CSVMode = False ):
"""
set up fro db connection, at least at some point
also made the connection
has a test mode
"""
self.myName = "DBAccess"
self.version = "2016 june 22"
self.controller = aController
self.parameters = aController.parameters
# self.myllogger = aController.myllogger
self.logger = logging.getLogger( self.controller.logger_id + ".DBAccess")
self.logger.info("in class DBAccess init")
#self.log = logging.getLogger( "well" ) # assign to logger or get all the time does logger have aname or does this assign
#self.log = logging
self.logger.info( "In DBAccess init ", )
self.CSVMode = CSVMode
self.fileout = None
self.db_open = False
self.db_connection = None # another indicator of no connection
# Initialize so that will need new value in db on first call
# !! move to processing, delete all ref
self.last_pa = -1
self.last_time = 0
self.last_on = -1
# !! move to processing, delete all ref
self.db_delta_t = 5 # max time between postings
self.db_delat_p = 5 # max pressure change between postings
self.dbConnect( ) # perhaps move out of init
return
#================================
def dbConnect( self, ):
"""
connect to the db and save connection and state
uses parameters for connection parameters
!! need more error handeling
"""
if self.CSVMode :
self.fileout = open( "CSVMode.csv", "w" )
return
try:
self.logger.info( "DBAccess try a connection " )
#conn = pymysql.connect( host='127.0.0.1', port=3306, user='root', passwd='FreeData99', db='well_monitor_1')
conn = pymysql.connect( host = self.parameters.db_host,
port = self.parameters.db_port,
db = self.parameters.db_db,
user = self.parameters.db_user,
passwd = self.parameters.db_passwd,
)
except Exception as e:
self.logger.error( "got exception on connect" )
self.logger.error( self.parameters.db_host )
self.logger.error( self.parameters.db_port )
self.logger.error( self.parameters.db_db )
self.logger.error( self.parameters.db_user )
self.logger.error( self.parameters.db_passwd )
print( "got exception on connect" )
print( e )
self.db_open = False
return
# except mysql.connector.Error as err:
# if err.errno == errorcode.ER_ACCESS_DENIED_ERROR:
# print("Something is wrong with your user name or password")
# elif err.errno == errorcode.ER_BAD_DB_ERROR:
# print("Database does not exist")
# else:
# print(err)
# else:
# cnx.close()
self.logger.info( "dbConnect got connection " )
self.db_connection = conn
self.db_open = True
# self.test_retrieve() for testing
#================================
def test_retrieve( self, ):
"""
just for fun and testing do a fetch
"""
cur = self.db_connection.cursor()
cur.execute("select * from pressure_history")
print(cur.description)
print()
for row in cur:
print(row)
#cur.close()
#conn.close()
#================================
def dbClose( self, ):
"""
!! do we need to check if open?
in fact it may not exist check against none??
note early return
"""
if self.CSVMode :
self.fileout.close()
self.db_open = False
self.db_connection = None
return
if self.db_connection is None:
self.db_open = False
return
self.db_connection.close()
self.db_open = False
self.db_connection = None
#================================
def dbAddRow( self, ts, pressure, well_on ):
"""
add a row to the pressure_history table
!! may want some error checking
return nothing??
"""
log_msg = "dbAddRow " + str(ts) + " " + str( pressure )
#print( log_msg )
self.logger.info( log_msg )
if self.CSVMode:
db_string = ( "insert into pressure_history, " +
str( ts ) + ", " +
str( pressure ) + ", " +
str( well_on ) )
# "( ph_timestamp, ph_pressure_a, ph_well_on ) " +
# "values (%s, %s, %s)" )
#fileout.write( str( o_num ) + "\r" )
self.fileout.write( db_string + "\r" )
return
add_ph = ( "insert into pressure_history"
"( ph_timestamp, ph_pressure_a, ph_well_on ) "
"values (%s, %s, %s)" )
print ( add_ph )
data_ph = ( ts, pressure, well_on )
cursor = self.db_connection.cursor()
cursor.execute( add_ph, data_ph )
self.db_connection.commit()
#================================
def dbNewValues( self, ts, pressure, well_on ):
"""
update db if new values require it
note early return
"""
log_msg = "dbNewValues data at ts " + str(ts) + " " + str( pressure )
# print( log_msg )
self.logger.info( log_msg )
need_update = self.needUpdate( ts, pressure, well_on )
if not( need_update ):
return
self.last_time = ts
self.last_pa = pressure
self.last_on = well_on
print( " >>> add it " )
self.dbAddRow( ts, pressure, well_on )
#================================
def logit( self, adata ):
"""
for compatibility with old code
!! clean up
"""
self.logger.info( adata )
return
# ---------------------------------------
def log( self, adata, ato ):
"""
just a call to logger
is this needed/used, can component
call directly?
!! clean up
"""
self.logger.info( adata )
return
# ---------------------------------------
def isDiff_xxxx( self, another_point ):
"""
compare vaues to see if this is significantly different
a bit overdone, partly for future expansion
"""
if self.time is None :
return False # we must have no data
if another_point is None: # something is different than nothing
return True
if another_point.time is None:
return True # something is different from nothing
if ( another_point.time - self.time ) > self.deltaTime:
return True
# next is done only if we have data
if self.pressure is not None:
if another_point.pressure is None:
return True
if abs( another_point.pressure - self.pressure ) > self.deltaP:
return True
return False
# ---------------------------------------
def needUpdate( self, ts, pressure, well_on ):
"""
! note early return
!! dup with isfiff?
"""
if ts > self.last_time + self.db_delta_t: # pre-compute ??
return True
#print "pressure"
#print self.last_pa
if abs( pressure - self.last_pa ) > self.db_delat_p:
return True
if abs( self.last_on != - well_on ):
return True
return False
# ================================ eof =================================