Skip to content

Commit

Permalink
Add new risk feature (#2068)
Browse files Browse the repository at this point in the history
  • Loading branch information
Pierre-Gilles authored May 9, 2024
1 parent 3854c30 commit cd825ab
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Text } from 'preact-i18n';
import get from 'get-value';
import cx from 'classnames';

import { DEVICE_FEATURE_CATEGORIES } from '../../../../../../../server/utils/constants';
Expand Down Expand Up @@ -46,37 +47,72 @@ const getVocIndexColor = value => {
return 'danger';
};

const RISK_COLORS = {
'no-risk': 'success',
'low-risk': 'warning',
'medium-risk': 'orange',
'high-risk': 'danger',
unknown: 'secondary'
};

const getRiskColor = value => {
return RISK_COLORS[value];
};

const BADGE_CATEGORIES = {
[DEVICE_FEATURE_CATEGORIES.CO2_SENSOR]: value => colorLowAsGreen(value, 600, 1200),
[DEVICE_FEATURE_CATEGORIES.VOC_SENSOR]: value => colorLowAsGreen(value, 250, 2000),
[DEVICE_FEATURE_CATEGORIES.VOC_INDEX_SENSOR]: value => getVocIndexColor(value),
[DEVICE_FEATURE_CATEGORIES.PM25_SENSOR]: value => colorLowAsGreen(value, 12, 35),
[DEVICE_FEATURE_CATEGORIES.FORMALDEHYD_SENSOR]: value => colorLowAsGreen(value, 50, 120),
[DEVICE_FEATURE_CATEGORIES.AIRQUALITY_SENSOR]: value => getAqiColor(value)
[DEVICE_FEATURE_CATEGORIES.AIRQUALITY_SENSOR]: value => getAqiColor(value),
[DEVICE_FEATURE_CATEGORIES.RISK]: value => getRiskColor(value)
};

const BADGE_VALUE_CONVERTERS = {
[DEVICE_FEATURE_CATEGORIES.RISK]: {
0: 'no-risk',
1: 'low-risk',
2: 'medium-risk',
3: 'high-risk'
}
};

const BadgeNumberDeviceValue = props => {
const { category, last_value: lastValue = null, unit } = props.deviceFeature;
const { category, type, last_value: lastValue = null, unit } = props.deviceFeature;

const colorMethod = BADGE_CATEGORIES[category];
if (!colorMethod) {
return <RawDeviceValue {...props} />;
}

const value = lastValue === null ? -1 : lastValue;
let value = lastValue === null ? -1 : lastValue;
let valueIsEnum = false;
const valued = value !== -1;

// If the category is an enum
// We need to convert the string to text
if (BADGE_VALUE_CONVERTERS[category]) {
value = get(BADGE_VALUE_CONVERTERS[category], value, 'unknown');
valueIsEnum = true;
}

const colorClass = `bg-${valued ? colorMethod(value) : 'secondary'}`;

return (
<span class={cx('badge', colorClass)}>
{!valued && <Text id="dashboard.boxes.devicesInRoom.noValue" />}
{valued && (
{valued && !valueIsEnum && (
<span>
{`${lastValue} `}
<Text id={`deviceFeatureUnitShort.${unit}`} />
</span>
)}
{valued && valueIsEnum && (
<span>
<Text id={`deviceFeatureValue.category.${category}.${type}.${value}`} />
</span>
)}
</span>
);
};
Expand Down
13 changes: 13 additions & 0 deletions front/src/config/i18n/de.json
Original file line number Diff line number Diff line change
Expand Up @@ -2772,6 +2772,15 @@
"zero": "Nein",
"one": "Ja"
}
},
"risk": {
"integer": {
"no-risk": "Kein Risiko",
"low-risk": "Niedrig",
"medium-risk": "Mittel",
"high-risk": "Hoch",
"unknown": "Unbekannt"
}
}
}
},
Expand Down Expand Up @@ -3062,6 +3071,10 @@
"shortCategoryName": "Winkelsensor",
"integer": "Winkel (ganze Zahl)"
},
"risk": {
"shortCategoryName": "Risiko",
"integer": "Risikostufe"
},
"unknown": {
"shortCategoryName": "Unbekannt",
"unknown": "Unbekannt"
Expand Down
13 changes: 13 additions & 0 deletions front/src/config/i18n/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -2772,6 +2772,15 @@
"zero": "No",
"one": "Yes"
}
},
"risk": {
"integer": {
"no-risk": "No Risk",
"low-risk": "Low",
"medium-risk": "Medium",
"high-risk": "High",
"unknown": "Unknown"
}
}
}
},
Expand Down Expand Up @@ -3062,6 +3071,10 @@
"shortCategoryName": "Angle sensor",
"integer": "Angle (integer)"
},
"risk": {
"shortCategoryName": "Risk",
"integer": "Risk Level"
},
"unknown": {
"shortCategoryName": "Unknown",
"unknown": "Unknown"
Expand Down
13 changes: 13 additions & 0 deletions front/src/config/i18n/fr.json
Original file line number Diff line number Diff line change
Expand Up @@ -2772,6 +2772,15 @@
"zero": "Non",
"one": "Oui"
}
},
"risk": {
"integer": {
"no-risk": "Pas de risque",
"low-risk": "Faible",
"medium-risk": "Moyen",
"high-risk": "Élevé",
"unknown": "Inconnu"
}
}
}
},
Expand Down Expand Up @@ -3062,6 +3071,10 @@
"shortCategoryName": "Angle",
"integer": "Angle (entier)"
},
"risk": {
"shortCategoryName": "Risque",
"integer": "Niveau de risque"
},
"unknown": {
"shortCategoryName": "Inconnu",
"unknown": "Inconnu"
Expand Down
3 changes: 3 additions & 0 deletions front/src/utils/consts.js
Original file line number Diff line number Diff line change
Expand Up @@ -339,5 +339,8 @@ export const DeviceFeatureCategoriesIcon = {
[DEVICE_FEATURE_TYPES.MUSIC.NEXT]: 'fast-forward',
[DEVICE_FEATURE_TYPES.MUSIC.PLAYBACK_STATE]: 'bar-chart-2',
[DEVICE_FEATURE_TYPES.MUSIC.PLAY_NOTIFICATION]: 'bell'
},
[DEVICE_FEATURE_CATEGORIES.RISK]: {
[DEVICE_FEATURE_TYPES.RISK.INTEGER]: 'alert-circle'
}
};
4 changes: 4 additions & 0 deletions server/utils/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,7 @@ const DEVICE_FEATURE_CATEGORIES = {
PRECIPITATION_SENSOR: 'precipitation-sensor',
PRESENCE_SENSOR: 'presence-sensor',
PRESSURE_SENSOR: 'pressure-sensor',
RISK: 'risk',
SHUTTER: 'shutter',
SIGNAL: 'signal',
SIREN: 'siren',
Expand Down Expand Up @@ -654,6 +655,9 @@ const DEVICE_FEATURE_TYPES = {
TEXT: {
TEXT: 'text',
},
RISK: {
INTEGER: 'integer',
},
};

const DEVICE_FEATURE_UNITS = {
Expand Down

0 comments on commit cd825ab

Please sign in to comment.