RequestClass.py 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. import psycopg2 as pg
  2. from sshtunnel import SSHTunnelForwarder
  3. from config import SSH_H
  4. import datetime
  5. #Проверка соединения и само соединение
  6. def ConnectToDatbase():
  7. try:
  8. print('Connecting to the PostgreSQL Database...')
  9. ssh_tunnel = SSHTunnelForwarder(
  10. (SSH_H, 334), #ip address and port
  11. ssh_username="artem_valiakhmetov", #имя пользователя
  12. ssh_private_key= 'D:/keys/home/.ssh/id_rsa',# путь к файлу где расположен ssh ключ (не .pub)
  13. ssh_private_key_password= '',# пароль (в данном случае пароль отсутствует)
  14. remote_bind_address=("localhost", 5432) # не особо понял что это
  15. )
  16. ssh_tunnel.start()
  17. print("Success ssh connect!")
  18. conn = pg.connect(
  19. host = "localhost",
  20. port = ssh_tunnel.local_bind_port,
  21. user = "postgres",
  22. password = "",
  23. database = "postgres"
  24. )
  25. print("Successfully connection...")
  26. return conn
  27. except Exception as ex:
  28. print("Error connection to database...")
  29. print(ex)
  30. class RequestDataBase:
  31. def __init__(self):
  32. self.conn = ConnectToDatbase()
  33. def add_car_log(self, operation):
  34. try:
  35. with self.conn.cursor() as cursor:
  36. date_cur = datetime.datetime.now()
  37. insert_car_log = "INSERT INTO cars_log (id, resource_id, operation, time_log_created, user_id) VALUES(%s, %s, %s, %s, %s)"
  38. val = [2, 3, operation, date_cur, 1]
  39. cursor.execute(insert_car_log, val)
  40. self.conn.commit()
  41. return "Успешный запрос!"
  42. except Exception as ex:
  43. return str(ex)
  44. def del_car_log(self, id):
  45. try:
  46. with self.conn.cursor() as cursor:
  47. delete_car_log = "DELETE FROM cars_log WHERE id = %s"
  48. cursor.execute(delete_car_log, id)
  49. self.conn.commit()
  50. return "Успешно!"
  51. except Exception as ex:
  52. return str(ex)
  53. def insert_current_order(self, order_id, service_type_id, provider_id, resourse_id, time_start_predicted,time_finished_predicted):
  54. try:
  55. with self.conn.cursor() as cursor:
  56. gap = 60
  57. insert_current_order = "INSERT INTO current_timetable (order_id, service_type_id, provider_id, resourse_id, time_start_predicted, time_finished_predicted, gap) VALUES (%s, %s, %s, %s, %s, %s, %s)"
  58. val = [order_id, service_type_id, provider_id, resourse_id, time_start_predicted,time_finished_predicted, gap]
  59. cursor.execute(insert_current_order, val)
  60. self.conn.commit()
  61. return "Успешный запрос!"
  62. except Exception as ex:
  63. return str(ex)
  64. def del_current_order(self, order_id):
  65. try:
  66. with self.conn.cursor() as cursor:
  67. delete_current_order = "DELETE FROM current_timetable WHERE order_id = %s"
  68. val = [order_id]
  69. cursor.execute(delete_current_order, val)
  70. self.conn.commit()
  71. return "Успешный запрос!"
  72. except Exception as ex:
  73. return str(ex)
  74. def get_upcoming_orders(self):
  75. try:
  76. with self.conn.cursor() as cursor:
  77. select_upcoming_orders = "SELECT * FROM current_timetable"
  78. cursor.execute(select_upcoming_orders)
  79. self.conn.commit()
  80. return "Успешный запрос!"
  81. except Exception as ex:
  82. return str(ex)
  83. def change_status(self, status):
  84. try:
  85. with self.conn.cursor() as cursor:
  86. date_cur = datetime.datetime.now()
  87. insert_car_log = "INSERT INTO cars_log (resource_id, operation, time_log_created, user_id) VALUES(%s, %s, %s, %s)"
  88. val = [2, operation, date_cur, 5]
  89. cursor.execute(insert_car_log, val)
  90. self.conn.commit()
  91. return "Успешный запрос!"
  92. except Exception as ex:
  93. return str(ex)
  94. def create_ticket(self, type, reporter, assignee, time_created, time_assigned, time_changed, time_closed, status, description):
  95. try:
  96. with self.conn.cursor() as cursor:
  97. date_cur = datetime.datetime.now()
  98. insert_car_log = "INSERT INTO cars_log (resource_id, operation, time_log_created, user_id) VALUES(%s, %s, %s, %s)"
  99. val = [2, operation, date_cur, 5]
  100. cursor.execute(insert_car_log, val)
  101. self.conn.commit()
  102. return "Успешный запрос!"
  103. except Exception as ex:
  104. return str(ex)
  105. def change_ticket_status(self, id, status):
  106. try:
  107. with self.conn.cursor() as cursor:
  108. date_cur = datetime.datetime.now()
  109. insert_car_log = "INSERT INTO cars_log (resource_id, operation, time_log_created, user_id) VALUES(%s, %s, %s, %s)"
  110. val = [2, operation, date_cur, 5]
  111. cursor.execute(insert_car_log, val)
  112. self.conn.commit()
  113. return "Успешный запрос!"
  114. except Exception as ex:
  115. return str(ex)
  116. def add_car(self, resource_id, user_id, order_status, order_id, direction, battery_level, board_voltage):
  117. try:
  118. with self.conn.cursor() as cursor:
  119. date_cur = datetime.datetime.now()
  120. insert_car_log = "INSERT INTO cars_log (resource_id, operation, time_log_created, user_id) VALUES(%s, %s, %s, %s)"
  121. val = [2, operation, date_cur, 5]
  122. cursor.execute(insert_car_log, val)
  123. self.conn.commit()
  124. return "Успешный запрос!"
  125. except Exception as ex:
  126. return str(ex)
  127. def del_car(self, id):
  128. try:
  129. with self.conn.cursor() as cursor:
  130. date_cur = datetime.datetime.now()
  131. insert_car_log = "INSERT INTO cars_log (resource_id, operation, time_log_created, user_id) VALUES(%s, %s, %s, %s)"
  132. val = [2, operation, date_cur, 5]
  133. cursor.execute(insert_car_log, val)
  134. self.conn.commit()
  135. return "Успешный запрос!"
  136. except Exception as ex:
  137. return str(ex)
  138. def change_car(self, id, resource_id, user_id, order_status, order_id, doors_output, engine_input, central_lock_input, ignition_input, doors_input, input4, controller_status, signal_gsm, operator_gsm, signal_gps, location, speed, direction, address, battery_level, board_voltage):
  139. try:
  140. with self.conn.cursor() as cursor:
  141. date_cur = datetime.datetime.now()
  142. insert_car_log = "INSERT INTO cars_log (resource_id, operation, time_log_created, user_id) VALUES(%s, %s, %s, %s)"
  143. val = [2, operation, date_cur, 5]
  144. cursor.execute(insert_car_log, val)
  145. self.conn.commit()
  146. return "Успешный запрос!"
  147. except Exception as ex:
  148. return str(ex)
  149. def change_current_token(self, id, current_token):
  150. try:
  151. with self.conn.cursor() as cursor:
  152. change_current_token = "UPDATE current_cars SET current_token = %s WHERE id = %s"
  153. val = [current_token, id]
  154. cursor.execute(change_current_token, val)
  155. self.conn.commit()
  156. return "Успешный запрос!"
  157. except Exception as ex:
  158. return str(ex)
  159. def update_current_order(self, order_id, service_type_id, provider_id, resource_id, time_start_predicted, time_finished_predicted):
  160. try:
  161. with self.conn.cursor() as cursor:
  162. update_current_order = "UPDATE current_timetable SET order_id = %s, service_type_id = %s, provider_id = %s, resource_id = %s, time_start_predicted = %s, time_finished_predicted = %s, gap = %s"
  163. val = [order_id, service_type_id, provider_id, resource_id, time_start_predicted,
  164. time_finished_predicted]
  165. cursor.execute(update_current_order, val)
  166. self.conn.commit()
  167. return "Успешный запрос!"
  168. except Exception as ex:
  169. return str(ex)
  170. def actuality_check(self,id):
  171. try:
  172. with self.conn.cursor() as cursor:
  173. date_cur = datetime.datetime.now()
  174. insert_car_log = "INSERT INTO cars_log (resource_id, operation, time_log_created, user_id) VALUES(%s, %s, %s, %s)"
  175. val = [2, operation, date_cur, 5]
  176. cursor.execute(insert_car_log, val)
  177. self.conn.commit()
  178. select_orders = "SELECT * FROM current_orders WHERE id = %s AND state = 151 OR state = 159"
  179. val = [id]
  180. res = cursor.execute(select_orders)
  181. if (res == True):
  182. delete_orders = "DELETE FROM current_timetable WHERE id = %s"
  183. val = [id]
  184. cursor.execute(delete_orders, val)
  185. return "Успешный запрос!"
  186. except Exception as ex:
  187. return str(ex)