Skip to content

Commit

Permalink
Merge pull request #11 from chel0vert/monitor
Browse files Browse the repository at this point in the history
merge monitor branch
  • Loading branch information
chel0vert committed Sep 29, 2023
2 parents c257fa5 + 35a1dc4 commit 4528b1d
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 39 deletions.
9 changes: 5 additions & 4 deletions modules/LGsmartthinq/LGAPI.php
Expand Up @@ -69,7 +69,7 @@ function lgedm_post($url = '', $data = array(), $add_headers = Null, $data_root
$result = Null;

$json_request = $this->generate_json_request($data, $data_root);
debmes($json_request);
//debmes($json_request);
do {

$headers = $this->headers($add_headers);
Expand Down Expand Up @@ -537,6 +537,7 @@ function set_devices()
$url = $this->api_root . "/service/homes/" . $homeId;
$data = array();
$result = $this->lgedm_get($url, $data);
//print_r( $result);
if ($result && count($result->devices) > 0) {
$home_devices = $this->get_items($result);
$devices = array_merge($devices, $home_devices);
Expand Down Expand Up @@ -646,7 +647,7 @@ function signature($url, $date){
$message_bytes = utf8_encode($message);
$result = hash_hmac('sha1', $message_bytes, $secret_bytes, true);
$result = base64_encode($result);
debmes($result);
debmes($result, 'lgsmarthinq');
return $result;
}

Expand Down Expand Up @@ -761,8 +762,8 @@ function get_device_config($device, $category = Null, $command='Get', $value='')
);

$response = $this->lgedm_post($url, $data, Null, 'lgedmRoot');
#print_r($response);
#echo "\n";
//print_r($response);
//echo "\n";
$code = $response->returnCd;
$result = Null;
if ($code == '0000') {
Expand Down
90 changes: 55 additions & 35 deletions modules/LGsmartthinq/LGsmartthinq.class.php
Expand Up @@ -395,6 +395,7 @@ function processCycle()
} else {
$devices = $this->api->get_devices(); # получение устройств с api
foreach ($devices as $device) {
//print_r($device);
$device_id = $this->getMJDDeviceId($device);
if (!$device_id) {
$device_id = $this->addDevice2MJD($device);
Expand All @@ -406,8 +407,20 @@ function processCycle()
}
}
if ($device_id) {
$values = SQLSelect("SELECT * FROM lgsmarthinq_values WHERE DEVICE_ID='$device_id'");
foreach($values as $val){
$prop[$val['TITLE']] = $val;
}
foreach ($device as $key => $value) {
$this->set_device_property($device_id, $key, $value);
$data = json_decode(json_encode($value), true);
//print_r($data);
if(isset($data['washerDryer'])){
//print_r($data);
foreach ($data['washerDryer'] as $key1 => $value1){
$this->set_device_property($device_id, $key1, $value1, $prop);
}
}
else $this->set_device_property($device_id, $key, $value, $prop);
}

if (isset($device) && $device->deviceState != 'D') { # $device->deviceState == 'E' значит включена
Expand All @@ -427,10 +440,10 @@ function processCycle()
} while ($try < 5);
if ($decoded_properties) {
foreach ($decoded_properties as $key => $value) {
#print_r($key);
#print_r($value);
//print_r($key);
//print_r($value);
echo $key . " => " . $value . "\n";
$this->set_device_property($device_id, $key, $value);
$this->set_device_property($device_id, $key, $value, $prop);
}
}
$this->api->monitor_stop($device->deviceId);
Expand Down Expand Up @@ -500,7 +513,7 @@ function dbInstall($data)
lgsmarthinq_devices: UPDATED datetime
lgsmarthinq_values: ID int(10) unsigned NOT NULL auto_increment
lgsmarthinq_values: TITLE varchar(100) NOT NULL DEFAULT ''
lgsmarthinq_values: VALUE varchar(255) NOT NULL DEFAULT ''
lgsmarthinq_values: VALUE varchar(255) DEFAULT ''
lgsmarthinq_values: DEVICE_ID int(10) NOT NULL DEFAULT '0'
lgsmarthinq_values: LINKED_OBJECT varchar(100) NOT NULL DEFAULT ''
lgsmarthinq_values: LINKED_PROPERTY varchar(100) NOT NULL DEFAULT ''
Expand Down Expand Up @@ -609,50 +622,57 @@ function get_device_property($id, $property)
return $result;
}

function set_device_property($id, $property, $value)
function set_device_property($id, $property, $value, $values="")
{

if (!$id || !$property || is_object($value) || !is_string($property)) {
return Null;
} else if (is_array($value)) {
$value = json_encode($value);
}
$values = SQLSelectOne("SELECT * FROM lgsmarthinq_values WHERE DEVICE_ID='$id' and TITLE='$property'");
$device_values = SQLSelectOne("SELECT * FROM lgsmarthinq_devices WHERE ID='$id'");
$device_linked_object = $device_values['LINKED_OBJECT'];
if (isset($values) && isset($values['ID'])) {
$values['VALUE'] = $value;
if (!$values['LINKED_PROPERTY']) {
$values['LINKED_PROPERTY'] = $property;
}
if (!$values['LINKED_OBJECT']) {
$values['LINKED_OBJECT'] = $device_linked_object;
}
SQLUpdate('lgsmarthinq_values', $values);

$value = json_encode($value);
}

if (isset($values[$property]) && isset($values[$property]['ID'])) {
$values = $values[$property];
//if (!$values['LINKED_PROPERTY']) {
// $values['LINKED_PROPERTY'] = $property;
//}
//if (!$values['LINKED_OBJECT']) {
// $values['LINKED_OBJECT'] = $device_linked_object;
//}
if($values['VALUE']!= $value){
$linked_object = $values['LINKED_OBJECT'];
//if (!$linked_object) {
// $linked_object = $device_linked_object;
//}
if (isset($linked_object)) {
sg("$linked_object.$property", $value);
$linked_method = $values['LINKED_METHOD'];
if (isset($linked_method)) {
$params['VALUE'] = $value;
$params['OLD_VALUE'] = $values['VALUE'];
callMethodSafe("$linked_object.$linked_method", $params);
}
}
$values['VALUE'] = isset($values['VALUE'])? $value:0;
$values['UPDATED'] = date('Y-m-d H:i:s');
SQLUpdate('lgsmarthinq_values', $values);
}
} else {
$device_values = SQLSelectOne("SELECT * FROM lgsmarthinq_devices WHERE ID='$id'");
$device_linked_object = $device_values['LINKED_OBJECT'];
$values = array(
'TITLE' => $property,
'DEVICE_ID' => $id,
'VALUE' => $value,
'LINKED_PROPERTY' => $property,
'LINKED_OBJECT' => $device_values['LINKED_OBJECT'],
'LINKED_OBJECT' => isset($device_values['LINKED_OBJECT']) ? $device_values['LINKED_OBJECT'] : 0,
'UPDATED' => date('Y-m-d H:i:s'),
);
#debmes("insert device id($id) property $property => $value", 'lgsmarthinq');
SQLInsert('lgsmarthinq_values', $values);
}

$linked_object = $values['LINKED_OBJECT'];
if (!$linked_object) {
$linked_object = $device_linked_object;
}

if (isset($linked_object)) {
sg("$linked_object.$property", $value);
$linked_method = $values['LINKED_METHOD'];
if (isset($linked_method)) {
callMethodSafe("$linked_object.$linked_method");
}
}


return $values;
}

Expand Down

0 comments on commit 4528b1d

Please sign in to comment.