Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add support for chuangmiplug #9

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions chuangmiplug-sample.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php
/*
* ChuangMiPlug via miIO protocol.
*
* Copyright (C) 2017 Agaphonov Dmitri aka skysilver [mailto:skysilver.da@gmail.com]
*/

include_once('./devices/chuangmiplug.class.php');

error_reporting(-1);
ini_set('display_errors', 1);

$ip = '192.168.2.100';
$token = 'd3f180ade2ea8266db00521637caeffb';
$bind_ip = null;
$debug = false;

$cmd_id = 100;

$plug = new chuangMiPlug($ip, $bind_ip, $token, $debug);

// By default, the ID of all commands is 1.
// If you need to auto-generate unique IDs for commands from the id.json file, then call the enableAutoMsgID () function.
// Or you can generate IDs dynamically and pass their values ​​to the function explicitly.
// $plug->enableAutoMsgID();

echo PHP_EOL . date('H:i:s', time());

if($plug->getStatus($cmd_id)) {
echo ' Status Received.' . PHP_EOL;
echo 'Power: ' . $plug->status['power'] . PHP_EOL;
$cmd_id += 1;
sleep(2);

echo PHP_EOL . date('H:i:s', time()) . PHP_EOL;
echo $plug->getInfo($cmd_id) . PHP_EOL;
$cmd_id += 1;
sleep(2);

echo PHP_EOL . date('H:i:s', time());
if($plug->powerOn($cmd_id)) echo ' The plug is on.' . PHP_EOL;
else echo " The plug is not connected. Error: $plug->error" . PHP_EOL;
$cmd_id += 1;
sleep(2);

echo PHP_EOL . date('H:i:s', time());
if($plug->powerOff($cmd_id)) echo ' The plug is off.' . PHP_EOL;
else echo " The plug is not connected. Error: $plug->error" . PHP_EOL;
$cmd_id += 1;
sleep(2);

} else echo " Plug status not received. Error: $plug->error" . PHP_EOL ;
155 changes: 155 additions & 0 deletions devices/chuangmiplug.class.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
<?php
/*
* Класс для работы с wifi-лампами Philips Light Bulb по протоколу miIO.
*
* Copyright (C) 2017 Agaphonov Dmitri aka skysilver [mailto:skysilver.da@gmail.com]
*
*/

include_once('miio.class.php');

class chuangMiPlug {

public $ip = '';
public $token = '';
public $debug = '';
public $error = '';

public $status = array('power' => '');

public $dev = NULL;

public function __construct($ip = NULL, $bind_ip = NULL, $token = NULL, $debug = false) {

$this->ip = $ip;
$this->token = $token;
$this->debug = $debug;

if ($bind_ip != NULL) $this->bind_ip = $bind_ip;
else $this->bind_ip = '0.0.0.0';

$this->dev = new miIO($this->ip, $this->bind_ip, $this->token, $this->debug);

}

/*
Activates auto-generation of unique message IDs with their saving to the id.json file
*/

public function enableAutoMsgID() {

$this->dev->useAutoMsgID = true;

}

/*
Deactivates auto-generation of unique message IDs with their saving to the id.json file
Message IDs must be passed as an argument each time a command is sent,
or do not specify at all, then the ID will be 1 for all messages.
*/

public function disableAutoMsgID() {

$this->dev->useAutoMsgID = false;

}

/*
Get Advanced Information
*/

public function getInfo($msg_id = 1) {

if ($this->dev->getInfo($msg_id)) return $this->dev->data;
else return false;

}

/*
Get current status:
power - power (on or off)
*/

public function getStatus($msg_id = 1) {

$result = $this->dev->msgSendRcv('get_prop', '["power"]', $msg_id);

if ($result) {
if ($this->dev->data != '') {
$res = json_decode($this->dev->data);
if (isset($res->{'result'})) {
$i = 0;
foreach($this->status as $key => $value) {
$this->status[$key] = $res->{'result'}[$i];
$i++;
}
return true;
} else if (isset($res->{'error'})) {
$this->error = $res->{'error'}->{'message'};
return false;
}
} else {
$this->error = 'No data';
return false;
}
} else {
$this->error = 'No response received';
return false;
}

}

/*
Power On
*/

public function powerOn($msg_id = 1) {

$result = $this->dev->msgSendRcv('set_power', '["on"]', $msg_id);
return $this->verify($result);

}

/*
Power Off
*/

public function powerOff($msg_id = 1) {

$result = $this->dev->msgSendRcv('set_power', '["off"]', $msg_id);
return $this->verify($result);

}


/*
Response Check
*/

private function verify ($result) {

if ($result) {
if ($this->dev->data != '') {
$res = json_decode($this->dev->data);
if (isset($res->{'result'})) {
if ($res->{'result'}[0] == 'ok') return true;
if ($res->{'result'}[0] == 'error') {
$this->error = 'Unknown error.';
return false;
}
} else if (isset($res->{'error'})) {
$this->error = $res->{'error'}->{'message'};
return false;
}
} else {
$this->error = 'Нет данных';
return false;
}
} else {
$this->error = 'Ответ не получен';
return false;
}

}

}