Skip to content

Commit

Permalink
Fix concurrency issue when posting characteristics and animation name
Browse files Browse the repository at this point in the history
  • Loading branch information
BinaryBrain committed Aug 18, 2024
1 parent 71c1c28 commit 8b38672
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
7 changes: 3 additions & 4 deletions client/src/views/LampPage.vue
Original file line number Diff line number Diff line change
Expand Up @@ -145,11 +145,10 @@ function onNewAnimation() {
}
function applyAnimation() {
axiosInstance.post("/lamp/animation", {
animation: currentConfig.selectedAnimation,
axiosInstance.post("/lamp/characteristics", {
characteristics: characteristics.array,
animation: currentConfig.selectedAnimation
});
axiosInstance.post("/lamp/characteristics", characteristics.array);
}
function onBrightnessChange(brightness: number) {
Expand Down
12 changes: 8 additions & 4 deletions server/lamp/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ export function initLamp() {
});

lamp.post("/characteristics", (req, res) => {
const characteristics: Characteristic[] = req.body;
const { characteristics, animation }: { characteristics: Characteristic[], animation: string } = req.body;
characteristics.forEach((c) => {
if (c.type === "color") {
if (c.value.type === "static") {
Expand All @@ -132,10 +132,11 @@ export function initLamp() {
}
}
});
currentCharacteristics[currentAnimation] = characteristics;

currentCharacteristics[animation] = characteristics;

startLamp();
res.send("OK");
changeAnimation(animation, res);
});

lamp.get("/animation", (req, res) => {
Expand All @@ -144,7 +145,10 @@ export function initLamp() {

lamp.post("/animation", (req, res) => {
const animation: string = req.body.animation;
changeAnimation(animation, res);
});

function changeAnimation(animation: string, res: express.Response) {
if (animation.toLowerCase() === "off") {
lampShouldStop = true;
res.send("OK");
Expand All @@ -159,7 +163,7 @@ export function initLamp() {
currentAnimation = animation;
startLamp();
res.send("OK");
});
}

lamp.get("/brightness", (req, res) => {
res.send({ brightness: display.brightness });
Expand Down

0 comments on commit 8b38672

Please sign in to comment.