/
models.py
301 lines (268 loc) · 8.19 KB
/
models.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
301
#coding: gbk
import db
import datetime
import log
import config
class Model(object):
TABLE_DEF = {}
tblname = None
def __init__(self):
pass
@classmethod
def checkTable(cls):
if not cls.tblname:
cls.tblname = cls.__name__
if not db.obj.tableExists(cls.tblname):
tabledesc = ''
for k, v in cls.TABLE_DEF.iteritems():
tabledesc += '%s %s,'%(k, v)
tabledesc = tabledesc[:-1] # remove the last comma
db.obj.createTable(cls.tblname, tabledesc)
db.obj.Commit()
log.info('db: table %s created.'%cls.tblname)
class TerminalHeart(Model):
TABLE_DEF = {
"id": "int primary key",
#"typ": "int NOT NULL",
#"iostat": "int NOT NULL",
#"curr": "int NOT NULL",
#"total": "int NOT NULL",
"stat": "int NOT NULL",
#"cnter": "int NOT NULL"
'updateTime': "datetime",
}
tblname = 'HeartBeat'
def __init__(self, tid, stat):
super(TerminalHeart, self).__init__()
self.tid = tid
self.stat = stat
def checkInDB(self):
cmd = 'select count(*) from %s where id=%d;'%(self.tblname, self.tid)
cursor = db.obj.Exec(cmd)
results = cursor.fetchone()
if results and results[0] > 0:
return True
else:
return False
def insertNewToDB(self):
#cmd = 'insert into %s(id,typ,iostat,curr,total,stat,cnter) values(%d,%d,%d,%d,%d,%d,%d);'%( \
# self.tblname, self.tid, self.typ, self.io, self.curr, self.tot, self.stat, self.counter)
now = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S.%f")[:-3]
cmd = "insert into %s(id,stat,updateTime) values(%d,%d,'%s');"%( \
self.tblname, self.tid, self.stat, now)
db.obj.Exec(cmd)
db.obj.Commit()
def updateToDB(self):
#cmd = 'update %s set typ=%d,iostat=%d, curr=%d, total=%d,stat=%d,cnter=%d where id=%d;'%( \
# self.tblname, self.typ, self.io, self.curr, self.tot, self.stat, self.counter, self.tid)
now = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S.%f")[:-3]
cmd = "update %s set stat=%d,updateTime='%s' where id=%d;"%( \
self.tblname, self.stat, now, self.tid)
db.obj.Exec(cmd)
db.obj.Commit()
def writeToDB(self):
if self.checkInDB():
self.updateToDB()
else:
self.insertNewToDB()
class ParkLogIdentity(Model):
TABLE_DEF = {
"id": "uniqueidentifier PRIMARY KEY NOT NULL DEFAULT newid()",
"tid": "int NOT NULL",
"tdid": "int NOT NULL",
"typ": "int NOT NULL",
"curr": "int NOT NULL",
"sinc": "int NOT NULL",
"sdec": "int NOT NULL",
"stat": "int NOT NULL",
"cnter": "int NOT NULL",
'updateTime': "datetime",
}
tblname = 'park_LogUniqIdent5'
err = 0
def __init__(self, tid, tdid, typ, curr, sinc, sdec, stat, counter):
super(ParkLogIdentity, self).__init__()
self.tid = tid
self.tdid = tdid
self.typ = typ
self.curr = curr
self.sinc = sinc
self.sdec = sdec
self.stat = stat
self.counter = counter
def insertNewToDB(self):
if self.err > 0:
return
now = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S.%f")[:-3]
cmd = "insert into %s(tid, tdid, typ, curr, sinc, sdec, stat, cnter,updateTime) values(%d,%d,%d,%d,%d,%d,%d,%d,'%s');"%( \
self.tblname, self.tid, self.tdid, self.typ, self.curr, self.sinc, self.sdec, self.stat, self.counter, now)
#log.debug("insert logident: %s" % cmd)
db.obj.Exec(cmd)
db.obj.Commit()
def writeToDB(self):
if self.err > 0:
return
self.insertNewToDB()
class ParkLog(Model):
RB_NONE = 0 #不重设base
RB_MANUAL = 1 #手工重设base
RB_AUTO = 2 #自动重设base
TABLE_DEF = {
"id": "int primary key", #终端ID(实际表示车场ID)
"typ": "int NOT NULL", #车场类型
"curr": "int NOT NULL", #当前剩余车位数
"stat": "int NOT NULL", #工作状态
"base": "int NOT NULL", #车位基数
'updateTime': "datetime", #更新时间
}
tblname = 'park_Log5'
err = 0
def __init__(self, tid, tdid, typ, curr, sinc, sdec, stat, reset_base=0):
super(ParkLog, self).__init__()
self.tid = tid
self.tdid = tdid
self.typ = typ
self.curr = curr
self.sinc = sinc
self.sdec = sdec
self.stat = stat
self.reset_base = reset_base
if self.reset_base == self.RB_MANUAL:
self.curr = curr
# else:
# if self.typ > 0x01: #多门
# last_curr = self.queryBase()
# self.curr = last_curr + self.sinc - self.sdec
def resetCurrent(self, curr):
if self.reset_base == self.RB_AUTO:
self.curr = self.queryCurrent() #从数据库读取当前车位数作为当前基数
else:
self.curr = curr
# @classmethod
# def checkTable(cls):
# if not db.obj.tableExists(cls.tblname):
# cls.err = 1
# log.critical('table %s not exists.'%cls.tblname)
def checkInDB(self):
if self.err > 0:
return
cmd = 'select count(*) from %s where id=%d;'%(self.tblname, self.tid)
cursor = db.obj.Exec(cmd)
results = cursor.fetchone()
if results and results[0] > 0:
return True
else:
return False
def insertNewToDB(self):
if self.err > 0:
return
base = self.curr
now = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S.%f")[:-3]
cmd = "insert into %s(id, typ, curr, stat, base, updateTime) values(%d,%d,%d,%d,%d,'%s');"%( \
self.tblname, self.tid, self.typ, self.curr, self.stat, base, now)
db.obj.Exec(cmd)
db.obj.Commit()
def queryCurrent(self):
if self.err > 0:
return
cmd = "select curr from %s where id=%d;"%(self.tblname, self.tid)
cursor = db.obj.Exec(cmd)
results = cursor.fetchone()
if results and len(results) > 0:
return results[0]
else:
log.warning("query current failed, tblname: %s, tid: 0x%0X" %( self.tblname, self.tid ))
return 0
def queryBase(self):
if self.err > 0:
return
cmd = "select base from %s where id=%d;"%(self.tblname, self.tid)
cursor = db.obj.Exec(cmd)
results = cursor.fetchone()
if results and len(results) > 0:
return results[0]
else:
log.warning("query base failed, tblname: %s, tid: 0x%0X" %( self.tblname, self.tid ))
return 0
def updateToDB(self):
if self.err > 0:
return
now = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S.%f")[:-3]
if self.reset_base:
#将当前车位数和基数都设置为重置的基数
cmd = "update %s set curr=%d, base=%d, updateTime='%s' where id=%d;"%( \
self.tblname, self.curr, self.curr, now, self.tid)
log.debug('manual set update db. tid: 0x%0X, cur: %d' %(self.tid, self.curr))
else:
cmd = "update %s set typ=%d, curr=%d, stat=%d, updateTime='%s' where id=%d;"%( \
self.tblname, self.typ, self.curr, self.stat, now, self.tid)
db.obj.Exec(cmd)
db.obj.Commit()
def writeToDB(self):
if self.err > 0:
return
if self.checkInDB():
self.updateToDB()
else:
self.insertNewToDB()
class ParkEquip(Model):
tblname = 'park_equip'
def __init__(self, tid):
super(ParkEquip, self).__init__()
self.tid = tid
self.pid = None
# def __str__(self):
# return '[DBRecord]Terminal tid: %d, pid: %d'%(self.tid, self.pid)
def getParkId(self):
if not self.tid:
return
cmd = "select parkcode from %s where equipid='%d'" % (self.tblname, self.tid)
cursor = db.obj.Exec(cmd)
results = cursor.fetchone()
if results and results[0] > 0:
self.pid = results[0]
return results[0]
class ParkInfo(Model):
tblname = config.get('table', 'park_info_tbl', 'parkInfo')
cache_stot = {}
def __init__(self, pid):
super(ParkInfo, self).__init__()
self.pid = pid
self.stot = self.queryTotal()
def queryTotal(self):
#用cache缓存,减少数据库查询
if self.cache_stot.has_key(self.pid):
return self.cache_stot[self.pid]
column_name = config.get('table', 'park_info_column_total')
cmd = "select %s from %s where id=%d;"%(column_name, self.tblname, self.pid)
cursor = db.obj.Exec(cmd)
results = cursor.fetchone()
if results and results[0] > 0:
self.cache_stot[self.pid] = results[0]
return results[0]
else:
log.critical("query part info failed, tblname: %s, id: 0x%0X" %( self.tblname, self.pid ))
return 0
def checkInDB(self):
cmd = 'select count(*) from %s where Code=%d;'%(self.tblname, self.pid)
cursor = db.obj.Exec(cmd)
results = cursor.fetchone()
if results and results[0] > 0:
return True
else:
return False
TerminalHeart.checkTable()
ParkLog.checkTable()
ParkLogIdentity.checkTable()
if __name__ == "__main__":
tid = 0x10
typ = 0x11
io = 0x01
curr = 100
tot = 200
stat = 1
counter = 1
#p1 = TerminalHeart(tid, typ, io, curr, tot, stat, counter)
#p1.writeToDB()
p2 = ParkLog(1111, 111)
p2.writeToDB()