import subprocess import tkinter import json import paramiko import time from datetime import datetime from tkinter import messagebox import os import git from paramiko import file import pymssql window = tkinter.Tk() settings =[] customer_code = '' surgery_date = str(time.strftime('%Y%m%d', time.localtime(time.time()))) cnxn = pymssql.connect('52.231.39.219' , 'mc365', 'tkadbrdhmc1!', 'tsfmc_data') cursor = cnxn.cursor() with open('./settings.json', encoding='utf-8')as json_file: json_data = json.load(json_file) settings.append(json_data['window_name']) settings.append(json_data['window_position']) settings.append(json_data['branch_location']) settings.append(json_data['surgery_room_number']) settings.append(json_data['ddns']) settings.append(json_data['account']) #settings[5] settings.append(json_data['password']) #settings[6] window.title(settings[0]) window.geometry(settings[1]) window.resizable(False,False) def sshConnect(cs, sd): cs = cs sd = sd filepath = './log.txt' ssh = paramiko.SSHClient() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) ssh.connect(settings[4],port= settings[3], username=settings[5], password=settings[6]) data_root_dir = './data/'+cs+sd stdin, stdout, stderr = ssh.exec_command('mkdir ' + data_root_dir) #TODO: 여기에 도커 명령어 및 동시 녹화 코드 입력 cmd = 'python3 /home/mc365/sources/rec_ubuntu.py' stdin, stdout, stderr = ssh.exec_command(cmd) f = open(filepath, 'w') r_time = time.ctime(time.time()) f.write('record succesfully started at ' + str(r_time) + '\n') f.close() def sshQuit(cs, sd): cs = cs sd = sd filepath = './log.txt' data_root_dir = './data/'+cs+sd f = open(filepath, 'a') r_time = time.ctime(time.time()) f.write('record succesfully stopped at ' + str(r_time)+ '\n') f.close() transport = paramiko.Transport((settings[4], settings[3])) transport.connect(username=settings[5], password=settings[6]) sftp = paramiko.SFTPClient.from_transport(transport) sftp.put(filepath, data_root_dir + filepath[1:]) sftp.close() transport.close() os.remove(filepath) ssh = paramiko.SSHClient() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) ssh.connect(settings[4],port= settings[3], username=settings[5], password=settings[6]) t = ssh.get_transport() c = t.open_session() c.exec_command('docker stop mail_recorder && mv ' + data_root_dir + ' /hdd/') label_branch = tkinter.Label(window, text = '지점 위치: ' + settings[2] + ' / 수술방 번호: ' + str(settings[3])) label_customer_code = tkinter.Label(window, text="고객번호(지점 번호 포함 9자리)") def dummyexit(): pass def askInformation(customer_code,surgery_date, customer_name): cs = customer_code sd = surgery_date global pk pk = cs + sd if len(cs) != 9: err_box = messagebox.showerror("고객 번호 오류", '지점 번호를 포함한 9자리를 입력해주세요.') else: msg_box = messagebox.askquestion("입력 정보 확인", "\n고객 이름 : " + customer_name + "\n\n고객 번호 : " + customer_code + '\n\n맞으면 Yes를 눌러 기록시작을,\n 틀리면 No를 눌러 수정해주세요.') #TODO: try: 형태로 고칠 것. Err 메세지도 종류 정할 것 if msg_box == 'yes': global start_time start_time = datetime.now().strftime('%Y-%m-%d %H:%M:%S') err_status = True if err_status == False: label_recording = tkinter.Label(window, text='분석 PC에 문제가 발생하였습니다.\nHOBIT LAB으로 연락 부탁드립니다.\n내선번호: 1103 \n에러 코드: REQUEST TIME OUT') label_recording.pack() else: #TODO: paramiko로 ssh 접속 후 도커 명령어 보낼 것 sshConnect(cs, sd) button_confirm['text'] = '기록 종료' window.protocol('WM_DELETE_WINDOW', dummyexit) label_recording = tkinter.Label(window, text='MAIL system에 수술을 기록 중입니다.\n기록 중단을 원할 시 \n기록 종료 버튼을 눌러주세요.') label_recording.pack() def askQuit(cn, sd): customer_number = cn surgery_date = sd msg_box = messagebox.askquestion("기록 중지 확인", '기록을 중지하시겠습니까?') if msg_box == 'yes': end_time = datetime.now().strftime('%Y-%m-%d %H:%M:%S') # print(str(end_time)) data = [(pk, json_data['branch_location'], json_data['surgery_room_number'], str(start_time), str(end_time))] cursor.executemany("INSERT into tsfmc_mailsystem.dbo.MAIL_SURGERYCOUNT values ( %s, %s, %d, %s, %s)", data) cnxn.commit() #TODO: docker 중지 명령어 보내기 sshQuit(customer_number, surgery_date) window.destroy() def buttonAction(event): customer_code = str(entry_customer_code.get()) cursor.execute("SELECT PSNAME FROM ADM010T WHERE PSENTRY='{}';".format(customer_code) ) customer_name = cursor.fetchone()[0] if button_confirm['text'] == '입력완료': askInformation(customer_code, surgery_date, customer_name) else: askQuit(customer_code,surgery_date) # def fillSurgeryDate(): entry_customer_code = tkinter.Entry(window) entry_surgery_date = tkinter.Entry(window) button_confirm = tkinter.Button(window, text="입력완료", width=20, height=2) button_confirm.bind("", buttonAction) label_branch.pack() label_customer_code.pack() entry_customer_code.pack() button_confirm.pack() if __name__ == "__main__": check_git = subprocess.Popen(['git','merge','origin/alpha'], stdout=subprocess.PIPE, universal_newlines=True) stdout, stderr = check_git.communicate() if (stdout != "Already up to date.\n"): print('stdout not match') messagebox.showinfo("365mc MAIL System","프로그램이 업데이트되었습니다. 프로그램을 재시작해주세요.") pass else: window.mainloop()