function updateDevice($id) { if (!defined('ONEWIRE_SERVER')) { return 0; } $rec = SQLSelectOne("SELECT * FROM owdevices WHERE ID='" . $id . "'"); if (!$rec['ID']) { return 0; } $ow = new OWNet(ONEWIRE_SERVER); $device = '/' . $rec['UDID']; $rec['CHECK_LATEST'] = date('Y-m-d H:i:s'); $rec['CHECK_NEXT'] = date('Y-m-d H:i:s', time() + (int) $rec['ONLINE_INTERVAL']); $old_status = $rec['STATUS']; $tmp = $ow->get($device, OWNET_MSG_DIR, false); if (!$tmp) { $rec['STATUS'] = 0; } else { $rec['STATUS'] = 1; } SQLUpdate('owdevices', $rec); if ($rec['STATUS'] != $old_status && ($rec['SCRIPT_ID'] || $rec['CODE'])) { $params = array(); $params['DEVICE'] = $device; $params['STATUS'] = $rec['STATUS']; $params['STATUS_CHANGED'] = 1; if ($rec['SCRIPT_ID']) { runScript($rec['SCRIPT_ID'], $params); } elseif ($rec['CODE']) { try { $code = $rec['CODE']; $success = eval($code); if ($success === false) { DebMes("Error in 1-wire action code: " . $code); } } catch (Exception $e) { DebMes('Error: exception ' . get_class($e) . ', ' . $e->getMessage() . '.'); } } } if (!$rec['STATUS']) { return 0; } $changed_values = array(); $changed = 0; $properties = explode(',', $tmp); $totalp = count($properties); for ($ip = 0; $ip < $totalp; $ip++) { $sysname = str_replace($device . '/', '', $properties[$ip]); //echo $properties[$ip]." (".$sysname."): "; $prec = SQLSelectOne("SELECT * FROM owproperties WHERE DEVICE_ID='" . $rec['ID'] . "' AND SYSNAME='" . DBSafe($sysname) . "'"); if (!$prec['ID']) { $prec['DEVICE_ID'] = $rec['ID']; $prec['SYSNAME'] = $sysname; $prec['PATH'] = $properties[$ip]; $prec['ID'] = SQLInsert('owproperties', $prec); } $old_value = $prec['VALUE']; $value = $ow->get($properties[$ip], OWNET_MSG_READ, false); if (is_null($value)) { $ow->get("/", OWNET_MSG_DIR, false); // hack. for some reason it didn't work correct without it on some devices $value = $ow->get($properties[$ip], OWNET_MSG_READ, false); } if (!is_null($value)) { $value = iconv("CP1251", "UTF-8", $value); // value updated $prec['VALUE'] = $value; $prec['UPDATED'] = date('Y-m-d H:i:s'); $prec['CHECK_LATEST'] = $prec['UPDATED']; SQLUpdate('owproperties', $prec); //$rec['LOG']=date('Y-m-d H:i:s')." ".$prec['SYSNAME'].": ".$prec['VALUE']."\n".$rec['LOG']; //SQLUpdate('owdevices', $rec); if ($prec['LINKED_OBJECT'] && $prec['LINKED_PROPERTY']) { setGlobal($prec['LINKED_OBJECT'] . '.' . $prec['LINKED_PROPERTY'], $prec['VALUE'], array($this->name => '0')); } if ($old_value != $value) { $changed = 1; $changed_values[$prec['SYSNAME']] = array('OLD_VALUE' => $old_value, 'VALUE' => $prec['VALUE']); } } } if ($changed) { $params = $changed_values; $params['DEVICE'] = $device; if ($rec['SCRIPT_ID']) { runScript($rec['SCRIPT_ID'], $params); } elseif ($rec['CODE']) { try { $code = $rec['CODE']; $success = eval($code); if ($success === false) { DebMes("Error in code: " . $code); } } catch (Exception $e) { DebMes('Error: exception ' . get_class($e) . ', ' . $e->getMessage() . '.'); } } } }
//check to make sure the file exists if (!function_exists('bcadd')) { if (file_exists("/opt/owfs/bin/bcadd.php")) { require "/opt/owfs/bin/bcadd.php"; } else { if (file_exists("bcadd.php")) { require "bcadd.php"; } else { die("File 'bcadd.php' is not found.\n"); } } } //check to make sure the file exists if (file_exists("/opt/owfs/bin/ownet.php")) { require "/opt/owfs/bin/ownet.php"; } else { if (file_exists("ownet.php")) { require "ownet.php"; } else { die("File 'ownet.php' is not found.\n"); } } $ow = new OWNet("tcp://127.0.0.1:1234"); var_dump($ow->get("/", OWNET_MSG_DIR, true)); var_dump($ow->get("/10.67C6697351FF/temperature", OWNET_MSG_READ, true)); var_dump($ow->get("/10.67C6697351FF", OWNET_MSG_PRESENCE, true)); var_dump($ow->get("/WRONG VALUE", OWNET_MSG_PRESENCE, true)); var_dump($ow->get("/", OWNET_MSG_DIR, false)); var_dump($ow->get("/10.67C6697351FF/temperature", OWNET_MSG_READ, false)); var_dump($ow->get("/10.67C6697351FF", OWNET_MSG_PRESENCE, false)); var_dump($ow->get("/WRONG VALUE", OWNET_MSG_PRESENCE, false));
function port20031_gettemp($num_temp, $climat) { global $wpdb; //read name temp datchik SELECT `name1wire` FROM `wp_port20031_name1wire` WHERE `id` = $table_name = $wpdb->prefix . "port20031_name1wire"; $sql = "SELECT `name1wire` FROM `" . $table_name . "` WHERE `id` =" . $num_temp . ""; $res = $wpdb->get_col($sql, 0); $val = $res[0]; $port20031_pathownet = "" . get_option('port20031_pathownet') . ""; if (file_exists($port20031_pathownet)) { //echo "<br>Файл $port20031_pathownet существует<br>"; //require_once $port20031_pathownet; } else { $port20031_pathownet = "ownet.php"; } require_once $port20031_pathownet; //update_option('port20031_pathownet2', $port20031_pathownet); //require_once "/opt/owfs/share/php/OWNet/ownet.php"; //require_once "/opt/owfs/share/php/OWNet/ownet.php"; unset($ow); $port20031_portownet = get_option('port20031_portownet'); $ow = new OWNet("tcp://localhost:" . $port20031_portownet . ""); $temp1 = null; $i = 900; while ($temp1 == null) { $temp1 = $ow->read($val . "/temperature"); $i = $i + 1; if (1000 == $i) { $temp1 = $i; } } unset($ow); if ($temp1 > $climat + 1) { return 0; } elseif ($temp1 < $climat) { return 1; } else { return 0; } }
/** * Query %OWNet daemon */ public function Query() { $host = $this->GetHost(); $port = $this->GetPort(); $connect = "{$host}:{$port}"; $ow = new OWNet("tcp://" . $connect); if ($ow) { //we are connected, proceed $this->debug(__FUNCTION__, "Connected to '{$connect}'"); // retrieve owfs directory from given root $ow_dir = $ow->dir($this->ow_path); if ($ow_dir && isset($ow_dir['data_php'])) { //walk through the retrieved tree $dirs = explode(",", $ow_dir['data_php']); if (is_array($dirs) && count($dirs) > 0) { foreach ($dirs as $dev) { #print_r($ow->dir($dev)); $data = array(); $caps = ''; /* read standard device details */ //get family id $fam = $ow->read("{$dev}/family"); if (!$fam) { continue; } //not a device path //get device id $id = $ow->read("{$dev}/id"); //get alias (if any) and owfs detected device description as type $alias = $ow->get("{$dev}/alias"); $type = $ow->get("{$dev}/type"); if (!$type) { $type = "1Wire Family " . $fam; } //assign names for ips categories $name = $id; if ($alias) { $name = $alias; $caps = 'Name'; } //save date $data['Date'] = date('Y-m-d H:i:s', time()); //get varids $addr = "{$fam}.{$id}"; //print "$id ($alias): Type $type Family $fam\n"; //retrieve device specific data switch ($fam) { case '28': //DS18B20 temperature sensors //DS18B20 temperature sensors case '10': //DS18S20 temperature sensors //DS18S20 temperature sensors case '22': //DS1820 temperature sensors $temp = trim($ow->read("{$dev}/temperature", true)); if (strlen($temp) > 0) { //store new temperature value $this->debug(__FUNCTION__, "{$type} {$id} ({$alias}): {$temp}"); $data['Name'] = $name; $data['Id'] = $addr; $data['Typ'] = $type; $data['Temp'] = sprintf("%4.1f", $temp); //print " Alias '$alias',Temp $temp\n"; $caps .= ';Temp'; $this->SendWSData($caps, $data); $this->log_data($data); } break; default: IPS_LogMessage(__CLASS__, "{$id} ({$alias}): Type {$type} Family {$fam} not implemented yet"); } } //for } else { //no device fount IPS_LogMessage(__CLASS__, "No 1Wire Device found"); } } else { //dir command failed, stop here $this->debug(__FUNCTION__, "Dir using {$this->ow_path}' failed"); } } else { //no object, connect has been failed, stop here IPS_LogMessage(__CLASS__, "Connect to '{$connect}' failed"); } }