Skip to content

Commit

Permalink
fix: fix bug in DMX channel validation
Browse files Browse the repository at this point in the history
  • Loading branch information
plaa committed Jun 7, 2024
1 parent 8b6040f commit b2efbd1
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 8 deletions.
26 changes: 22 additions & 4 deletions src/dmx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ import { processDmxSignal } from './tplink/tplink-control';
const UNIVERSE_NAME = 'backend';
const EVENT_DURATION = 1000; // ms

export const DMX_MAX_CHANNEL = 511;
export const DMX_MAX_VALUE = 255;

export const CHANNELS = {
JumpFixed: 100,
JumpPrepReady: 101,
Expand All @@ -20,6 +23,7 @@ export const CHANNELS = {
JumpEnd: 110,
JumpEndBreaking: 111,
JumpAbort: 112,
JumpEndingSoon: 113,

ShipAnnouncement: 119,

Expand Down Expand Up @@ -179,8 +183,15 @@ function findChannelName(channel: Channel) {
return 'UNKNOWN';
}

export function fireEvent(channel: Channel, value = 255) {
if (!isNumber(channel) || !isNumber(value) || channel < 0 || channel > 255 || value < 0 || value > 255) {
export function fireEvent(channel: Channel, value = DMX_MAX_VALUE) {
if (
!isNumber(channel) ||
!isNumber(value) ||
channel < 0 ||
channel > DMX_MAX_CHANNEL ||
value < 0 ||
value > DMX_MAX_VALUE
) {
logger.error(`Attempted DMX fireEvent with invalid channel=${channel} or value=${value}`);
return;
}
Expand All @@ -194,13 +205,20 @@ export function fireEvent(channel: Channel, value = 255) {

export function mapDmxValue(value: number, inMin: number, inMax: number): number {
const outMin = 0;
const outMax = 255;
const outMax = DMX_MAX_VALUE;
const outValue = Math.round(outMin + ((outMax - outMin) * (value - inMin)) / (inMax - inMin));
return Math.min(Math.max(outValue, outMin), outMax);
}

export function setDmxValue(channel: Channel, value: number) {
if (!isNumber(channel) || !isNumber(value) || channel < 0 || channel > 255 || value < 0 || value > 255) {
if (
!isNumber(channel) ||
!isNumber(value) ||
channel < 0 ||
channel > DMX_MAX_CHANNEL ||
value < 0 ||
value > DMX_MAX_VALUE
) {
logger.error(`Attempted DMX setDmxValue with invalid channel=${channel} or value=${value}`);
return;
}
Expand Down
7 changes: 3 additions & 4 deletions src/routes/dmx.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { CHANNELS, fireEvent } from '../dmx';
import { CHANNELS, fireEvent, DMX_MAX_CHANNEL, DMX_MAX_VALUE } from '../dmx';
import httpErrors from 'http-errors';
import { isFinite } from 'lodash';
import { Router } from 'express';
Expand All @@ -15,7 +15,6 @@ router.get('/channels', (req, res) => {
res.json(CHANNELS);
});


/**
* Fires an event (value for one second, then back to zero) on a DMX channel.
*
Expand All @@ -34,13 +33,13 @@ router.post('/event/:channel', (req, res) => {
if (!channelInt) {
channelInt = parseInt(channel, 10);
}
if (!channel || !channelInt || channelInt < 0 || channelInt > 255) {
if (!channel || !channelInt || !isFinite(channelInt) || channelInt < 0 || channelInt > DMX_MAX_CHANNEL) {
throw new httpErrors.BadRequest(`Invalid channel number`);
}

if (typeof value !== 'undefined') {
value = parseInt(value, 10);
if (!isFinite(value) || value <= 0 || value > 255) {
if (!isFinite(value) || value <= 0 || value > DMX_MAX_VALUE) {
throw new httpErrors.BadRequest(`Invalid value`);
}
}
Expand Down

0 comments on commit b2efbd1

Please sign in to comment.