Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

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 NameParametersResponse
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)

Code Block
languagexml
titleCloud Service API Response
linenumberstrue
<current>
  <city id="5393015" name="Santa Clara">
    <coord lon="-121.96" lat="37.35"/>
    <country>US</country>
    <sun rise="2016-03-17T14:13:28" set="2016-03-18T02:18:25"/>
  </city>
  <temperature value="297.67" min="296.15" max="301.15" unit="kelvin"/>
  <humidity value="41" unit="%"/>
  <pressure value="1012" unit="hPa"/>
  <wind>
    <speed value="3.1" name="Light breeze"/>
    <gusts/>
    <direction value="0" code="N" name="North"/>
  </wind>
  <clouds value="5" name="clear sky"/>
  <visibility value="16093"/>
  <precipitation mode="no"/>
  <weather number="800" value="clear sky" icon="02d"/>
  <lastupdate value="2016-03-17T23:30:57"/>
</current>

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.

TagResolution
[%:Event.Argument.DCBaseURL.Value:%]
DCBaseURL value passed when creating/updating model instance.
[%:Event.Argument.DCQueryParam.Value:%]
DCQueryParam value passed when creating/updating model instance.
&amp;
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
    languagexml
    titleHandleResponse 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>
    TagResolution
    "/'[%: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
languagexml
titleSubscribe To External Env
linenumberstrue
<Query Storage="TqlSubscription">
  <Save>
    <TqlSubscription Label="Changes To External Env" sid="1">
      <Topic>
        *Atomiton.ExternalEnvServiceDeviceCloudProvider.ExternalEnv*
      </Topic>
    </TqlSubscription>
  </Save>
</Query>

...