flaskDBUtils4PG.py 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. import datetime
  2. import time
  3. from .DButilsFunction import flightDB
  4. loginsts = 'loginsts'
  5. logintoken = 'logintoken'
  6. infolog = 'infolog'
  7. IpPhoneip='IpPhoneip'
  8. callingPhone='callingPhone'
  9. vin="vin"
  10. class flaskDB(flightDB) :
  11. def __init__(self , host , port , user , password , database) :
  12. super(flaskDB , self).__init__(host = host , port = port , user = user , password = password ,
  13. database = database)
  14. # self._dir = './DATABASE'
  15. # self.conn = sqlite3.connect(os.path.join(self._dir,DBName))
  16. # self.c = self.conn.cursor()
  17. def insertManyData(self , sql: str , data_list: list) :
  18. self.c.executemany(sql , data_list)
  19. self.conn.commit()
  20. def initIpPhoneip(db: flaskDB) :
  21. db.initTable(IpPhoneip , 'ID' , 'INTEGER PRIMARY KEY NOT NULL' , { 'ip' : 'text' , 'token' : 'text' })
  22. result = db.getAlldata(IpPhoneip)
  23. if result == [ ] :
  24. db.insertData('IpPhoneip' , { 'ID' : 1 , 'ip' : 'None' , 'token' : 'None' })
  25. def initVin(db: flaskDB) :
  26. db.initTable(vin , 'ID' , 'INTEGER PRIMARY KEY NOT NULL' , { 'VINA' : 'text' , 'VINB' : 'text' })
  27. result = db.getAlldata(vin)
  28. if result == [ ] :
  29. db.insertData('vin' , { 'ID' : 1 , 'VINA' : '%s' % time.time() , 'VINB' : '%s' % time.time() })
  30. db.insertData('vin' , { 'ID' : 2 , 'VINA' : '%s' % time.time() , 'VINB' : '%s' % time.time() })
  31. db.insertData('vin' , { 'ID' : 3 , 'VINA' : '%s' % time.time() , 'VINB' : '%s' % time.time() })
  32. def initcallingPhone(db: flaskDB) :
  33. db.initTable(callingPhone , 'ID' , 'INTEGER PRIMARY KEY NOT NULL' , { 'phonenumber' : 'text' , 'time' : 'text' })
  34. result = db.getAlldata(callingPhone)
  35. if result == [ ] :
  36. db.insertData('callingPhone' , { 'ID' : 1 , 'phonenumber' : 'None' , 'time' : 'None' })
  37. def initLoginsts(db: flaskDB) :
  38. db.initTable(loginsts , 'ID' , 'INTEGER PRIMARY KEY NOT NULL' , { 'sts' : 'int' })
  39. result = db.getAlldata(loginsts)
  40. if len(result) != 2 :
  41. try:
  42. db.insertData('loginsts' , { 'ID' : 1 , 'sts' : 0 })
  43. except:
  44. pass
  45. try:
  46. db.insertData('loginsts' , { 'ID' : 2 , 'sts' : 0 })
  47. except:
  48. pass
  49. def initLoginToken(db: flaskDB) :
  50. db.initTable(logintoken , 'ID' , 'INTEGER PRIMARY KEY NOT NULL' , { 'username' : 'text' , 'token' : 'text' })
  51. result = db.getAlldata(logintoken)
  52. if len(result) != 2 :
  53. try:
  54. db.insertData(logintoken , { 'ID' : 1 , 'username' : 'None' , 'token' : 'None' })
  55. except:
  56. pass
  57. try:
  58. db.insertData(logintoken , { 'ID' : 2 , 'username' : 'None' , 'token' : 'None' })
  59. except:
  60. pass
  61. def initInfolog(db: flaskDB) :
  62. db.initTable(infolog , 'ID' , 'SERIAL PRIMARY KEY' , { 'datatime' : 'text' , 'sts' : 'text' })
  63. def checkLogin(db: flaskDB) :
  64. initLoginsts(db)
  65. result = db.queryTabel(loginsts , 'sts' , 'ID=1')
  66. if result[0][0] == 1 :
  67. # print('已登录')
  68. return 1
  69. else :
  70. return 0
  71. def checkLoginflightplan(db: flaskDB) :
  72. initLoginsts(db)
  73. result = db.queryTabel(loginsts , 'sts' , 'ID=2')
  74. if result[ 0 ][ 0 ] == 1 :
  75. # print('已登录')
  76. return 1
  77. else :
  78. return 0
  79. def logout(db: flaskDB) :
  80. initLoginsts(db)
  81. initLoginToken(db)
  82. db.upDateItem(loginsts , { 'sts' : 0 } , 'ID=1')
  83. db.upDateItem(logintoken , { 'username' : "'None'" , 'token' : "'None'" } , 'ID=1')
  84. def logoutflightplan(db: flaskDB) : #飞机排班掉线会直接摧毁amro登录
  85. initLoginsts(db)
  86. initLoginToken(db)
  87. db.upDateItem(loginsts , { 'sts' : 0 } , 'ID=2')
  88. db.upDateItem(loginsts , { 'sts' : 0 } , 'ID=1')
  89. db.upDateItem(logintoken , { 'username' : "'None'" , 'token' : "'None'" } , 'ID=1')
  90. db.upDateItem(logintoken , { 'username' : "'None'" , 'token' : "'None'" } , 'ID=2')
  91. def setIpPhone(db: flaskDB , ip , token) :
  92. initIpPhoneip(db)
  93. db.upDateItem(IpPhoneip , { 'ip' : "'%s'" % ip , 'token' : "'{}'".format(token) } , 'ID=1')
  94. def callingphoneUpdate(db: flaskDB , phonenumber , time) :
  95. initcallingPhone(db)
  96. db.upDateItem(callingPhone , { 'phonenumber' : "'%s'" % phonenumber , 'time' : "'%s'" % time } , 'ID=1')
  97. def getPhoneNum(db: flaskDB) :
  98. initcallingPhone(db)
  99. res = db.queryTabel(callingPhone , "phonenumber" , "ID = '1'")[ 0 ][ 0 ]
  100. return res
  101. def getVin(db: flaskDB , mode , num) :
  102. initVin(db)
  103. if num == 1 : # 昨天
  104. res = db.queryTabel(vin , "VIN%s" % mode , "ID = '1'")[ 0 ][ 0 ]
  105. elif num == 2 : # 今天
  106. res = db.queryTabel(vin , "VIN%s" % mode , "ID = '2'")[ 0 ][ 0 ]
  107. elif num == 3 : # 明天
  108. res = db.queryTabel(vin , "VIN%s" % mode , "ID = '3'")[ 0 ][ 0 ]
  109. return res
  110. def getallVin(db: flaskDB) :
  111. initVin(db)
  112. res = [ ]
  113. for mode in [ "A" , "B" ] :
  114. for num in [ 1 , 2 , 3 ] :
  115. res.append(db.queryTabel(vin , "VIN%s" % mode , "ID = '%s'" % num)[ 0 ][ 0 ])
  116. return res
  117. def updateVin(db: flaskDB , mode , num) :
  118. initVin(db)
  119. Vin = time.time()
  120. if num == 1 :
  121. db.upDateItem(vin , { 'VIN%s' % mode : "'%s'" % Vin } , 'ID=1')
  122. elif num == 2 :
  123. db.upDateItem(vin , { 'VIN%s' % mode : "'%s'" % Vin } , 'ID=2')
  124. elif num == 3 :
  125. db.upDateItem(vin , { 'VIN%s' % mode : "'%s'" % Vin } , 'ID=3')
  126. return Vin
  127. def updateALlVin(db: flaskDB) :
  128. initVin(db)
  129. Vin = time.time()
  130. for mode in [ "A" , "B" ] :
  131. for num in [ 1 , 2 , 3 ] :
  132. db.upDateItem(vin , { 'VIN%s' % mode : "'%s'" % Vin } , 'ID=%s' % num)
  133. def getIpPhoneip(db: flaskDB) :
  134. initIpPhoneip(db)
  135. res1 = { "ip" : "" , "token" : "" }
  136. res = db.queryTabel(IpPhoneip , "*" , "ID = '1'")
  137. if res[ 0 ][ 2 ] != None :
  138. res1 = { "ip" : "%s" % res[ 0 ][ 1 ] , "token" : "%s" % res[ 0 ][ 2 ] }
  139. return res1
  140. else :
  141. return res1
  142. def login(db: flaskDB , user , token , x_access_token) :
  143. initLoginsts(db)
  144. initLoginToken(db)
  145. db.upDateItem(loginsts, { 'sts' : 1 } , 'ID=1')
  146. db.upDateItem(loginsts, { 'sts' : 1 } , 'ID=2')
  147. db.upDateItem(logintoken, { 'username' : user , 'token' : "'{}'".format(token) } , 'ID=1')
  148. db.upDateItem(logintoken, { 'username' : user , 'token' : "'{}'".format(x_access_token) } , 'ID=2')
  149. def getToken(db: flaskDB) :
  150. initLoginToken(db)
  151. result = db.getAlldata(logintoken)
  152. # print(result)
  153. return result[ 0 ][ 2 ] , result[ 1 ][ 2 ]
  154. def insertinfolog(db: flaskDB , datatime , sts) :
  155. initInfolog(db)
  156. db.insertData(infolog , { 'datatime' : "{}".format(datatime) , 'sts' : "{}".format(sts) })
  157. def updatecheck(db: flaskDB) :
  158. now = datetime.datetime.now()
  159. testtime = (now - datetime.timedelta(minutes = 6)).strftime("%Y%m%d %H:%M:%S")
  160. log_str = db.queryTabel(infolog , "ID" , "datatime >'%s' and sts = '200'" % testtime)
  161. if log_str == [ ] :
  162. updatests = "0"
  163. else :
  164. updatests = "1"
  165. return updatests
  166. def clearinfolog(db: flaskDB) :
  167. initInfolog(db)
  168. now = datetime.datetime.now()
  169. passtime = (now - datetime.timedelta(days = 1)).strftime("%Y%m%d %H:%M:%S")
  170. log_str = db.queryTabel(infolog , "ID" , "datatime <'%s'" % passtime)
  171. if log_str != [ ] :
  172. for i in log_str :
  173. db.deleteSingledata(infolog , "ID='%s'" % i[ 0 ])
  174. def getinfolog(db: flaskDB) :
  175. initLoginsts(db)
  176. result = db.getAlldata(infolog)
  177. # print(result)
  178. return result
  179. def testInsertMany(db: flaskDB) :
  180. initInfolog(db)
  181. sql = "insert into infolog (datatime, sts) values (?, ?);"
  182. data_list = [ ]
  183. for i in range(1000) :
  184. nowDay = datetime.datetime.now()
  185. nowDayStr = nowDay.strftime("%Y%m%d")
  186. nowStr = nowDay.strftime("%Y%m%d %H:%M:%S")
  187. data_list.append((nowStr , '200'))
  188. time1 = time.time()
  189. db.insertManyData(sql , data_list)
  190. # print("插入多条数据:{}".format(time.time()-time1))
  191. def testInsert(db: flaskDB) :
  192. for i in range(1000) :
  193. nowDay = datetime.datetime.now()
  194. nowDayStr = nowDay.strftime("%Y%m%d")
  195. nowStr = nowDay.strftime("%Y%m%d %H:%M:%S")
  196. insertinfolog(db , nowStr , '200')
  197. if __name__ == '__main__' :
  198. flaskDB = flaskDB(host = '192.168.2.65' ,
  199. port = '9527' ,
  200. # host='101.42.5.211', #测试服务器
  201. # port='5444',
  202. user = 'user1' ,
  203. password = 'a123456s' ,
  204. database = "flaskDatabase"
  205. )
  206. initLoginsts(flaskDB)
  207. # flaskDB.insertData('loginsts', {'ID':1,'sts':1})
  208. # checkLogin(flaskDB)
  209. # login(flaskDB, '022673', '7de7ce62-e02f-4f9e-bcec-03b9084e890d')
  210. # checkLogin(flaskDB)
  211. # logout(flaskDB)
  212. #
  213. # getToken(flaskDB)
  214. testInsertMany(flaskDB)
  215. time1 = time.time()
  216. testInsert(flaskDB)
  217. print('插入时间:{}'.format(time.time() - time1))