-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathMiGenericSocket.php
More file actions
108 lines (92 loc) · 3.18 KB
/
Copy pathMiGenericSocket.php
File metadata and controls
108 lines (92 loc) · 3.18 KB
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
106
107
108
<?php
/*
* Homegear Xiaomi Smarthome V0.1 for homegear 0.7.x
* (c) Frank Motzkau 2017
*/
include_once 'MiConstants.php';
include_once 'MiBaseDevice.php';
class MiGenericSocket extends MiBaseDevice
{
const STATE_OFF = 0;
const STATE_ON = 1;
const STATE_UNKNOWN = 2;
private $_type_id;
private $_status;
private $_load_voltage;
private $_load_power;
private $_power_consumed;
public function __construct($config, $model)
{
$this->_model = $model;
switch ($model)
{
case MiConstants::MODEL_PLUG:
$this->_type_id = 0x28f0;
break;
case MiConstants::MODEL_86PLUG:
case MiConstants::MODEL_CTRL_86PLUG:
case MiConstants::MODEL_CTRL_86PLUG_AQ1:
$this->_type_id = 0x28f1;
break;
default:
$this->_model = MiConstants::MODEL_UNKNOWN;
break;
}
//parent::__construct($config);
}
public function getTypeId() { return $this->_type_id; }
public function updateData($hg, $data)
{
//parent::updateData($hg, $data);
// update heartbeat timestamp
$hg->setValue($this->_peerId, 0, 'HEARTBEAT', time());
if (property_exists($data, 'status'))
{
switch ($data->status)
{
case 'unknown':
$hg->setValue($this->_peerId, 1, 'STATE', MiGenericSocket::STATE_UNKNOWN);
break;
case 'on':
$hg->setValue($this->_peerId, 1, 'STATE', MiGenericSocket::STATE_ON);
break;
case 'off':
$hg->setValue($this->_peerId, 1, 'STATE', MiGenericSocket::STATE_OFF);
break;
}
}
if ($this->setProperty($data, 'load_voltage'))
{
$hg->setValue($this->_peerId, 1, 'LOAD_VOLTAGE', intval($data->load_voltage));
}
if ($this->setProperty($data, 'load_power'))
{
$hg->setValue($this->_peerId, 1, 'LOAD_POWER', floatval($data->load_power));
}
if ($this->setProperty($data, 'power_consumed'))
{
$hg->setValue($this->_peerId, 1, 'POWER_CONSUMED', floatval($data->power_consumed));
}
}
public function updateEvent($hg, $event)
{
$result = FALSE;
if (($event['TYPE'] == 'event')
&& ($event['PEERID'] == $this->_peerId)
&& ($event['VARIABLE'] == 'STATE'))
{
$new_state = intval($event['VALUE']);
if ($new_state == MiGenericSocket::STATE_OFF || $new_state == MiGenericSocket::STATE_ON)
{
$state = $hg->getValue($this->_peerId, 1, 'STATE');
if ($new_state!=$state)
{
$hg->setValue($this->_peerId, 1, 'STATE', $new_state);
$result = parent::updateEvent($hg, $event);
$result->data->status = ($new_state ? 'on' : 'off');
}
}
}
return $result;
}
}