Custom ACKs

Query Response Example

This is a great technique for implementing a query response interface. Say you have an incoming message that looks like:

MSH|^~&|Acme||||20100927160935||QRY^A19|2010927169956|P|2.4|
QRD|20100927160935|R|I|92716934|||1^RD|4525285|DEM|

For which you need to send back an ADR^A19 message giving patients matching the given ID.

Here’s an example. As you can see the principle is simple. The query comes in. It’s parsed and used to generate a SQL query against the database. The data from the SQL query is used to populate the HL7 message that is returned. The logic can be made more elaborate but the principle is the same.

Note: We set live = true to execute SQL queries interactively in the Iguana script editor.

Tip: For a production version it is recommended best practice to escape the Id to avoid a SQL injection attack, see escaping values correctly for SQL.

Here’s the vmd query.vmd and copy of the code used for this example:

require('node')
-- main() is given the original HL7 message.
function main(Data)
   local Msg = hl7.parse{vmd='query.vmd', data=Data}
   local R = hl7.message{vmd='query.vmd', name='Response'}

   if Msg:nodeName() == 'Unrecognized' then
      ack.send('Unrecognized message')
      return
   end

   MakeMSH(R.MSH, Msg)
   FindPatient(R, Msg.QRD[8][1][1])
   ack.send(R:S())
end

function MakeMSH(MSH, Q)  
   MSH[3][1] = Q.MSH[5][1]
   MSH[4][1] = Q.MSH[6][1]
   MSH[5][1] = Q.MSH[3][1]
   MSH[6][1] = Q.MSH[4][1]
   MSH[10] = Q.MSH[10]
   MSH[9][1] = 'ADR'
   MSH[9][2] = 'A19'
   MSH[11][1] = 'P'
   MSH[12][1] = '2.6.1'
   MSH[7] = os.date('%Y%m%d%h%M%S')
   return MSH
end

function FindPatient(R, Id)
   local Q = db.query{api=db.MY_SQL, name='test',
      user='root', password='password', live=true,
      sql = 'SELECT * FROM Patient WHERE Id ="'..Id..'"'}
   for i=1,#Q do
      AddPatient(R.Group1[i].PID, Q[i])      
   end
end

function AddPatient(PID, R) 
   PID[5][1] = R.LastName
   PID[5][2] = R.GivenName
   PID[2][1] = R.Id
   PID[8] = R.Sex
   return PID
end

Next: Failover Solution

Leave a Reply