Skip to content

Commit

Permalink
Fix relay re-activating
Browse files Browse the repository at this point in the history
  • Loading branch information
martenmatrix committed Dec 23, 2021
1 parent 73f48ff commit 0c6bf27
Show file tree
Hide file tree
Showing 25 changed files with 152 additions and 121 deletions.
24 changes: 5 additions & 19 deletions backend/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ const showOnDisplay = require('./raspberry').showOnDisplay;
const activatePump = require('./raspberry').activatePump;
const checkPassword = require('./misc').checkPassword;
const getTask = require('./currentTask').getTask;
const pumpsJSONPATH = './data/pumps.json';
const path = require('path');
const pumpsJSONPATH = path.resolve(__dirname, '../data/pumps.json');

const result = dotenv.config({
path: './.env'
path: path.resolve(__dirname, '../.env')
});
if (result.error) {
throw result.error;
Expand Down Expand Up @@ -94,29 +95,14 @@ app.post('/password', async (req, res) => {
res.send(response);
});


let lastRequests = [
Promise.resolve(),
Promise.resolve(),
Promise.resolve(),
Promise.resolve(),
Promise.resolve(),
Promise.resolve(),
Promise.resolve(),
Promise.resolve(),
];

app.post('/startPump', async (req, res) => {
res.status(200);
res.send({success: true});
const pumpID = req.body.id;
const timeInMs = req.body.time;

const response = await Promise.race([lastRequests[pumpID - 1], 'loading']);
if (response === 'loading') return;

const promise = activatePump(pumpID, timeInMs);
lastRequests[pumpID - 1] = promise;
const newTime = parseFloat(timeInMs);
activatePump(pumpID, newTime);
});

// get ip with -hostname I
Expand Down
3 changes: 2 additions & 1 deletion backend/databaseHandler.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const sqlite3 = require('sqlite3').verbose();
const drinksDatabase = new sqlite3.Database('./data/database.db', (err) => {
const path = require('path');
const drinksDatabase = new sqlite3.Database(path.resolve(__dirname, '../data/database.db'), (err) => {
if (err) {
console.error(err.message);
} else {
Expand Down
3 changes: 2 additions & 1 deletion backend/misc.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const ingredientsJSONPATH = './data/ingredients.json';
const fs = require('fs');
const path = require('path');
const ingredientsJSONPATH = path.resolve(__dirname, '../data/ingredients.json');

function checkPassword(password) {
const response = {
Expand Down
66 changes: 45 additions & 21 deletions backend/raspberry.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,57 @@
const gpio = require('onoff').Gpio;

//index +1 stands for pumpid
const relays = [
new gpio(11, 'out'),
new gpio(13, 'out'),
new gpio(19 , 'out'),
new gpio(17, 'out'),
new gpio(27, 'out'),
new gpio(22, 'out'),
new gpio(10, 'out'),
new gpio(9, 'out'),
]
let relays;
try {
relays = [
new gpio(11, 'high'),
new gpio(13, 'high'),
new gpio(19 , 'high'),
new gpio(17, 'high'),
new gpio(27, 'high'),
new gpio(22, 'high'),
new gpio(10, 'high'),
new gpio(9, 'high'),
]
} catch (e) {
console.error(e);
}

function showOnDisplay(text, removeAfter = null) {

}



let lastTimeoutID = [
null,
null,
null,
null,
null,
null,
null,
null,
];

async function activatePump(pumpID, activateFor) {
const pumpArrayIndex = pumpID - 1;
const relay = relays[pumpArrayIndex];
relay.writeSync(1);
await new Promise((resolve, reject) => {
setTimeout(() => {
resolve();
}, Math.floor(activateFor));
});
relay.writeSync(0);
relay.unexport();
return true;
try {
const pumpArrayIndex = pumpID - 1;
const relay = relays[pumpArrayIndex];
if (!lastTimeoutID[pumpArrayIndex]) relay.writeSync(0);
await new Promise((resolve, reject) => {
clearTimeout(lastTimeoutID[pumpArrayIndex]);
lastTimeoutID[pumpArrayIndex] = setTimeout(() => {
lastTimeoutID[pumpArrayIndex] = null;
relay.writeSync(1);
resolve();
}, Math.floor(activateFor));
});
return true;
} catch (e) {
console.error(e);
return false;
}
}

module.exports.showOnDisplay = showOnDisplay;
Expand Down
11 changes: 6 additions & 5 deletions backend/routes/drinks.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
const path = require('path');

const DrinksDatabase = require('../databaseHandler');
const checkPassword = require('../misc').checkPassword;
const getCategoryOfIngredient = require('../misc').getCategoryOfIngredient;
const activatePump = require('../raspberry').activatePump;
const setTask = require('../currentTask').setTask;
const getTask = require('../currentTask').getTask;
const ingredientsJSONPATH = './data/ingredients.json';
const pumpsJSONPATH = './data/pumps.json';
const ingredientsJSONPATH = path.resolve(__dirname, '../../data/ingredients.json');
const pumpsJSONPATH = path.resolve(__dirname, '../../data/pumps.json');

const express = require('express');
const fs = require('fs');
Expand Down Expand Up @@ -220,14 +222,12 @@ router.post('/make', async (req, res) => {
const ingredients = await DrinksDatabase.getIngredients(idOfDrink);
const notAdded = [];
await Promise.all(ingredients.map(async (ingredient) => {

const pumpID = await getPumpWithIngredient(ingredient.ingredient);
if (pumpID === -1) {
notAdded.push(ingredient);
return;
return Promise.resolve();
}

console.log(ingredient);
const unit = ingredient.unitOfMeasurement;
const amount = ingredient.amountOfIngredient;

Expand All @@ -237,6 +237,7 @@ router.post('/make', async (req, res) => {

const activationTimeInSeconds = mlToDispense / pumpRateMlPerSecond;
const activationTime = activationTimeInSeconds * 1000;
console.log({mlToDispense, pumpRateMlPerSecond, activationTimeInSeconds, activationTime});
await activatePump(pumpID, activationTime);
}));
res.status(200);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ const showOnDisplay = require('./raspberry').showOnDisplay;
const activatePump = require('./raspberry').activatePump;
const checkPassword = require('./misc').checkPassword;
const getTask = require('./currentTask').getTask;
const pumpsJSONPATH = './data/pumps.json';
const path = require('path');
const pumpsJSONPATH = path.resolve(__dirname, '../data/pumps.json');

const result = dotenv.config({
path: './.env'
path: path.resolve(__dirname, '../.env')
});
if (result.error) {
throw result.error;
Expand Down Expand Up @@ -94,29 +95,14 @@ app.post('/password', async (req, res) => {
res.send(response);
});


let lastRequests = [
Promise.resolve(),
Promise.resolve(),
Promise.resolve(),
Promise.resolve(),
Promise.resolve(),
Promise.resolve(),
Promise.resolve(),
Promise.resolve(),
];

app.post('/startPump', async (req, res) => {
res.status(200);
res.send({success: true});
const pumpID = req.body.id;
const timeInMs = req.body.time;

const response = await Promise.race([lastRequests[pumpID - 1], 'loading']);
if (response === 'loading') return;

const promise = activatePump(pumpID, timeInMs);
lastRequests[pumpID - 1] = promise;
const newTime = parseFloat(timeInMs);
activatePump(pumpID, newTime);
});

// get ip with -hostname I
Expand Down
34 changes: 0 additions & 34 deletions buildBackend/backend/backend/raspberry.js

This file was deleted.

File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const sqlite3 = require('sqlite3').verbose();
const drinksDatabase = new sqlite3.Database('./data/database.db', (err) => {
const path = require('path');
const drinksDatabase = new sqlite3.Database(path.resolve(__dirname, '../data/database.db'), (err) => {
if (err) {
console.error(err.message);
} else {
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const ingredientsJSONPATH = './data/ingredients.json';
const fs = require('fs');
const path = require('path');
const ingredientsJSONPATH = path.resolve(__dirname, '../data/ingredients.json');

function checkPassword(password) {
const response = {
Expand Down
File renamed without changes.
58 changes: 58 additions & 0 deletions buildBackend/backend/raspberry.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
const gpio = require('onoff').Gpio;

//index +1 stands for pumpid
let relays;
try {
relays = [
new gpio(11, 'high'),
new gpio(13, 'high'),
new gpio(19 , 'high'),
new gpio(17, 'high'),
new gpio(27, 'high'),
new gpio(22, 'high'),
new gpio(10, 'high'),
new gpio(9, 'high'),
]
} catch (e) {
console.error(e);
}

function showOnDisplay(text, removeAfter = null) {

}



let lastTimeoutID = [
null,
null,
null,
null,
null,
null,
null,
null,
];

async function activatePump(pumpID, activateFor) {
try {
const pumpArrayIndex = pumpID - 1;
const relay = relays[pumpArrayIndex];
if (!lastTimeoutID[pumpArrayIndex]) relay.writeSync(0);
await new Promise((resolve, reject) => {
clearTimeout(lastTimeoutID[pumpArrayIndex]);
lastTimeoutID[pumpArrayIndex] = setTimeout(() => {
lastTimeoutID[pumpArrayIndex] = null;
relay.writeSync(1);
resolve();
}, Math.floor(activateFor));
});
return true;
} catch (e) {
console.error(e);
return false;
}
}

module.exports.showOnDisplay = showOnDisplay;
module.exports.activatePump = activatePump;
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
const path = require('path');

const DrinksDatabase = require('../databaseHandler');
const checkPassword = require('../misc').checkPassword;
const getCategoryOfIngredient = require('../misc').getCategoryOfIngredient;
const activatePump = require('../raspberry').activatePump;
const setTask = require('../currentTask').setTask;
const getTask = require('../currentTask').getTask;
const ingredientsJSONPATH = './data/ingredients.json';
const pumpsJSONPATH = './data/pumps.json';
const ingredientsJSONPATH = path.resolve(__dirname, '../../data/ingredients.json');
const pumpsJSONPATH = path.resolve(__dirname, '../../data/pumps.json');

const express = require('express');
const fs = require('fs');
Expand Down Expand Up @@ -220,14 +222,12 @@ router.post('/make', async (req, res) => {
const ingredients = await DrinksDatabase.getIngredients(idOfDrink);
const notAdded = [];
await Promise.all(ingredients.map(async (ingredient) => {

const pumpID = await getPumpWithIngredient(ingredient.ingredient);
if (pumpID === -1) {
notAdded.push(ingredient);
return;
return Promise.resolve();
}

console.log(ingredient);
const unit = ingredient.unitOfMeasurement;
const amount = ingredient.amountOfIngredient;

Expand All @@ -237,6 +237,7 @@ router.post('/make', async (req, res) => {

const activationTimeInSeconds = mlToDispense / pumpRateMlPerSecond;
const activationTime = activationTimeInSeconds * 1000;
console.log({mlToDispense, pumpRateMlPerSecond, activationTimeInSeconds, activationTime});
await activatePump(pumpID, activationTime);
}));
res.status(200);
Expand Down
4 changes: 3 additions & 1 deletion buildBackend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
"startFrontend": "cd ./frontend && npm run start",
"startBackend": "node ./backend/app",
"start": "npm run startFrontend & npm run startBackend",
"build": "rm -f -r ./buildBackend && mkdir -p ./buildBackend/backend && cp -r ./backend ./buildBackend/backend && cp -t ./buildBackend .env package.json package-lock.json"
"build": "rm -f -r ./buildBackend && mkdir -p ./buildBackend/backend && cp -r ./backend/* ./buildBackend/backend && cp -t ./buildBackend .env package.json package-lock.json",
"uploadBackend": "rsync --recursive --progress ./buildBackend/ [email protected]:~/Desktop/backend-server",
"uploadFrontend": "rsync --recursive --progress ./frontend/build/ [email protected]:var/www/html"
},
"author": "",
"license": "ISC",
Expand Down
Binary file modified data/database.db
Binary file not shown.
2 changes: 1 addition & 1 deletion data/pumps.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"pumps":[{"id":"1","select":"Passion fruit"},{"id":"2","select":"Orange"},{"id":"3","select":"Ginger Ale"},{"id":"4","select":"Coca Cola"},{"id":"5","select":"Vodka"},{"id":"6","select":"Vodka"},{"id":"7","select":"Orange"},{"id":"8","select":"Orange"}]}
{"pumps":[{"id":"1","select":"Passion fruit"},{"id":"2","select":"Orange"},{"id":"3","select":"Ginger Ale"},{"id":"4","select":"Coca Cola"},{"id":"5","select":"Vodka"},{"id":"6","select":"Vodka"},{"id":"7","select":"Orange"},{"id":"8","select":"Ginger Ale"}]}
Loading

0 comments on commit 0c6bf27

Please sign in to comment.