Guest User

Untitled

a guest
Feb 22nd, 2017
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 26.86 KB | None | 0 0
  1. #!/usr/bin/perl
  2. #Dependency: libwww-perl, libjson-perl
  3. # Original script : gysmo38
  4. # Modifications : bagnico
  5.  
  6. ###################################################
  7. # Release notes
  8. ###################################################
  9. # Version 0.4:
  10. # - add vane option horizontal
  11. # - Change report to domoticz for informations (MODE | T°C | TIME_NEXT_UPDATE)
  12. # - Change split time (remove .xxx)
  13. # Version 0.3:
  14. # - setDomDeviceMode OK
  15. # - Script works with no Areas
  16. # - add 1 hours for uptime (for french)
  17. # Version 0.2:
  18. # - add 2 hours for uptime time (for french)
  19. # - add mode option
  20. # - add fan option
  21. # - add vane option (only vertical)
  22. # - add settemp option
  23. # Version 0.1: First release (with very dirty code...)
  24. # - switch on / off unit
  25. # - report room temperature
  26. # - report unit temperature set
  27. # - report next unit update
  28.  
  29. use HTTP::Request::Common qw(POST GET);
  30. use HTTP::Headers;
  31. use LWP::UserAgent;
  32. use JSON;
  33. use Data::Dumper;
  34.  
  35. #####################################################
  36. # Configuration
  37. #####################################################
  38.  
  39. my $Email = "user\@mail.com"; # Email used to login to MELcloud
  40. my $Password = "password"; # Password used lo login to MELcloud
  41. my $debug = 1; # 0 = no debug, 1 = debug
  42.  
  43. my $domIP = "192.168.0.251"; # Domoticz IP
  44. my $domPORT = "8080"; # Domoticz PORT
  45.  
  46. my @devicesInfos; # List of all devices. Add block for new devices
  47. #Device SALON
  48. $devicesInfos{"SALON"}{"idx_status"} = "1104"; # IDX of status device (type selector)
  49. $devicesInfos{"SALON"}{"idx_temp"} = "658"; # IDX of temperature device (type temp)
  50. $devicesInfos{"SALON"}{"idx_info"} = "666"; # IDX of mode (type text)
  51.  
  52. #Device ETAGE
  53. $devicesInfos{"ETAGE"}{"idx_status"} = "2749"; # IDX of status device (type switch)
  54. $devicesInfos{"ETAGE"}{"idx_temp"} = "2748"; # IDX of temperature device (type temp)
  55. $devicesInfos{"ETAGE"}{"idx_info"} = "2747"; # IDX of mode (type text)
  56.  
  57.  
  58. ##########################################################
  59. # Functions
  60. ##########################################################
  61.  
  62. sub debug {
  63. my ($msg) = @_;
  64. if ($debug) {
  65. print $msg."\n";
  66. }
  67. }
  68.  
  69. ##########################################################
  70. # A activer pour l'heure d'hiver
  71. ##########################################################
  72. sub time_offset {
  73. my ($time,$offset) = @_;
  74. @split_time = split(/:/,$time);
  75. debug("Time: ".$split_time[0]);
  76. if ($split_time[0] <= 22) {
  77. $split_time[0] = $split_time[0] + 1;
  78. }
  79. elsif ($split_time[0] == 23) {
  80. $split_time[0] = 00;
  81. }
  82. $result = join(":",$split_time[0],$split_time[1],$split_time[2]);
  83. @timeok = split(/\./,$result);
  84. return $timeok[0];
  85. }
  86.  
  87. ##########################################################
  88. # A activer pour l'heure d'ete
  89. ##########################################################
  90. # sub time_offset {
  91. # my ($time,$offset) = @_;
  92. # @split_time = split(/:/,$time);
  93. # debug("Time: ".$split_time[0]);
  94. # if ($split_time[0] <= 21) {
  95. # $split_time[0] = $split_time[0] + 2;
  96. # }
  97. # elsif ($split_time[0] == 22) {
  98. # $split_time[0] = 00;
  99. # }
  100. # elsif ($split_time[0] == 23) {
  101. # $split_time[0] = 01;
  102. # }
  103. # $result = join(":",$split_time[0],$split_time[1],$split_time[2]);
  104. # @timeok = split(/\./,$result);
  105. # return $timeok[0];
  106. #}
  107.  
  108. sub getDOMDeviceStatus {
  109. my ($idx) = @_;
  110. my $url = "http://$domIP:$domPORT/json.htm?type=devices&rid=".$idx;
  111. my $ua = LWP::UserAgent->new();
  112. # Set our own user-agent string!
  113. $ua->agent("Domoticz Gysmo");
  114. require HTTP::Request;
  115. $req = HTTP::Request->new(GET => $url);
  116. # Fire the cannon now !
  117. my $res = $ua->request($req);
  118. # Get the error back from the server if any
  119. my $err = $res->status_line;
  120. # Get server body text, $_ used in regexp on next line
  121. $_ = $res->decoded_content;
  122.  
  123. if (/Illegal Operation/ig || $err != 200) {
  124. return "Server returned error: $err\n";
  125. }
  126. else {
  127. $result = $1 if /(?:Status\"\ :)+(.*?),/s;
  128. debug("receive from domoticz: ".$result);
  129. return $result;
  130. }
  131. }
  132.  
  133. sub login {
  134. #Parameters
  135. my ($Email,$Password) = @_;
  136. #Variables
  137. my $AppVersion = "1.9.3.0";
  138. my $Language = "7";
  139. my $CaptchaChallenge = "";
  140. my $CaptchaResponse = "";
  141. my $Persist = "true";
  142. my $url = "https://app.melcloud.com/Mitsubishi.Wifi.Client/Login/ClientLogin";
  143.  
  144. # set up the stuff
  145. my $ua = LWP::UserAgent->new();
  146. # Set our own user-agent string!
  147. $ua->agent("Domoticz Gysmo");
  148.  
  149. # file.cgi should just return the data sent for this test
  150. # These seem to be like <input type=text name=A value=$A > off a form...
  151. my $req = POST $url, [
  152. AppVersion => "$AppVersion",
  153. CaptchaChallenge => "$CaptchaChallenge",
  154. CaptchaResponse => "$CaptchaResponse",
  155. Email => "$Email",
  156. Language => "$Language",
  157. Password => "$Password",
  158. Persist => "$Persist"
  159. ];
  160.  
  161. # Fire the cannon now !
  162. my $res = $ua->request($req);
  163.  
  164. # Get the error back from the server if any
  165. my $err = $res->status_line;
  166. # Get server body text, $_ used in regexp on next line
  167. $_ = $res->decoded_content;
  168.  
  169. if (/Illegal Operation/ig || $err != 200) {
  170. return "Server returned error: $err\n";
  171. }
  172. elsif(/\"ErrorId\":1/ig) {
  173. return "Bad password\n";
  174. }
  175. elsif(/\"ErrorId\":null/ig) {
  176. $result = $1 if /(?:ContextKey\":\")+(.*?)\"/s;
  177. debug ("context ID: ".$result);
  178. return $result;
  179. }
  180. else {
  181. return "Unknow error";
  182. }
  183. }
  184.  
  185. sub getMELBuildingID {
  186. #Parameters
  187. my ($ContextKey,$devicename) = @_;
  188. #Variables
  189. my $url = "https://app.melcloud.com/Mitsubishi.Wifi.Client/User/ListDevices";
  190. # set up the stuff
  191. my $ua = LWP::UserAgent->new();
  192. # Set our own user-agent string!
  193. $ua->agent("Domoticz Gysmo");
  194. require HTTP::Request;
  195. $req = HTTP::Request->new(GET => $url);
  196. $req->header("X-MitsContextKey" => $ContextKey);
  197. # Fire the cannon now !
  198. my $res = $ua->request($req);
  199. # Get the error back from the server if any
  200. my $err = $res->status_line;
  201. # Get server body text, $_ used in regexp on next line
  202. $_ = $res->decoded_content;
  203. my $decoded = JSON->new->utf8(0)->decode($_);
  204. my $result = "0";
  205. foreach my $building (@$decoded) {
  206. @devices = @{$building->{'Structure'}{'Devices'}};
  207. foreach my $device (@devices) {
  208. if ( $device->{'DeviceName'} =~ $devicename) {
  209.  
  210. $result = $device->{'BuildingID'};
  211. }
  212. }
  213. # debug("BuildingID: ".Dumper($result));
  214. }
  215. if (/Illegal Operation/ig || $err != 200) {
  216. return "Server returned error: $err\n";
  217. }
  218. else {
  219. # debug("Building ID: ".Dumper($decoded));
  220. return "$result";
  221. }
  222. }
  223.  
  224. sub getMELDeviceID {
  225. #Parameters
  226. my ($ContextKey,$devicename) = @_;
  227. #Variables
  228. my $url = "https://app.melcloud.com/Mitsubishi.Wifi.Client/User/ListDevices";
  229. # set up the stuff
  230. my $ua = LWP::UserAgent->new();
  231. # Set our own user-agent string!
  232. $ua->agent("Domoticz Gysmo");
  233.  
  234. require HTTP::Request;
  235. $req = HTTP::Request->new(GET => $url);
  236. $req->header("X-MitsContextKey" => $ContextKey);
  237. # Fire the cannon now !
  238. my $res = $ua->request($req);
  239.  
  240. # Get the error back from the server if any
  241. my $err = $res->status_line;
  242. # Get server body text, $_ used in regexp on next line
  243. $_ = $res->decoded_content;
  244. my $decoded = JSON->new->utf8(0)->decode($_);
  245. my $result = "0";
  246. foreach my $building (@$decoded) {
  247. @devices = @{$building->{'Structure'}{'Devices'}};
  248. foreach my $device (@devices) {
  249. if ( $device->{'DeviceName'} =~ $devicename) {
  250.  
  251. $result = $device->{'DeviceID'};
  252. }
  253. }
  254. # debug("DeviceID: ".Dumper($result));
  255. }
  256. if (/Illegal Operation/ig || $err != 200) {
  257. return "Server returned error: $err\n";
  258. }
  259. else {
  260. # debug("Device ID: ".Dumper($decoded));
  261. return "$result";
  262. }
  263. }
  264.  
  265. sub listDevices {
  266. #Parameters
  267. my ($ContextKey) = @_;
  268. #Variables
  269. my $url = "https://app.melcloud.com/Mitsubishi.Wifi.Client/User/ListDevices";
  270. # set up the stuff
  271. my $ua = LWP::UserAgent->new();
  272. # Set our own user-agent string!
  273. $ua->agent("Domoticz Gysmo");
  274.  
  275. require HTTP::Request;
  276. $req = HTTP::Request->new(GET => $url);
  277. $req->header("X-MitsContextKey" => $ContextKey);
  278. # Fire the cannon now !
  279. my $res = $ua->request($req);
  280.  
  281. # Get the error back from the server if any
  282. my $err = $res->status_line;
  283. # Get server body text, $_ used in regexp on next line
  284. $_ = $res->decoded_content;
  285.  
  286. if (/Illegal Operation/ig || $err != 200) {
  287. return "Server returned error: $err\n";
  288. }
  289. else {
  290. $result = $_;
  291. debug("list devices: ".$result);
  292. return "$result";
  293. }
  294. }
  295.  
  296. sub getAirConInfos {
  297. #Parameters
  298. my ($contextKey,$deviceID,$buildingID) = @_;
  299. debug("getairconinfos : ".$contextKey,$deviceID,$buildingID);
  300. #Variables
  301. my $url = 'https://app.melcloud.com/Mitsubishi.Wifi.Client/Device/Get?id=' . $deviceID . '&buildingID=' . $buildingID;
  302. # debug("device status : ". $url);
  303. #return $url;
  304. # set up the stuff
  305. my $ua = LWP::UserAgent->new();
  306. # Set our own user-agent string!
  307. $ua->agent("Domoticz Gysmo");
  308.  
  309. require HTTP::Request;
  310.  
  311. $req = HTTP::Request->new(GET => $url);
  312. $req->header("X-MitsContextKey" => $contextKey);
  313.  
  314. # Fire the cannon now !
  315. my $res = $ua->request($req);
  316.  
  317. # Get the error back from the server if any
  318. my $err = $res->status_line;
  319. # Get server body text, $_ used in regexp on next line
  320. $json = $res->decoded_content;
  321. my $decoded = JSON->new->utf8(0)->decode($json);
  322. if (/Illegal Operation/ig || $err != 200) {
  323. return "Server returned error: $err\n";
  324. }
  325. else {
  326. # debug("device status : ". $decoded);
  327. return $decoded;
  328. }
  329. }
  330.  
  331. sub setDomDeviceStatus {
  332. #Parameters
  333. my ($idDomDevice,$deviceInfos) = @_;
  334. #Variables
  335. $domStatus = getDOMDeviceStatus($idDomDevice);
  336. if ($deviceInfos->{'Power'} =~ /true/ && $domStatus =~ /"Off"/) {
  337. $switchcmd = "On";
  338. my $url = "http://$domIP:$domPORT/json.htm?type=command&param=switchlight&idx=".$idDomDevice."&switchcmd=".$switchcmd;
  339. debug("send to domoticz: ".$url);
  340. my $ua = LWP::UserAgent->new();
  341. # Set our own user-agent string!
  342. $ua->agent("Domoticz Gysmo");
  343.  
  344. require HTTP::Request;
  345.  
  346. $req = HTTP::Request->new(GET => $url);
  347. # Fire the cannon now !
  348. my $res = $ua->request($req);
  349.  
  350. # Get the error back from the server if any
  351. my $err = $res->status_line;
  352. # Get server body text, $_ used in regexp on next line
  353. $_ = $res->decoded_content;
  354.  
  355. if (/Illegal Operation/ig || $err != 200) {
  356. return "Server returned error: $err\n";
  357. }
  358. else {
  359. $result = $1 if /(?:status\"\ :)+(.*?),/s;
  360. debug("receive from domoticz: ".$result);
  361. return $result;
  362. }
  363. }
  364. elsif ($deviceInfos->{'Power'} =~ /false/ && $domStatus =~ /"On"/) {
  365. $switchcmd = "Off";
  366. my $url = "http://$domIP:$domPORT/json.htm?type=command&param=switchlight&idx=".$idDomDevice."&switchcmd=".$switchcmd;
  367. debug("send to domoticz: ".$url);
  368. my $ua = LWP::UserAgent->new();
  369. # Set our own user-agent string!
  370. $ua->agent("Domoticz Gysmo");
  371.  
  372. require HTTP::Request;
  373.  
  374. $req = HTTP::Request->new(GET => $url);
  375. # Fire the cannon now !
  376. my $res = $ua->request($req);
  377.  
  378. # Get the error back from the server if any
  379. my $err = $res->status_line;
  380. # Get server body text, $_ used in regexp on next line
  381. $_ = $res->decoded_content;
  382.  
  383. if (/Illegal Operation/ig || $err != 200) {
  384. return "Server returned error: $err\n";
  385. }
  386. else {
  387. $result = $1 if /(?:status\"\ :)+(.*?),/s;
  388. debug("receive from domoticz: ".$result);
  389. return $result;
  390. }
  391. }
  392. else {
  393. debug("send to domoticz: nothing to send");
  394. }
  395.  
  396. }
  397.  
  398. sub setDomDeviceTempRoom {
  399. #Parameters
  400. my ($idDomDevice,$deviceInfos) = @_;
  401. my $url = "http://$domIP:$domPORT/json.htm?type=command&param=udevice&idx=".$idDomDevice."&nvalue=0&svalue=".$deviceInfos->{'RoomTemperature'};
  402. debug("send to domoticz: ".$url);
  403. my $ua = LWP::UserAgent->new();
  404. # Set our own user-agent string!
  405. $ua->agent("Domoticz Gysmo");
  406. require HTTP::Request;
  407. $req = HTTP::Request->new(GET => $url);
  408. # Fire the cannon now !
  409. my $res = $ua->request($req);
  410. # Get the error back from the server if any
  411. my $err = $res->status_line;
  412. # Get server body text, $_ used in regexp on next line
  413. $_ = $res->decoded_content;
  414. if (/Illegal Operation/ig || $err != 200) {
  415. return "Server returned error: $err\n";
  416. }
  417. else {
  418. $result = $1 if /(?:status\"\ :)+(.*?),/s;
  419. debug("receive from domoticz: ".$result);
  420. return $result;
  421. }
  422. }
  423.  
  424. sub setDomDeviceInfo {
  425. #Parameters
  426. my ($idDomDevice,$deviceInfos) = @_;
  427. my $mode = $deviceInfos->{'OperationMode'};
  428. @timenextupdate = split(/T/,$deviceInfos->{'NextCommunication'});
  429. if ( $mode == 1) {
  430. $modeclim = 'CHAUD';
  431. }
  432. elsif ( $mode == 2) {
  433. $modeclim = 'SECHAGE';
  434. }
  435. elsif ( $mode == 3) {
  436. $modeclim = 'FROID';
  437. }
  438. elsif ( $mode == 7) {
  439. $modeclim = 'VENTILATION';
  440. }
  441. elsif ( $mode == 8) {
  442. $modeclim = 'AUTO';
  443. }
  444. if ($deviceInfos->{'Power'} =~ /true/ ) {
  445. $powerclim = "ON";
  446. }
  447. elsif ($deviceInfos->{'Power'} =~ /false/ ) {
  448. $powerclim = "OFF";
  449. }
  450. my $url = "http://$domIP:$domPORT/json.htm?type=command&param=udevice&idx=".$idDomDevice."&nvalue=0&svalue=".$powerclim."%20|%20 ".$modeclim."%20|%20 ".$deviceInfos->{'SetTemperature'}."%20°C%20|%20".time_offset($timenextupdate[1],2);
  451. debug("send to domoticz: ".$url);
  452. my $ua = LWP::UserAgent->new();
  453. # Set our own user-agent string!
  454. $ua->agent("Domoticz Gysmo");
  455. require HTTP::Request;
  456. $req = HTTP::Request->new(GET => $url);
  457. # Fire the cannon now !
  458. my $res = $ua->request($req);
  459. # Get the error back from the server if any
  460. my $err = $res->status_line;
  461. # Get server body text, $_ used in regexp on next line
  462. $_ = $res->decoded_content;
  463. if (/Illegal Operation/ig || $err != 200) {
  464. return "Server returned error: $err\n";
  465. }
  466. else {
  467. $result = $1 if /(?:status\"\ :)+(.*?),/s;
  468. debug("receive from domoticz: ".$result);
  469. return $result;
  470. }
  471. }
  472.  
  473. sub setMELDevicePower {
  474. #Parameters
  475. my ($contextKey,$status,$deviceInfos,$idx) = @_;
  476. if($status =~ /on/ ) {
  477. $deviceInfos->{'Power'} = "true";
  478. }
  479. elsif($status =~ /off/ ) {
  480. $deviceInfos->{'Power'} = "false";
  481. }
  482. $deviceInfos->{'EffectiveFlags'} = "1";
  483. $deviceInfos->{'HasPendingCommand'} = "true";
  484. $json_device = JSON->new->utf8->encode($deviceInfos);
  485. #Variables
  486. my $url = 'https://app.melcloud.com/Mitsubishi.Wifi.Client/Device/SetAta';
  487. #return $url;
  488. # set up the stuff
  489. my $ua = LWP::UserAgent->new();
  490. # Set our own user-agent string!
  491. $ua->agent("Domoticz Gysmo");
  492.  
  493. require HTTP::Request;
  494. $req = HTTP::Request->new(POST => $url);
  495. $req->header("X-MitsContextKey" => $contextKey);
  496. $req->header('content-type' => 'application/json');
  497. $req->content($json_device);
  498. # Fire the cannon now !
  499. my $res = $ua->request($req);
  500.  
  501. # Get the error back from the server if any
  502. my $err = $res->status_line;
  503. # Get server body text, $_ used in regexp on next line
  504. $_ = $res->decoded_content;
  505. debug($_);
  506. if (/Illegal Operation/ig || $err != 200) {
  507. print "Server returned error: $err\n";
  508. }
  509. else {
  510. debug("Update setMELDevicePower : OK");
  511. }
  512. }
  513.  
  514. sub setMELDeviceFan {
  515. #Parameters
  516. my ($contextKey,$temp,$deviceInfos,$idx) = @_;
  517. $deviceInfos->{'SetFanSpeed'} = $temp;
  518. $deviceInfos->{'EffectiveFlags'} = "8";
  519. $deviceInfos->{'HasPendingCommand'} = "true";
  520. $json_device = JSON->new->utf8->encode($deviceInfos);
  521. #Variables
  522. my $url = 'https://app.melcloud.com/Mitsubishi.Wifi.Client/Device/SetAta';
  523. # set up the stuff
  524. my $ua = LWP::UserAgent->new();
  525. # Set our own user-agent string!
  526. $ua->agent("Domoticz Gysmo");
  527. require HTTP::Request;
  528. $req = HTTP::Request->new(POST => $url);
  529. $req->header("X-MitsContextKey" => $contextKey);
  530. $req->header('content-type' => 'application/json');
  531. $req->content($json_device);
  532. # Fire the cannon now !
  533. my $res = $ua->request($req);
  534. # Get the error back from the server if any
  535. my $err = $res->status_line;
  536. # Get server body text, $_ used in regexp on next line
  537. $_ = $res->decoded_content;
  538. if (/Illegal Operation/ig || $err != 200) {
  539. print "Server returned error: $err\n";
  540. }
  541. else {
  542. debug("Update setMELDeviceFan : OK");
  543. }
  544. }
  545.  
  546. sub setMELDeviceMode {
  547. #Parameters
  548. my ($contextKey,$mode,$deviceInfos,$idx) = @_;
  549. # Mode value : 1 warm, 2 dry, 3 cool, 7 vent, 8 auto
  550. if ($mode =~ warm) {
  551. $deviceInfos->{'OperationMode'} = 1;
  552. }
  553. elsif ($mode =~ dry) {
  554. $deviceInfos->{'OperationMode'} = 2;
  555. }
  556. elsif ($mode =~ cool) {
  557. $deviceInfos->{'OperationMode'} = 3;
  558. }
  559. elsif ($mode =~ vent) {
  560. $deviceInfos->{'OperationMode'} = 7;
  561. }
  562. elsif ($mode =~ auto) {
  563. $deviceInfos->{'OperationMode'} = 8;
  564. }
  565. $deviceInfos->{'EffectiveFlags'} = "6";
  566. $deviceInfos->{'HasPendingCommand'} = "true";
  567. $json_device = JSON->new->utf8->encode($deviceInfos);
  568. #Variables
  569. my $url = 'https://app.melcloud.com/Mitsubishi.Wifi.Client/Device/SetAta';
  570. # set up the stuff
  571. my $ua = LWP::UserAgent->new();
  572. # Set our own user-agent string!
  573. $ua->agent("Domoticz Gysmo");
  574. require HTTP::Request;
  575. $req = HTTP::Request->new(POST => $url);
  576. $req->header("X-MitsContextKey" => $contextKey);
  577. $req->header('content-type' => 'application/json');
  578. $req->content($json_device);
  579. # Fire the cannon now !
  580. my $res = $ua->request($req);
  581. # Get the error back from the server if any
  582. my $err = $res->status_line;
  583. # Get server body text, $_ used in regexp on next line
  584. $_ = $res->decoded_content;
  585. if (/Illegal Operation/ig || $err != 200) {
  586. print "Server returned error: $err\n";
  587. }
  588. else {
  589. debug("Update setMELDeviceMode : OK");
  590. }
  591. }
  592.  
  593. sub setMELDeviceVaneVertical {
  594. #Parameters
  595. my ($contextKey,$vane,$deviceInfos,$idx) = @_;
  596. $deviceInfos->{'VaneVertical'} = $vane;
  597. $deviceInfos->{'EffectiveFlags'} = "16";
  598. $deviceInfos->{'HasPendingCommand'} = "true";
  599. $json_device = JSON->new->utf8->encode($deviceInfos);
  600. #Variables
  601. my $url = 'https://app.melcloud.com/Mitsubishi.Wifi.Client/Device/SetAta';
  602. # set up the stuff
  603. my $ua = LWP::UserAgent->new();
  604. # Set our own user-agent string!
  605. $ua->agent("Domoticz Gysmo");
  606. require HTTP::Request;
  607. $req = HTTP::Request->new(POST => $url);
  608. $req->header("X-MitsContextKey" => $contextKey);
  609. $req->header('content-type' => 'application/json');
  610. $req->content($json_device);
  611. # Fire the cannon now !
  612. my $res = $ua->request($req);
  613. # Get the error back from the server if any
  614. my $err = $res->status_line;
  615. # Get server body text, $_ used in regexp on next line
  616. $_ = $res->decoded_content;
  617. if (/Illegal Operation/ig || $err != 200) {
  618. print "Server returned error: $err\n";
  619. }
  620. else {
  621. debug("Update setMELDeviceVaneVertical : OK");
  622. }
  623. }
  624.  
  625. sub setMELDeviceVaneHorizontal {
  626. #Parameters
  627. my ($contextKey,$vane,$deviceInfos,$idx) = @_;
  628. $deviceInfos->{'VaneHorizontal'} = $vane;
  629. $deviceInfos->{'EffectiveFlags'} = "16";
  630. $deviceInfos->{'HasPendingCommand'} = "true";
  631. $json_device = JSON->new->utf8->encode($deviceInfos);
  632. #Variables
  633. my $url = 'https://app.melcloud.com/Mitsubishi.Wifi.Client/Device/SetAta';
  634. # set up the stuff
  635. my $ua = LWP::UserAgent->new();
  636. # Set our own user-agent string!
  637. $ua->agent("Domoticz Gysmo");
  638. require HTTP::Request;
  639. $req = HTTP::Request->new(POST => $url);
  640. $req->header("X-MitsContextKey" => $contextKey);
  641. $req->header('content-type' => 'application/json');
  642. $req->content($json_device);
  643. # Fire the cannon now !
  644. my $res = $ua->request($req);
  645. # Get the error back from the server if any
  646. my $err = $res->status_line;
  647. # Get server body text, $_ used in regexp on next line
  648. $_ = $res->decoded_content;
  649. if (/Illegal Operation/ig || $err != 200) {
  650. print "Server returned error: $err\n";
  651. }
  652. else {
  653. debug("Update setMELDeviceVaneHorizontal : OK");
  654. }
  655. }
  656.  
  657. sub setMELDeviceTemp {
  658. #Parameters
  659. my ($contextKey,$temp,$deviceInfos,$idx) = @_;
  660. $deviceInfos->{'SetTemperature'} = $temp;
  661. $deviceInfos->{'EffectiveFlags'} = "4";
  662. $deviceInfos->{'HasPendingCommand'} = "true";
  663. $json_device = JSON->new->utf8->encode($deviceInfos);
  664. #Variables
  665. my $url = 'https://app.melcloud.com/Mitsubishi.Wifi.Client/Device/SetAta';
  666. # set up the stuff
  667. my $ua = LWP::UserAgent->new();
  668. # Set our own user-agent string!
  669. $ua->agent("Domoticz Gysmo");
  670. require HTTP::Request;
  671. $req = HTTP::Request->new(POST => $url);
  672. $req->header("X-MitsContextKey" => $contextKey);
  673. $req->header('content-type' => 'application/json');
  674. $req->content($json_device);
  675. # Fire the cannon now !
  676. my $res = $ua->request($req);
  677. # Get the error back from the server if any
  678. my $err = $res->status_line;
  679. # Get server body text, $_ used in regexp on next line
  680. $_ = $res->decoded_content;
  681. if (/Illegal Operation/ig || $err != 200) {
  682. print "Server returned error: $err\n";
  683. }
  684. else {
  685. debug("Update setMELDeviceTemp : OK");
  686. }
  687. }
  688.  
  689. sub init_mel {
  690. #Parameters
  691. my ($search_name) = @_;
  692. $contextID = login($Email,$Password);
  693. foreach my $name (keys %devicesInfos) {
  694. if ( $name =~ $search_name ) {
  695. $buildingID = getMELBuildingID($contextID,$name);
  696. $deviceID = getMELDeviceID($contextID,$name);
  697. }
  698. }
  699. }
  700.  
  701. #####################################################
  702. # Main program
  703. #####################################################
  704.  
  705.  
  706. #####################################################
  707. # Arguments
  708. #####################################################
  709. if ($ARGV[0] =~ /report/) {
  710. $contextID = login($Email,$Password);
  711. foreach my $name (keys %devicesInfos) {
  712. $buildingID = getMELBuildingID($contextID,$name);
  713. $deviceID = getMELDeviceID($contextID,$name);
  714. debug("############################");
  715. debug("# Reporting device: " . $name ." #");
  716. debug("############################");
  717. $infosAirCon = getAirConInfos($contextID,$deviceID,$buildingID);
  718. setDomDeviceTempRoom($devicesInfos{$name}{"idx_temp"},$infosAirCon);
  719. setDomDeviceStatus($devicesInfos{$name}{"idx_status"},$infosAirCon);
  720. setDomDeviceInfo($devicesInfos{$name}{"idx_info"},$infosAirCon);
  721. }
  722. }
  723. elsif ($ARGV[0] =~ /test/) {
  724. init_mel($ARGV[1]);
  725. debug("Send command to building:" . $buildingID);
  726. debug("Send command to device:" . $deviceID);
  727.  
  728. }
  729.  
  730. elsif ($ARGV[0] =~ /temp/ && exists $devicesInfos{$ARGV[2]}{'idx_status'}) {
  731. init_mel($ARGV[2]);
  732. @temp = split(/:/,$ARGV[1]);
  733. $infosAirCon = getAirConInfos($contextID,$deviceID,$buildingID);
  734. setMELDeviceTemp($contextID,$temp[0],$infosAirCon,$devicesInfos{$ARGV[2]}{'idx_update'});
  735. }
  736. elsif ($ARGV[0] =~ /fan/ && exists $devicesInfos{$ARGV[2]}{'idx_status'}) {
  737. init_mel($ARGV[2]);
  738. debug("Send commande to device:" . $buildingID);
  739. $infosAirCon = getAirConInfos($contextID,$deviceID,$buildingID);
  740. setMELDeviceFan($contextID,$ARGV[1],$infosAirCon,$devicesInfos{$ARGV[2]}{'idx_update'});
  741. }
  742. elsif ($ARGV[0] =~ /power/ && exists $devicesInfos{$ARGV[2]}{'idx_status'}) {
  743. init_mel($ARGV[2]);
  744. $infosAirCon = getAirConInfos($contextID,$deviceID,$buildingID);
  745. setMELDevicePower($contextID,$ARGV[1],$infosAirCon,$devicesInfos{$ARGV[2]}{'idx_update'});
  746. }
  747. elsif ($ARGV[0] =~ /mode/ && exists $devicesInfos{$ARGV[2]}{'idx_status'}) {
  748. init_mel($ARGV[2]);
  749. $infosAirCon = getAirConInfos($contextID,$deviceID,$buildingID);
  750. setMELDeviceMode($contextID,$ARGV[1],$infosAirCon,$devicesInfos{$ARGV[2]}{'idx_update'});
  751. }
  752. elsif ($ARGV[0] =~ /vanevertical/ && exists $devicesInfos{$ARGV[2]}{'idx_status'}) {
  753. init_mel($ARGV[2]);
  754. $infosAirCon = getAirConInfos($contextID,$deviceID,$buildingID);
  755. setMELDeviceVaneVertical($contextID,$ARGV[1],$infosAirCon,$devicesInfos{$ARGV[2]}{'idx_update'});
  756. }
  757. elsif ($ARGV[0] =~ /vanehorizontal/ && exists $devicesInfos{$ARGV[2]}{'idx_status'}) {
  758. init_mel($ARGV[2]);
  759. $infosAirCon = getAirConInfos($contextID,$deviceID,$buildingID);
  760. setMELDeviceVaneHorizontal($contextID,$ARGV[1],$infosAirCon,$devicesInfos{$ARGV[2]}{'idx_update'});
  761. }
  762. elsif ($ARGV[0] =~ /help/) {
  763. print "-------------------------- MELCLOUD PERL FOR DOMOTICZ v0.4 --------------------------\n";
  764. print "report: send status of devices to domoticz. Use it for crontab\n";
  765. print "power on/off DEVICENAME: power on or off a device.\n";
  766. print "mode modename DEVICENAME: choose mode warm,dry,cool,vent or auto\n";
  767. print "temp T° DEVICENAME: set temperature device. \n";
  768. print "fan FAN_SPEED DEVICENAME: set fan speed of the device. \n";
  769. print "vanevertical VANE_POS DEVICENAME: set van position to 1 - 5, 0 to auto and 7 to move . \n";
  770. print "vanehorizontal VANE_POS DEVICENAME: set van position to 1 - 5, 0 to auto and 12 to move . \n";
  771. print "more to come...\n";
  772. }
  773. else {
  774. print "Nothing to do. Type help for help ;)\n";
  775. }
Advertisement
Add Comment
Please, Sign In to add comment