Der Funktionsbaustein wird gestartet, indem die Variable bPutPhilipsHue im Hauptprogramm auf TRUE gesetzt wird.
Die steigende Flanke wird dann verwendet, um eine PUT-Anfrage mit dem Funktionsbaustein IotHttpRequest zu senden.
Nachdem die Anfrage abgeschlossen ist, wird die Fehlerbehandlung durchlaufen. Wenn weder der Funktionsbaustein selbst noch der HTTP-Statuscode einen Fehler aufweisen, könnte die JSON-Antwort von der Philips Hue Bridge geparst werden. Die JSON-Antwort zeigt dem Benutzer, ob die Anfrage erfolgreich war.
CASE nState OF 0: fbJsonWriter.StartObject(); fbJsonWriter.AddKey('on'); fbJsonWriter.AddBool(TRUE); fbJsonWriter.AddKey('sat'); fbJsonWriter.AddUdint(50); fbJsonWriter.AddKey('bri'); fbJsonWriter.AddUdint(100); fbJsonWriter.AddKey('hue'); fbJsonWriter.AddUdint(nColor); fbJsonWriter.EndObject(); sSend:=fbJsonWriter.GetDocument(); fbJsonWriter.ResetDocument(); IF RisingEdge.Q THEN IF fbRequest.SendRequest(sUri:='/api/qiyut7ubQQQv2xKJfFe9cVvIroUGHtIk2eYsIfGX/lights/1/state', fbClient:=fbClient, eRequestType:=ETcIotHttpRequestType.HTTP_PUT, pContent:=ADR(sSend), nContentSize:=LEN2(ADR(sSend)), 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 ; // do something with status response nValidResCount:= nValidResCount+1; bError:= FALSE; 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