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:
30
.devcontainer/devcontainer.json
Normal file
30
.devcontainer/devcontainer.json
Normal 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"
|
||||
]
|
||||
}
|
||||
@@ -18,7 +18,9 @@ ENV NODE_ENV development-dockerfile
|
||||
# USING BASE_IMAGE
|
||||
FROM node:12-alpine
|
||||
COPY --from=BASE_IMAGE /app /
|
||||
CMD npm start
|
||||
# CMD npm start
|
||||
CMD ["npm", "run", "start:debug"]
|
||||
|
||||
EXPOSE 2080
|
||||
# CMD [“dumb-init”, “node”, dht.js”]
|
||||
# CMD ["npm","run", "pm2"]
|
||||
@@ -3,6 +3,7 @@ version: '3.3'
|
||||
services:
|
||||
node-app:
|
||||
build: .
|
||||
command: npm run start:debug
|
||||
environment:
|
||||
- DATABASE_HOST=node-mysql
|
||||
- DATABASE_NAME=iot
|
||||
@@ -10,13 +11,13 @@ services:
|
||||
- DATABASE_PASS=mitko2021
|
||||
- GARDEN_IP=0.0.0.0
|
||||
# - GARDEN_IP=192.168.0.100
|
||||
- MQTT_HOST=node-mqtt
|
||||
- MQTT_HOST=192.168.0.10
|
||||
- MQTT_PORT_HTTP=1883
|
||||
- MQTT_PORT_HTTPS=8444
|
||||
- MQTT_ESP_NAME=Garden
|
||||
depends_on:
|
||||
- node-mysql
|
||||
- node-mqtt
|
||||
#- node-mqtt
|
||||
restart: always
|
||||
ports:
|
||||
- 2081:2080
|
||||
@@ -32,13 +33,13 @@ services:
|
||||
volumes:
|
||||
- iotdbdata:/var/lib/mysql
|
||||
# for mitko:mqtt
|
||||
node-mqtt:
|
||||
image: eclipse-mosquitto
|
||||
command: mosquitto -c /mosquitto-no-auth.conf
|
||||
hostname: node-mqtt
|
||||
ports:
|
||||
- 1885:1883
|
||||
- 10883:1883
|
||||
restart: always
|
||||
# node-mqtt:
|
||||
# image: eclipse-mosquitto
|
||||
# command: mosquitto -c /mosquitto-no-auth.conf
|
||||
# hostname: node-mqtt
|
||||
# ports:
|
||||
# - 1885:1883
|
||||
# - 10883:1883
|
||||
# restart: always
|
||||
volumes:
|
||||
iotdbdata:
|
||||
@@ -6,7 +6,9 @@
|
||||
"scripts": {
|
||||
"pm2": "pm2 start ecosystem.config.js --no-daemon",
|
||||
"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": {
|
||||
"type": "git",
|
||||
|
||||
@@ -98,6 +98,7 @@ app.get('/accontrol',
|
||||
|
||||
//app.listen(81);
|
||||
|
||||
try{
|
||||
var fs = require("fs");
|
||||
var https = require('https');
|
||||
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, () => {
|
||||
console.log('HTTP server listening on port 8443');
|
||||
});
|
||||
}catch(e){
|
||||
console.log("failed to start HTTPS: " + e.toString())
|
||||
}
|
||||
|
||||
@@ -7,7 +7,7 @@ const util = require('util');
|
||||
var app = require('express')();
|
||||
|
||||
var db = require('./db');
|
||||
var config = require('./setup_const');
|
||||
//var config = require('./setup_const');
|
||||
var passport = require('passport');
|
||||
var Strategy = require('passport-local').Strategy;
|
||||
var CookieStrategy = require('passport-cookie').Strategy;
|
||||
|
||||
27
src/mqtt.js
27
src/mqtt.js
@@ -2,7 +2,10 @@
|
||||
var db = require("./db");
|
||||
const env = process.env.NODE_ENV || "development";
|
||||
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_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);
|
||||
}
|
||||
}
|
||||
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) {
|
||||
console.log(topic + " > " + message);
|
||||
|
||||
Reference in New Issue
Block a user