Skip to content

hchw/device-rest-go

 
 

Repository files navigation

device-rest-go

Build Status Code Coverage Go Report Card GitHub Latest Dev Tag) GitHub Latest Stable Tag) GitHub License GitHub go.mod Go version GitHub Pull Requests GitHub Contributors GitHub Committers GitHub Commit Activity

EdgeX device service for REST protocol

This device service provides easy way for 3'rd party applications, such as Point of Sale, CV Analytics, etc., to push data into EdgeX via the REST protocol.

The current implementation is meant for one-way communication into EdgeX via async readings. If future use cases determine a need forcommanding, i.e. two-communication, it can be added then.

Runtime Prerequisite

  • core-data
    • Redis DB
  • core-metadata

Build with NATS Messaging

Currently, the NATS Messaging capability (NATS MessageBus) is opt-in at build time. This means that the published Docker image and Snaps do not include the NATS messaging capability.

The following make commands will build the local binary or local Docker image with NATS messaging capability included.

make build-nats
make docker-nats

The locally built Docker image can then be used in place of the published Docker image in your compose file. See Compose Builder nat-bus option to generate compose file for NATS and local dev images.

REST Endpoints

This device service creates the additional parametrized REST endpoint:

/api/v2/resource/{deviceName}/{resourceName}
  • deviceName refers to a device managed by the REST device service.
  • resourceNamerefers to the device resource defined in the device profile associated with the given deviceName .

The data, text or binary, posted to this endpoint is type validated and type casted (text data only) to the type defined by the specified device resource. The resulting value is then sent into EdgeX via the Device SDK's async values channel.

Note: When binary data is used the EdgeX event/reading is CBOR encoded by the Device SDK and the binary value in the reading isNOT be stored in the database by Core Data. The CBOR encoded event/reading, with the binary value, is published to the Message Bus for Application Services to consume

Note: All non-binary data is consumed as text. The text is casted to the specific type of the specified device resource once it passes type validation.

Configuration

This device service use the standard configuration defined by the Device SDK.

The DeviceList configuration is standard except that the DeviceList.Protocols can be empty. The following is a sample DeviceList that works with the sample device profiles referenced below.

[[DeviceList]]
  Name = "sample-json"
  ProfileName = "sample-json"
  Description = "RESTful Device that sends in JSON data"
  Labels = [ "rest", "json" ]
  [DeviceList.Protocols]
    [DeviceList.Protocols.other]
[[DeviceList]]
  Name = "sample-image"
  ProfileName = "sample-image"
  Description = "RESTful Device that sends in binary image data"
  Labels = [ "rest", "binary", "image" ]
  [DeviceList.Protocols]
    [DeviceList.Protocols.other]    
[[DeviceList]]
  Name = "sample-numeric"
  ProfileName = "sample-numeric"
  Description = "RESTful Device that sends in numeric data"
  Labels = [ "rest", "numeric", "float", "int" ]
  [DeviceList.Protocols]
    [DeviceList.Protocols.other]

Device Profile

As with all device services the device profile is where the Device Name, Device Resources and Device Commands are define. The parameterized REST endpoint described above references these definitions. Each Device has it's own device profile. There are three sample device profiles that define the devices referenced in the above sample configuration.

Note: TheisHidden field is set to true since this device service does not support Commanding.

Testing/Simulation

The best way to test this service with simulated data is to use PostMan to send data to the following endpoints defined for the above device profiles.

  • http://localhost:59986/api/v2/resource/sample-image/jpeg

    • POSTing a JPEG binary image file will result in the BinaryValue of the Reading being set to the JPEG image data posted.
    • Example test JPEG to post:
      • Select any JPEG file from your computer or the internet
  • http://localhost:59986/api/v2/resource/sample-image/png

    • POSTing a PNG binary image file will result in the BinaryValue of the Reading being set to the PNG image data posted.
    • Example test PNG to post:
      • Select any PNG file from your computer or the internet
  • http://localhost:59986/api/v2/resource/sample-json/json

    • POSTing a JSON string value will result in the Value of the Reading being set to the JSON string value posted.

      Note: Currently there isn't a JSON data type, thus there is no validation that the string value is valid JSON. It is up to the Application Service using the JSON to first validate it.

    • Example test JSON value to post:

      {
          "id" : "1234",
          "name" : "test data",
          "payload" : "test payload"
      }
  • http://localhost:59986/api/v2/resource/sample-numeric/int

    • POSTing a text integer value will result in the Value of the Reading being set to the string representation of the value as an Int64. The POSTed value is verified to be a valid Int64 value.

    • A 400 error will be returned if the POSTed value fails the Int64 type verification.

    • Example test int value to post:

      1001
      
  • http://localhost:59986/api/v2/resource/sample-numeric/float

    • POSTing a text float value will result in the Value of the Reading being set to the string representation of the value as an Float64. The POSTed value is verified to be a valid Float64 value.

    • A 400 error will be returned if the POSTed value fails the Float64 type verification.

    • Example test float value to post:

      500.568
      

AutoEvents

Since Commanding is not implemented, specifying AutoEvents in the configuration will result in errors. Thus AutoEvents should not be specified in the configuration.

Build Instructions

  1. Clone the device-rest-go repo with the following command:

     git clone https://github.com/edgexfoundry/device-rest-go.git
    
  2. Build a docker image by using the following command:

     make docker
    
  3. Alternatively the device service can be built natively:

     make build
    

Packaging

This component is packaged as docker image and snap.

For docker, please refer to the Dockerfile and Docker Compose Builder scripts.

For the snap, refer to the snap directory.

Packages

No packages published

Languages

  • Go 82.7%
  • Makefile 7.2%
  • Dockerfile 5.2%
  • Shell 4.9%