Skip to content

Commit eb1f256

Browse files
Create melcloud.php
1 parent daf5703 commit eb1f256

File tree

1 file changed

+226
-0
lines changed

1 file changed

+226
-0
lines changed

melcloud.php

Lines changed: 226 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,226 @@
1+
<?php
2+
/**
3+
* Update https://github.com/oheguy/eedomus-melcloud script to be able to use preset
4+
*/
5+
6+
/**
7+
* Build context header
8+
*
9+
* */
10+
function sdk_getHeader(){
11+
$contextKey = loadVariable('ContextKey');
12+
return array("X-MitsContextKey: $contextKey", "Content-Type: application/json");
13+
}
14+
15+
/**
16+
* Get Building Id and DeviceId from deviceName
17+
* DeviceNme could be full name or serial number recored in melcloud
18+
*
19+
* */
20+
function sdk_setIds($moduleId, $deviceName, $username, $password){
21+
22+
//echo "---------------------------------------------------------sdk_setIds<br>";
23+
$json = httpQuery("https://app.melcloud.com/Mitsubishi.Wifi.Client/User/ListDevices", "GET", "", "", sdk_getHeader(), false);
24+
$result = sdk_json_decode($json);
25+
26+
if (isset($result['Success'])) {
27+
sdk_connect($username, $password);
28+
$json = httpQuery("https://app.melcloud.com/Mitsubishi.Wifi.Client/User/ListDevices", "GET", "", "", sdk_getHeader(), false);
29+
$result = sdk_json_decode($json);
30+
}
31+
32+
$devices = $result[0]['Structure']['Devices'];
33+
34+
35+
foreach ($devices as $device){
36+
if ($device['DeviceName'] == $deviceName || $device['SerialNumber'] == $deviceName){
37+
saveVariable($moduleId.'-buildingId', $device['BuildingID']);
38+
saveVariable($moduleId.'-deviceId', $device['DeviceID']);
39+
40+
}
41+
}
42+
43+
}
44+
45+
/**
46+
* Log and set ContextId
47+
*
48+
* */
49+
function sdk_connect( $username, $password ){
50+
51+
//echo "---------------------------------------------------------sdk_connect<br>";
52+
$headers = array("Content-Type: application/json");
53+
$jsonTest = '{Email: "' . $username . '", Password: "' . $password . '", Language: 7, AppVersion: "1.15.3.0", Persist: true}';
54+
55+
$relogin = httpQuery('https://app.melcloud.com/Mitsubishi.Wifi.Client/Login/ClientLogin', 'POST', $jsonTest, '', sdk_getHeader(), false);
56+
57+
//echo $relogin."<br>";
58+
$loginResult = sdk_json_decode($relogin);
59+
60+
if ($loginResult['ErrorId'] == null) {
61+
$contextKey = $loginResult['LoginData']['ContextKey'];
62+
saveVariable('ContextKey', $contextKey);
63+
} else
64+
echo "Erreur de login " . "<br>";//TODO : handle exceptions*/
65+
66+
}
67+
68+
/**
69+
* Get informations from MelCloud
70+
* rebuild context if needed
71+
* rebuild buildingId and DeviceId if needed
72+
*
73+
* */
74+
function sdk_get($moduleId, $deviceName, $username, $password){
75+
76+
//echo "---------------------------------------------------------sdk_get<br>";
77+
//if no contextKey reconnect
78+
$contextKey = loadVariable('ContextKey');
79+
if ($contextKey == '')
80+
sdk_connect($username, $password);
81+
82+
//get building ids
83+
$buildingId = loadVariable($moduleId.'-buildingId');
84+
$deviceId = loadVariable($moduleId.'-deviceId');
85+
if ($buildingId == ''){
86+
sdk_setIds($moduleId, $deviceName, $username, $password);
87+
$buildingId = loadVariable($moduleId.'-buildingId');
88+
$deviceId = loadVariable($moduleId.'-deviceId');
89+
}
90+
91+
$json = httpQuery("https://app.melcloud.com/Mitsubishi.Wifi.Client/Device/Get?id=$deviceId&buildingID=$buildingId", "GET", "", "", sdk_getHeader(), false);
92+
93+
$result = sdk_json_decode($json);
94+
95+
//if (count($device) == 2) {//if no session only 2 datas are raised : { Success: false, ErrorMessage: ""}
96+
if (isset($result['Success']) && $result['Success'] == false) {
97+
sdk_connect($username, $password);
98+
$json = httpQuery("https://app.melcloud.com/Mitsubishi.Wifi.Client/Device/Get?id=$deviceId&buildingID=$buildingId", "GET", "", "", sdk_getHeader(), false);
99+
}
100+
101+
return $json;
102+
103+
}
104+
105+
//get devicename
106+
$deviceName = getArg('deviceName');
107+
108+
//use lock to avoid simultaneous calls
109+
$lockName = md5($deviceName).'-lock';
110+
$retry = 0;
111+
do {
112+
if($retry) {
113+
sleep(1);
114+
}
115+
$lock = loadVariable($lockName);
116+
$retry++;
117+
}while($lock && $retry<10);
118+
119+
if (empty($lock) || $lock-time() > 120) {
120+
saveVariable($lockName, time());
121+
}
122+
123+
//get User and password
124+
$userPass = getArg('userpass');
125+
126+
$tabUserPass = explode(":", $userPass);
127+
128+
$username = $tabUserPass[0];
129+
$password = $tabUserPass[1];
130+
131+
//get parameters
132+
133+
$onoff = getArg('power', false, "");
134+
$fanspeed = getArg('fanspeed', false, "");
135+
$temperature = getArg('temperature', false, "");
136+
$mode = getArg('mode', false, '');
137+
/*
138+
* 1 => Heating
139+
* 8 => Auto
140+
* 7 => Fan
141+
* 2 => Drying
142+
* 3 => Cooling
143+
*/
144+
145+
$moduleId = getArg('eedomus_controller_module_id');
146+
147+
/**
148+
* Session managing
149+
*
150+
* off => nothing recorded
151+
* contextOnly => buildingid and contextid not recorded
152+
* idsOnly => contextKey not recorded
153+
* azzerty => change contextKey to defaut value to test timeouts
154+
*
155+
*/
156+
157+
$resetSession = getArg('session', false, '');
158+
if ($resetSession == "off") {
159+
saveVariable('ContextKey', '');
160+
saveVariable($moduleId.'-buildingId', '');
161+
saveVariable($moduleId.'-deviceId', '');
162+
163+
}else if ($resetSession == "contextOnly") {
164+
saveVariable($moduleId.'-buildingId', '');
165+
saveVariable($moduleId.'-deviceId', '');
166+
167+
} else if ($resetSession == "idsOnly") {
168+
saveVariable('ContextKey', '');
169+
170+
sdk_connect($username, $password);
171+
} else if ($resetSession == "azerty") {
172+
saveVariable('ContextKey', 'azerty');
173+
}
174+
175+
$json = sdk_get($moduleId, $deviceName, $username, $password);
176+
177+
178+
$device = sdk_json_decode($json);
179+
$effectiveFlag = 0;
180+
181+
$resultat = "";
182+
183+
if ($onoff != "") {
184+
185+
$aremplacer = array('"Power":false', '"Power":true');
186+
$json = str_replace($aremplacer, '"Power":' . $onoff, $json);
187+
188+
$effectiveFlag += 1;
189+
190+
}
191+
if ($fanspeed != "") {
192+
193+
$aremplacer = '"SetFanSpeed":' . $device['SetFanSpeed'];
194+
$json = str_replace($aremplacer, '"SetFanSpeed":' . $fanspeed, $json);
195+
196+
$effectiveFlag += 8;
197+
198+
}
199+
if ($temperature != "") {
200+
201+
$aremplacer = '"SetTemperature":' . $device['SetTemperature'];
202+
$json = str_replace($aremplacer, '"SetTemperature":' . $temperature, $json);
203+
204+
$effectiveFlag += 4;
205+
206+
}
207+
if ($mode != "") {
208+
209+
$aremplacer = '"OperationMode":' . $device['OperationMode'];
210+
$json = str_replace($aremplacer, '"OperationMode":' . $mode, $json);
211+
212+
$effectiveFlag += 2;
213+
214+
}
215+
216+
$aremplacer = '"EffectiveFlags":' . $device['EffectiveFlags'];
217+
$json = str_replace($aremplacer, '"EffectiveFlags":'.$effectiveFlag, $json);
218+
219+
$aremplacer = '"HasPendingCommand":false';
220+
$json = str_replace($aremplacer, '"HasPendingCommand":true', $json);
221+
222+
$json = httpQuery('https://app.melcloud.com/Mitsubishi.Wifi.Client/Device/SetAta', 'POST', $json, '', sdk_getHeader(), false);
223+
saveVariable($lockName, '');
224+
225+
echo jsonToXML($json);
226+

0 commit comments

Comments
 (0)