-
Notifications
You must be signed in to change notification settings - Fork 3
/
warningsobject.js
175 lines (152 loc) · 4.26 KB
/
warningsobject.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/* Magic Mirror
* WarningsObject: MMM-UKMOWeatherWarnings
*
* By Malcolm Oakes https://github.com/maloakes
*
* MIT Licensed.
*
* This class represents Weather Warning message data from the UK Met Office.
* This will contain 0 or more WarningItemObjects.
*/
class WarningsObject {
constructor(warningsResult) {
this.title = warningsResult.title
this.items = null
this.ordering = null
// build the Warning Item ordering map
this.buildOrdering()
// create the list of Warning items
var itemList = []
for (var i in warningsResult.items) {
const item = new WarningItemObject(warningsResult.items[i])
// only add the item is valid in the future
if (item.isFuture()) {
itemList.push(item)
}
}
// sort the list if it contains objects
if (itemList.length > 0) {
this.items = itemList.sort(function(a,b){return a.validFrom.diff(b.validFrom)})
}
}
/*
* get colour (severity) of item
*/
getColor(idx) {
return this.items[idx].getColor()
}
/*
* get type of item
*/
getTypeTc(idx) {
return this.items[idx].getTypeTc()
}
/*
* get validity of item
*/
getValidity(idx) {
this.items[idx].getValidity()
}
/*
* get the maximum current warning
*/
getMaxCurrentWarn(delta) {
// get warnings that are now current
// return level and info of highest level
let ordArray = this.getMaxPeriodWarn("current", delta)
// just return level and type (title case)
let res = {}
if (ordArray) {
res[0] = ordArray.length > 0 ? { level: ordArray[0].getLevelCode(),
color: ordArray[0].getColor(),
type: ordArray[0].getTypeTc() } : null
}
return res
}
/*
* determine the warnings for the required period
* parameter:
* mode: current - active now
* today - active from now to midnight
* day - active on day specified by offset
* offset:
* mode = 'current' - offset (in minutes) from now
* mode = 'day' - offset (in days) from today
*/
getMaxPeriodWarn(mode, offset) {
if (this.items) {
let newArray = null
switch (mode) {
case "current":
// get warnings that are now current
// return level and info of highest level
newArray = this.items.filter(function (el) {
return el.validFrom.isBefore(moment().add(offset, "minutes")) && el.validTo.isAfter(moment())
});
break;
case "today":
// get warnings active from now until end of day
// return highest
// Vf < Ed & Vt > Nw
newArray = this.items.filter(function (el) {
return el.validFrom.isBefore(moment().endOf("day")) && el.validTo.isAfter(moment())
});
break;
case "day":
// get warnings active during the day
// return highest
// Vf < Ed & Vt > Sd
newArray = this.items.filter(function (el) {
return el.validFrom.isBefore(moment().add(offset, "days").endOf("day")) && el.validTo.isAfter(moment().add(offset, "days").startOf("day"))
});
break;
}
return this.sortByLevel(newArray)
} else {
return null
}
}
/*
* Get the highest level warning for each day of the period
*/
getMaxWarnFcList(listSize) {
// get warnings from now to end of today
// & get warnings the following days
// return highest level for each day
let fcArray = []
let tmp = this.getMaxPeriodWarn("today", null)
fcArray.push(tmp && tmp.length > 0 ? tmp[0] : null)
for (let offset = 1; offset < listSize; offset++) {
let tmp = this.getMaxPeriodWarn("day", offset)
fcArray.push(tmp && tmp.length > 0 ? tmp[0] : null)
}
let res = {}
for (let i in fcArray) {
res[i] = fcArray[i] != null ? { level: fcArray[i].getLevelCode(),
color: fcArray[i].getColor(),
type: fcArray[i].getTypeTc() } : null
}
return res
}
/*
* sort the list by level
*/
sortByLevel(warnArray) {
let ord = this.ordering
var ordArray = warnArray.sort( function(a, b) {
return (ord[a.level] - ord[b.level]) || a.type.localeCompare(b.type);
});
return ordArray
}
/*
* build the ordering map in the constuctor by calling this
* The warning items should be sorted in order of severity
*/
buildOrdering(warnArray) {
this.ordering = {} // map for efficient lookup of sortIndex
let sortOrder = ["RED","AMBER","YELLOW"];
for (let i=0; i<sortOrder.length; i++) {
this.ordering[sortOrder[i]] = i;
}
}
}