...
3. Select the list of APIs to be integrated in the TQL Model (ThingFacets). For this tutorial we have selected API to get temperature, humidity, light, pressure for a given City and Country.
API Name | Parameters | Response | |||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
http://api.openweathermap.org/data/2.5/weather | q - Query String. Example, Santa Clara,CA mode - XML or JSON or HTML APPID - Your API Key from step (2) |
|
Write a ThingFacet
To abstract the interactions with the OpenWeatherMap Cloud Service we write a ExternalEnvFacet ThingFacet.
...
Note that the Event Parameter values are dereferenced using TP Notation. The notation [%: is part of the /wiki/spaces/TEACH/pages/21170773. More details on /wiki/spaces/TEACH/pages/21170773 can be found in the Developer's Guide.
Tag | Resolution |
---|---|
[%:Event.Argument.DCBaseURL.Value:%] | DCBaseURL value passed when creating/updating model instance. |
[%:Event.Argument.DCQueryParam.Value:%] | DCQueryParam value passed when creating/updating model instance. |
& | Escape the XML "&". Remember that the query string is q=xxx&... here & must be escaped. |
[%:Event.Argument.OutputFormat.Value:%] | OutputFormat value passed when creating/updating model instance. |
[%:Event.Argument.DCAPIKey.Value:%] | DCAPIKey value passed when creating/updating model instance. |
4. Process Response.
- Check if HTTP code is 200 else log the error message
Convert the temperature value from Kelvin to Centigrade.
Code Block language xml title HandleResponse to GET <Invoke Name="HandleResponse"> <FacetScript> <If Condition="/'[%:Invoke.ReadValues.Status:%]' eq '200'"> <Then> <Log Message="Response Status is 200"/> <If Condition="/'[%:Invoke.ReadValues.Message.Value.current.temperature.unit:%]' eq 'kelvin'"> <Then> <Log Message="Temperature Unit is Kelvin.."/> <SetContextData Key="CelTemp" Value="[%:/number([%:Invoke.ReadValues.Message.Value.current.temperature.value:%]-273):]"/> <Log Message="Temperature Value in Centigrade is.. [:$ContextData.CelTemp:]"/> </Then> <Else> <SetContextData Key="CelTemp" Value="[%:Invoke.ReadValues.Message.Value.current.temperature.value:%]"/> </Else> </If> </Then> <Else> <Log Message="Non-200 Response Status. Send Alert to DevOps"/> </Else> </If> </FacetScript> </Invoke>
Tag Resolution "/'[%:Invoke.ReadValues.Message.Value.current.temperature.unit:%]' eq 'kelvin'"
This is an XPath Expression to compare string. Example: "/<StringValue>' eq 'StringValue2' SetContextData
This is a temporary key/value storage provided by TQLEngine as part of FacetScript. Here Key is CelTemp, Value is converted value to Centigrade to Original Value (if original value is not in Kelvin). [%:/number([%:Invoke.ReadValues.Message.Value.current.temperature.value:%]-273):]
This is XPath Expression to perform Subtraction.
5. Store the output value in EvnDetails Attribute.
...
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
<Query Storage="TqlSubscription"> <Save> <TqlSubscription Label="Changes To External Env" sid="1"> <Topic> *Atomiton.ExternalEnvServiceDeviceCloudProvider.ExternalEnv* </Topic> </TqlSubscription> </Save> </Query> |
...