A LAN device is pushing XML data to HE. My device driver's parse() function is being driven as expected. Here's the code that I'm using so far to just look at the data that comes in...
Here's the data that is written to the log by the "log.debug(xmlData)" statement. Sorry for the way it strings it out on one line, but I couldn't figure out how to post it so that it could be copied by someone...
As an example, I understand that I should be able to extract the value for the humidity in the "owd_EDS0068" sensor element data by doing something like...
My problem is I can't figure out how to get from the XML-ish data shown to XML data that can be processed. Part of the problem, I think, is that I need to strip off all of the data before and after the "Device-Detail-Response" element, but I don't have any idea how to do that.
I've written a lot of code, but none using XML, Groovy, or Java.
If someone can explain to me how to add the XML in a vertically scrollable form (instead of it appearing as a single scrollable horizontal line of text), I will edit the post.
Can you post a sample of the raw message received?
Also curious, what type of LAN device is it? Do they have any description of their data method?
UPDATE:
Not sure if this is based on the same type of data... but this appears similar to what you might have been receiving:
UPDATE2:
Some success... The below example gets the value for the Humidex. You could replace that with whatever specific values you want. Still working on a map method that is not turning it into garbage...
@snell The data in the stackoverflow thread is for the same device I'm using, although the data received by the parse function contains additional header data at the beginning, e.g., MAC address, IP address, etc. Maybe HE is prepending the additional data to the data it received before calling the driver's parse function?
The device is the OW-Server 1-wire Ethernet server. There's already a community driver for it, but it polls the device instead waiting for the device to push the data. That's what I'm trying to change as a learning exercise.
I'm hoping there's a better way to extract values than using a bunch of regex's. There's over 50 values for the sensor connected to the server, and the server supports up to 22 1-wire sensors of multiple types.
You are talking about this thread correct? From what I can see the data is just in screenshots, not in any text I can toss in and "throw code at". Or did I miss it (or a link to it) in there?
Yes, I am sure having them all there would be a pain (I had to do that with something a long time ago). I am trying to bang away at the sample above... but it really does not want to allow it to act as a string so I can "cut" the garbage at the beginning and end.
UPDATE:
Seemed like it would be easy enough to "minus" the extra (Data is just a copy of the line from above):
But it looks like it REALLY does not want to be treated as a string, as the debug logs show below. It loses all the node names for each one even when it is just immediately dumping the string to the log BEFORE any manipulations:
The XML data I provided in my first post above (the single, long text line that is horizontally scrollable) is the same thing except it has the extra data before and after the Devices-Detail-Response parent element.
I copied the XML document from the device's web UI page and pasted it into the driver's parse function (just to make it easy to run the code) to create an XML object as part of the parse function at runtime. I added this code after the XML object definition...
When it ran, it logged the correct value for the humidity.
Bottom line is it looks like the XML data being pushed by the device is good if the data before and after the parent element is removed. I don't have any way to see the data exactly as it is pushed to HE, so I don't know if the device is adding the extra data or if HE is doing it.
Part of my confusion stems from not having any idea what HE is doing with the data when it receives it, when I use the log function to write it to the log, why the tags sometimes get stripped out when the data is written to the log, etc.
I wonder if the data you are logging after minus'ing the extra stuff still has the XML tags, but the HE log is stripping them out when it writes it to the log.
The frustrating thing is I could strip the extra data out in a few minutes of coding if I used a "legacy language", e.g., C, COBOL, REXX, or even assembler. I guess that's revealing my age.
Thanks to @tomw for providing the following code that strips out the leading and trailing data in the initial LAN message. This was exactly what I was hoping for...
def parse(String message)
{
Map parsedMessage = parseLanMessage(message)
// extract the boundary string from the headers
String boundary = parsedMessage?.headers?.getAt('Content-Type')?.split('boundary=')?.getAt(1)
// get the 'middle' part of the multi-part body, which has the XML (and then some)
String subBody = parsedMessage?.body?.split(boundary)?.getAt(1)
// strip off the extraneous content from beginning and end
subBody = subBody?.split('text/plain\r\n\r\n')?.getAt(1)
subBody -= "--"
// parse the XML so we can work with it
def xmlObj = parseXML(subBody)
xmlObj.owd_EDS0068.each{ sensor->
log.debug ("ROM ID: ${sensor.ROMId} Temperature: ${sensor.Temperature} Humidity: ${sensor.Humidity}")
}
return
}