<?xml version="1.0" encoding="utf-8"?>
<TcPlcObject Version="1.1.0.1" ProductVersion="3.1.4024.14">
  <POU Name="FB_GetProductByBarCode" Id="{3e3d48d8-4d4f-4a4b-849f-efc2e44959cc}" SpecialFunc="None">
    <Declaration><![CDATA[FUNCTION_BLOCK FB_GetProductByBarCode
VAR_INPUT
	
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;
	
	
	sUri:STRING;
	fbJsonDomParser		:FB_JsonDomParser;
	jsonDoc			:SJsonValue;
	
	emptyProduct:ST_Product;
END_VAR

//HMI
VAR
	bSend:BOOL;
	sBarCode:STRING;
	bProductNotFound:BOOL;
	nStatusCode:UINT;
	product:ST_Product;
	
END_VAR
]]></Declaration>
    <Implementation>
      <ST><![CDATA[RisingEdge(CLK:= bSend);
CASE nState OF
	0:
		sUri:=CONCAT('/Product/barcode/',sBarCode);
		IF RisingEdge.Q AND sBarCode<>'' THEN
			nStatusCode:=0;
			product:=emptyProduct;
			IF fbRequest.SendRequest(sUri:=sUri, fbClient:=fbClient, eRequestType:=ETcIotHttpRequestType.HTTP_Get, 0, 0, 0) THEN				
				nState:= 10;
				bBusy:= TRUE;
				bError:= FALSE;
				bProductNotFound:=FALSE;
			END_IF
		END_IF
		
	10:
		IF NOT fbRequest.bBusy THEN
			bError:=TRUE;
			nStatusCode:=fbRequest.nStatusCode;
			IF NOT fbRequest.bError THEN
				bError:=FALSE;
				IF fbRequest.nStatusCode=404 THEN
					bProductNotFound:=TRUE;
					nState:=100;
				END_IF
				IF fbRequest.nStatusCode=200 THEN
					jsonDoc:=fbRequest.GetJsonDomContent(fbJsonDomParser);
					IF jsonDoc<>0 THEN
						product.Id:=fbJsonDomParser.GetInt(fbJsonDomParser.FindMember(jsonDoc,'id'));
						product.CreatDateTime:=fbJsonDomParser.GetDateTime(fbJsonDomParser.FindMember(jsonDoc,'creatDateTime'));
						product.BarCode:=fbJsonDomParser.GetString(fbJsonDomParser.FindMember(jsonDoc,'barCode'));
						product.FinalPressure:=fbJsonDomParser.GetDouble(fbJsonDomParser.FindMember(jsonDoc,'finalPressure'));
						product.FinalDisplacement:=fbJsonDomParser.GetDouble(fbJsonDomParser.FindMember(jsonDoc,'finalDisplacement'));
						product.IsQualified:=fbJsonDomParser.GetBool(fbJsonDomParser.FindMember(jsonDoc,'isQualified'));
						nState:=100;
					END_IF
				END_IF
			END_IF
		END_IF
	100:
		bBusy:=FALSE;
		nState:=0;
END_CASE]]></ST>
    </Implementation>
    <LineIds Name="FB_GetProductByBarCode">
      <LineId Id="9" Count="0" />
      <LineId Id="30" Count="1" />
      <LineId Id="72" Count="1" />
      <LineId Id="148" Count="1" />
      <LineId Id="77" Count="4" />
      <LineId Id="74" Count="1" />
      <LineId Id="35" Count="0" />
      <LineId Id="41" Count="2" />
      <LineId Id="147" Count="0" />
      <LineId Id="45" Count="0" />
      <LineId Id="52" Count="0" />
      <LineId Id="46" Count="0" />
      <LineId Id="48" Count="0" />
      <LineId Id="51" Count="0" />
      <LineId Id="49" Count="0" />
      <LineId Id="55" Count="1" />
      <LineId Id="61" Count="1" />
      <LineId Id="64" Count="5" />
      <LineId Id="63" Count="0" />
      <LineId Id="57" Count="0" />
      <LineId Id="47" Count="0" />
      <LineId Id="44" Count="0" />
      <LineId Id="53" Count="0" />
      <LineId Id="109" Count="0" />
      <LineId Id="54" Count="0" />
      <LineId Id="32" Count="0" />
    </LineIds>
  </POU>
</TcPlcObject>