-
-
Notifications
You must be signed in to change notification settings - Fork 378
/
ViomiConsumableMonitoringCapability.js
137 lines (126 loc) · 4.9 KB
/
ViomiConsumableMonitoringCapability.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
const ConsumableMonitoringCapability = require("../../../core/capabilities/ConsumableMonitoringCapability");
const ConsumableStateAttribute = require("../../../entities/state/attributes/ConsumableStateAttribute");
/**
* @extends ConsumableMonitoringCapability<import("../ViomiValetudoRobot")>
*/
class ViomiConsumableMonitoringCapability extends ConsumableMonitoringCapability {
/**
* This function polls the current consumables state and stores the attributes in our robotState
*
* @abstract
* @returns {Promise<Array<import("../../../entities/state/attributes/ConsumableStateAttribute")>>}
*/
async getConsumables() {
const data = await this.robot.sendCommand("get_consumables");
const rawConsumables = {
mainBrush: data[0],
sideBrush: data[1],
filter: data[2],
mop: data[3]
};
const consumables = [
new ConsumableStateAttribute({
type: ConsumableStateAttribute.TYPE.BRUSH,
subType: ConsumableStateAttribute.SUB_TYPE.MAIN,
remaining: {
value: Math.round(Math.max(0, (360 - rawConsumables.mainBrush) * 60)),
unit: ConsumableStateAttribute.UNITS.MINUTES
}
}),
new ConsumableStateAttribute({
type: ConsumableStateAttribute.TYPE.BRUSH,
subType: ConsumableStateAttribute.SUB_TYPE.SIDE_RIGHT,
remaining: {
value: Math.round(Math.max(0, (180 - rawConsumables.sideBrush) * 60)),
unit: ConsumableStateAttribute.UNITS.MINUTES
}
}),
new ConsumableStateAttribute({
type: ConsumableStateAttribute.TYPE.FILTER,
subType: ConsumableStateAttribute.SUB_TYPE.MAIN,
remaining: {
value: Math.round(Math.max(0, (180 - rawConsumables.filter) * 60)),
unit: ConsumableStateAttribute.UNITS.MINUTES
}
}),
new ConsumableStateAttribute({
type: ConsumableStateAttribute.TYPE.MOP, // According to python-miio, unverified
subType: ConsumableStateAttribute.SUB_TYPE.MAIN,
remaining: {
value: Math.round(Math.max(0, (180 - rawConsumables.mop) * 60)),
unit: ConsumableStateAttribute.UNITS.MINUTES
}
}),
];
consumables.forEach(c => {
return this.robot.state.upsertFirstMatchingAttribute(c);
});
this.robot.emitStateAttributesUpdated();
return consumables;
}
/**
* @abstract
* @param {string} type
* @param {string} [subType]
* @returns {Promise<void>}
*/
async resetConsumable(type, subType) {
let idx;
switch (type) {
case ConsumableStateAttribute.TYPE.BRUSH:
switch (subType) {
case ConsumableStateAttribute.SUB_TYPE.MAIN:
idx = 1;
break;
case ConsumableStateAttribute.SUB_TYPE.SIDE_RIGHT:
idx = 2;
break;
}
break;
case ConsumableStateAttribute.TYPE.FILTER:
switch (subType) {
case ConsumableStateAttribute.SUB_TYPE.MAIN:
idx = 3;
break;
}
break;
case ConsumableStateAttribute.TYPE.MOP:
switch (subType) {
case ConsumableStateAttribute.SUB_TYPE.MAIN:
idx = 4;
break;
}
break;
}
if (idx) {
await this.robot.sendCommand("set_consumables", [idx, 0], {});
} else {
throw new Error("No such consumable");
}
}
getProperties() {
return {
availableConsumables: [
{
type: ConsumableStateAttribute.TYPE.BRUSH,
subType: ConsumableStateAttribute.SUB_TYPE.MAIN,
unit: ConsumableStateAttribute.UNITS.MINUTES,
maxValue: 360 * 60
},
{
type: ConsumableStateAttribute.TYPE.BRUSH,
subType: ConsumableStateAttribute.SUB_TYPE.SIDE_RIGHT,
unit: ConsumableStateAttribute.UNITS.MINUTES,
maxValue: 180 * 60
},
{
type: ConsumableStateAttribute.TYPE.FILTER,
subType: ConsumableStateAttribute.SUB_TYPE.MAIN,
unit: ConsumableStateAttribute.UNITS.MINUTES,
maxValue: 180 * 60
}
]
};
}
}
module.exports = ViomiConsumableMonitoringCapability;