forked from andreypopov/node-red-contrib-miio-roborock
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.js
executable file
·105 lines (92 loc) · 4.15 KB
/
api.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
var NODE_PATH = '/miio-roborock/';
const MiioRoborockVocabulary = require('./lib/miio-roborock-vocabulary.js');
const mihome = require('node-mihome');
mihome.miioProtocol.init();
mihome.aqaraProtocol.init();
module.exports = function (RED) {
/**
* Enable http route to static files
*/
RED.httpAdmin.get(NODE_PATH + 'static/*', function (req, res) {
var options = {
root: __dirname + '/static/',
dotfiles: 'deny'
};
res.sendFile(req.params[0], options);
});
RED.httpAdmin.get(NODE_PATH + 'getStatus', function (req, res) {
// res.json({"msg_ver":3,"msg_seq":19069,"state":"charging","batteryLevel":100,"cleanTime":2008,"cleanArea":33300000,"error":{"code":3,"message":"Try moving the vacuum to a different place."},"map_present":1,"in_cleaning":0,"in_returning":0,"in_fresh_state":1,"lab_status":1,"water_box_status":0,"fanSpeed":101,"dnd_enabled":0,"map_status":3,"lock_status":0,"error_code":0});
var config = req.query;
var controller = RED.nodes.getNode(config.controllerID);
if (controller && controller.constructor.name === "ServerNode") {
res.json(controller.status);
} else {
res.json([]);
}
});
RED.httpAdmin.get(NODE_PATH + 'getCommands', function (req, res) {
res.json(MiioRoborockVocabulary.commands);
});
RED.httpAdmin.get(NODE_PATH + 'getVoices', function (req, res) {
res.json(MiioRoborockVocabulary.voices);
});
RED.httpAdmin.get(NODE_PATH + 'find', function (req, res) {
var config = req.query;
// var controller = RED.nodes.getNode(config.controllerID);
// if (controller && controller.constructor.name === "ServerNode") {
find(config.email, config.password, config.ipAddress).then(function(response){
res.json(response);
}).catch(error => {
res.json(error);
});
// } else {
// res.json({"error_description":"You have to deploy before searching token"});
// }
function find(email, password, ipAddress) {
var node = this;
return new Promise(function (resolve, reject) {
//********* CONFIGURATION *********
var _COUNTRY = "de"
//********* CONFIGURATION END *********
if (mihome.miCloudProtocol.isLoggedIn) {
GetDeviceByIP(ipAddress).then(data => {
resolve(data)
}).catch(err => {
reject({"error_description": "Device not found, check IP address", "err":err});
});
} else {
LoginMethod().then(_ => {
GetDeviceByIP(ipAddress).then(data => {
resolve(data)
}).catch(err => {
reject({"error_description": "Device not found, check IP address", "err":err});
});
}).catch(err => {
reject({"error_description": "Error: Invalid email/password", "err":err});
});
}
async function LoginMethod() {
await mihome.miCloudProtocol.login(email, password);
}
async function GetDeviceByIP(ip_address) {
return new Promise((resolve, reject) => {
GetDevices().then(devices => {
for (var i in devices) {
if (devices[i].localip === ip_address) {
resolve(devices[i]);
break;
}
}
reject({"error_description": 'Device not found'});
}).catch(err => {
reject({"error_description": 'Get devices error'});
});
});
}
async function GetDevices() {
return await mihome.miCloudProtocol.getDevices(null, {country: _COUNTRY});
}
});
}
});
}