Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.env
compose.yml
README.md
database
database
7 changes: 5 additions & 2 deletions .env.sample
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
DB_HOST="172.0.0.1"
DB_HOST="db"
DB_ROOT_PASSWORD="root_password"
DB_USER="openunitstate"
DB_PASSWORD=""
DB_NAME="openunitstate"
MQTT_URL="mqtt://broker.example.com:1883"
MQTT_CLIENT_ID="njs_1"
MQTT_USERNAME="ousbackendmaster"
MQTT_PASSWORD=""
MQTT_PUB_CLIENT_ID="njs_publisher"
MQTT_PUB_CLIENT_ID="njs_publisher"
ROOT_TOPIC="openunitstate"
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -137,3 +137,4 @@ dist
# Vite logs files
vite.config.js.timestamp-*
vite.config.ts.timestamp-*
database
6 changes: 6 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
FROM node:25
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
CMD ["npm", "start"]
22 changes: 11 additions & 11 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,9 @@ function parseMessage(topic, message) {

var meta = topic.replace(ROOT_TOPIC + "/", "").split("/")

var orgId = meta[0]
var chipId = meta[1]
var func = meta[2]
var orgId = '-1' // meta[0]
var chipId = meta[0]
var func = meta[1]
var payload = message.toString()

return {
Expand Down Expand Up @@ -142,10 +142,10 @@ async function pushUnitConfig(chipId) {
var name = await getUnitName(chipId)
var unitOrg = await getUnitOrg(chipId);
if (unitOrg != null) {
console.log("Setting name on", chipId, "mqtt message:", ROOT_TOPIC + "/" + unitOrg + "/" + chipId + "/config_name", name)
mqtt_client.publish(ROOT_TOPIC + "/" + unitOrg + "/" + chipId + "/config_name", name)
console.log("Setting status on", chipId, "mqtt message:", ROOT_TOPIC + "/" + unitOrg + "/" + chipId + "/config_status", status)
mqtt_client.publish(ROOT_TOPIC + "/" + unitOrg + "/" + chipId + "/config_status", status.toString())
console.log("Setting name on", chipId, "mqtt message:", ROOT_TOPIC + "/" + chipId + "/config_name", name)
mqtt_client.publish(ROOT_TOPIC + "/" + chipId + "/config_name", name)
console.log("Setting status on", chipId, "mqtt message:", ROOT_TOPIC + "/" + chipId + "/config_status", status)
mqtt_client.publish(ROOT_TOPIC + "/" + chipId + "/config_status", status.toString())
} else {
console.log(chipId, "unitOrg is null")
}
Expand Down Expand Up @@ -328,8 +328,8 @@ async function unlockUnit(chipId, unlockTime = 0) {
return;
}
unlockTime = unlockTime * 1000;
console.log("Unlocking", chipId, "mqtt message:", ROOT_TOPIC + "/" + unitOrg + "/" + chipId + "/unlocked_time", unlockTime)
mqtt_client.publish(ROOT_TOPIC + "/" + unitOrg + "/" + chipId + "/unlocked_time", unlockTime.toString())
console.log("Unlocking", chipId, "mqtt message:", ROOT_TOPIC + "/" + chipId + "/unlocked_time", unlockTime)
mqtt_client.publish(ROOT_TOPIC + "/" + chipId + "/unlocked_time", unlockTime.toString())
} else {
console.log("unlockUnit: Couldn't find org associated with chipId", chipId);
}
Expand All @@ -341,8 +341,8 @@ async function showMessage(chipId, message) {
var unitOrg = await getUnitOrg(chipId);
console.log(unitOrg)
if (unitOrg != null) {
console.log("Showing message", ROOT_TOPIC + "/" + unitOrg + "/" + chipId + "/quick_display_msg", message)
mqtt_client.publish(ROOT_TOPIC + "/" + unitOrg + "/" + chipId + "/quick_display_msg", message)
console.log("Showing message", ROOT_TOPIC + "/" + chipId + "/quick_display_msg", message)
mqtt_client.publish(ROOT_TOPIC + "/" + chipId + "/quick_display_msg", message)
}
});
}
Expand Down
39 changes: 39 additions & 0 deletions compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
name: openunitstate-backend
services:
db:
image: mariadb:latest
environment:
MYSQL_ROOT_PASSWORD: ${DB_ROOT_PASSWORD}
MYSQL_DATABASE: ${DB_NAME}
MYSQL_USER: ${DB_USER}
MYSQL_PASSWORD: ${DB_PASSWORD}
volumes:
- ./database:/var/lib/mysql
- ./sql_structure_openunitstate.sql:/docker-entrypoint-initdb.d/init.sql
healthcheck:
test: ["CMD", "healthcheck.sh", "--connect", "--innodb_initialized"]
timeout: 20s
retries: 10
restart: unless-stopped

app:
build: .
depends_on:
db:
condition: service_healthy
environment:
DB_HOST: db
DB_USER: ${DB_USER}
DB_PASSWORD: ${DB_PASSWORD}
DB_NAME: ${DB_NAME}
MQTT_URL: ${MQTT_URL}
MQTT_CLIENT_ID: ${MQTT_CLIENT_ID}
MQTT_USERNAME: ${MQTT_USERNAME}
MQTT_PASSWORD: ${MQTT_PASSWORD}
ROOT_TOPIC: ${ROOT_TOPIC}
restart: unless-stopped

networks:
default:
driver: bridge
name: openunitstate
25 changes: 25 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"name": "openunitstate-backend",
"version": "1.0.0",
"description": "Backend service for OpenUnitState",
"main": "app.js",
"scripts": {
"start": "node app.js",
"dev": "node app.js"
},
"dependencies": {
"mqtt": "^4.3.7",
"mysql": "^2.18.1"
},
"engines": {
"node": ">=12.0.0"
},
"keywords": [
"mqtt",
"mysql",
"iot",
"unit-state"
],
"author": "",
"license": "GPL-3.0-only"
}