Skip to content

Instantly share code, notes, and snippets.

@rahulsom
Created May 17, 2012 17:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rahulsom/2720302 to your computer and use it in GitHub Desktop.
Save rahulsom/2720302 to your computer and use it in GitHub Desktop.
Generating random ADT Messages
@Grapes([
@Grab(group='com.github.groovy-wslite', module='groovy-wslite', version='0.7.0'),
@Grab('ca.uhn.hapi:hapi-base:2.0-alpha1'),
@Grab('ca.uhn.hapi:hapi-structures-v26:2.0-alpha1')
])
import wslite.rest.RESTClient
import java.security.SecureRandom
import java.text.DecimalFormat
import ca.uhn.hl7v2.app.*
import ca.uhn.hl7v2.parser.GenericParser
import ca.uhn.hl7v2.model.Message
import ca.uhn.hl7v2.parser.CanonicalModelClassFactory
import ca.uhn.hl7v2.parser.PipeParser
import ca.uhn.hl7v2.llp.MinLowerLayerProtocol
/**
* Creates ADT Messages for random patients
*
* @author rahulsomasunderam
* @since 5/16/12 5:22 PM
*/
class NGUtil {
def lastNamesSource = 'http://www.census.gov/genealogy/names/dist.all.last'.toURL().text.replaceAll('\r','\n').replaceAll('\n+', '\n')
def lastNames = lastNamesSource.split('\n').collect { it.split(' ')[0] }
def femaleSource = 'http://www.census.gov/genealogy/names/dist.female.first'.toURL().text.replaceAll('\r','\n').replaceAll('\n+', '\n')
def females = femaleSource.split('\n').collect { it.split(' ')[0] }
def maleSource = 'http://www.census.gov/genealogy/names/dist.male.first'.toURL().text.replaceAll('\r','\n').replaceAll('\n+', '\n')
def males = maleSource.split('\n').collect { it.split(' ')[0] }
final r = new SecureRandom()
def dateCenter = new Date(70, 01, 01)
def cityCenter = [37.346961,-121.882668]
def generateAddress() {
def latlng = [ cityCenter[0] + r.nextGaussian()*0.2,cityCenter[1] + r.nextGaussian()*0.2 ]
def geocoder = new RESTClient('http://maps.googleapis.com/maps/api/geocode')
def resp = geocoder.get(path: "json?latlng=${latlng[0]},${latlng[1]}&sensor=true")
if (resp.statusCode == 200) {
def firstShot = resp.json.results[0].address_components
def retVal = [
streetNumber: firstShot.find{it.types.contains('street_number')}.short_name,
streetName:firstShot.find{it.types.contains('route')}.short_name,
city:firstShot.find{it.types.contains('locality')}.short_name,
state:firstShot.find{it.types.contains('administrative_area_level_1')}.short_name,
zipCode:firstShot.find{it.types.contains('postal_code')}.short_name,
]
retVal.street = "${retVal.streetNumber} ${retVal.streetName}"
return retVal
} else {
return null
}
}
void setCenter(address) {
address = address.replaceAll(' ', '+')
def geocoder = new RESTClient('http://maps.googleapis.com/maps/api/geocode')
def resp = geocoder.get(path: "json?address=${address}&sensor=true")
if (resp.statusCode == 200) {
def location = resp.json.results[0].geometry.location
def retVal = [location.lat, location.lng]
if (retVal && retVal[0] && retVal[1]) {
cityCenter = retVal
}
println "Center Set"
} else {
println resp.contentAsString
}
}
def getLastName() {
lastNames[r.nextInt(lastNames.size())]
}
def getMale() {
males[r.nextInt(males.size())]
}
def getFemale() {
females[r.nextInt(females.size())]
}
def getDob() {
dateCenter + (int)(r.nextGaussian() * 365 * 40)
}
}
def util = new NGUtil()
util.setCenter('560 S Winchester Blvd, San Jose CA 95128')
ConnectionHub connectionHub = ConnectionHub.instance;
Connection connection = connectionHub.attach('localhost', 8002, new PipeParser(), MinLowerLayerProtocol);
def i = connection.initiator
1.times {
try {
def id = util.r.nextInt(9999999)
def nsid = "NSID"
def oid = "1.2.3.44.3"
def ln = util.lastName
def g = util.r.nextBoolean() ? 'M' : 'F'
def fn = g == 'M' ? util.male : util.female
def dob = util.dob
def phone = '408'+ new DecimalFormat('0000000').format(util.r.nextInt(9999999))
def ssn = new DecimalFormat('000000000').format(util.r.nextInt(999999999))
def address = util.generateAddress()
while (!address) {
address = util.generateAddress()
}
String messageString = """MSH|^~\\&|ABCDEFG&1.23.4&ISO|CDS|LABADT|MCM|20120109|SECURITY|ADT^A04|MSG00001|P|2.4
|EVN|A01|198808181123
|PID|||${id}||${ln}^${fn}||${dob.format('yyyyMMdd')}|$g||2106-3|${address.street}^^${address.city}^${address.state}^${address.zipCode}|GL||||S||ADT_PID18^2^M10|$ssn|9-87654^NC
|NK1|1|JONES^BARBARA^K|SPO|||||20011105
|NK1|1|JONES^MICHAEL^A|FTH""".stripMargin().replaceAll('\n', '\r')
println messageString.replaceAll('\r', '\n')
println ''
def p = new GenericParser(new CanonicalModelClassFactory('2.6'));
Message adt = p.parse(messageString);
i.setTimeoutMillis(10000)
def resp = i.sendAndReceive(adt)
println resp.encode().replaceAll('\r', '\n')
println '\n\n'
} catch (Exception e) {
println "Missed chance"
e.printStackTrace()
}
}
connection.close()
connectionHub.discard(connection);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment