Back to Main
CurrentCost Data - Extraction of Full MsgDate:
The next step was to get the full message and then push up to a little ColdFusion XML consumer to put it all in a database structure. Step 1: Starting from the previous bash script I had to then grab the full message to POST up the ColdFusion website to process the data.
The bash script is something like:
#!/bin/sh
APIKEY="your pachube api key"
PACHUBEURL="http://www.pachube.com/api/feeds/feedname.csv"
POWERSITES_REPORTER_URL="address of the website consumer"
echo "Getting Readings.."
export MSG=`head /dev/ttyUSB1 -n1 | /bin/grep '<msg>' | /bin/grep '</msg>'`
export WATTS=`echo $MSG | /bin/grep '<ch1><watts>' | /bin/sed -e s/\<ch1\>\<watts\>/\ / | /bin/cut -d' ' -f2 | /bin/cut -c1-5`
export TMPR=`echo $MSG | /bin/grep '<tmpr>' | /bin/sed -e s/\<tmpr\>/\ / | /bin/cut -d' ' -f2 | /bin/cut -c1-4`
/bin/echo MSG: $MSG
/bin/echo Power: $WATTS
/bin/echo Temp : $TMPR
if [ "$WATTS" = "" ]
then
echo "No Watts."
break
else
/usr/bin/curl --request PUT --header "X-PachubeApiKey: $APIKEY" --data "$WATTS,$TMPR" "$PACHUBEURL"
echo "Updated Pachube OK."
fi
if [ "$MSG" = "" ]
then
echo "No Msg."
break
else
/usr/bin/curl --request PUT --header "X-PachubeApiKey: $APIKEY" --data "$WATTS,$TMPR" "$PACHUBEURL"
# Now post it
curl --output /usr/local/pachube/remote_curl.lis --request POST --header "X-Redhead-Curl" --data "msg=$MSG" $POWERSITES_REPORTER_URL
echo "Update OK"
fi
#
The full message includes hourly, daily, monthly and yearly history (this is for a CurrentCost 'Classic / WattsClever - sometimes called CC02). The spec of the XML structure is outlined in a few places but a goos source is http://www.currentcost.com/cc128/xml.htm . This is for the XX128/Envi but the structure is very similar.
An abbreviated version of the ColdFusion code to parse the XML (with a bit of developer debug code) is shown below (to protect some of our commercial IP I haven't included all code, but you should be able to get the gist of it all)
The parser:
<!--- This routine picks up the data as transmitted from a CurrentCost / Watts Clever Power Monitor and uploads into into the tables ---> <!--- The XML structure is documented at http://www.currentcost.com/cc128/xml.htm ---> <!--- Written by MB July 2010 *** THis one works with the older CurrentCost Classics --->
<cfset variables.msgDebug = "TRUE">
<cfif isdefined("form.msg")>
<cfset XMLFile = "<?xml version='1.0'?>" & form.msg>
<cfset collection_mode="live">
<cfelse>
<!--- Get some dev data --->
<cfinclude template="energy_reporter/mod_dev_xml_data.cfm">
TEST MODE
<cfset collection_mode="test">
</cfif>
<cftry>
<!--- Parse the XML --->
<cfset MyXMLDoc = xmlParse(XMLFile) />
<!--- Starting date is 02/Apr/2009 --->
<cfinclude template="qry_get_energy_site_details.cfm">
<cfif isdefined("get_ps_energy_site_details.dsb_start_date") and get_ps_energy_site_details.dsb_start_date is not "" >
<cfset variables.startBirthDate = CreateDate(get_ps_energy_site_details.dsb_year,get_ps_energy_site_details.dsb_month,get_ps_energy_site_details.dsb_day)>
<cfelse>
<cfset variables.startBirthDate = CreateDate(2009,04,02)>
</cfif>
<!--- Get all Nodes --->
<cfset msgNodes = xmlSearch(MyXMLDoc,'/msg')>
<cfoutput>
<h2>MSG Nodes</h2>
<cfloop from="1" to="#arraylen(msgNodes)#" index="i">
<!--- The array contents need to parsed so you can easily get at the child nodes children and attributes. --->
<cfset msgXML = xmlparse(msgNodes[i]) />
<cfif isdefined("msgXML.msg.sensor.name.xmltext") and msgXML.msg.sensor.name.xmltext is not "">
<h3>Device: #msgXML.msg.src.name.xmltext#-S#msgXML.msg.sensor.name.xmltext#</h3><br>
<cfset variables.msgDevice = msgXML.msg.src.name.xmltext & "-S" & msgXML.msg.sensor.name.xmltext>
<cfelse>
<h3>Device: #msgXML.msg.src.name.xmltext#</h3><br>
<cfset variables.msgDevice = msgXML.msg.src.name.xmltext>
</cfif>
<cfif collection_mode is "live">
<cfset variables.msgDSB = #msgXML.msg.date.dsb.xmltext#>
<cfset variables.msgDateTime = lsDateFormat(DateAdd("d",variables.msgDSB,variables.startBirthDate),'DD/MM/YYYY') & " " & msgXML.msg.date.hr.xmltext & ":" & msgXML.msg.date.min.xmltext & ":" & msgXML.msg.date.sec.xmltext >
<cfset variables.msgCH1Watts = msgXML.msg.ch1.watts.xmltext>
<cfset variables.msgCH2Watts = msgXML.msg.ch2.watts.xmltext>
<cfset variables.msgCH3Watts = msgXML.msg.ch3.watts.xmltext>
<cfset variables.msgTemp = msgXML.msg.tmpr.xmltext>
<cfelse>
<cfset variables.msgDSB = #DateDiff("d",variables.startBirthDate,lsDateFormat(now()))#>
<cfset variables.msgDateTime = lsDateFormat(DateAdd("d",variables.msgDSB,variables.startBirthDate),'MM/DD/YYYY') & " " & LSTimeFormat(now(),'HH') & ":" & LSTimeFormat(now(),'mm') & ":" & LSTimeFormat(now(),'ss')>
<cfset variables.msgCH1Watts = RandRange(300,1000)>
<cfset variables.msgCH2Watts = 0>
<cfset variables.msgCH3Watts = 0>
<cfset variables.msgTemp = RandRange(180,220)/10>
</cfif>
<cfif variables.msgDebug is "true">
<b>Days Since Birth:</b> #variables.msgDSB#<br>
<b>Message Date Time:</b> #variables.msgDateTime#<br>
<b>CH1 Watts:</b> #variables.msgCH1Watts#<br>
<b>CH2 Watts:</b> #variables.msgCH2Watts#<br>
<b>CH3 Watts:</b> #variables.msgCH3Watts#<br>
<b>Temp:</b> #variables.msgTemp#<br>
<h3>History</h3>
<h4>Hours</h4>
</cfif>
<cfif isdefined("msgXML.msg.hist") >
<cfloop from="2" to="26" index="h" step="2">
<cfset looper = right("0" & h,2)>
<cfif variables.msgDebug is "true">
#looper# = #Evaluate("msgXML.msg.hist.hrs.h#looper#.xmltext")# <br/>
</cfif>
<cfset "variables.msgH#looper#" = #Evaluate("msgXML.msg.hist.hrs.h#looper#.xmltext")#>
</cfloop>
<!--- <h4>Days</h4> --->
<cfloop from="1" to="31" index="d">
<cfset looper = right("0" & d,2)>
<cfif variables.msgDebug is "true">
#looper# = #Evaluate("msgXML.msg.hist.days.d#looper#.xmltext")# <br/>
</cfif>
<cfset "variables.msgD#looper#" = #Evaluate("msgXML.msg.hist.days.d#looper#.xmltext")#>
</cfloop>
<!--- <h4>Months</h4> --->
<cfloop from="1" to="12" index="m">
<cfset looper = right("0" & m,2)>
<cfif variables.msgDebug is "true">
#looper# = #Evaluate("msgXML.msg.hist.mths.m#looper#.xmltext")# <br/>
</cfif>
<cfset "variables.msgM#looper#" = #Evaluate("msgXML.msg.hist.mths.m#looper#.xmltext")#>
</cfloop>
<!--- <h4>Years</h4> --->
<cfloop from="1" to="4" index="y">
<cfset looper = y>
<cfif variables.msgDebug is "true">
#looper# = #Evaluate("msgXML.msg.hist.yrs.y#looper#.xmltext")# <br/>
</cfif>
<cfset "variables.msgY#looper#" = #Evaluate("msgXML.msg.hist.yrs.y#looper#.xmltext")#>
</cfloop>
</cfif>
</cfloop>
<cfinclude template="energy_reporter/qry_insert_xml.cfm">
</cfoutput>
<cfcatch type="Any">
<!--- <cfinclude template="/#request.common_code_path#/admin/mod_mail_error.cfm"> --->
<cfset variables.cfcatch_message = #CFCATCH.message#>
<cfset variables.cfcatch_NativeErrorCode = ''>
<cfset variables.cfcatch_sqlstate = ''>
<cfset variables.cfcatch_detail = #CFCATCH.DETAIL#>
<cfoutput>#CFCATCH.message#<br>#CFCATCH.DETAIL#</cfoutput>
</cfcatch>
</cftry>
<!--- Dump the XML --->
<h2>Dump</h2>
<cfdump var="#MyXMLDoc#">
|








