Advertisement
Guest User

Untitled

a guest
Mar 2nd, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.82 KB | None | 0 0
  1. from __future__ import absolute_import
  2. from __future__ import print_function
  3. import uuid
  4. from hl7apy.core import Message, Segment
  5. from hl7apy.parser import parse_message
  6. from datetime import datetime
  7. from dateutil.parser import parse
  8. import mysql.connector
  9. from mysql.connector import errorcode
  10.  
  11. import re
  12.  
  13. try:
  14. from SocketServer import StreamRequestHandler, TCPServer
  15. except ImportError: # Python 3
  16. from socketserver import StreamRequestHandler, TCPServer
  17.  
  18. #: caracteres para codificação MLLP
  19. SB = "\x0b"
  20. EB = "\x1c"
  21. CR = "\x0d"
  22.  
  23.  
  24.  
  25.  
  26. def guardaMsg(idPedido, utente_name, morada, telefone, data, hora , descricao):
  27. try:
  28. cnx = mysql.connector.connect(user='root',
  29. password='root',
  30. database='BDhospital')
  31. except mysql.connector.Error as err:
  32. if err.errno == errorcode.ER_ACCESS_DENIED_ERROR:
  33. print("Something is wrong with your user name or password")
  34. elif err.errno == errorcode.ER_BAD_DB_ERROR:
  35. print("Database does not exist")
  36. else:
  37. print(err)
  38. else:
  39. cursor = cnx.cursor()
  40.  
  41. add_exame = ("INSERT INTO pedido"
  42. "(idPedido, utente_name, morada, telefone, data, hora,estado,observacao , descricao, relatorio ) "
  43. "VALUES (%s, %s, %s, %s, %s, %s,0 ,\"observacao\" , %s, \"observacao\" )")
  44.  
  45. dados_exame = (idPedido, utente_name, morada, telefone, data, hora , descricao)
  46.  
  47. cursor.execute(add_exame, dados_exame)
  48.  
  49. cnx.commit()
  50.  
  51. print("Exame Registado!")
  52.  
  53. cursor.close()
  54.  
  55. cnx.close()
  56.  
  57. def parser(mensagem):
  58. try:
  59. # parse the incoming HL7 message
  60. m = parse_message(mensagem, find_groups=False)
  61. except:
  62. print ('parsing failed'), repr(mensagem)
  63. else:
  64. idPedido = m.msh.msh_10.to_er7()
  65. print ("Numero do Pedido: ", idPedido)
  66.  
  67. idDoente = m.PID.PID_3.to_er7()
  68. print ("ID do paciente: ", idDoente)
  69.  
  70. moradaDoente = m.pid.pid_11.to_er7()
  71. print ("Morada do Paciente: ", moradaDoente)
  72.  
  73. telefoneDoente = m.pid.pid_13.to_er7()
  74. print ("Telefone do Paciente: ", telefoneDoente)
  75.  
  76. dataPedido = datetime.strptime(m.msh.msh_7.to_er7(),'%Y%d%m%H%M%S')
  77. print ("Data Pedido e Hora: ", dataPedido)
  78.  
  79. descricao = m.obr.obr_4.to_er7()
  80. print ("Descrição: ", descricao)
  81.  
  82.  
  83. print ("========")
  84. guardaMsg(idPedido, idDoente, moradaDoente, telefoneDoente, dataPedido, descricao)
  85.  
  86.  
  87.  
  88.  
  89. class MLLProtocol(object):
  90. """
  91. Apenas verifica se a mensagem está codificada no seguinte padrão:
  92. \x0b{conteúdo_da_mensagem}\x1c\x0d
  93. """
  94. validator = re.compile(SB + "(([^\r]+\r){1,})" + EB + CR)
  95.  
  96. @staticmethod
  97. def get_message(line):
  98. message = None
  99. matched = MLLProtocol.validator.match(line)
  100. if matched is not None:
  101. message = matched.groups()[0]
  102. return message
  103.  
  104.  
  105. class MLLPServer(StreamRequestHandler):
  106. """
  107. Simplistic implementation of a TCP server implementing the MLLP protocol
  108. HL7 messages are encoded between bytes \x0b and \x1c\x0d
  109. """
  110.  
  111. def handle(self):
  112. line = ''
  113. while True:
  114. char = self.rfile.read(1).decode()
  115. if not char:
  116. print('Cliente desconectado')
  117. break
  118. line += char
  119. # verifica se existe alguma mensagem HL7 no buffer
  120. message = MLLProtocol.get_message(line)
  121. if message is not None:
  122. print(line)
  123. parser(message)
  124.  
  125. if __name__ == "__main__":
  126. HOST, PORT = '172.26.83.74', 5678
  127.  
  128. server = TCPServer((HOST, PORT), MLLPServer)
  129. server.serve_forever()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement