Skip to content

NPPElement/visiobas-gateway

Repository files navigation

GitHub tag (latest SemVer) Run Checks Checked with mypy Code style: black License

VisioBAS-Gateway

Polling controllers on various protocols for the VisioBAS system.

Contents

  1. Features
  2. API
  3. Installation
  4. Setting
  5. Launch/Update
  6. Develop
  7. Remove

Features

  • Gateway provides the opportunity to poll devices from various vendors, using various protocols. Standardization based on BACnet protocol.
  • Supported protocols: BACnet, ModbusTCP, ModbusRTU, ModbusRTUoverTCP , SUNAPI.
  • Clients: HTTP, MQTT.
  • JSON-RPC 2.0 API (over HTTP and MQTT) to control devices and request info about device.
  • For processing events related to object's properties you can use verifier class.
  • Devices and clients periodically updates.

API

JSON-RPC 2.0 API Available on http://host:port/json-rpc. Also you may use it by MQTT (provide topics for subscribe to use).

curl --header "Content-Type: application/json" \
  --request POST \
  --data '{"jsonrpc":"2.0","method":"writeSetPoint","params":{"device_id":"35","object_type":"2","object_id":"1","property":"85","priority":"10","index":"-1","tag":"9","value":"40"},"id":""}' \
  http://127.0.0.1:7070/json-rpc

Installation

curl -sSL https://raw.githubusercontent.com/NPPElement/visiobas-gateway/main/run/install.sh | bash -

It will update the system and download gtw_installer.py to install VisioBAS Gateway. Please follow the output instructions to install VisioBAS Gateway in the preferred way.

Setting

Configuration

Application configures via environment variables. Environment variables are provided via .env file. Paths to .env file are specified in docker-compose.yaml.

Configuration can be changed in file:

Objects configuration

/docs contains JSON-schema definitions for used pydantic models.

Setting Serial ports

sudo apt-get install minicom  # install minicom
dmesg | grep tty  # show ports
sudo minicom -s # launch minicom
# Setup serial ports from minicom, then save as dfl.

sudo nano /etc/udev/rules.d/99-serial.rules
# then write line: KERNEL=="ttyUSB[0-9]*",MODE="0666"

# Explanations: https://www.losant.com/blog/how-to-access-serial-devices-in-docker

# Before launch visiobas_gateway, ensure user in the `dialout` group
sudo usermod -a -G dialout username # add to `dialout` group
id username # check user\group info

Launch/Update

To launch on the same machine with VisioBAS system - add in docker-compose.yaml the following network settings:

    networks:
      - backend

and

networks:
  backend: # your network name
    driver: bridge

Scripts for common actions available:

. run/logs_clear.sh  # Clear logs

. run/update.sh  # Git pull + build + launch

Build and copy docker image

cd /opt/visiobas-gateway

docker build -t visiobas-gateway .

# docker save -o <path for generated tar file> <image name>
docker save -o /opt/gtw-latest-image.tar.gz visiobas-gateway:latest

# Copy via scp here
# Example: scp /opt/gtw-latest-image.tar.gz user@10.21.80.240:/opt

docker load -i <path to copied image file>

Develop

Setting up a Dev Environment

  1. Make sure you have Poetry installed and up to date.
  2. Make sure you have a supported Python version (e.g. 3.9) installed and accessible to Poetry (e.g. with pyenv.
  3. Use poetry install in the project directory to create a virtual environment with the relevant dependencies.
  4. Enter a poetry shell to make running commands easier.

Writing Code

  1. Write some code and make sure it's covered by unit tests. All unit tests are in the tests directory and the file structure should mirror the structure of the source code in the visiobas_gateway directory.
  2. When in a Poetry shell (poetry shell) run task check in order to run most of the same checks CI runs. This will auto-reformat the code, check type annotations, run unit tests, check code coverage, and lint the code.
  3. If writing support for a new protocol, regenerate the /docs with task docs. It will update JSON-schema definition for used pydantic models.

Remove

To clean docker:

sudo docker-compose down 
sudo docker images

sudo docker rmi -f [image_id]
# OR
sudo docker images -a | xargs -n 1 -I {} sudo docker rmi -f {}
# Delete all containers
sudo docker ps -a -q | xargs -n 1 -I {} sudo docker rm -f {}

# Remove all unused images, not just dangling ones
sudo docker image prune -a -f

# If deleting or stopping the container is hopeless
sudo systemctl daemon-reload
sudo systemctl restart docker