RequestClass.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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 add_order_log(self, operation):
  45. try:
  46. with self.conn.cursor() as cursor:
  47. date_cur = datetime.datetime.now()
  48. insert_order_log = "INSERT INTO order_list_log (id serial primary key, payment_transaction_id int NOT NULL, service_id int NOT NULL, service_type_id int NOT NULL, time_placed timestamptz NOT NULL, time_start timestamptz NOT NULL, time_finish_predicted timestamptz, time_finish_real timestamptz NOT NULL, order_place_start varchar(255) NOT NULL, order_place_predicted varchar(255) NOT NULL, order_place_real varchar(255) NOT NULL, price money NOT NULL, provider int NOT NULL, receiver int NOT NULL, client int NOT NULL) VALUES (1,1,1,'2022-06-17 01:02:03', '2022-06-17 01:02:03','2022-06-17 12:02:03','2022-06-17 12:02:03', 'Walmart street, 14.24', 'Times street, 13.14', 'Walmart street, 14.24', 100, 1,1,1);"
  49. val = [2, 3, operation, date_cur, 1]
  50. cursor.execute(insert_car_log, val)
  51. self.conn.commit()
  52. return "Успешный запрос!"
  53. except Exception as ex:
  54. return str(ex)
  55. def del_car_log(self, id):
  56. try:
  57. with self.conn.cursor() as cursor:
  58. delete_car_log = "DELETE FROM cars_log WHERE id = %s"
  59. cursor.execute(delete_car_log, id)
  60. self.conn.commit()
  61. return "Успешно!"
  62. except Exception as ex:
  63. return str(ex)
  64. def insert_current_order(self, order_id, service_type_id, provider_id, resourse_id, time_start_predicted,time_finished_predicted):
  65. try:
  66. with self.conn.cursor() as cursor:
  67. gap = 60
  68. 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)"
  69. val = [order_id, service_type_id, provider_id, resourse_id, time_start_predicted,time_finished_predicted, gap]
  70. cursor.execute(insert_current_order, val)
  71. self.conn.commit()
  72. return "Успешный запрос!"
  73. except Exception as ex:
  74. return str(ex)
  75. def del_current_order(self, order_id):
  76. try:
  77. with self.conn.cursor() as cursor:
  78. delete_current_order = "DELETE FROM current_timetable WHERE order_id = %s"
  79. val = [order_id]
  80. cursor.execute(delete_current_order, val)
  81. self.conn.commit()
  82. return "Успешный запрос!"
  83. except Exception as ex:
  84. return str(ex)
  85. def get_upcoming_orders(self):
  86. try:
  87. with self.conn.cursor() as cursor:
  88. select_upcoming_orders = "SELECT * FROM current_timetable"
  89. cursor.execute(select_upcoming_orders)
  90. self.conn.commit()
  91. return "Успешный запрос!"
  92. except Exception as ex:
  93. return str(ex)
  94. def change_status(self, status):
  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 create_ticket(self, type, reporter, assignee, time_created, time_assigned, time_changed, time_closed, status, description):
  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 change_ticket_status(self, id, status):
  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 add_car(self, resource_id, user_id, order_status, order_id, direction, battery_level, board_voltage):
  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 del_car(self, id):
  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_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):
  150. try:
  151. with self.conn.cursor() as cursor:
  152. date_cur = datetime.datetime.now()
  153. insert_car_log = "INSERT INTO cars_log (resource_id, operation, time_log_created, user_id) VALUES(%s, %s, %s, %s)"
  154. val = [2, operation, date_cur, 5]
  155. cursor.execute(insert_car_log, val)
  156. self.conn.commit()
  157. return "Успешный запрос!"
  158. except Exception as ex:
  159. return str(ex)
  160. def change_current_token(self, id, current_token):
  161. try:
  162. with self.conn.cursor() as cursor:
  163. change_current_token = "UPDATE current_cars SET current_token = %s WHERE id = %s"
  164. val = [current_token, id]
  165. cursor.execute(change_current_token, val)
  166. self.conn.commit()
  167. return "Успешный запрос!"
  168. except Exception as ex:
  169. return str(ex)
  170. def update_current_order(self, order_id, service_type_id, provider_id, resource_id, time_start_predicted, time_finished_predicted):
  171. try:
  172. with self.conn.cursor() as cursor:
  173. 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"
  174. val = [order_id, service_type_id, provider_id, resource_id, time_start_predicted,
  175. time_finished_predicted]
  176. cursor.execute(update_current_order, val)
  177. self.conn.commit()
  178. return "Успешный запрос!"
  179. except Exception as ex:
  180. return str(ex)
  181. def actuality_check(self,id):
  182. try:
  183. with self.conn.cursor() as cursor:
  184. date_cur = datetime.datetime.now()
  185. insert_car_log = "INSERT INTO cars_log (resource_id, operation, time_log_created, user_id) VALUES(%s, %s, %s, %s)"
  186. val = [2, operation, date_cur, 5]
  187. cursor.execute(insert_car_log, val)
  188. self.conn.commit()
  189. select_orders = "SELECT * FROM current_orders WHERE id = %s AND state = 151 OR state = 159"
  190. val = [id]
  191. res = cursor.execute(select_orders)
  192. if (res == True):
  193. delete_orders = "DELETE FROM current_timetable WHERE id = %s"
  194. val = [id]
  195. cursor.execute(delete_orders, val)
  196. return "Успешный запрос!"
  197. except Exception as ex:
  198. return str(ex)