Exemplo n.º 1
0
 /**
 * Title
 *
 * Description
 *
 * @access public
 */
 function setProperty($property, $value, $no_linked = 0)
 {
     startMeasure('setProperty');
     startMeasure('setProperty (' . $property . ')');
     $id = $this->getPropertyByName($property, $this->class_id, $this->id);
     $old_value = '';
     if ($id) {
         $prop = SQLSelectOne("SELECT * FROM properties WHERE ID='" . $id . "'");
         $v = SQLSelectOne("SELECT * FROM pvalues WHERE PROPERTY_ID='" . (int) $id . "' AND OBJECT_ID='" . (int) $this->id . "'");
         $old_value = $v['VALUE'];
         $v['VALUE'] = $value;
         if ($v['ID']) {
             $v['UPDATED'] = date('Y-m-d H:i:s');
             if ($old_value != $value) {
                 SQLUpdate('pvalues', $v);
                 //DebMes("Setting [".$this->object_title.".".$property."] to new value [".$value."]");
             } else {
                 SQLExec("UPDATE pvalues SET UPDATED='" . $v['UPDATED'] . "' WHERE ID='" . $v['ID'] . "'");
                 //DebMes("Setting [".$this->object_title.".".$property."] to the same value [".$value."]");
             }
         } else {
             $v['PROPERTY_ID'] = $id;
             $v['OBJECT_ID'] = $this->id;
             $v['VALUE'] = $value;
             $v['UPDATED'] = date('Y-m-d H:i:s');
             $v['ID'] = SQLInsert('pvalues', $v);
         }
         //DebMes(" $id to $value ");
     } else {
         $prop = array();
         $prop['OBJECT_ID'] = $this->id;
         $prop['TITLE'] = $property;
         $prop['ID'] = SQLInsert('properties', $prop);
         $v['PROPERTY_ID'] = $prop['ID'];
         $v['OBJECT_ID'] = $this->id;
         $v['VALUE'] = $value;
         $v['UPDATED'] = date('Y-m-d H:i:s');
         $v['ID'] = SQLInsert('pvalues', $v);
     }
     if ($prop['KEEP_HISTORY'] > 0) {
         startMeasure('DeleteOldHistory');
         SQLExec("DELETE FROM phistory WHERE VALUE_ID='" . $v['ID'] . "' AND TO_DAYS(NOW())-TO_DAYS(ADDED)>" . (int) $prop['KEEP_HISTORY']);
         endMeasure('DeleteOldHistory', 1);
         $h = array();
         $h['VALUE_ID'] = $v['ID'];
         $h['ADDED'] = date('Y-m-d H:i:s');
         $h['VALUE'] = $value;
         $h['ID'] = SQLInsert('phistory', $h);
     }
     /*
      $h=array();
      $h['ADDED']=date('Y-m-d H:i:s');
      $h['OBJECT_ID']=$this->id;
      $h['VALUE_ID']=$v['ID'];
      $h['OLD_VALUE']=$old_value;
      $h['NEW_VALUE']=$value;
      SQLInsert('history', $h);
     */
     //commands, owproperties, snmpproperties, zwave_properties, mqtt
     $tables = array('commands', 'owproperties', 'snmpproperties', 'zwave_properties', 'mqtt', 'modbusdevices');
     if (!is_array($no_linked) && $no_linked) {
         $no_linked = array();
         foreach ($tables as $t) {
             $no_linked[$k] = '0';
         }
     } elseif (is_array($no_linked)) {
         foreach ($tables as $t) {
             if (!isset($no_linked[$k])) {
                 $no_linked[$k] = '1';
             }
         }
     } else {
         $no_linked = array();
         foreach ($tables as $t) {
             $no_linked[$k] = '1';
         }
     }
     foreach ($tables as $t) {
         if ($no_linked[$t] == '') {
             $no_linked[$t] = '1';
         }
     }
     if ($no_linked['commands'] != '') {
         $commands = SQLSelect("SELECT * FROM commands WHERE LINKED_OBJECT LIKE '" . DBSafe($this->object_title) . "' AND LINKED_PROPERTY LIKE '" . DBSafe($property) . "' AND " . $no_linked['commands']);
         $total = count($commands);
         for ($i = 0; $i < $total; $i++) {
             $commands[$i]['CUR_VALUE'] = $value;
             SQLUpdate('commands', $commands[$i]);
         }
     }
     if ($no_linked['owproperties'] != '' && file_exists(DIR_MODULES . '/onewire/onewire.class.php')) {
         $owp = SQLSelect("SELECT ID FROM owproperties WHERE LINKED_OBJECT LIKE '" . DBSafe($this->object_title) . "' AND LINKED_PROPERTY LIKE '" . DBSafe($property) . "' AND " . $no_linked['owproperties']);
         $total = count($owp);
         if ($total) {
             include_once DIR_MODULES . '/onewire/onewire.class.php';
             $on_wire = new onewire();
             for ($i = 0; $i < $total; $i++) {
                 $on_wire->setProperty($owp[$i]['ID'], $value);
             }
         }
     }
     if ($no_linked['snmpproperties'] != '' && file_exists(DIR_MODULES . '/snmpdevices/snmpdevices.class.php')) {
         $snmpdevices = SQLSelect("SELECT ID FROM snmpproperties WHERE LINKED_OBJECT LIKE '" . DBSafe($this->object_title) . "' AND LINKED_PROPERTY LIKE '" . DBSafe($property) . "' AND " . $no_linked['snmpproperties']);
         $total = count($snmpdevices);
         if ($total) {
             include_once DIR_MODULES . '/snmpdevices/snmpdevices.class.php';
             $snmp = new snmpdevices();
             for ($i = 0; $i < $total; $i++) {
                 $snmp->setProperty($snmpdevices[$i]['ID'], $value);
             }
         }
     }
     if ($no_linked['zwave_properties'] != '' && file_exists(DIR_MODULES . '/zwave/zwave.class.php')) {
         $zwave_properties = SQLSelect("SELECT ID FROM zwave_properties WHERE LINKED_OBJECT LIKE '" . DBSafe($this->object_title) . "' AND LINKED_PROPERTY LIKE '" . DBSafe($property) . "' AND " . $no_linked['zwave_properties']);
         $total = count($zwave_properties);
         if ($total) {
             include_once DIR_MODULES . '/zwave/zwave.class.php';
             $zwave = new zwave();
             for ($i = 0; $i < $total; $i++) {
                 $zwave->setProperty($zwave_properties[$i]['ID'], $value);
             }
         }
     }
     if ($no_linked['mqtt'] != '' && file_exists(DIR_MODULES . '/mqtt/mqtt.class.php')) {
         $mqtt_properties = SQLSelect("SELECT ID FROM mqtt WHERE LINKED_OBJECT LIKE '" . DBSafe($this->object_title) . "' AND LINKED_PROPERTY LIKE '" . DBSafe($property) . "' AND " . $no_linked['mqtt']);
         $total = count($mqtt_properties);
         if ($total) {
             include_once DIR_MODULES . '/mqtt/mqtt.class.php';
             $mqtt = new mqtt();
             for ($i = 0; $i < $total; $i++) {
                 $mqtt->setProperty($mqtt_properties[$i]['ID'], $value);
             }
         }
     }
     if ($no_linked['modbusdevices'] != '' && file_exists(DIR_MODULES . '/modbus/modbus.class.php')) {
         $modbusdevices = SQLSelect("SELECT ID FROM modbusdevices WHERE LINKED_OBJECT LIKE '" . DBSafe($this->object_title) . "' AND LINKED_PROPERTY LIKE '" . DBSafe($property) . "' AND " . $no_linked['modbusdevices']);
         $total = count($modbusdevices);
         if ($total) {
             include_once DIR_MODULES . '/modbus/modbus.class.php';
             $modbus = new modbus();
             for ($i = 0; $i < $total; $i++) {
                 $modbus->poll_device($modbusdevices[$i]['ID']);
             }
         }
     }
     if ($prop['ONCHANGE']) {
         global $property_linked_history;
         if (!$property_linked_history[$property][$prop['ONCHANGE']]) {
             $property_linked_history[$property][$prop['ONCHANGE']] = 1;
             global $on_change_called;
             $params = array();
             $params['PROPERTY'] = $property;
             $params['NEW_VALUE'] = (string) $value;
             $params['OLD_VALUE'] = (string) $old_value;
             $this->callMethod($prop['ONCHANGE'], $params);
             unset($property_linked_history[$property][$prop['ONCHANGE']]);
         }
     }
     endMeasure('setProperty (' . $property . ')', 1);
     endMeasure('setProperty', 1);
 }
Exemplo n.º 2
0
<?php

chdir(dirname(__FILE__) . '/../');
include_once "./config.php";
include_once "./lib/loader.php";
include_once "./lib/threads.php";
set_time_limit(0);
// connecting to database
$db = new mysql(DB_HOST, '', DB_USER, DB_PASSWORD, DB_NAME);
include_once "./load_settings.php";
include_once DIR_MODULES . "control_modules/control_modules.class.php";
// ...
set_time_limit(0);
require "./lib/mqtt/phpMQTT.php";
include_once DIR_MODULES . "mqtt/mqtt.class.php";
$mqtt = new mqtt();
$mqtt->getConfig();
if ($mqtt->config['MQTT_AUTH']) {
    $username = $mqtt->config['MQTT_USERNAME'];
    $password = $mqtt->config['MQTT_PASSWORD'];
}
if ($mqtt->config['MQTT_HOST']) {
    $host = $mqtt->config['MQTT_HOST'];
} else {
    $host = 'localhost';
}
if ($mqtt->config['MQTT_PORT']) {
    $port = $mqtt->config['MQTT_PORT'];
} else {
    $port = 1883;
}