<?xml version="1.0" encoding="utf-8"?>
<TcPlcObject Version="1.1.0.1" ProductVersion="3.1.4024.15">
  <POU Name="FB_TestHTTP_Post_Xml" Id="{992af924-9b26-4c77-8a45-51a7b9a33865}" SpecialFunc="None">
    <Declaration><![CDATA[FUNCTION_BLOCK FB_TestHTTP_Post_Xml
VAR_INPUT
	bSend				: BOOL;
END_VAR
VAR_IN_OUT
	fbClient			: FB_IotHttpClient;
END_VAR
VAR_OUTPUT
	bBusy				: BOOL;
	bError				: BOOL;
END_VAR
VAR	
	fbRequest			: FB_IotHttpRequest;
	fbJson				: FB_JsonDomParser;
	nState				: UDINT;
	RisingEdge			: R_TRIG;
	
	bGetContentResult	: BOOL;
	sContent			: STRING(511);
	
	bGetJsonResult		: BOOL;
	jsonDoc				: SJsonValue;
	jsonVal				: SJsonValue;
	sResultValue		: STRING;
	
	nReqCount			: UDINT;
	nResCount			: UDINT;
	nValidResCount		: UDINT;
	nErrCount			: UDINT;

//-------------------struct2xml-----------------------
	fbXml 				: FB_XmlDomParser;
  	objRoot 			: SXmlNode;
 	objMachines 		: SXmlNode;
  	objMachine 			: SXmlNode;
  	objControllers 		: SXmlNode;
  	objController 		: SXmlNode;
  	objAttribute 		: SXmlAttribute;
 	sXmlString 			: STRING(1000);
  	bCreate     		: BOOL := FALSE;
  	bSave 				: BOOL := TRUE;
  	nLength    			: UDINT;
  	newAttr 			: SXmlAttribute;
//-----------------------------------------------------	
END_VAR
]]></Declaration>
    <Implementation>
      <ST><![CDATA[A_Struct2Xml();

RisingEdge(CLK:= bSend);
CASE nState OF
0:		
	IF RisingEdge.Q THEN 					
		IF fbRequest.SendXmlDomRequest(sUri:= '/test_post_xml', fbClient:= fbClient , eRequestType:= ETcIotHttpRequestType.HTTP_POST, fbXml, 0) THEN
			nState:= 1;
			nReqCount:= nReqCount+1;
			bBusy:= TRUE;
			bError:= FALSE;
		END_IF					
	END_IF
1:
	IF NOT fbRequest.bBusy THEN
		bError:= TRUE;
		IF NOT fbRequest.bError THEN				 				
			bGetContentResult:= fbRequest.GetContent(pContent:= ADR(sContent), nContentSize:= SIZEOF(sContent), bSetNullTermination:= TRUE);	
			IF fbRequest.nStatusCode >= 200 AND fbRequest.nStatusCode < 300 THEN
				bGetJsonResult:= FALSE;
				jsonDoc:= fbRequest.GetJsonDomContent(fbJson);				
				IF jsonDoc <> 0 THEN
					bGetJsonResult:= TRUE;
					IF fbJson.HasMember(jsonDoc, 'response')  THEN
						jsonVal:= fbJson.FindMember(jsonDoc, 'response');
							nValidResCount:= nValidResCount+1;
							bError:= FALSE;						
							IF fbJson.IsString(jsonVal) THEN							
								sResultValue:= fbJson.GetString(jsonVal);									
							END_IF		
					END_IF
				END_IF		
				nResCount:= nResCount+1;					
			END_IF				
		END_IF
		nState:= 0;
		bBusy:= FALSE;
		IF bError THEN
			nErrCount:= nErrCount+1;
		END_IF		
	END_IF  	
END_CASE
]]></ST>
    </Implementation>
    <Action Name="A_Struct2Xml" Id="{75118407-99e6-4e2d-bb23-cfb2971f3dae}">
      <Implementation>
        <ST><![CDATA[
IF bCreate THEN
  (* Create an empty XML document *)
  objRoot := fbXml.GetDocumentNode();

  (* Create a new XML node 'Machines' and add to the empty document *)
  objMachines := fbXml.AppendNode(objRoot, 'Machines');
  
  (* Create a new XML node 'Machine' and add an attribute to this node. Append node to 'Machines' *)
  objMachine := fbXml.AppendNode(objMachines, 'Machine');
  objAttribute := fbXml.AppendAttribute(objMachine, 'Name', 'Wilde Nelli');

  (* Create a new XML node 'Controllers' and add to the 'Machine' node *)
  objControllers := fbXml.AppendNode(objMachine, 'Controllers');

  (* Create a new XML node 'Controller' and add some attributes. Append node to 'Controllers'. *)
  objController := fbXml.AppendChild(objControllers, 'Controller', 'CX5120', FALSE);
  objAttribute := fbXml.AppendAttribute(objController, 'Type', 'EPC');
  objAttribute := fbXml.AppendAttribute(objController, 'OS', 'Windows Embedded Compact 7');

  (* Create a new XML node 'Controller' and add some attributes. Append node to 'Controllers'. *)
  objController := fbXml.AppendChild(objControllers, 'Controller', 'CX2040', FALSE);
  objAttribute := fbXml.AppendAttribute(objController, 'Type', 'EPC');
  objAttribute := fbXml.AppendAttribute(objController, 'OS', 'Windows Embedded Standard 7');

  (* Create a new XML node 'Controller' and add some attributes. Append node to 'Controllers'. *)
  objController := fbXml.AppendChild(objControllers, 'Controller', 'C6015', FALSE);
  objAttribute := fbXml.AppendAttribute(objController, 'Type', 'IPC');
  objAttribute := fbXml.AppendAttribute(objController, 'OS', 'Windows 10 IoT Enterprise');

  (* Create a new XML node 'Machine' and add an attribute to this node. Append node to 'Machines' *)
  objMachine := fbXml.AppendNode(objMachines, 'Machine');
  objAttribute := fbXml.AppendAttribute(objMachine, 'Name', 'Stanze Oscar');
  
  (* Create a new XML node 'Controllers' and add to the 'Machine' node *)
  objControllers := fbXml.AppendNode(objMachine, 'Controllers');

  (* Create a new XML node 'Controller' and add some attributes. Append node to 'Controllers'. *)
  objController := fbXml.AppendChild(objControllers, 'Controller', 'C6017', FALSE);
  objAttribute := fbXml.AppendAttribute(objController, 'Type', 'IPC');
  objAttribute := fbXml.AppendAttribute(objController, 'OS', 'Windows 10 IoT Enterprise');
  newAttr := fbXml.InsertAttribute(objController, objAttribute, 'AddAttribute');
  fbXml.SetAttribute(newAttr, 'Hola');

  (* Retrieve XML document and store in a variable of data type STRING(1000) *)
  nLength := fbXml.CopyDocument(sXmlString, SIZEOF(sXmlString));
  bCreate := FALSE;
END_IF]]></ST>
      </Implementation>
    </Action>
  </POU>
</TcPlcObject>