# An Automatic Embedded Device Registration Procedure Based on the OGC SensorThings API
## 3 Methodology
### Automatic Registration Procedure
#### 3.3.2 Registration Procedure for the Sensing Capability  
When a device joins an SW-IoT system, the device will be registered in the gateway's lookup table and the SensorThings API services. The gateway holds a lookup table which records the description files of devices exist in the local network as well as their corresponding virtual identities on the SensorThings API service. Figure 7 is the overall registration procedure for the sensing capability.  
1. First of all, the registration procedure is triggered when a device tries to upload observations or new locations by sending an UploadObs operation (Arrow 1) to the connected gateway. The UploadObs operation contains the observations or locations in the msg_body (Table 3).
2. Once the gateway receives the UploadObs operation, the gateway will check if the device has been registered in its lookup table according to the device UID.
3. If the device has been registered in the lookup table, the gateway can directly upload the observations or new locations to the service (Arrow 2) as the device's virtual identities have also been also stored in the lookup table, and then the gateway sends a Confirm operation (Arrow 12) to the device to confirm the end of the procedure.
4. However, if the device has not been registered in the lookup table (i.e., a new device, a new gateway, or a mobile device), the gateway will send the GetServURL operation (Arrow 3) to the device to ask for the service URL that the device wants to be registered to.
5. The device responds with the SendServURL operation (Arrow 4) containing the service URL. With the service URL, the gateway can query the service based on the device UID, to check whether the device has been registered in the service or not.  
If the device has been registered in the service (i.e., a new gateway or a mobile device), the gateway can collect the device's sensing capabilities and virtual identities from the service to update its lookup table (Arrow 5), and finally upload the observations or locations to the service (Arrow 11).  
If the device has not been registered to the service (i.e., a new device), the gateway will send a GetDesc operation (Arrow 6) to the device to ask for the description file of the device.  
The device responds with the SendDesc operation (Arrow 7) containing the description file.  
Based on the description file, the gateway helps the device register to the service and create necessary entities, including Thing (Arrow 8), Datstream, or TaskingCapability (Arrow 9), etc. In this step, the gateway checks if there is already the same ObservedProperties registered on the service. If the ObservedProperties have been registered, the gateway will retrieve the IDs and link new Datastreams to the existing ObservedProperties.  
Finally, after the gateway uploads the observations or locations to the service, the gateway sends a Confirm operation (Arrow 12) to the device to confirm the end of the procedure.  
Please note that although this workflow is mainly for the sensing capability, the gateway registers both sensing and tasking capabilities to the service.