From a541f3abff9494fda792cbfd62dbca10b06fc95c Mon Sep 17 00:00:00 2001 From: jiahui Date: Wed, 8 Apr 2020 23:05:31 +0800 Subject: [PATCH 1/2] add support for chuangmiplug --- chuangmiplug-sample.php | 52 +++++++++++ devices/chuangmiplug.class.php | 155 +++++++++++++++++++++++++++++++++ 2 files changed, 207 insertions(+) create mode 100644 chuangmiplug-sample.php create mode 100644 devices/chuangmiplug.class.php diff --git a/chuangmiplug-sample.php b/chuangmiplug-sample.php new file mode 100644 index 0000000..eb3edfd --- /dev/null +++ b/chuangmiplug-sample.php @@ -0,0 +1,52 @@ +enableAutoMsgID(); + +echo PHP_EOL . date('H:i:s', time()); + +if($plug->getStatus($cmd_id)) { + echo ' Статус получен.' . 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 on.' . 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 ; diff --git a/devices/chuangmiplug.class.php b/devices/chuangmiplug.class.php new file mode 100644 index 0000000..0810581 --- /dev/null +++ b/devices/chuangmiplug.class.php @@ -0,0 +1,155 @@ + ''); + + 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; + } + + } + +} From a6bae53c8253852b0093f1cda5eaf3c6bf7835ed Mon Sep 17 00:00:00 2001 From: jiahui Date: Wed, 8 Apr 2020 23:57:58 +0800 Subject: [PATCH 2/2] fix typo --- chuangmiplug-sample.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/chuangmiplug-sample.php b/chuangmiplug-sample.php index eb3edfd..8d33133 100644 --- a/chuangmiplug-sample.php +++ b/chuangmiplug-sample.php @@ -27,7 +27,7 @@ echo PHP_EOL . date('H:i:s', time()); if($plug->getStatus($cmd_id)) { - echo ' Статус получен.' . PHP_EOL; + echo ' Status Received.' . PHP_EOL; echo 'Power: ' . $plug->status['power'] . PHP_EOL; $cmd_id += 1; sleep(2); @@ -44,7 +44,7 @@ sleep(2); echo PHP_EOL . date('H:i:s', time()); - if($plug->powerOff($cmd_id)) echo ' The plug is on.' . PHP_EOL; + 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);