ThingModel is one kind of models Model (the other kinds are DataModels and AppModels). Therefore it has all the properties of Models (Lifecycle of models, Model Attributes, Unique and Constraints).
A ThingModel has
- a Namename,
- any number of Model Attributes
- and usually one or more Actions
However, as a recommended practice, the Attributes and Actions related to the interactions with an external thing (sensors, actuators, machines) are often defined in a reusable structure called ThingFacet, and combined into a ThingModel. This relationship was explained in the concept of Models and Model Facets. Application related information are often defined directly as attributes of the ThingModel.
Purpose of ThingModels:
- ThingModels are normally defined by users when interacting with Physical Things. The behavior of Physical Things are modeled via ThingFacet.
- ThingModels are driver for ThingFacets. ThingFacet (s) are reusable models and TQL CRUD operations can not be performed on ThingFacet. ThingFacet(s) are put to use only when combined within a ThingModel.
- ThingModels can contain application related information that are related to things.
Simplest form of defining a ThingModel is to provide a Name and Attributes. Example of a PhidgetServoModel ThingModel with one attribute PhidServoId of type In the following example, the ThingModel PhidgetServoModel combines the ThingFacet PhidgetServoFacet, where:
- PhidgetServoFacet contains the device interaction specific Attributes (e.g. ServoProtocolURL) and Actions (e.g. PhidgetServoAction);
- PhidgetServoModel contains application related Attributes: PhidServoId and InstalledAtZone (assuming the Phidget servo motor is installed in a particular zone within a Greenhouse environment, which has multiple zones).
- Combines is used as a modifier of ThingModel
In the simplest case, a ThingModel may only have one single directly defined attribute of type Sid. Although not required, it is best practice to always create a ThingModel with one attribute that is of type SystemId or Sid.
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
<ThingModel# ThingModel(Name=: "PhidgetServoModel"> <Sid Name="PhidServoId"/> </ThingModel> <ThingFacet Name="PhidgetServoFacet"> <String Name="ServoProtocolURL" Default=", Combines: "PhidgetServoFacet"): Sid(Name: "PhidServoId") String(Name: "InstalledAtZone") ThingFacet(Name: "PhidgetServoFacet"): String(Name: "ServoProtocolURL", Default: "phid://") /> <String String(Name=: "DeviceInterfaceIndex") /> <String String(Name=: "PhidgetDeviceType") /> <Integer Integer(Name=: "ServoAngle", KnownBy=: "PhidgetServoAction") /> <Action Action(Name=: "PhidgetServoAction", Documentation=: "Control servo motor"> ... </Action> </ThingFacet> |
ThingsFacets are used within ThingModels using Combines modifier. Example of a PhidgetServoModel ThingModel combining a PhidgetServoFacet ThingFacet
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
<ThingModel Name="PhidgetServoModel" Combines="PhidgetServoFacet">): <Sid Name="PhidServoId"/> </ThingModel> |
...
#... |
This pattern allows the ThingFacet to be a reusable artifact, and potentially combined by multiple ThingModels.
Note: One can combine multiple ThingFacets within a single ThingModel using comma separated list of ThingFacets.
For Example Esky ThingModel combining EskyPreset and EskyImage Thing Facets
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
<ThingFacet name=# ThingFacet(Name: "EskyPreset">): <String name=String(Name: "State", KnownBy=: "SyncPreset"/>) <String name=String(Name: "Preset", KnownBy=: "SyncPreset"/>) <Action name=Action(Name: "SyncPreset", documentation=: "Synchronize camera state and preset">): <WorkflowWorkflow Limit=: "1", Live=: "1", Timeout=: "PT20S">): <Task name=Task(Name: "Main", while=: "true">): <Event name=Event(Name: "Argument", as=: "ActionArgument"/>) #... </Task> </Workflow> </Action> </ThingFacet> <ThingFacet name=ThingFacet(Name: "EskyImage", combines=: "Login">): <String name=String(Name: "State", KnownBy=: "SyncImage"/>) <Clob name=Clob(Name: "Image", KnownBy=: "SyncImage"/>) <!-- Actions -->#Actions <Action name=Action(Name: "SyncImage", documentation=: "Synchronize camera state and snapshot image">): <WorkflowWorkflow Limit=: "1", Live=: "1", Timeout=: "PT10S">): <Task name=Task(Name: "Main", while=: "true">): <Event name=Event(Name: "Argument", as=: "ActionArgument"/> ... </Task> </Workflow> </Action> </ThingFacet> <ThingModel name="Esky" combines=) ThingModel(Name: "Esky", combines: "EskyPreset,EskyImage", documentation=: "actual single instance camera model">): <Sid name=Sid(Name: "CameraId"/> </ThingModel> |
Adding application related information to a ThingModel. For Example, assume that the Phidget Servo Motor is part of a Zone within a Greenhouse environment. Greenhouse contains multiple zones.
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
<ThingModel Name="PhidgetServoModel" Combines="PhidgetServoFacet">
<Sid Name="PhidServoId"/>
<String Name="InstalledAtZone"/>
</ThingModel> |
Examples of CRUD Operation on a ThingModel:
CREATE:
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
<Create>
<PhidgetServoModel>
<ServoProtocolURL>
phid://
</ServoProtocolURL>
<PhidgetDeviceType>
PhidgetAdvancedServo
</PhidgetDeviceType>
<DeviceInterfaceIndex>
0
</DeviceInterfaceIndex>
<ServoAngle>
110
</ServoAngle>
</PhidgetServoModel>
</Create> |
CREATE RESULT:
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
<Create Status="Success">
<PhidgetServoModel>
<PhidServoId>KNJN2YRVAAAAUAABA7LIQ4Y6</PhidServoId>
<ServoProtocolURL Status="Success+Created:1:1457384153653;" Value="phid://"/>
<DeviceInterfaceIndex Status="Success+Created:1:1457384153653;" Value="0"/>
<PhidgetDeviceType Status="Success+Created:1:1457384153653;" Value="PhidgetAdvancedServo"/>
<ServoAngle Status="Success+Created:1:1457384153654;" Value="110"/>
</PhidgetServoModel>
</Create> |
Note: If a ThingModel is combined with a ThingFacet, creation of a ThingModel results in instantiating (Trigger) of a ThingFacet action.
UPDATE:
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
<Query>
<Find format="version">
<PhidgetServoModel as="var.PS">
<PhidServoId ne=""/>
</PhidgetServoModel>
</Find>
<SetResponseData>
<key>
Message.Value.Find.Result.PhidgetServoModel.ServoAngle.Value
</key>
<value>
120
</value>
</SetResponseData>
<Update>
<from>
Result
</from>
<Include>
$Response.Message.Value.Find
</Include>
</Update>
</Query> |
UPDATE Result:
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
<Find Status="Success" Format="version">
<Result>
<PhidgetServoModel>
<PhidServoId>KNJPIFR6AAAAUAABA67ZRBQO</PhidServoId>
<PhidgetDeviceType Value="PhidgetAdvancedServo" Version="1"/>
<ServoAngle Version="1" Value="120"/>
<DeviceInterfaceIndex Value="0" Version="1"/>
<ServoProtocolURL Value="phid://" Version="1"/>
</PhidgetServoModel>
</Result>
</Find>
<Update Status="Success" Format="version">
<PhidgetServoModel>
<PhidServoId>KNJPIFR6AAAAUAABA67ZRBQO</PhidServoId>
<ServoProtocolURL Status="Success_NoAction:1:1457385641534;" Value="phid://" Version="1"/>
<DeviceInterfaceIndex Status="Success_NoAction:1:1457385641534;" Value="0" Version="1"/>
<PhidgetDeviceType Status="Success_NoAction:1:1457385641535;" Value="PhidgetAdvancedServo" Version="1"/>
<ServoAngle Status="Success=Updated:2:1457385767153;" Value="120" Version="2"/>
</PhidgetServoModel>
</Update> |
Note: If a ThingModel is combined with a ThingFacet updating a ThingModel attribute results in Trigger of a Action if that attribute is associated with an action.
DELETE:
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
<<Query>
<DeleteAll>
<PhidgetServoModel>
<PhidServoId ne=""/>
</PhidgetServoModel>
</DeleteAll>
</Query> |
DELETE Result
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
<DeleteAll Status="Success">
<Result>
<PhidgetServoModel>
<PhidServoId>KNJOOOBZAAAAUAABA72MMI6E</PhidServoId>
<PhidgetDeviceType>PhidgetAdvancedServo</PhidgetDeviceType>
<ServoAngle>110</ServoAngle>
<DeviceInterfaceIndex>0</DeviceInterfaceIndex>
<ServoProtocolURL>phid://</ServoProtocolURL>
</PhidgetServoModel>
</Result>
</DeleteAll> |
Note: If a ThingModel is combined with a ThingFacet, creation of a ThingModel results in deleting an instance results in cleanup of connections with actual things as per the protocol Handler used.
FIND
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
<Query>
<Find format="all">
<PhidgetServoModel>
<PhidServoId ne="" />
</PhidgetServoModel>
</Find>
</Query> |
Find result: Format="all" - returns all the internal attribute data as well.
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
<Find Status="Success" Format="all">
<Result>
<PhidgetServoModel QName="Atomiton.PhidgetServos.PhidgetServoModel">
<PhidServoId>KNJOOOBZAAAAUAABA72MMI6E</PhidServoId>
<PhidgetDeviceType Value="PhidgetAdvancedServo" Known="PhidgetAdvancedServo" Version="1" Timestamp="1457384798266" DateTime="2016-03-07 13:06:38.266" QName="Atomiton.PhidgetServos.PhidgetServoModel.PhidgetDeviceType" FName="PhidgetDeviceType"/>
<ServoAngle Value="110" Known="110" Version="1" Timestamp="1457384798266" DateTime="2016-03-07 13:06:38.266" QName="Atomiton.PhidgetServos.PhidgetServoModel.ServoAngle" FName="ServoAngle"/>
<DeviceInterfaceIndex Value="0" Known="0" Version="1" Timestamp="1457384798266" DateTime="2016-03-07 13:06:38.266" QName="Atomiton.PhidgetServos.PhidgetServoModel.DeviceInterfaceIndex" FName="DeviceInterfaceIndex"/>
<ServoProtocolURL Value="phid://" Known="phid://" Version="1" Timestamp="1457384798265" DateTime="2016-03-07 13:06:38.265" QName="Atomiton.PhidgetServos.PhidgetServoModel.ServoProtocolURL" FName="ServoProtocolURL"/>
</PhidgetServoModel>
</Result>
</Find> |
Note:
- When ThingModels are do not combine ThingFacet it is equivalent of a DataModel i.e pure Data only.
- Although not required, it is best practice to always create a ThingModel with one attribute that is of type SystemId or Sid.
...
) |
In summary
- ThingModels are normally used to represent and define interactions with external physical things. The specifics of interactions are often modeled via ThingFacet.
- ThingModels combine one or more ThingFacets. ThingFacets are reusable artifacts. In runtime, only instances of ThingModels are created and persisted (not ThingFacets).
- ThingModels can contain application related information that are related to things.
When ThingModels do not have any Actions or combine any ThingFacets they are equivalent to DataModel i.e pure Attributes only.
ThingModel can be created, read, update and deleted using TQL queries.
Expand | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
| |||||||||||
|