Skip to content

Commit

Permalink
E-Mail notifications framework, discovery fixes, logging tweaks.
Browse files Browse the repository at this point in the history
  • Loading branch information
jmptbl committed Mar 29, 2013
1 parent 09b13b4 commit 7c2e5c9
Show file tree
Hide file tree
Showing 4 changed files with 330 additions and 37 deletions.
17 changes: 14 additions & 3 deletions api/discover.php
Original file line number Diff line number Diff line change
Expand Up @@ -169,11 +169,11 @@ private function _discover_ros_wif ($val) {
$iface['bridgename'] = $props[17];
}
}
$iface['addresses'] = array();
if ($propc > 18) {
if ($propc > 24) {
$propc = 24;
}
$iface['addresses'] = array();
for ($i = 18; $i < $propc; $i++) {
if (!preg_match('|^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}/[0-9]{1,2}$|', $props[$i])) {
continue;
Expand All @@ -188,8 +188,19 @@ private function _discover_ros_wif ($val) {
protected function _discover_ros () {
$device = $this->_device;
if (!$device->load()) {
$this->_log(LOG_INFO, __FUNCTION__ . '(): adding new device');
}
$this->_log(LOG_NOTICE, __FUNCTION__ . '(): adding new device');
}
$device->addInterface(null);
$device->set('name', null);
$device->set('arch', null);
$device->set('cpu', null);
$device->set('cpufreq', null);
$device->set('board', null);
$device->set('model', null);
$device->set('bootver', null);
$device->set('osver', null);
$device->set('contact', null);
$device->set('ct', null);
foreach ($_REQUEST as $key => $val) {
if (substr($key, 0, 3) == 'wif') {
$jump = array($this, '_discover_ros_wif');
Expand Down
2 changes: 1 addition & 1 deletion api/update.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ protected function _doLog_ros () {
return false;
}
if (!$this->_device->load()) {
$this->_log(LOG_NOTICE, 'adding new device');
$this->_log(LOG_NOTICE, __FUNCTION__ . '(): adding new device');
$oldver = 0;
} else {
$oldver = $this->_device->updatever;
Expand Down
135 changes: 102 additions & 33 deletions lib/device.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php
require_once($_SERVER['WMS_PATH'] . '/notify.php');

class WMS_Device {
private $_wms;
private $_savedDevice;
Expand Down Expand Up @@ -203,10 +205,16 @@ private function _save_add_device () {
$param[':serial'] = $this->serial;
}
foreach ($this->_deviceColumns as $col) {
if ($this->$col) {
if (!is_null($this->$col)) {
$sqlcol .= ', ' . $col;
$sqlval .= ', :' . $col;
$param[':'.$col] = $this->$col;
if ($col == 'contact') {
$notify = new WMS_Notify('newdevice');
$notify->set('device', $this->$idfield);
$notify->set('devicename', $this->name);
$notify->set('address', $this->contact);
}
}
$savedDevice[$col] = $this->$col;
}
Expand All @@ -224,43 +232,21 @@ private function _save_add_device () {
. 'DB error getting device insertion ID');
return false;
}
if (isset($notify)) {
if (!$notify->send()) {
throw new Exception(__CLASS__ . '::' . __FUNCTION__ . '(): '
. 'error sending notification for: ' . $this->_platform . '/' . $this->$idfield);
return false;
}
}
$this->_savedDevice = $savedDevice;
return $this->_save_add_interfaces();
return true;
}

public function save () {
private function _save_update_interfaces () {
$db = $this->_wms->getDb();
$idfield = $this->_idfield;
if (is_null($this->_savedDevice)) {
$this->_load_device();
}
if (!$this->_savedDevice) {
$this->_save_add_device();
return true;
}
$devsql = '';
$devpar = array();
$savedDevice = $this->_savedDevice;
foreach ($this->_deviceColumns as $col) {
if ($this->$col != $savedDevice[$col]) {
$devsql .= ', ' . $col . '=:' . $col;
$devpar[':'.$col] = $this->$col;
$savedDevice[$col] = $this->$col;
}
}
$sql = 'UPDATE device SET lastseen=NOW()' . $devsql;
$sql .= ' WHERE id=:deviceid';
$devpar[':deviceid'] = $savedDevice['id'];
$st = $db->prepare($sql);
if (!$st->execute($devpar)) {
throw new Exception(__CLASS__ . '::' . __FUNCTION__ . '(): '
. 'DB error updating device for: ' . $this->_platform . '/' . $this->$idfield);
return false;
}
$this->_savedDevice = $savedDevice;
if (!$this->interfaces) {
return true;
}
if (!isset($savedDevice['interfaces'])) {
if (!$this->_load_interfaces()) {
return false;
Expand Down Expand Up @@ -330,6 +316,76 @@ public function save () {
return true;
}

private function _save_update_device () {
$db = $this->_wms->getDb();
$idfield = $this->_idfield;
$savedDevice = $this->_savedDevice;
$devsql = '';
$devpar = array();
foreach ($this->_deviceColumns as $col) {
if ($this->$col !== $savedDevice[$col]) {
if ($col == 'contact') {
$notify = new WMS_Notify('newcontact');
$notify->set('device', $this->$idfield);
$notify->set('devicename', $this->name);
if (isset($savedDevice['contact']{0})) {
$notify->set('oldaddress', $savedDevice['contact']);
}
if (isset($this->contact{0})) {
$notify->set('newaddress', $this->contact);
}
}
$devsql .= ', ' . $col . '=:' . $col;
$devpar[':'.$col] = $this->$col;
$savedDevice[$col] = $this->$col;
}
}
$sql = 'UPDATE device SET lastseen=NOW()' . $devsql;
$sql .= ' WHERE id=:deviceid';
$devpar[':deviceid'] = $savedDevice['id'];
$st = $db->prepare($sql);
if (!$st->execute($devpar)) {
throw new Exception(__CLASS__ . '::' . __FUNCTION__ . '(): '
. 'DB error updating device for: ' . $this->_platform . '/' . $this->$idfield);
return false;
}
if (isset($notify)) {
if (!$notify->send()) {
throw new Exception(__CLASS__ . '::' . __FUNCTION__ . '(): '
. 'error sending notification for: ' . $this->_platform . '/' . $this->$idfield);
return false;
}
}
$this->_savedDevice = $savedDevice;
return true;
}

public function save () {
$db = $this->_wms->getDb();
$idfield = $this->_idfield;
if (is_null($this->_savedDevice)) {
$this->_load_device();
}
if ($this->_savedDevice) {
if (!$this->_save_update_device()) {
return false;
}
if (is_array($this->interfaces)) {
if (!$this->_save_update_interfaces()) {
return false;
}
}
} else {
if (!$this->_save_add_device()) {
return false;
}
if (!$this->_save_add_interfaces()) {
return false;
}
}
return true;
}

public function load () {
if (is_null($this->_savedDevice)) {
$this->_load_device();
Expand Down Expand Up @@ -444,17 +500,30 @@ public function set ($prop, $val) {
if (!is_callable($jump)) {
return false;
}
if ($val === null) {
$this->$prop = null;
return true;
}
if (!call_user_func($jump, $val)) {
return false;
}
return true;
}

public function reset () {
foreach ($this->_deviceColumns as $col) {
$this->$col = null;
}
$this->interfaces = null;
}

public function addInterface ($interface) {
if (!$this->interfaces) {
$this->interfaces = array();
}
$this->interfaces[] = $interface;
if (is_array($interface)) {
$this->interfaces[] = $interface;
}
}

private function _pxlen2netmask ($pxlen) {
Expand Down
Loading

0 comments on commit 7c2e5c9

Please sign in to comment.