<?xml version="1.0" encoding="utf-8"?>
<TcPlcObject Version="1.1.0.1" ProductVersion="3.1.4024.14">
  <POU Name="FB_GetPagedProducts" Id="{53e7bb6f-36e3-451c-a880-17794501a2db}" SpecialFunc="None">
    <Declaration><![CDATA[FUNCTION_BLOCK FB_GetPagedProducts

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;
	SearchRisingEdge			: R_TRIG;
	NextPageRisingEdge			: R_TRIG;
	PreviousPageRisingEdge			: R_TRIG;
	emptyProducts:ARRAY[1..PageSize] OF ST_Product;
	
	sUri:STRING;
	
	fbJsonDomParser		:FB_JsonDomParser;
	jsonDoc			:SJsonValue;
	productArrayJsonDoc:SJsonValue;
	index:INT;
	arraySize:UDINT;
	productJsonDoc:SJsonValue;
	//HMI
	bSearch:BOOL;
	bNextPage:BOOL;
	bPreviousPage:BOOL;
	
	nCurrentPage:DINT;
	nTotalItems:DINT;
	nTotalPages:DINT;
	
	products:ARRAY[1..PageSize] OF ST_Product;
END_VAR
VAR CONSTANT
	PageSize:INT:=10;
END_VAR
]]></Declaration>
    <Implementation>
      <ST><![CDATA[SearchRisingEdge(CLK:=bSearch , Q=> );
NextPageRisingEdge(CLK:=bNextPage , Q=> );
PreviousPageRisingEdge(CLK:=bPreviousPage , Q=> );
CASE nState OF
	0:
		IF SearchRisingEdge.Q THEN
			nCurrentPage:=1;
			nState:= 5;
		END_IF
		IF NextPageRisingEdge.Q AND nCurrentPage<nTotalPages THEN
			nCurrentPage:=nCurrentPage+1;
			nState:= 5;
		END_IF
		IF PreviousPageRisingEdge.Q AND nCurrentPage>1 THEN
			nCurrentPage:=nCurrentPage-1;
			nState:= 5;
		END_IF
	5:	
		products:=emptyProducts;
		sUri:=CONCAT('/Product/paged?pageNumber=',DINT_TO_STRING(nCurrentPage));
		sUri:=CONCAT(sUri,'&pageSize=10');
		
		IF fbRequest.SendRequest(sUri:=sUri, fbClient:=fbClient, eRequestType:=ETcIotHttpRequestType.HTTP_Get, 0, 0, 0) THEN				
				nState:= 10;
				bBusy:= TRUE;
				bError:= FALSE;
				
		END_IF
	10:
		IF NOT fbRequest.bBusy THEN
			bError:=TRUE;
			IF NOT fbRequest.bError THEN
				bError:=FALSE;
				
				IF fbRequest.nStatusCode=200 THEN
					jsonDoc:=fbRequest.GetJsonDomContent(fbJsonDomParser);
					IF jsonDoc<>0 THEN
						nTotalItems:=fbJsonDomParser.GetInt(fbJsonDomParser.FindMember(jsonDoc,'totalItems'));
						nTotalPages:=fbJsonDomParser.GetInt(fbJsonDomParser.FindMember(jsonDoc,'totalPages'));
						productArrayJsonDoc:=fbJsonDomParser.FindMember(jsonDoc,'items');
						arraySize:=fbJsonDomParser.GetArraySize(productArrayJsonDoc);
						FOR index:=0 TO UDINT_TO_INT(arraySize) DO
							productJsonDoc:=fbJsonDomParser.GetArrayValueByIdx(idx:=index,v:=productArrayJsonDoc);
							products[index+1].Id:=fbJsonDomParser.GetInt(fbJsonDomParser.FindMember(productJsonDoc,'id'));
							products[index+1].CreatDateTime:=fbJsonDomParser.GetDateTime(fbJsonDomParser.FindMember(productJsonDoc,'creatDateTime'));
							products[index+1].BarCode:=fbJsonDomParser.GetString(fbJsonDomParser.FindMember(productJsonDoc,'barCode'));
							products[index+1].FinalPressure:=fbJsonDomParser.GetDouble(fbJsonDomParser.FindMember(productJsonDoc,'finalPressure'));
							products[index+1].FinalDisplacement:=fbJsonDomParser.GetDouble(fbJsonDomParser.FindMember(productJsonDoc,'finalDisplacement'));
							products[index+1].IsQualified:=fbJsonDomParser.GetBool(fbJsonDomParser.FindMember(productJsonDoc,'isQualified'));
						END_FOR
						nState:=100;
					END_IF
				END_IF
			END_IF
		END_IF
	100:
		nState:=0;
END_CASE]]></ST>
    </Implementation>
    <LineIds Name="FB_GetPagedProducts">
      <LineId Id="9" Count="0" />
      <LineId Id="188" Count="1" />
      <LineId Id="30" Count="1" />
      <LineId Id="72" Count="0" />
      <LineId Id="190" Count="0" />
      <LineId Id="193" Count="0" />
      <LineId Id="191" Count="0" />
      <LineId Id="35" Count="0" />
      <LineId Id="265" Count="0" />
      <LineId Id="270" Count="0" />
      <LineId Id="266" Count="0" />
      <LineId Id="268" Count="1" />
      <LineId Id="271" Count="0" />
      <LineId Id="267" Count="0" />
      <LineId Id="194" Count="0" />
      <LineId Id="210" Count="0" />
      <LineId Id="199" Count="1" />
      <LineId Id="207" Count="0" />
      <LineId Id="201" Count="5" />
      <LineId Id="41" Count="2" />
      <LineId Id="45" Count="0" />
      <LineId Id="52" Count="0" />
      <LineId Id="49" Count="0" />
      <LineId Id="55" Count="1" />
      <LineId Id="61" Count="0" />
      <LineId Id="208" Count="1" />
      <LineId Id="68" Count="0" />
      <LineId Id="213" Count="2" />
      <LineId Id="219" Count="4" />
      <LineId Id="218" Count="0" />
      <LineId Id="216" Count="0" />
      <LineId Id="69" Count="0" />
      <LineId Id="63" Count="0" />
      <LineId Id="57" Count="0" />
      <LineId Id="47" Count="0" />
      <LineId Id="44" Count="0" />
      <LineId Id="53" Count="1" />
      <LineId Id="32" Count="0" />
    </LineIds>
  </POU>
</TcPlcObject>