Skip to content

Latest commit

 

History

History
229 lines (195 loc) · 6.04 KB

README.md

File metadata and controls

229 lines (195 loc) · 6.04 KB

Quick access

thingsboard-sendbox

this is a simple service that can listen the request and response result

This project development ENV

  • node 16.13.0

How about this project

Feature

  1. create a simple service
  2. can recieve a data input and parse data by the header given
  3. can dynamic add, delete and change data parse rule by the json file
  4. can use the thingsboard API Calls rule node to create a webhook to handle data
  5. don't have other library

Limit

  1. no queue
  2. can not handle array data now

How to use

1. Set json config (you can use this config for test)

{
  "server": {
    "port": 8000,
    "dev_host": "127.0.0.1",
    "product_host": "0.0.0.0",
    "debug": true,
    "production": false
  },
  "device": {
    "device_type_A": {
      "get_cpu_loading": {
        "path": "CPU.loading"
      },
      "get_mem_loading": {
        "path": "MEM.loading"
      },
      "get_two": {
        "path": "Value.one.two"
      },
      "get_deep_value": {
        "path": "Value.one.two.three"
      }
    }
  }
}

2. Start service

npm run start

3. Listen the API request

This service will listen at POST http://host:port/device

4. Test the API can parse data

postman

you can use the postman to check this service API is work: thingsboard sendbox public postman

cURL

curl --location --request POST 'http://127.0.0.1:8000/device' \
--header 'datapath: MEM.loading' \
--header 'devicetype: device_type_A' \
--header 'Content-Type: application/json' \
--data-raw '{
    "MEM": {
        "loading": 77
    }
}'

API -- POST http://host:port/device

Required header

  • datapath

    • can tell the service how to parse data
    • please use . to split if you want to parse data deeper
  • devicetype

    • can tell the service current data is send by some device type

body

  • any data (json format) send by device or others

Project config -- config.json

sample config as follow

{
  "server": {
    "port": 8000,
    "dev_host": "127.0.0.1",
    "product_host": "0.0.0.0",
    "debug": true,
    "production": false
  },
  "device": {
    "device_type_A": {
      "get_cpu_loading": {
        "path": "CPU.loading"
      }
    },
    "device_type_B": {
      "get_mem_loading": {
        "path": "MEM.loading"
      }
    }
  }
}

config -- server

  • port
    • server port
  • dev_host
    • server listen host during dev mode
  • product_host
    • server listen host during production mode
  • debug
    • can print some debug log
  • production
    • if this attribute is true, the service will listen the product_host
    • otherwise the service listen the dev_host

config -- device

layer 1 -- device type (in this sample at device_type_A & device_type_B)

  • you can set your device type name
  • this config will tell this service will receive which device type data

layer 2 -- function name (in this sample at get_cpu_loading & get_mem_loading)

  • you can set the function name
  • the service don't care the name of function

layer 3 -- data path

  • you can use it to tell the service how to get some value
    • assump there is a data like as follow
        {
          "data": {
            "one": 1
          }
        }
    • if you want to get the "one" value in this structure, you can set the path to data.one
    • the . can tell the service how path to query the data

Create a webhook with thingsboard

1. create rule node as follow (in Root Rule Chain)

image

2. set the rest api call config (make sure that the network can connection with thingsboard!!!)

There are two header params at bottom are datapath and devicetype

The datapath can tell the service how to get the data. (data format please reference API chapter)

Another devicetype can tell the service current data is send by some device type. (how this service use it please reference config chapter)

image

Run the demo code to observe how the service parse data

  • there are some files in demo folder
  • the demo folder has isolated environment to run the core process
  • you can change the demo/config.demo.json and run npm run demo to observe how the service parse data

How to Deploy (Linux & Docker)

1. Set config.json

  "server": {
    "port": 8000,
    "dev_host": "127.0.0.1",
    "product_host": "0.0.0.0",
    "debug": true,
    "production": true // if true, the service will listen `product_host`
  },
  // other setting...

2. Deploy

a. npm script

sudo npm run project-deploy

b. Deploy by command line

cd release
sudo ./build.sh
sudo ./deploy.sh

3. check container is running

sudo docker ps | grep thingsboard-sendbox

you can see the container is runninng like as follow.

image

4. API test

you can reference Test the API can parse data

Customzied the docker

release/.env can control all the docker image config

# project build
OUTPUT_FOLDER=output # create folder in root path
OUTPUT_IMAGE_FILE_NAME=thingsboard-sendbox

# deploy
DEPLOY_VERSION=v0.2.0-beta
DEPLOY_PORT=8000
SERVER_LISTEN=8000 # must equal to the "config.json" at root folder

# docker
IMAGE_NAME=thingsboard-sendbox
CONTAINER_NAME=thingsboard-sendbox