Skip to content

File Example

This example demonstrates how to use the File Device Service to scan files from the specified storage.

Prepare a Storage

The storage can be a single folder or a NAS (network attached storage).

In this example, we create a folder as the storage.

mkdir -p my-storage

And mount the storage to the File Device Service by using the local docker-compose file.

version: '3.7'
services:
  device-file:
    volumes:
      - /path/to/my-storage:/storage:ro # Replace /path/to/my-storage with the correct storage path

Note

If you use NAS (Network-attached storage) as file storage system, you have to mount the NAS into your machine and then mount into the File Device Service.

Start the File Device Service

To start the File Device Service, enter the following command:

edgexpert up device-file

Device Onboarding

The File Device Service contains a pre-defined profile file-device which is added to Edge Xpert when the service is initialized, you must onboard the device with this predefined device profile.

name: "file-device"

deviceResources:
  - name: "FileInfo"
    isHidden: false
    description: "The file information"
    properties:
      valueType: "Object"
      readWrite: "R"
  • Onboard the device

The interval of autoEvent indicates how often the target folder storage is scanned; in this case, the storage folder will be scanned every 30 seconds. If the folder contains the new file, the file Device Service will send the event which contains the file information.

curl http://localhost:59881/api/v2/device -H "Content-Type:application/json" -X POST \
  -d '[
        {
          "apiVersion": "v2",
          "device": {
            "name": "my-storage",
            "description":"My storage",
            "labels":[
              "file"
            ],
            "adminState": "UNLOCKED",
            "operatingState": "UP",
            "protocols": {
              "file": {
                "StoragePath": "/storage"
              }
            },
            "serviceName": "device-file",
            "protocolName":"file",
            "profileName": "file-device",
            "autoEvents": [
              {
                "interval": "30s",
                "onChange": false,
                "sourceName": "FileInfo"
              }
            ]
          }
        }
      ]'
  • StoragePath must point to the folder of service container which defined in the section Prepare a Storage.

Info

If Edge Xpert is running in secure mode, you will need to replace localhost in the URL with the IP address for the service. See CLI Service Ports for details.

  • Check the number of events

    curl localhost:59880/api/v2/event/all
    
  • And the result will be similar as below:

    {
       "apiVersion":"v2",
       "requestId":"ad042107-830f-4224-b4db-fda540429667",
       "event":{
          "apiVersion":"v2",
          "id":"42d4bded-5ca2-487f-8d91-8f31073b0b3e",
          "deviceName":"my-storage",
          "profileName":"file-device",
          "sourceName":"FileInfo",
          "origin":1702448690354114907,
          "readings":[
             {
                "id":"93833801-842c-4af7-9aff-4f78aecc5ac1",
                "origin":1702448690354114907,
                "deviceName":"my-storage",
                "resourceName":"FileInfo",
                "profileName":"file-device",
                "valueType":"Object",
                "value":"",
                "objectValue":{
                   "storagePath":"/storage",
                   "filename":"test.txt"
                }
             }
          ]
       }
    }
    

Device Configuration Properties

The pre-defined device has the following properties:

Field Example Value Description Required
Name StoragePath The storage path to scan Required