HL7 to HL7

Lua code for our VIP alert

Add Lua code to our channel to print a message containing the word ALERT, for any HL7 messages with a VIP flag.

Are you wondering how print() sends a message to the log? print() shows it’s output it in the the live annotation block in the Translator editor, and re-directs to the log as an Informational message when a channel is running. For more advanced logging options check out the Iguana run-time functions iguana.logDebug(), iguana.logInfo() and iguana.logWarning().

To write the code that raises the alert we need a sample ADT message with a VIP.

Open the Translator editor for the Filter component of the Sender channel. Use the navigation control to import log data and search for lohan or VIP to find an ADT message with a VIP flag:

Now we can write the code:

Notice the Alert function? This is good practice as the same function can be reused for other Alerts.

We need to call this function from within main:

If you now

  1. Commit the milestone
  2. Stop the channel
  3. Requeue the data
  4. Run the channel

You should get an email like this:

Iguana Email Notification
Source: Sender
Log Type: Info
Logged message:

----------------------------------------------------
ALERT:
VIP: Lohan, Lynsay has arrived!
----------------------------------------------------

To view this log entry in Iguana's logs, go to:
http://84.224.201.102:6543/log_browse?refid=20111024-964498

Note: if the log entry link does not work it means the Iguana server IP address has changed, and you need to update the Iguana Host Name for Email Links setting. See the Gotcha on the page about configuring email notification.

We can see the log message in the logs too:

And here is our final code:

require 'split'
require 'zsegment'
require 'diff'
require 'hl7util'
require 'dateparse'
require 'stringutil'
require 'clean_phone'
require 'codemap'

local function trace(a,b,c,d) return end

function main(Data)
   local Orig = hl7.parse  {vmd = 'transform.vmd', data = Data}

   if Orig:nodeName() == 'Catchall' then
      iguana.logInfo('Filtered '..Orig.MSH[9][1]..'^'..Orig.MSH[9][2])
      return
   end

   local Out  = hl7.message{vmd = 'transform.vmd', name = Orig:nodeName()}

   CheckForVIP(Orig)

   Out:mapTree(Orig)

   local Copy = zsegment.copyZSegments(Data, Out:S())
   CheckTransform(Data:StripLastReturns(), Copy:StripLastReturns())

   AlterMSH(Out.MSH)
   local PID = hl7util.findSegment(Out, FindPID)
   if PID then
      AlterPID(PID)   
   end

   if Out:nodeName() == 'Lab' then 
      AddNote(Out)
   end

   trace(Out)
   local Diff = diff.Compare(Orig:S(), Out:S(), 'transform.vmd')
   local DataOut = Out:S()
   DataOut = zsegment.copyZSegments(Data, DataOut)
   DataOut = DataOut:StripLastReturns()
   trace(DataOut)
   queue.push{data = DataOut}
end

function CheckForVIP(Msg)
   if Msg:nodeName() == 'ADT' then 
      if Msg.PV1[16]:nodeValue() == 'VIP' then 
         Alert("VIP: "..Msg.PID[5][1][1][1]..', '..Msg.PID[5][1][2]..' has arrived')
      end
   end
end

function Alert(Message)
   print('ALERT:n'..Message)
end

local NoteTemplate=[[
Clown.  Good madonna, why mournest thou?
Olivia. Good fool, for my brother's death.
Clown.  I think his soul is in hell, madonna.
Olivia. I know his soul is in heaven, fool.

Clown.  The more fool, madonna, to mourn for your brother's
soul being in heaven--Take away the fool, gentleman.
]]

function AddNote(Out)
   local Lines = NoteTemplate:StripLastReturns():split('n')
   for i=1, #Lines do
      Out.NTE[i][1] = i
      Out.NTE[i][3][1] = Lines[i]
   end
   return Out
end

function FindPID(Segment)
   if Segment:nodeName() == 'PID' then
      return true
   end
end

SexMap = codemap.map({
      M='Male',
      m='Male',
      F='Female',
      f='Female'
      }, 'unknown')

function AlterPID(PID)
   local Date = dateparse.parse(PID[7]:nodeValue())
   PID[7]          = os.date("%Y%m%d", Date)
   PID[5][1][1][1] = PID[5][1][1][1]:nodeValue():capitalize()
   PID[5][1][2]    = PID[5][1][2]:nodeValue():capitalize()
   PID[13][1][1]   = PID[13][1][1]:CleanPhone()
   PID[8]          = SexMap[PID[8]]

   trace(SexMap['any other value'])
   return PID
end

function AlterMSH(MSH)
   MSH[3][1] = 'Acme'
   MSH[4][1] = 'Lab'
   return MSH
end

function CheckTransform(Orig, Copy)
   if Orig ~= Copy then
      trace(Orig)
      trace(Copy)
      error('Copy of HL7 message does not match the original')
   end   
end

function string.StripLastReturns(S)
   -- strip return(s) "r" & "n" from the end of a string
   local i = #S
   while S:byte(i) == 10 or S:byte(i) == 13 do
      i = i - 1
   end
   return S:sub(1,i)
end

Leave A Comment?