-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbattery.js
114 lines (107 loc) · 3.68 KB
/
battery.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
const { hasProperty} = require('./utils');
const {PowerSource} = require( "@matter/main/clusters")
const {PowerSourceServer} = require("@matter/main/behaviors");
function battery(node, msg) {
if (hasProperty(msg.battery, 'level')){
switch (msg.battery.level) {
case 'ok':
case '0':
case 0:
node.device.set({powerSource: {batChargeLevel: 0}})
break;
case 'low':
case 'warning':
case '1':
case 1:
node.device.set({powerSource: {batChargeLevel: 1}})
break;
case 'critical':
case '2':
case 2:
node.device.set({powerSource: {batChargeLevel: 2}})
break;
}
}
if (hasProperty(msg.battery, 'percent')){
node.device.set({powerSource: {batPercentRemaining: Number(msg.battery.percent)*2}})
}
if (hasProperty(msg.battery, 'charge' ) && node.bat == 'recharge'){
switch (msg.battery.charge) {
case 'unknown':
case '0':
case 0:
node.device.set({powerSource: {batChargeState: 0}})
break;
case 'charging':
case '1':
case 1:
node.device.set({powerSource: {batChargeState: 1}})
break;
case 'full':
case '2':
case 2:
node.device.set({powerSource: {batChargeState: 2}})
break;
case 'notcharging':
case '3':
case 3:
node.device.set({powerSource: {batChargeState: 3}})
break;
}
} else if (hasProperty(msg.battery, 'charge' ) && node.bat != 'recharge'){
node.error("Can't set charging state on non-rechargable battery")
}
msg.payload = 'ok'
node.send(msg)
}
function batFeatures(node) {
switch (node.bat) {
case 'recharge':
features = {
status: PowerSource.PowerSourceStatus.Active,
order: 1,
description: "Battery",
batChargeLevel: PowerSource.BatChargeLevel.Ok,
batReplacementNeeded: false,
batReplaceability: PowerSource.BatReplaceability.Unspecified,
batChargeState: PowerSource.BatChargeState.Unknown,
batFunctionalWhileCharging: true,
batQuantity: 1
}
break;
case 'replace':
features = {
status: PowerSource.PowerSourceStatus.Active,
order: 1,
description: "Battery",
batChargeLevel: PowerSource.BatChargeLevel.Ok,
batReplacementNeeded: false,
batReplaceability: PowerSource.BatReplaceability.UserReplaceable,
batReplacementDescription: "",
batQuantity: 1
}
break;
default:
features = {
status: PowerSource.PowerSourceStatus.Active,
order: 1,
description: "Battery",
batChargeLevel: PowerSource.BatChargeLevel.Ok,
batReplacementNeeded: false,
batReplaceability: PowerSource.BatReplaceability.UserReplaceable,
batReplacementDescription: "",
batQuantity: 1
}
break;
}
return features
}
function batCluster(node){
let cl = [
PowerSourceServer.with(
PowerSource.Feature.Battery,
(node.bat=='recharge') ? PowerSource.Feature.Rechargeable : PowerSource.Feature.Replaceable
)]
return cl
}
module.exports = { battery, batFeatures, batCluster };