Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from __future__ import absolute_import
- from __future__ import print_function
- import uuid
- from hl7apy.core import Message, Segment
- from hl7apy.parser import parse_message
- from datetime import datetime
- from dateutil.parser import parse
- import mysql.connector
- from mysql.connector import errorcode
- import re
- try:
- from SocketServer import StreamRequestHandler, TCPServer
- except ImportError: # Python 3
- from socketserver import StreamRequestHandler, TCPServer
- #: caracteres para codificação MLLP
- SB = "\x0b"
- EB = "\x1c"
- CR = "\x0d"
- def guardaMsg(idPedido, utente_name, morada, telefone, data, hora , descricao):
- try:
- cnx = mysql.connector.connect(user='root',
- password='root',
- database='BDhospital')
- except mysql.connector.Error as err:
- if err.errno == errorcode.ER_ACCESS_DENIED_ERROR:
- print("Something is wrong with your user name or password")
- elif err.errno == errorcode.ER_BAD_DB_ERROR:
- print("Database does not exist")
- else:
- print(err)
- else:
- cursor = cnx.cursor()
- add_exame = ("INSERT INTO pedido"
- "(idPedido, utente_name, morada, telefone, data, hora,estado,observacao , descricao, relatorio ) "
- "VALUES (%s, %s, %s, %s, %s, %s,0 ,\"observacao\" , %s, \"observacao\" )")
- dados_exame = (idPedido, utente_name, morada, telefone, data, hora , descricao)
- cursor.execute(add_exame, dados_exame)
- cnx.commit()
- print("Exame Registado!")
- cursor.close()
- cnx.close()
- def parser(mensagem):
- try:
- # parse the incoming HL7 message
- m = parse_message(mensagem, find_groups=False)
- except:
- print ('parsing failed'), repr(mensagem)
- else:
- idPedido = m.msh.msh_10.to_er7()
- print ("Numero do Pedido: ", idPedido)
- idDoente = m.PID.PID_3.to_er7()
- print ("ID do paciente: ", idDoente)
- moradaDoente = m.pid.pid_11.to_er7()
- print ("Morada do Paciente: ", moradaDoente)
- telefoneDoente = m.pid.pid_13.to_er7()
- print ("Telefone do Paciente: ", telefoneDoente)
- dataPedido = datetime.strptime(m.msh.msh_7.to_er7(),'%Y%d%m%H%M%S')
- print ("Data Pedido e Hora: ", dataPedido)
- descricao = m.obr.obr_4.to_er7()
- print ("Descrição: ", descricao)
- print ("========")
- guardaMsg(idPedido, idDoente, moradaDoente, telefoneDoente, dataPedido, descricao)
- class MLLProtocol(object):
- """
- Apenas verifica se a mensagem está codificada no seguinte padrão:
- \x0b{conteúdo_da_mensagem}\x1c\x0d
- """
- validator = re.compile(SB + "(([^\r]+\r){1,})" + EB + CR)
- @staticmethod
- def get_message(line):
- message = None
- matched = MLLProtocol.validator.match(line)
- if matched is not None:
- message = matched.groups()[0]
- return message
- class MLLPServer(StreamRequestHandler):
- """
- Simplistic implementation of a TCP server implementing the MLLP protocol
- HL7 messages are encoded between bytes \x0b and \x1c\x0d
- """
- def handle(self):
- line = ''
- while True:
- char = self.rfile.read(1).decode()
- if not char:
- print('Cliente desconectado')
- break
- line += char
- # verifica se existe alguma mensagem HL7 no buffer
- message = MLLProtocol.get_message(line)
- if message is not None:
- print(line)
- parser(message)
- if __name__ == "__main__":
- HOST, PORT = '172.26.83.74', 5678
- server = TCPServer((HOST, PORT), MLLPServer)
- server.serve_forever()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement