Repository for M.A.I.L system's recording client.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

main.py 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. import tkinter
  2. import json
  3. import paramiko
  4. import time
  5. from datetime import datetime
  6. from tkinter import messagebox
  7. import os
  8. import git
  9. from paramiko import file
  10. import pymssql
  11. #Test update
  12. window = tkinter.Tk()
  13. settings =[]
  14. customer_code = ''
  15. surgery_date = str(time.strftime('%Y%m%d', time.localtime(time.time())))
  16. cnxn = pymssql.connect('52.231.39.219' , 'mc365', 'tkadbrdhmc1!', 'tsfmc_data')
  17. cursor = cnxn.cursor()
  18. with open('./settings.json', encoding='utf-8')as json_file:
  19. json_data = json.load(json_file)
  20. settings.append(json_data['window_name'])
  21. settings.append(json_data['window_position'])
  22. settings.append(json_data['branch_location'])
  23. settings.append(json_data['surgery_room_number'])
  24. settings.append(json_data['ddns'])
  25. settings.append(json_data['account']) #settings[5]
  26. settings.append(json_data['password']) #settings[6]
  27. window.title(settings[0])
  28. window.geometry(settings[1])
  29. window.resizable(False,False)
  30. def sshConnect(cs, sd):
  31. cs = cs
  32. sd = sd
  33. filepath = './log.txt'
  34. ssh = paramiko.SSHClient()
  35. ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
  36. ssh.connect(settings[4],port= settings[3], username=settings[5], password=settings[6])
  37. data_root_dir = './data/'+cs+sd
  38. stdin, stdout, stderr = ssh.exec_command('mkdir ' + data_root_dir)
  39. #TODO: 여기에 도커 명령어 및 동시 녹화 코드 입력
  40. cmd = 'docker run --rm --name mail_recorder --gpus 1 --privileged -v /dev:/dev -v /home/mc365/sources:/sources -v /home/mc365/data/'+cs+sd+':/data/'+cs+sd+' stereolabs/zed:3.5-gl-devel-cuda11.1-ubuntu18.04 python3 /sources/rec_ubuntu.py'
  41. print(cmd)
  42. stdin, stdout, stderr = ssh.exec_command(cmd)
  43. f = open(filepath, 'w')
  44. r_time = time.ctime(time.time())
  45. f.write('record succesfully started at ' + str(r_time) + '\n')
  46. f.close()
  47. def sshQuit(cs, sd):
  48. cs = cs
  49. sd = sd
  50. filepath = './log.txt'
  51. data_root_dir = './data/'+cs+sd
  52. f = open(filepath, 'a')
  53. r_time = time.ctime(time.time())
  54. f.write('record succesfully stopped at ' + str(r_time)+ '\n')
  55. f.close()
  56. transport = paramiko.Transport((settings[4], settings[3]))
  57. transport.connect(username=settings[5], password=settings[6])
  58. sftp = paramiko.SFTPClient.from_transport(transport)
  59. sftp.put(filepath, data_root_dir + filepath[1:])
  60. sftp.close()
  61. transport.close()
  62. os.remove(filepath)
  63. ssh = paramiko.SSHClient()
  64. ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
  65. ssh.connect(settings[4],port= settings[3], username=settings[5], password=settings[6])
  66. t = ssh.get_transport()
  67. c = t.open_session()
  68. c.exec_command('docker stop mail_recorder && mv ' + data_root_dir + ' /hdd/')
  69. label_branch = tkinter.Label(window, text = '지점 위치: ' + settings[2] + ' / 수술방 번호: ' + str(settings[3]))
  70. label_customer_code = tkinter.Label(window, text="고객번호(지점 번호 포함 9자리)")
  71. def dummyexit():
  72. pass
  73. def askInformation(customer_code,surgery_date, customer_name):
  74. cs = customer_code
  75. sd = surgery_date
  76. global pk
  77. pk = cs + sd
  78. if len(cs) != 9:
  79. err_box = messagebox.showerror("고객 번호 오류", '지점 번호를 포함한 9자리를 입력해주세요.')
  80. else:
  81. msg_box = messagebox.askquestion("입력 정보 확인", "\n고객 이름 : " + customer_name + "\n\n고객 번호 : " + customer_code + '\n\n맞으면 Yes를 눌러 기록시작을,\n 틀리면 No를 눌러 수정해주세요.')
  82. #TODO: try: 형태로 고칠 것. Err 메세지도 종류 정할 것
  83. if msg_box == 'yes':
  84. global start_time
  85. start_time = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
  86. err_status = True
  87. if err_status == False:
  88. label_recording = tkinter.Label(window, text='분석 PC에 문제가 발생하였습니다.\nHOBIT LAB으로 연락 부탁드립니다.\n내선번호: 1103 \n에러 코드: REQUEST TIME OUT')
  89. label_recording.pack()
  90. else:
  91. #TODO: paramiko로 ssh 접속 후 도커 명령어 보낼 것
  92. sshConnect(cs, sd)
  93. button_confirm['text'] = '기록 종료'
  94. window.protocol('WM_DELETE_WINDOW', dummyexit)
  95. label_recording = tkinter.Label(window, text='MAIL system에 수술을 기록 중입니다.\n기록 중단을 원할 시 \n기록 종료 버튼을 눌러주세요.')
  96. label_recording.pack()
  97. def askQuit(cn, sd):
  98. customer_number = cn
  99. surgery_date = sd
  100. msg_box = messagebox.askquestion("기록 중지 확인", '기록을 중지하시겠습니까?')
  101. if msg_box == 'yes':
  102. end_time = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
  103. # print(str(end_time))
  104. data = [(pk, json_data['branch_location'], json_data['surgery_room_number'], str(start_time), str(end_time))]
  105. cursor.executemany("INSERT into tsfmc_mailsystem.dbo.MAIL_SURGERYCOUNT values ( %s, %s, %d, %s, %s)", data)
  106. cnxn.commit()
  107. #TODO: docker 중지 명령어 보내기
  108. sshQuit(customer_number, surgery_date)
  109. window.destroy()
  110. def buttonAction(event):
  111. customer_code = str(entry_customer_code.get())
  112. cursor.execute("SELECT PSNAME FROM ADM010T WHERE PSENTRY='{}';".format(customer_code) )
  113. customer_name = cursor.fetchone()[0]
  114. if button_confirm['text'] == '입력완료':
  115. askInformation(customer_code, surgery_date, customer_name)
  116. else:
  117. askQuit(customer_code,surgery_date)
  118. # def fillSurgeryDate():
  119. entry_customer_code = tkinter.Entry(window)
  120. entry_surgery_date = tkinter.Entry(window)
  121. button_confirm = tkinter.Button(window, text="입력완료", width=20, height=2)
  122. button_confirm.bind("<Button-1>", buttonAction)
  123. label_branch.pack()
  124. label_customer_code.pack()
  125. entry_customer_code.pack()
  126. button_confirm.pack()
  127. if __name__ == "__main__":
  128. lastmod = int(os.path.getmtime('./main.py'))
  129. #NOTE: UPDATE PROGRESSING....
  130. print(lastmod)
  131. os.system('git merge origin/alpha')
  132. if lastmod != int(os.path.getmtime('./main.py')):
  133. pass
  134. else:
  135. window.mainloop()