Skip to content

Commit

Permalink
feat: cria variáveis de ambiente
Browse files Browse the repository at this point in the history
  • Loading branch information
larissaperinoto committed Jul 9, 2024
1 parent cf55e82 commit a4895dd
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 15 deletions.
5 changes: 5 additions & 0 deletions server/.env-exemple
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
SOCKET_PORT=3001
DETECTION_INTERVAL=4000
PER_KG_BUFFET_PRICE=59.90
OPEN_BUFFET_PRICE=29.90
SCALE_1_PATH=/dev/ttyScale
9 changes: 6 additions & 3 deletions server/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
import { Buffet } from "./services/Buffet";
import { Meal } from "./services/Meal";
import { Scale, Units } from "./services/Scale";
import * as dotenv from "dotenv";

dotenv.config();

const scale_1 = new Scale(
{
path: "/dev/ttyScale",
path: process.env.SCALE_1_PATH ?? "/dev/ttyScale",
baudRate: 57600,
autoOpen: true,
},
Units.G
);

const perKgPrice = 59.9;
const openPrice = 29.9;
const perKgPrice = +(process.env.PER_KG_BUFFET_PRICE as string);
const openPrice = +(process.env.OPEN_BUFFET_PRICE as string);

const buffet_1 = new Buffet(perKgPrice, openPrice, [scale_1]);

Expand Down
31 changes: 19 additions & 12 deletions server/src/services/Meal.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,29 @@
import { Buffet } from "./Buffet";
import { SocketService } from "./Socket";

enum BuffetCategories {
PER_KG = "Buffet por quilo",
OPEN = "Buffet livre",
}

export class Meal {
private socket = new SocketService(3001);
private socket = new SocketService(
+(process.env.SOCKET_PORT as string) ?? 3001
);

constructor(private buffet: Buffet) {}

public detectMeal() {
const orderSeconds = 4000;
const time = 300;
const orderSeconds = +(process.env.DETECTION_INTERVAL as string) ?? 4000;
const interval = 300;
setInterval(() => {
for (const scale of this.buffet.scaleList) {
for (const scale of this.buffet.getScaleList()) {
const currentWeight = scale.getCurrentWeight();
let previousWeight = scale.getPreviousWeight();
let measuredSeconds = scale.getMeasuredSeconds();

if (previousWeight === currentWeight) {
measuredSeconds = measuredSeconds += time;
measuredSeconds = measuredSeconds += interval;
} else {
measuredSeconds = 0;
}
Expand Down Expand Up @@ -46,16 +53,16 @@ export class Meal {
scale.setMeasuredSeconds(0);
}
}
}, time);
}, interval);
}

private generateTotal(weight: number) {
let total = 0;

const totalPerKg = weight * this.buffet.perKgPrice;
const totalPerKg = weight * this.buffet.getPerKgPrice();

if (totalPerKg > this.buffet.openPrice) {
total = this.buffet.openPrice;
if (totalPerKg > this.buffet.getOpenPrice()) {
total = this.buffet.getOpenPrice();
} else {
total = totalPerKg;
}
Expand All @@ -70,10 +77,10 @@ export class Meal {
private generateBuffetType(weight: number) {
let buffetType = "";

if (weight * this.buffet.perKgPrice > this.buffet.openPrice) {
buffetType = "Buffet livre";
if (weight * this.buffet.getPerKgPrice() > this.buffet.getOpenPrice()) {
buffetType = BuffetCategories.OPEN;
} else {
buffetType = "Buffet por quilo";
buffetType = BuffetCategories.PER_KG;
}

return buffetType;
Expand Down

0 comments on commit a4895dd

Please sign in to comment.