<?xml version="1.0" encoding="utf-8"?>
<TcPlcObject Version="1.1.0.1" ProductVersion="3.1.4024.15">
  <POU Name="FB_AddProduct" Id="{185a3336-d0a7-46cc-9b60-b41eab261dd7}" SpecialFunc="None">
    <Declaration><![CDATA[FUNCTION_BLOCK FB_AddProduct
VAR_IN_OUT
	fbClient:FB_IotHttpClient;
END_VAR
VAR_OUTPUT
	bBusy:BOOL;
	bError:BOOL;
	
END_VAR
VAR
	fbRequest			: FB_IotHttpRequest;
	fbJsonSend				: FB_JsonDomParser;
	jsonDoc				: SJsonValue;
	nState				: UDINT;
	AddRisingEdge			: R_TRIG;
	fbGetProductByBarCode:FB_GetProductByBarCode;
//	domParse:FB_JsonDomParser;
	
END_VAR
//HMI
VAR
	bSend:BOOL;
	nStatusCode:UINT;
	newProductBarCode:STRING[13];
	newProduct:ST_Product;
END_VAR
]]></Declaration>
    <Implementation>
      <ST><![CDATA[AddRisingEdge(CLK:=bSend , Q=> );
CASE nState OF
	0:
		IF AddRisingEdge.Q AND newProductBarCode<>'' THEN
			nStatusCode:=0;
			nState:=10;
			bBusy:=TRUE;
			bError:=FALSE;
		END_IF
	
	10:
		
		jsonDoc:=fbJsonSend.NewDocument();
		fbJsonSend.AddInt64Member(jsonDoc,'id',0);
		fbJsonSend.AddStringMember(jsonDoc,'barCode',newProductBarCode);
		fbJsonSend.AddDoubleMember(jsonDoc,'finalPressure',newProduct.FinalPressure);
		fbJsonSend.AddDoubleMember(jsonDoc,'finalDisplacement',newProduct.FinalDisplacement);
		fbJsonSend.AddBoolMember(jsonDoc,'isQualified',newProduct.IsQualified);
		IF fbRequest.SendJsonDomRequest(sUri:='/Product',fbClient:=fbClient,eRequestType:=ETcIotHttpRequestType.HTTP_POST,fbJson:=fbJsonSend,0) THEN
			nState:=20;
		END_IF
	20:
		IF NOT fbRequest.bBusy THEN
			nStatusCode:=fbRequest.nStatusCode;
			IF nStatusCode=200 THEN
				newProductBarCode:='';
			END_IF
			nState:=100;
		END_IF
			
	100:
		bBusy:=FALSE;
		nState:=0;
END_CASE]]></ST>
    </Implementation>
  </POU>
</TcPlcObject>