in source/simulator/lib/device/generators/random/generator.js [63:166]
_processSpecAttribute(attribute) {
let _value = null;
//Use default property if available
if (attribute.hasOwnProperty('default')) {
if(typeof attribute.default === 'string') {
if (attribute.default.trim() !== '') {
return (attribute.default.trim());
}
} else if (attribute.type === 'bool') {
return (!!attribute.default);
} else {
return (attribute.default);
}
}
//Add attribute to state if not already added
if (!this.currentState.hasOwnProperty(attribute.name)) {
this.currentState[attribute.name] = {
cnt: 1
};
}
//generate value according to type
switch (attribute.type) {
case 'id': {
let length = attribute.length || 21;
if (attribute.charSet) {
_value = customAlphabet(attribute.charSet, length)();
} else {
_value = nanoid(length);
}
if (attribute.static) {
this.staticValues[attribute.name] = _value;
}
break;
}
case 'string': {
let { min, max } = attribute;
let length = faker.datatype.number({ min: min, max: max, precision: 1 })
_value = faker.datatype.string(length);
if (attribute.static) {
this.staticValues[attribute.name] = _value;
}
break;
}
case 'int': {
let { min, max } = attribute;
_value = faker.datatype.number({ min: min, max: max });
break;
}
case 'timestamp': {
if (attribute.tsformat === 'unix') {
_value = moment().format('x');
} else {
_value = moment().utc().format('YYYY-MM-DDTHH:mm:ss');
}
break;
}
case 'bool': {
_value = faker.datatype.boolean();
break;
}
case 'float': {
let { min, max, precision } = attribute;
_value = faker.datatype.number({ min: min, max: max, precision: precision });
break;
}
case 'pickOne': {
_value = faker.random.arrayElement(attribute.arr);
if (attribute.hasOwnProperty('static') && attribute.static) {
this.staticValues[attribute.name] = _value;
}
break;
}
case 'location': {
const _center = {
latitude: attribute.lat,
longitude: attribute.long
}
_value = randomLocation.randomCirclePoint(_center, attribute.radius);
break;
}
case 'sinusoidal': {
_value = this.sin(attribute.min, attribute.max, this.currentState[attribute.name].cnt);
break;
}
case 'decay': {
_value = this.decay(attribute.min, attribute.max, this.currentState[attribute.name].cnt);
break;
}
case 'object': {
_value = this.generatePayload(attribute.payload);
break;
}
default: {
_value = '';
break;
}
}
//increment current state for attribute
this.currentState[attribute.name] = {
cnt: this.currentState[attribute.name].cnt + 1
}
return (_value);
}