ice-frontend-react-mobx
Version:
ICE Frontend REACT+MobX
181 lines (145 loc) • 5.65 kB
JavaScript
const debug = require('debug')('ice:models:group'); // eslint-disable-line no-unused-vars
import { observable, computed } from 'mobx';
export default class Group {
_id = null;
name = null;
limit = 1E9;
rackshareEnabled = false;
devices = [];
sensorData = new GroupSensorData();
store = null;
constructor (store) {
this.store = store;
}
get id () {
return this._id;
}
get deviceIds () {
return this.devices.slice().map((device) => (device.id));
}
get toJson () {
let group = {
name: this.name,
deviceIds: this.deviceIds,
rackshareConfig: {
rackshareEnabled: this.rackshareEnabled,
maxLimit: Number(this.limit)
}
};
if (this.id) {
group.id = this.id;
}
return group;
}
updateFromJson (json) {
// clear out our devices
this.devices.clear();
// parse the json
this._id = json.id || null;
this.name = json.name || null;
this.limit = (json.rackshareConfig && json.rackshareConfig.maxLimit)
? json.rackshareConfig.maxLimit : 1E9;
this.rackshareEnabled = (json.rackshareConfig && json.rackshareConfig.rackshareEnabled)
? json.rackshareConfig.rackshareEnabled : false;
// resolve our devices
if (json.deviceIds) {
json.deviceIds.forEach((deviceId) => {
let device = this.store.deviceStore.getById(deviceId);
if (device) {
this.devices.push(device);
}
});
}
}
save () {
return this.store.save(this);
}
delete () {
return this.store.delete(this.id);
}
}
export class GroupSensorData {
battAvgSocProp = '';
battAvgSocSec = '';
battAvgSocWh = '';
battChargePowerW = '';
battDischargePowerW = '';
battMinSocProp = '';
battMinSocSec = '';
battMinSocWh = '';
battTotalSocWh = '';
battUpsActive = '';
capacityUtilizationProp = '';
inputPower1W = '';
inputPower2W = '';
inputPower3W = '';
inputPowerW = '';
numDevicesCharging = '';
numDevicesDischarging = '';
numDevicesUps = '';
outputPower1W = '';
outputPower2W = '';
outputPower3W = '';
outputPowerW = '';
powerLimitW = '';
timestamp = '';
get battChargePower () {
return this.battChargePowerW / 1000;
}
get battDischargePower () {
return this.battDischargePowerW / 1000;
}
get inputPower () {
return this.inputPowerW / 1000;
}
get inputPower1 () {
return this.inputPower1W / 1000;
}
get inputPower2 () {
return this.inputPower2W / 1000;
}
get inputPower3 () {
return this.inputPower3W / 1000;
}
get outputPower () {
return this.outputPowerW / 1000;
}
get outputPower1 () {
return this.outputPower1W / 1000;
}
get outputPower2 () {
return this.outputPower2W / 1000;
}
get outputPower3 () {
return this.outputPower3W / 1000;
}
get powerLimit () {
return this.powerLimitW / 1000;
}
updateFromJson (json) {
this.battAvgSocProp = (json.hasOwnProperty('battAvgSocProp')) ? json.battAvgSocProp : '';
this.battAvgSocSec = (json.hasOwnProperty('battAvgSocSec')) ? json.battAvgSocSec : '';
this.battAvgSocWh = (json.hasOwnProperty('battAvgSocWh')) ? json.battAvgSocWh : '';
this.battChargePowerW = (json.hasOwnProperty('battChargePowerW')) ? json.battChargePowerW : '';
this.battDischargePowerW = (json.hasOwnProperty('battDischargePowerW')) ? json.battDischargePowerW : '';
this.battMinSocProp = (json.hasOwnProperty('battMinSocProp')) ? json.battMinSocProp : '';
this.battMinSocSec = (json.hasOwnProperty('battMinSocSec')) ? json.battMinSocSec : '';
this.battMinSocWh = (json.hasOwnProperty('battMinSocWh')) ? json.battMinSocWh : '';
this.battTotalSocWh = (json.hasOwnProperty('battTotalSocWh')) ? json.battTotalSocWh : '';
this.battUpsActive = (json.hasOwnProperty('battUpsActive')) ? json.battUpsActive : '';
this.capacityUtilizationProp = (json.hasOwnProperty('capacityUtilizationProp')) ? json.capacityUtilizationProp : '';
this.inputPower1W = (json.hasOwnProperty('inputPower1W')) ? json.inputPower1W : '';
this.inputPower2W = (json.hasOwnProperty('inputPower2W')) ? json.inputPower2W : '';
this.inputPower3W = (json.hasOwnProperty('inputPower3W')) ? json.inputPower3W : '';
this.inputPowerW = (json.hasOwnProperty('inputPowerW')) ? json.inputPowerW : '';
this.numDevicesCharging = (json.hasOwnProperty('numDevicesCharging')) ? json.numDevicesCharging : '';
this.numDevicesDischarging = (json.hasOwnProperty('numDevicesDischarging')) ? json.numDevicesDischarging : '';
this.numDevicesUps = (json.hasOwnProperty('numDevicesUps')) ? json.numDevicesUps : '';
this.outputPower1W = (json.hasOwnProperty('outputPower1W')) ? json.outputPower1W : '';
this.outputPower2W = (json.hasOwnProperty('outputPower2W')) ? json.outputPower2W : '';
this.outputPower3W = (json.hasOwnProperty('outputPower3W')) ? json.outputPower3W : '';
this.outputPowerW = (json.hasOwnProperty('outputPowerW')) ? json.outputPowerW : '';
this.powerLimitW = (json.hasOwnProperty('powerLimitW')) ? json.powerLimitW : '';
this.timestamp = (json.timestamp) ? new Date(json.timestamp) : '';
}
}