diff --git a/public/images/Blue Lagoon.png b/public/images/Blue Lagoon.png deleted file mode 100644 index a546cca..0000000 Binary files a/public/images/Blue Lagoon.png and /dev/null differ diff --git a/public/images/Caipirinha.png b/public/images/Caipirinha.png deleted file mode 100644 index a261129..0000000 Binary files a/public/images/Caipirinha.png and /dev/null differ diff --git a/public/images/Cosmopolitan.png b/public/images/Cosmopolitan.png deleted file mode 100644 index 32dc501..0000000 Binary files a/public/images/Cosmopolitan.png and /dev/null differ diff --git a/public/images/Cubralibre.png b/public/images/Cubralibre.png deleted file mode 100644 index 1cb6b1e..0000000 Binary files a/public/images/Cubralibre.png and /dev/null differ diff --git a/public/images/Fanta.png b/public/images/Fanta.png deleted file mode 100644 index 56faeea..0000000 Binary files a/public/images/Fanta.png and /dev/null differ diff --git a/public/images/Gin Tonic.png b/public/images/Gin Tonic.png deleted file mode 100644 index 9b79df1..0000000 Binary files a/public/images/Gin Tonic.png and /dev/null differ diff --git a/public/images/Making_Drink.gif b/public/images/Making_Drink.gif deleted file mode 100644 index 9ebdb8e..0000000 Binary files a/public/images/Making_Drink.gif and /dev/null differ diff --git a/public/images/Malibu Beach.png b/public/images/Malibu Beach.png deleted file mode 100644 index 78ff35d..0000000 Binary files a/public/images/Malibu Beach.png and /dev/null differ diff --git a/public/images/Mezzo Mix.png b/public/images/Mezzo Mix.png deleted file mode 100644 index be5fe95..0000000 Binary files a/public/images/Mezzo Mix.png and /dev/null differ diff --git a/public/images/Mojito.png b/public/images/Mojito.png deleted file mode 100644 index 3ec43d6..0000000 Binary files a/public/images/Mojito.png and /dev/null differ diff --git a/public/images/Moscow Mule.png b/public/images/Moscow Mule.png deleted file mode 100644 index 465b52e..0000000 Binary files a/public/images/Moscow Mule.png and /dev/null differ diff --git a/public/images/Pina Colada.png b/public/images/Pina Colada.png deleted file mode 100644 index 163482f..0000000 Binary files a/public/images/Pina Colada.png and /dev/null differ diff --git a/public/images/Sex on the Beach.png b/public/images/Sex on the Beach.png deleted file mode 100644 index 9711fa2..0000000 Binary files a/public/images/Sex on the Beach.png and /dev/null differ diff --git a/public/images/Swimming Pool.png b/public/images/Swimming Pool.png deleted file mode 100644 index 04f86c1..0000000 Binary files a/public/images/Swimming Pool.png and /dev/null differ diff --git a/public/images/Tequila Sunrise.png b/public/images/Tequila Sunrise.png deleted file mode 100644 index 61db825..0000000 Binary files a/public/images/Tequila Sunrise.png and /dev/null differ diff --git a/public/images/Wodka Bull.png b/public/images/Wodka Bull.png deleted file mode 100644 index 045fa93..0000000 Binary files a/public/images/Wodka Bull.png and /dev/null differ diff --git a/public/images/Wodka Cola.png b/public/images/Wodka Cola.png deleted file mode 100644 index 530eb8b..0000000 Binary files a/public/images/Wodka Cola.png and /dev/null differ diff --git a/public/images/Wodka Fanta.png b/public/images/Wodka Fanta.png deleted file mode 100644 index b91d9d7..0000000 Binary files a/public/images/Wodka Fanta.png and /dev/null differ diff --git a/public/images/Wodka Lemon.png b/public/images/Wodka Lemon.png deleted file mode 100644 index a8d17f6..0000000 Binary files a/public/images/Wodka Lemon.png and /dev/null differ diff --git a/public/images/Wodka O.png b/public/images/Wodka O.png deleted file mode 100644 index 4d04285..0000000 Binary files a/public/images/Wodka O.png and /dev/null differ diff --git a/public/images/Zombie.png b/public/images/Zombie.png deleted file mode 100644 index ec50688..0000000 Binary files a/public/images/Zombie.png and /dev/null differ diff --git a/src/Utils.ts b/src/Utils.ts index 4e33f53..63fb74f 100644 --- a/src/Utils.ts +++ b/src/Utils.ts @@ -1,5 +1,6 @@ import * as dns from "dns"; -import * as ping from "net-ping"; +import * as fs from "fs"; +import * as https from 'https'; export class Utils { public static checkInternet(): Promise { @@ -20,4 +21,26 @@ export class Utils { }) } + static downloadImage(url, filepath) { + return new Promise((resolve, reject) => { + https.get(url, (res) => { + if (res.statusCode === 200) { + res.pipe(fs.createWriteStream(filepath)) + .on('error', reject) + .once('close', () => resolve(filepath)); + } else { + // Consume response data to free up memory + res.resume(); + reject(new Error(`Request Failed With a Status Code: ${res.statusCode}`)); + + } + }); + }); + } + + static deleteImage( id ) + { + fs.unlinkSync("./public/images/" + id + ".png"); + } + } \ No newline at end of file diff --git a/src/database/Drink.ts b/src/database/Drink.ts index a04fbfa..5c92895 100644 --- a/src/database/Drink.ts +++ b/src/database/Drink.ts @@ -3,8 +3,7 @@ import * as mongoose from "mongoose"; export const DrinkSchema = new mongoose.Schema({ name: {type: String, required: true}, - ingredients: [{type: {type: mongoose.Types.ObjectId, ref: "Ingredient", required: true}, amount: { type: Number } }], - category: String + ingredients: [{type: {type: mongoose.Types.ObjectId, ref: "Ingredient", required: true}, amount: { type: Number } }] }); const Drink = mongoose.model('Drink', DrinkSchema); diff --git a/src/database/IDrink.ts b/src/database/IDrink.ts index ca58dcc..6ef1149 100644 --- a/src/database/IDrink.ts +++ b/src/database/IDrink.ts @@ -1,5 +1,4 @@ import {IIngredient} from "./IIngredient"; -import {Category} from "../Category"; import * as mongoose from "mongoose"; export interface IDrink extends mongoose.Document { @@ -9,8 +8,5 @@ export interface IDrink extends mongoose.Document { // Ingredients ingredients: { type: IIngredient, amount: Number }[]; - // Category of the drink - category: Category; - } \ No newline at end of file diff --git a/src/iTender.ts b/src/iTender.ts index 55560cf..f95906f 100644 --- a/src/iTender.ts +++ b/src/iTender.ts @@ -19,6 +19,7 @@ import {clearInterval} from "timers"; import {RejectReason} from "./RejectReason"; import {Settings} from "./Settings"; import GPIO from "rpi-gpio"; +import axios from "axios"; const log = debug("itender:station"); const mixLog = debug("itender:mix"); @@ -56,6 +57,7 @@ export class iTender { this._status = status; if (WebSocketHandler.ws && WebSocketHandler.ws.readyState == 1) WebSocketHandler.sendStatus().then().catch(console.error); + log("Status is now " + status); } static get status(): iTenderStatus { @@ -138,11 +140,11 @@ export class iTender { let timers: NodeJS.Timeout[] = []; for (let x of job.amounts) { // Start pump here - await GPIO.setup(x.container.pumpPin,GPIO.DIR_OUT); + await GPIO.setup(x.container.pumpPin, GPIO.DIR_OUT); await GPIO.write(x.container.pumpPin, true); let waitTime = (Settings.get("secondsPer100ml") as number) / 100 * x.amount * 1000; - mixLog( `Starting output of pump ${x.container.pumpPin}` ); + mixLog(`Starting output of pump ${x.container.pumpPin}`); //mixLog(x.ingredient + " takes " + (waitTime / 1000) + "s for " + x.amount + "ml"); let timer = setTimeout(() => { // Stop pump here @@ -154,7 +156,7 @@ export class iTender { if (timers[i] != timer) arr.push(timers[i]); } - mixLog( `Stopping output of pump ${x.container.pumpPin}` ) + mixLog(`Stopping output of pump ${x.container.pumpPin}`) timers = arr; }, waitTime); @@ -287,11 +289,97 @@ export class iTender { static refreshFromServer(): Promise { return new Promise(async (resolve, reject) => { iTender.setStatus(iTenderStatus.DOWNLOADING) + log("Refreshing drinks from server..."); + try { + const requestIngredients = await axios.get("https://itender.iif.li/api/ingredients"); + let serverIngredients = requestIngredients.data as IIngredient[]; + log("Got " + serverIngredients.length + " ingredients from server"); + + let localIngredients = await Ingredient.find(); + for (let local of localIngredients) { + let found = false; + for (let remote of serverIngredients) { + if (local.name == remote.name) { + found = true; + break; + } + } + + if (!found) + { + await Ingredient.deleteOne({"_id": local._id}); + for( let c of (await Container.find( {content: local._id } )) ) + { + c.content = undefined; + c.save(); + } + } + + } + for (let remote of serverIngredients) { + let ingredient = await Ingredient.findOne({name: remote.name}); + if (!ingredient) + ingredient = new Ingredient(); + + ingredient.name = remote.name; + await ingredient.save(); + } + + + + const requestDrinks = await axios.get("https://itender.iif.li/api/drinks"); + let serverDrinks = requestDrinks.data as IDrink[]; + log("Got " + serverDrinks.length + " drinks from server"); + + + let localDrinks = await Drink.find(); + for (let local of localDrinks) { + let found = false; + for (let remote of serverDrinks) { + if (local.name == remote.name) { + found = true; + break; + } + } + + if (!found) + { + Utils.deleteImage(local._id); + await Drink.deleteOne({"_id": local._id}); + } + + } + + + for (let remote of serverDrinks) { + let drink = await Drink.findOne({name: remote.name}); + if (!drink) + drink = new Drink(); + + drink.name = remote.name; + drink.ingredients = remote.ingredients; + + await drink.save(); + + + // Download thumbnail + Utils.downloadImage("https://itender.iif.li/images/" + remote._id + ".png", "./public/images/" + drink._id + ".png").then(filepath => { + log("Drink " + remote.name + "'s Thumbnail downloaded to " + filepath); + }).catch(e => { + log("Drink " + remote.name + " failed to download thumbnail!\n" + e); + }); + + } + + } catch (e) { + console.error(e); + } iTender.setStatus(iTenderStatus.READY); resolve(); + iTender.refreshDrinks(); }); } diff --git a/src/routes/ws/websocketRoute.ts b/src/routes/ws/websocketRoute.ts index 5dc4b3b..ae00efd 100644 --- a/src/routes/ws/websocketRoute.ts +++ b/src/routes/ws/websocketRoute.ts @@ -124,7 +124,7 @@ router.ws('/', async (ws, req, next) => { break; } case RequestType.CONTAINERS: { - WebSocketHandler.answerRequest(msg.data["type"] as RequestType, (await Container.find().sort({"slot": 1}))); + WebSocketHandler.answerRequest(msg.data["type"] as RequestType, (await Container.find().sort({"slot": 1}).populate("content"))); break; } case RequestType.INGREDIENTS: {