Skip to content

Commit

Permalink
Merge pull request #1 from chpro/feature/webserver
Browse files Browse the repository at this point in the history
added webserver to provide reset, on, off, config webservices
  • Loading branch information
chpro committed Aug 21, 2023
2 parents aa3bd69 + 917a4ce commit e7fa4b7
Show file tree
Hide file tree
Showing 6 changed files with 663 additions and 9 deletions.
8 changes: 5 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ FROM node:20-alpine

# Create app directory
WORKDIR /usr/src/app
COPY package*.json ./

RUN npm install
# Bundle app source
COPY heatingrodcontrol.js .

CMD [ "node", "heatingrodcontrol.js" ]
COPY heatingrodcontrol*.js ./
EXPOSE 3000
CMD [ "node", "heatingrodcontrolws.js" ]
28 changes: 24 additions & 4 deletions heatingrodcontrol.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
if (process.env.DRY_RUN) {
DRY_RUN = true;
}

if (typeof DRY_RUN === 'undefined') {
DRY_RUN = false;
} else {
Expand Down Expand Up @@ -31,17 +35,20 @@ const CONFIG = {
timerPeriodOffLowEnergy: Number(process.env.TIMER_PERIOD_OFF_LOW_ENERGY) || 10 * MINUTE,
timerPeriodOffHighTemperature: Number(process.env.TIMER_PERIOD_OFF_HIGH_TEMPERATURE) || 60 * MINUTE,
timerPeriodOffNight: Number(process.env.TIMER_PERIOD_OFF_NIGHT) || 60 * MINUTE,
timerPeriodManually: Number(process.env.TIMER_PERIOD_MANUALLY) || 60 * MINUTE,
};

console.log(new Date(), "CONFIG: ", CONFIG)

const SWITCH_STATUS = {
ON_MANUALLY: {position: true, status: 4, message: "On due to manual intervention", timerPeriod: CONFIG.timerPeriodManually},
ON_FALLBACK: {position: true, status: 3, message: "On due to no value for energy production was available", timerPeriod: CONFIG.timerPeriodOnFallback},
ON_LOW_TEMPERATURE: {position: true, status: 2, message: "On due to low water temperature", timerPeriod: CONFIG.timerPeriodOnLowTemperature},
ON_ENERGY: {position: true, status: 1, message: "On due to excess energy", timerPeriod: CONFIG.timerPeriodOnEnergy},
OFF_LOW_ENERGY: {position: false, status: 0, message: "Off due to not enough energy production", timerPeriod: CONFIG.timerPeriodOffLowEnergy},
OFF_HIGH_TEMPERATURE: {position: false, status: -1, message: "Off due to high water temperature", timerPeriod: CONFIG.timerPeriodOffHighTemperature},
OFF_NIGHT: {position: false, status: -2, message: "Off due to time resitrected to day hours", timerPeriod: CONFIG.timerPeriodOffNight},
OFF_MANUALLY: {position: false, status: -3, message: "Off due to manual intervention", timerPeriod: CONFIG.timerPeriodManually},
};

const INFLUX_WATER_TEMPERATURE_LAST = {url: CONFIG.influxBaseUrl + '/query?pretty=true&db=prometheus&q=SELECT last("value") FROM "autogen"."eta_buffer_temperature_sensor_top_celsius" WHERE time >= now() - 5m and time <= now()'};
Expand Down Expand Up @@ -139,9 +146,7 @@ function update() {
HTTP.get(INFLUX_WATER_TEMPERATURE_LAST.url, INFLUX_REQUEST_HEADER, function(result) {
let waterTemperature = getValue(result);
let switchStatus = determineNewSwitchStatus(gridUsageMean, gridUsageLast, waterTemperature, switchOn);
sendStatusChange(switchStatus);
setSwitch(switchStatus);
updateTimer(switchStatus);
setNewSwitchStatus(switchStatus);
});
});
});
Expand Down Expand Up @@ -195,6 +200,11 @@ function setSwitch(switchStatus) {
* @param {SWITCH_STATUS} switchStatus the new status of the switch which is transmitted as json to influx db
*/
function sendStatusChange(switchStatus) {
if(DRY_RUN) {
console.log(new Date(), "send status change", switchStatus);
return;
}

const jsonDataString = JSON.stringify(switchStatus);

// HTTP request options
Expand Down Expand Up @@ -234,6 +244,16 @@ function sendStatusChange(switchStatus) {
request.end();
}

/**
* Sends a status update to telegraf to write it into influx db
* @param {SWITCH_STATUS} switchStatus the new status to be set
*/
function setNewSwitchStatus(switchStatus) {
sendStatusChange(switchStatus);
setSwitch(switchStatus);
updateTimer(switchStatus);
}

/**
*
* @param {Number} wattGridUsageMean If null ON_FALLBACK will be activated durring day hours
Expand Down Expand Up @@ -301,4 +321,4 @@ if (!DRY_RUN) {
}


module.exports = {determineNewSwitchStatus, CONFIG, SWITCH_STATUS, isDay, isNight, setSwitch, HTTP, switch0}
module.exports = {update, determineNewSwitchStatus, setNewSwitchStatus, CONFIG, SWITCH_STATUS, isDay, isNight, setSwitch, HTTP, switch0}
35 changes: 35 additions & 0 deletions heatingrodcontrolws.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
const hrc = require('./heatingrodcontrol')
const express = require('express')
const cors = require('cors')
const ws = express()
const port = 3000


var corsOptions = {
origin: 'http://tig:3000',
credentials: true,
}
ws.use(cors(corsOptions))

ws.get('/config', (req, res) => {
res.json({"config": hrc.CONFIG, "status": hrc.SWITCH_STATUS})
})

ws.post('/reset', (req, res) => {
hrc.update()
res.send()
})

ws.post('/on', (req, res) => {
hrc.setNewSwitchStatus(hrc.SWITCH_STATUS.ON_MANUALLY)
res.send()
})

ws.post('/off', (req, res) => {
hrc.setNewSwitchStatus(hrc.SWITCH_STATUS.OFF_MANUALLY)
res.send()
})

ws.listen(port, () => {
console.log(new Date(), `Heatingrod webserver is listening on port ${port}`)
})
Loading

0 comments on commit e7fa4b7

Please sign in to comment.