Looking to try and change a bit of XLM in to JSON code using gamma ( scripting language for cogent. (based on C#). It has a scew parser but thats beyond me .
Above is the XML looking to try and put each "Row" into a MQTT Message with each column as a JSON element.
THis is the example code they give
Code:
#{<TableData xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">\n <column name="EventStamp" type="timestamp" datatype="11" />\n <column name="AlarmState" type="string" datatype="-9" />\n <column name="TagName" type="string" datatype="-9" />\n <column name="Description" type="string" datatype="-9" />\n <column name="Area" type="string" datatype="-9" />\n <column name="Type" type="string" datatype="-9" />\n <column name="Value" type="string" datatype="-9" />\n <column name="CheckValue" type="string" datatype="-9" />\n <column name="Priority" type="int" datatype="5" />\n <column name="Category" type="string" datatype="-9" />\n <column name="Provider" type="string" datatype="-9" />\n <column name="Operator" type="string" datatype="-9" />\n <column name="DomainName" type="string" datatype="-9" />\n <column name="UserFullName" type="string" datatype="-9" />\n <column name="UnAckDuration" type="string" datatype="-9" />\n <column name="User1" type="double" datatype="6" />\n <column name="User2" type="double" datatype="6" />\n <column name="User3" type="string" datatype="-9" />\n <column name="EventStampUTC" type="timestamp" datatype="11" />\n <column name="MilliSec" type="int" datatype="5" />\n <column name="OperatorNode" type="string" datatype="-9" />\n <row>\n <EventStamp>2021-11-19 12:24:45.617</EventStamp>\n <AlarmState>UNACK_ALM</AlarmState>\n <TagName>DEF_Communication_PLC_A1</TagName>\n <Description>Communication Failed - PLC A1</Description>\n <Area>$System</Area>\n <Type>DSC </Type>\n <Value>ON</Value>\n <CheckValue>ON</CheckValue>\n <Priority>50</Priority>\n <Category>DSC </Category>\n <Provider>USER-PC\\InTouch</Provider>\n <Operator>None</Operator>\n <DomainName>InTouch</DomainName>\n <UserFullName>None</UserFullName>\n <UnAckDuration> </UnAckDuration>\n <User1>0</User1>\n <User2>0</User2>\n <User3></User3>\n <EventStampUTC>2021-11-19 12:24:45.617</EventStampUTC>\n <MilliSec>6150</MilliSec>\n <OperatorNode>USER-PC</OperatorNode>\n </row>\n <row>\n <EventStamp>2021-11-19 12:24:45.617</EventStamp>\n <AlarmState>UNACK_ALM</AlarmState>\n <TagName>DEF_Communication_PLC_A2</TagName>\n <Description>Communication Failed - PLC A2</Description>\n <Area>$System</Area>\n <Type>DSC </Type>\n <Value>ON</Value>\n <CheckValue>ON</CheckValue>\n <Priority>50</Priority>\n <Category>DSC </Category>\n <Provider>USER-PC\\InTouch</Provider>\n <Operator>None</Operator>\n <DomainName>InTouch</DomainName>\n <UserFullName>None</UserFullName>\n <UnAckDuration> </UnAckDuration>\n <User1>0</User1>\n <User2>0</User2>\n <User3></User3>\n <EventStampUTC>2021-11-19 12:24:45.617</EventStampUTC>\n <MilliSec>6150</MilliSec>\n <OperatorNode>USER-PC</OperatorNode>\n </row>\n <row>\n <EventStamp>2021-11-19 12:24:45.617</EventStamp>\n <AlarmState>UNACK_ALM</AlarmState>\n <TagName>DEF_Communication_PLC_B</TagName>\n <Description>Communication Failed - PLC B</Description>\n <Area>$System</Area>\n <Type>DSC </Type>\n <Value>ON</Value>\n <CheckValue>ON</CheckValue>\n <Priority>50</Priority>\n <Category>DSC </Category>\n <Provider>USER-PC\\InTouch</Provider>\n <Operator>None</Operator>\n <DomainName>InTouch</DomainName>\n <UserFullName>None</UserFullName>\n <UnAckDuration> </UnAckDuration>\n <User1>0</User1>\n <User2>0</User2>\n <User3></User3>\n <EventStampUTC>2021-11-19 12:24:45.617</EventStampUTC>\n <MilliSec>6150</MilliSec>\n <OperatorNode>USER-PC</OperatorNode>\n </row>\n <row>\n <EventStamp>2021-11-19 12:24:45.617</EventStamp>\n <AlarmState>UNACK_ALM</AlarmState>\n <TagName>DEF_Communication_PLC_C</TagName>\n <Description>Communication Failed - PLC C</Description>\n <Area>$System</Area>\n <Type>DSC </Type>\n <Value>ON</Value>\n <CheckValue>ON</CheckValue>\n <Priority>50</Priority>\n <Category>DSC </Category>\n <Provider>USER-PC\\InTouch</Provider>\n <Operator>None</Operator>\n <DomainName>InTouch</DomainName>\n <UserFullName>None</UserFullName>\n <UnAckDuration> </UnAckDuration>\n <User1>0</User1>\n <User2>0</User2>\n <User3></User3>\n <EventStampUTC>2021-11-19 12:24:45.617</EventStampUTC>\n <MilliSec>6150</MilliSec>\n <OperatorNode>USER-PC</OperatorNode>\n </row>\n <row>\n <EventStamp>2021-11-19 12:24:45.617</EventStamp>\n <AlarmState>UNACK_ALM</AlarmState>\n <TagName>DEF_Communication_PLC_D</TagName>\n <Description>Communication Failed - PLC D</Description>\n <Area>$System</Area>\n <Type>DSC </Type>\n <Value>ON</Value>\n <CheckValue>ON</CheckValue>\n <Priority>50</Priority>\n <Category>DSC </Category>\n <Provider>USER-PC\\InTouch</Provider>\n <Operator>None</Operator>\n <DomainName>InTouch</DomainName>\n <UserFullName>None</UserFullName>\n <UnAckDuration> </UnAckDuration>\n <User1>0</User1>\n <User2>0</User2>\n <User3></User3>\n <EventStampUTC>2021-11-19 12:24:45.617</EventStampUTC>\n <MilliSec>6150</MilliSec>\n <OperatorNode>USER-PC</OperatorNode>\n </row>\n <row>\n <EventStamp>2021-11-19 12:24:45.617</EventStamp>\n <AlarmState>UNACK_ALM</AlarmState>\n <TagName>DEF_Communication_PLC_E</TagName>\n <Description>Communication Failed - PLC E</Description>\n <Area>$System</Area>\n <Type>DSC </Type>\n <Value>ON</Value>\n <CheckValue>ON</CheckValue>\n <Priority>50</Priority>\n <Category>DSC </Category>\n <Provider>USER-PC\\InTouch</Provider>\n <Operator>None</Operator>\n <DomainName>InTouch</DomainName>\n <UserFullName>None</UserFullName>\n <UnAckDuration> </UnAckDuration>\n <User1>0</User1>\n <User2>0</User2>\n <User3></User3>\n <EventStampUTC>2021-11-19 12:24:45.617</EventStampUTC>\n <MilliSec>6150</MilliSec>\n <OperatorNode>USER-PC</OperatorNode>\n </row>\n <row>\n <EventStamp>2021-11-19 12:24:45.617</EventStamp>\n <AlarmState>UNACK_ALM</AlarmState>\n <TagName>DEF_Communication_PLC_EB</TagName>\n <Description>Communication Failed - PLC EB</Description>\n <Area>$System</Area>\n <Type>DSC </Type>\n <Value>ON</Value>\n <CheckValue>ON</CheckValue>\n <Priority>50</Priority>\n <Category>DSC </Category>\n <Provider>USER-PC\\InTouch</Provider>\n <Operator>None</Operator>\n <DomainName>InTouch</DomainName>\n <UserFullName>None</UserFullName>\n <UnAckDuration> </UnAckDuration>\n <User1>0</User1>\n <User2>0</User2>\n <User3></User3>\n <EventStampUTC>2021-11-19 12:24:45.617</EventStampUTC>\n <MilliSec>6150</MilliSec>\n <OperatorNode>USER-PC</OperatorNode>\n </row>\n \n</TableData>\n}
Above is the XML looking to try and put each "Row" into a MQTT Message with each column as a JSON element.
THis is the example code they give
Code:
require ("Application");
/* Get the Gamma library functions and methods for XML parsing and
wget command-line tool */
require ("XMLSupport");
require ("WgetSupport");
class XMLReader Application
{
DocumentUrl = "http://localhost/points?name=DataPid:PID1.Mv|
DataPid:PID1.Pv|DataPid:PID1.Sp";
TickSeconds = 5;
}
method XMLReader.loadXml ()
{
Wget(.DocumentUrl, `progn {
if (!destroyed_p(@self))
(@self).processWgetResult();
});
}
method XMLReader.processWgetResult()
{
if (ExitCode == 0) // success
{
// Print the raw XML document
princ ("------- Original Document -------\n");
princ (ResultString);
princ ("---------------------------------\n");
// Parse the XML into a class hierarchy that we can easily
// manipulate
local reader =
scew_reader_buffer_create(string_to_buffer(ResultString));
local parser = new scew_parser();
local tree = parser.load(reader);
local model = tree.create_model("myxml_");
// Print the whole XML file model. // pretty_princ(model, "\n");
// The XML model consists of a class instance for each XML
// Element. Each class has a member called _xml_children
// that is an array of the sub-elements.
// In addition, each class has member variables that are
// named as the attributes of the XML element. Replace this
// with your own custom processing.
with point in model._xml_children do
{
princ (point.name, " = ", point.value, "\n");
}
}
else
{
princ ("XML read failed with exit code: ", ExitCode, "\n");
}
}
/* Write the 'main line' of the program here. */
method XMLReader.constructor ()
{
.TimerEvery(.TickSeconds, `(@self).loadXml());
}
/* Any code to be run when the program gets shut down. */
method XMLReader.destructor ()
{
}
Last edited: