1 import { Injectable } from '@angular/core';
3 import * as moment from 'moment';
5 import { CdDevice } from '../models/devices';
10 export class DeviceService {
14 * Calculates additional data and appends them as new attributes to the given device.
16 calculateAdditionalData(device: CdDevice): CdDevice {
17 if (!device.life_expectancy_min || !device.life_expectancy_max) {
18 device.state = 'unknown';
21 const hasDate = (float: string): boolean => !!Number.parseFloat(float);
22 const weeks = (isoDate1: string, isoDate2: string): number =>
23 !isoDate1 || !isoDate2 || !hasDate(isoDate1) || !hasDate(isoDate2)
25 : moment.duration(moment(isoDate1).diff(moment(isoDate2))).asWeeks();
27 const ageOfStamp = moment
28 .duration(moment(moment.now()).diff(moment(device.life_expectancy_stamp)))
30 const max = weeks(device.life_expectancy_max, device.life_expectancy_stamp);
31 const min = weeks(device.life_expectancy_min, device.life_expectancy_stamp);
34 device.state = 'stale';
35 } else if (max !== null && max <= 2) {
37 } else if (min !== null && min <= 4) {
38 device.state = 'warning';
40 device.state = 'good';
43 device.life_expectancy_weeks = {
44 max: max !== null ? Math.round(max) : null,
45 min: min !== null ? Math.round(min) : null
51 readable(device: CdDevice): CdDevice {
52 device.readableDaemons = device.daemons.join(' ');
56 prepareDevice(device: CdDevice): CdDevice {
57 return this.readable(this.calculateAdditionalData(device));