ice-frontend-react-mobx
Version:
ICE Frontend REACT+MobX
108 lines (86 loc) • 2.75 kB
JavaScript
const debug = require('debug')('ice:models:rack'); // eslint-disable-line no-unused-vars
import { observable, computed } from 'mobx';
export default class Rack {
_id = null;
name = null;
allocatedPower = null;
redundancy = '2N';
priority = 1;
rpdus = [];
defaultRpdus = [];
metrics = new RackMetrics();
store = null;
constructor (store) {
this.store = store;
}
get id () {
return this._id;
}
get rpduIds () {
return this.rpdus.map((rpdu) => (rpdu.id));
}
get toJson () {
let rack = {
name: this.name,
allocatedPower: Number(this.allocatedPower),
priority: this.priority,
redundancyStrategy: this.redundancy,
rPdus: this.rpduIds,
defaultRpdus: this.defaultRpdus
};
if (this.id) {
rack.id = this.id;
}
return rack;
}
updateFromJson (json) {
this.rpdus.clear();
this._id = json.id;
this.name = json.name || null;
this.allocatedPower = json.allocatedPower || null;
this.redundancy = json.redundancyStrategy || '2N';
this.priority = json.priority || 1;
this.defaultRpdus = json.defaultRpdus || [];
if (json.rPdus) {
json.rPdus.forEach((rpdu) => {
let device = this.store.deviceStore.getById(rpdu);
if (device) {
this.rpdus.push(device);
}
});
}
}
save () {
return this.store.save(this);
}
delete () {
return this.store.delete(this.id);
}
}
export class RackMetrics {
availableCapacityW = '';
averageSocProp = '';
instantaneousInputPowerW = '';
maxInputPowerW = '';
maxUtilizatinProp = '';
phasePowerIn = [];
powerLimits = [];
get instantaneousInputPower () {
return this.instantaneousInputPowerW / 1000;
}
get maxInputPower () {
return this.maxInputPowerW / 1000;
}
get averageSoc () {
return this.averageSocProp * 100;
}
updateFromJson (json) {
this.availableCapacityW = (json.hasOwnProperty('availableCapacityW')) ? json.availableCapacityW : '';
this.averageSocProp = (json.hasOwnProperty('averageSocProp')) ? json.averageSocProp : '';
this.instantaneousInputPowerW = (json.hasOwnProperty('instantaneousInputPowerW')) ? json.instantaneousInputPowerW : '';
this.maxInputPowerW = (json.hasOwnProperty('maxInputPowerW')) ? json.maxInputPowerW : '';
this.maxUtilizationProp = (json.hasOwnProperty('maxUtilizationProp')) ? json.maxUtilizationProp : '';
this.phasePowerIn.replace(json.phasePowerIn);
this.powerLimits.replace(json.powerLimits);
}
}