mirror of
https://github.com/emsesp/EMS-ESP32.git
synced 2025-12-07 00:09:51 +03:00
analogsensor default name, validators
This commit is contained in:
@@ -465,7 +465,7 @@ const Sensors: FC = () => {
|
||||
onClose={onTemperatureDialogClose}
|
||||
onSave={onTemperatureDialogSave}
|
||||
selectedItem={selectedTemperatureSensor}
|
||||
validator={temperatureSensorItemValidation()}
|
||||
validator={temperatureSensorItemValidation(sensorData.ts)}
|
||||
/>
|
||||
)}
|
||||
{sensorData?.analog_enabled === true && (
|
||||
|
||||
@@ -7,7 +7,8 @@ import type {
|
||||
DeviceValue,
|
||||
EntityItem,
|
||||
ScheduleItem,
|
||||
Settings
|
||||
Settings,
|
||||
TemperatureSensor
|
||||
} from './types';
|
||||
|
||||
export const GPIO_VALIDATOR = {
|
||||
@@ -289,7 +290,6 @@ export const uniqueNameValidator = (schedule: ScheduleItem[], o_name?: string) =
|
||||
if (
|
||||
name !== '' &&
|
||||
(o_name === undefined || o_name !== name) &&
|
||||
name !== '' &&
|
||||
schedule.find((si) => si.name === name)
|
||||
) {
|
||||
callback('Name already in use');
|
||||
@@ -307,8 +307,8 @@ export const schedulerItemValidation = (
|
||||
name: [
|
||||
{
|
||||
type: 'string',
|
||||
pattern: /^[a-zA-Z0-9_\\.]{0,15}$/,
|
||||
message: "Must be <15 characters: alpha numeric, '_' or '.'"
|
||||
pattern: /^[a-zA-Z0-9_\\.]{0,19}$/,
|
||||
message: "Must be <20 characters: alpha numeric, '_' or '.'"
|
||||
},
|
||||
...[uniqueNameValidator(schedule, scheduleItem.o_name)]
|
||||
],
|
||||
@@ -349,8 +349,8 @@ export const entityItemValidation = (entity: EntityItem[], entityItem: EntityIte
|
||||
{ required: true, message: 'Name is required' },
|
||||
{
|
||||
type: 'string',
|
||||
pattern: /^[a-zA-Z0-9_\\.]{1,15}$/,
|
||||
message: "Must be <15 characters: alpha numeric, '_' or '.'"
|
||||
pattern: /^[a-zA-Z0-9_\\.]{1,19}$/,
|
||||
message: "Must be <20 characters: alpha numeric, '_' or '.'"
|
||||
},
|
||||
...[uniqueCustomNameValidator(entity, entityItem.o_name)]
|
||||
],
|
||||
@@ -388,14 +388,25 @@ export const entityItemValidation = (entity: EntityItem[], entityItem: EntityIte
|
||||
]
|
||||
});
|
||||
|
||||
export const temperatureSensorItemValidation = () =>
|
||||
export const uniqueTemperatureNameValidator = (sensors: TemperatureSensor[]) => ({
|
||||
validator(rule: InternalRuleItem, n: string, callback: (error?: string) => void) {
|
||||
if (n !== '' && sensors.find((ts) => ts.n === n)) {
|
||||
callback('Name already in use');
|
||||
} else {
|
||||
callback();
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
export const temperatureSensorItemValidation = (sensors: TemperatureSensor[]) =>
|
||||
new Schema({
|
||||
n: [
|
||||
{
|
||||
type: 'string',
|
||||
pattern: /^[a-zA-Z0-9_\\.]{0,17}$/,
|
||||
message: "Must be <18 characters: alpha numeric, '_' or '.'"
|
||||
}
|
||||
pattern: /^[a-zA-Z0-9_\\.]{0,19}$/,
|
||||
message: "Must be <20 characters: alpha numeric, '_' or '.'"
|
||||
},
|
||||
...[uniqueTemperatureNameValidator(sensors)]
|
||||
]
|
||||
});
|
||||
|
||||
@@ -413,13 +424,30 @@ export const isGPIOUniqueValidator = (sensors: AnalogSensor[]) => ({
|
||||
}
|
||||
});
|
||||
|
||||
export const uniqueAnalogNameValidator = (sensors: AnalogSensor[]) => ({
|
||||
validator(rule: InternalRuleItem, n: string, callback: (error?: string) => void) {
|
||||
if (n !== '' && sensors.find((as) => as.n === n)) {
|
||||
callback('Name already in use');
|
||||
} else {
|
||||
callback();
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
export const analogSensorItemValidation = (
|
||||
sensors: AnalogSensor[],
|
||||
creating: boolean,
|
||||
platform: string
|
||||
) =>
|
||||
new Schema({
|
||||
n: [{ required: true, message: 'Name is required' }],
|
||||
n: [
|
||||
{
|
||||
type: 'string',
|
||||
pattern: /^[a-zA-Z0-9_\\.]{0,19}$/,
|
||||
message: "Must be <20 characters: alpha numeric, '_' or '.'"
|
||||
},
|
||||
...[uniqueAnalogNameValidator(sensors)]
|
||||
],
|
||||
g: [
|
||||
{ required: true, message: 'GPIO is required' },
|
||||
platform === 'ESP32-S3'
|
||||
|
||||
@@ -759,8 +759,8 @@ AnalogSensor::Sensor::Sensor(const uint8_t gpio, const std::string & name, const
|
||||
// returns name of the analog sensor or creates one if its empty
|
||||
std::string AnalogSensor::Sensor::name() const {
|
||||
if (name_.empty()) {
|
||||
char name[50];
|
||||
snprintf(name, sizeof(name), "Analog Sensor GPIO %02d", gpio_);
|
||||
char name[20];
|
||||
snprintf(name, sizeof(name), "%s_%02d", FL_(AnalogTypeName)[type_], gpio_);
|
||||
return name;
|
||||
}
|
||||
return name_;
|
||||
|
||||
@@ -27,6 +27,9 @@
|
||||
|
||||
namespace emsesp {
|
||||
|
||||
// names, same order as AnalogType
|
||||
MAKE_ENUM_FIXED(AnalogTypeName, "disabled", "dig_in", "counter", "adc", "timer", "rate", "pwm0", "pwm1", "pwm2")
|
||||
|
||||
class AnalogSensor {
|
||||
public:
|
||||
class Sensor {
|
||||
|
||||
Reference in New Issue
Block a user