added devcontainer.

fix build error, deploy using docker-compose works,
misc changes
using global MQTT instead of local one to tap to already existin broker
This commit is contained in:
Dobromir Popov ONE
2023-05-14 21:40:57 +03:00
parent 145e202d24
commit 95ce6e8c87
7 changed files with 78 additions and 14 deletions

View File

@@ -0,0 +1,30 @@
// For format details, see https://aka.ms/devcontainer.json. For config options, see the
// README at: https://github.com/devcontainers/templates/tree/main/src/javascript-node
{
///"name": "Node.js",
// Or use a Dockerfile or Docker Compose file. More info: https://containers.dev/guide/dockerfile
///"image": "mcr.microsoft.com/devcontainers/javascript-node:0-20"
// Features to add to the dev container. More info: https://containers.dev/features.
// "features": {},
// Use 'forwardPorts' to make a list of ports inside the container available locally.
// "forwardPorts": [],
// Use 'postCreateCommand' to run commands after the container is created.
// "postCreateCommand": "yarn install",
// Configure tool-specific properties.
// "customizations": {},
// Uncomment to connect as root instead. More info: https://aka.ms/dev-containers-non-root.
// "remoteUser": "root"
"name": "garden.iot",
"dockerComposeFile": "docker-compose.yml",
"service": "node-app",
"workspaceFolder": "/workspace",
"appPort": 2081,
"extensions": [
"dbaeumer.vscode-eslint"
]
}

View File

@@ -18,7 +18,9 @@ ENV NODE_ENV development-dockerfile
# USING BASE_IMAGE # USING BASE_IMAGE
FROM node:12-alpine FROM node:12-alpine
COPY --from=BASE_IMAGE /app / COPY --from=BASE_IMAGE /app /
CMD npm start # CMD npm start
CMD ["npm", "run", "start:debug"]
EXPOSE 2080 EXPOSE 2080
# CMD [“dumb-init”, “node”, dht.js”] # CMD [“dumb-init”, “node”, dht.js”]
# CMD ["npm","run", "pm2"] # CMD ["npm","run", "pm2"]

View File

@@ -3,6 +3,7 @@ version: '3.3'
services: services:
node-app: node-app:
build: . build: .
command: npm run start:debug
environment: environment:
- DATABASE_HOST=node-mysql - DATABASE_HOST=node-mysql
- DATABASE_NAME=iot - DATABASE_NAME=iot
@@ -10,13 +11,13 @@ services:
- DATABASE_PASS=mitko2021 - DATABASE_PASS=mitko2021
- GARDEN_IP=0.0.0.0 - GARDEN_IP=0.0.0.0
# - GARDEN_IP=192.168.0.100 # - GARDEN_IP=192.168.0.100
- MQTT_HOST=node-mqtt - MQTT_HOST=192.168.0.10
- MQTT_PORT_HTTP=1883 - MQTT_PORT_HTTP=1883
- MQTT_PORT_HTTPS=8444 - MQTT_PORT_HTTPS=8444
- MQTT_ESP_NAME=Garden - MQTT_ESP_NAME=Garden
depends_on: depends_on:
- node-mysql - node-mysql
- node-mqtt #- node-mqtt
restart: always restart: always
ports: ports:
- 2081:2080 - 2081:2080
@@ -32,13 +33,13 @@ services:
volumes: volumes:
- iotdbdata:/var/lib/mysql - iotdbdata:/var/lib/mysql
# for mitko:mqtt # for mitko:mqtt
node-mqtt: # node-mqtt:
image: eclipse-mosquitto # image: eclipse-mosquitto
command: mosquitto -c /mosquitto-no-auth.conf # command: mosquitto -c /mosquitto-no-auth.conf
hostname: node-mqtt # hostname: node-mqtt
ports: # ports:
- 1885:1883 # - 1885:1883
- 10883:1883 # - 10883:1883
restart: always # restart: always
volumes: volumes:
iotdbdata: iotdbdata:

View File

@@ -6,7 +6,9 @@
"scripts": { "scripts": {
"pm2": "pm2 start ecosystem.config.js --no-daemon", "pm2": "pm2 start ecosystem.config.js --no-daemon",
"dev": "nodemon dht.js", "dev": "nodemon dht.js",
"start": "node dht.js" "startold": "node dht.js",
"start": "node index.js",
"start:debug": "node --inspect=0.0.0.0:9229 index.js"
}, },
"repository": { "repository": {
"type": "git", "type": "git",

View File

@@ -98,6 +98,7 @@ app.get('/accontrol',
//app.listen(81); //app.listen(81);
try{
var fs = require("fs"); var fs = require("fs");
var https = require('https'); var https = require('https');
var privateKey = fs.readFileSync('/etc/letsencrypt/live/iot.d-popov.com/privkey.pem', 'utf8'); var privateKey = fs.readFileSync('/etc/letsencrypt/live/iot.d-popov.com/privkey.pem', 'utf8');
@@ -108,3 +109,6 @@ var httpsServer = https.createServer(credentials, app);
httpsServer.listen(8443, () => { httpsServer.listen(8443, () => {
console.log('HTTP server listening on port 8443'); console.log('HTTP server listening on port 8443');
}); });
}catch(e){
console.log("failed to start HTTPS: " + e.toString())
}

View File

@@ -7,7 +7,7 @@ const util = require('util');
var app = require('express')(); var app = require('express')();
var db = require('./db'); var db = require('./db');
var config = require('./setup_const'); //var config = require('./setup_const');
var passport = require('passport'); var passport = require('passport');
var Strategy = require('passport-local').Strategy; var Strategy = require('passport-local').Strategy;
var CookieStrategy = require('passport-cookie').Strategy; var CookieStrategy = require('passport-cookie').Strategy;

View File

@@ -2,7 +2,10 @@
var db = require("./db"); var db = require("./db");
const env = process.env.NODE_ENV || "development"; const env = process.env.NODE_ENV || "development";
console.log("ENV: %s", env); console.log("ENV: %s", env);
const config = require(__dirname + "/../config/config.json")[env];
const path = require('path');
const configPath = path.join(__dirname, '/../config/config.json');
const config = require(configPath)[env];
console.log("MQTT: Checking config sources: process.env.MQTT_HOST: %s", process.env.MQTT_HOST); console.log("MQTT: Checking config sources: process.env.MQTT_HOST: %s", process.env.MQTT_HOST);
console.log("MQTT: Checking config sources: process.env.MQTT_PORT_HTTP: %s", process.env.MQTT_PORT_HTTP); console.log("MQTT: Checking config sources: process.env.MQTT_PORT_HTTP: %s", process.env.MQTT_PORT_HTTP);
@@ -169,6 +172,28 @@ mqtt_client.on("message", function (topic, message) {
console.log("MQTT> unexpected json payload at /Water/stop:" + message); console.log("MQTT> unexpected json payload at /Water/stop:" + message);
} }
} }
if(topic === EspDeviceName + "/ping"){
try {
console.log("MQTT> processing /ping message. Payload:" + context);
var data = context.replace(/^\{|\}$/g, '').split('|');
var ip = data[4].replace(/"/g, '');
console.log("got reporded garden controller IP: " + ip);
console.log("MQTT> /ping got us " + data.length + "data items");
config.gardenIP = ip;
// Make a copy of config and change that
let newConfig = Object.assign({}, config);
newConfig.gardenIP = ip;
// Write the new config back to the original file
fs.writeFileSync(configPath, JSON.stringify(newConfig, null, 2));
} catch (e) {
console.log("MQTT> error at /ping: " + e);
}
}
if (!handled) { if (!handled) {
console.log(topic + " > " + message); console.log(topic + " > " + message);