Skip to content

Commit

Permalink
add device_monitor
Browse files Browse the repository at this point in the history
  • Loading branch information
nmred committed Jan 27, 2014
1 parent 0bb6366 commit 8f5f2af
Show file tree
Hide file tree
Showing 7 changed files with 321 additions and 33 deletions.
22 changes: 1 addition & 21 deletions src/lib/member/operator/device/sw_monitor.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public function get_monitor(\lib\member\condition\sw_get_device_monitor $conditi
{
$condition->check_params();
$select = $this->__db->select()
->from(SWAN_TBN_DEVICE_MONITOR);
->from(SWAN_TBN_DEVICE_MONITOR, null);
$condition->where($select, true);
return $this->_get($select, $condition->params());
}
Expand All @@ -123,26 +123,6 @@ public function mod_monitor(\lib\member\condition\sw_mod_device_monitor $conditi
return;
}

$property_key = $this->get_device_operator()->get_device_key_property();
$key_attributes = $property_key->attributes();

if (!isset($key_attributes['device_id'])) {
throw new sw_exception('Unknow device id.');
}

$monitor_basic_property = $monitor_property->get_monitor_basic();
$monitor_attribute_property = $monitor_property->get_monitor_attribute();
$monitor_basic = $monitor_basic_property->attributes();
$monitor_attributes = $monitor_attribute_property->attributes();
$attributes = $monitor_property->attributes();

// 判断是否已经存在
$this->exists($key_attributes['device_id'], $monitor_basic['monitor_id'], $monitor_attributes['attr_id']);

$attributes['device_id'] = $key_attributes['device_id'];
$attributes['monitor_id'] = $monitor_basic['monitor_id'];
$attributes['attr_id'] = $monitor_attributes['attr_id'];

$this->__db->update(SWAN_TBN_DEVICE_MONITOR, $attributes, $where);
}

Expand Down
5 changes: 4 additions & 1 deletion src/lib/member/operator/sw_device.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,13 @@ public function get_device(\lib\member\condition\sw_get_device $condition)
$params = $condition->params();
$key_columns = $condition->columns(SWAN_TBN_DEVICE_KEY);
$basic_columns = $condition->columns(SWAN_TBN_DEVICE_BASIC);
if (empty($basic_columns)) { // 没有指定查询关联的字段
$basic_columns = null;
}
$select = $this->__db->select()
->from(array('k' => SWAN_TBN_DEVICE_KEY), $key_columns)
->join(array('b' => SWAN_TBN_DEVICE_BASIC), "k.device_id = b.device_id", $basic_columns);
$condition->set_columns(null);
$condition->set_columns(array());
$condition->where($select, true);
return $this->_get($select, $condition->params());
}
Expand Down
78 changes: 68 additions & 10 deletions src/lib/swdata/action/user/sw_device.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,66 @@ public function action_add()
*/
public function action_del()
{
return $this->render_json(array('swan server!'), 10000);
$did = $this->__request->get_post('did', '');
if (!$did) {
return $this->render_json(null, 10001, '`did` not allow is empty.');
}

$db = \swan\db\sw_db::singleton();
$db->begin_transaction();
// 删除设备
$device = sw_member::operator_factory('device');
try {
$condition = sw_member::condition_factory('del_device_key', array('device_id' => $did));
$condition->set_in('device_id');
$device->get_operator('key')->del_key($condition);
} catch (\swan\exception\sw_exception $e) {
$db->rollback();
return $this->render_json(null, 10002, $e->getMessage());
}

try {
$condition = sw_member::condition_factory('del_device_basic', array('device_id' => $did));
$condition->set_in('device_id');
$device->get_operator('basic')->del_basic($condition);
} catch (\swan\exception\sw_exception $e) {
$db->rollback();
return $this->render_json(null, 10003, $e->getMessage());
}
$db->commit();
return $this->render_json(null, 10000, 'delete device success.');
}

// }}}
// {{{ public function action_mod()

/**
* 修改设备
*
* @access public
* @return void
*/
public function action_mod()
{
$did = $this->__request->get_post('did', '');
$display_name = $this->__request->get_post('display_name', '');
if (!$did) {
return $this->render_json(null, 10001, '`did` not allow is empty.');
}

// 修改 device basic
try {
$property_basic = sw_member::property_factory('device_basic', array('device_display_name' => $display_name));
$condition = sw_member::condition_factory('mod_device_basic', array('device_id' => $did));
$condition->set_in('device_id');
$condition->set_property($property_basic);
$device = sw_member::operator_factory('device');
$device->get_operator('basic')->mod_basic($condition);
} catch (\swan\exception\sw_exception $e) {
return $this->render_json(null, 10002, $e->getMessage());
}

return $this->render_json(null, 10000, 'mod device success.');
}

// }}}
Expand All @@ -105,10 +164,10 @@ public function action_json()
$page_count = $this->__request->get_post('page_count', 10);
$count = 0;
try {
$condition = sw_member::condition_factory('get_monitor_basic');
$condition = sw_member::condition_factory('get_device');
$condition->set_is_count(true);
$monitor = sw_member::operator_factory('monitor');
$count = $monitor->get_operator('basic')->get_basic($condition_basic);
$device = sw_member::operator_factory('device');
$count = $device->get_device($condition);
} catch (\swan\exception\sw_exception $e) {
return $this->render_json(null, 10001, $e->getMessage());
}
Expand All @@ -118,11 +177,10 @@ public function action_json()
}

try {
$condition_basic->set_is_count(false);
$condition_basic->set_columns('*');
$condition_basic->set_limit_page(array('page' => $page, 'rows_count' => $page_count));
$monitor = sw_member::operator_factory('monitor');
$data = $monitor->get_operator('basic')->get_basic($condition_basic);
$condition = sw_member::condition_factory('get_device');
$condition->set_is_count(false);
$condition->set_limit_page(array('page' => $page, 'rows_count' => $page_count));
$data = $device->get_device($condition);
} catch (\swan\exception\sw_exception $e) {
return $this->render_json(null, 10001, $e->getMessage());
}
Expand All @@ -132,7 +190,7 @@ public function action_json()
'count' => $count,
);

return $this->render_json($result, 10000, 'get monitor success.');
return $this->render_json($result, 10000, 'get device success.');
}

// }}}
Expand Down
187 changes: 187 additions & 0 deletions src/lib/swdata/action/user/sw_device_monitor.class.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
<?php
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4 foldmethod=marker: */
// +---------------------------------------------------------------------------
// | SWAN [ $_SWANBR_SLOGAN_$ ]
// +---------------------------------------------------------------------------
// | Copyright $_SWANBR_COPYRIGHT_$
// +---------------------------------------------------------------------------
// | Version $_SWANBR_VERSION_$
// +---------------------------------------------------------------------------
// | Licensed ( $_SWANBR_LICENSED_URL_$ )
// +---------------------------------------------------------------------------
// | $_SWANBR_WEB_DOMAIN_$
// +---------------------------------------------------------------------------

namespace lib\swdata\action\user;
use \lib\swdata\action\sw_abstract;
use lib\swdata\action\user\exception\sw_exception;
use \lib\member\sw_member;

/**
+------------------------------------------------------------------------------
* 设备监控器接口
+------------------------------------------------------------------------------
*
* @uses sw_abstract
* @package
* @version $_SWANBR_VERSION_$
* @copyright $_SWANBR_COPYRIGHT_$
* @author $_SWANBR_AUTHOR_$
+------------------------------------------------------------------------------
*/
class sw_device_monitor extends sw_abstract
{
// {{{ functions
// {{{ public function action_add()

/**
* 添加设备监控器
*
* @access public
* @return intger
*/
public function action_add()
{
$did = $this->__request->get_post('did', '');
$aid = $this->__request->get_post('aid', '');
$mid = $this->__request->get_post('mid', '');
$value = $this->__request->get_post('value', '');
if (!$did || !$aid || !$mid) {
return $this->render_json(null, 10001, '`aid`/`did`/`mid` not allow is empty.');
}

// 添加 device monitor
try {
$device_monitor_property = sw_member::property_factory('device_monitor', array('value' => $value));
$device_key_property = sw_member::property_factory('device_key', array('device_id' => $did));
$monitor_basic_property = sw_member::property_factory('monitor_basic', array('monitor_id' => $mid));
$monitor_attr_property = sw_member::property_factory('monitor_attribute', array('attr_id' => $aid));
$device_monitor_property->set_monitor_basic($monitor_basic_property);
$device_monitor_property->set_monitor_attribute($monitor_attr_property);
$device = sw_member::operator_factory('device', $device_key_property);
$value_id = $device->get_operator('monitor')->add_monitor($device_monitor_property);
} catch (\swan\exception\sw_exception $e) {
return $this->render_json(null, 10002, $e->getMessage());
}

return $this->render_json(array('did' => $did, 'aid' => $aid, 'vid' => $value_id, 'mid' => $mid), 10000, 'add device monitor attributes success.');
}

// }}}
// {{{ public function action_del()

/**
* 删除设备监控器
*
* @access public
* @return void
*/
public function action_del()
{
$vid = $this->__request->get_post('vid', '');
if (!$vid) {
return $this->render_json(null, 10001, '`vid` not allow is empty.');
}

// 删除设备
try {
$device = sw_member::operator_factory('device');
$condition = sw_member::condition_factory('del_device_monitor', array('value_id' => $vid));
$condition->set_in('value_id');
$device->get_operator('monitor')->del_monitor($condition);
} catch (\swan\exception\sw_exception $e) {
return $this->render_json(null, 10002, $e->getMessage());
}

return $this->render_json(null, 10000, 'delete device monitor success.');
}

// }}}
// {{{ public function action_mod()

/**
* 修改设备监控器
*
* @access public
* @return void
*/
public function action_mod()
{
$vid = $this->__request->get_post('vid', '');
$value = $this->__request->get_post('value', '');
if (!$vid) {
return $this->render_json(null, 10001, '`vid` not allow is empty.');
}

// 修改 device monitor
try {
$device_monitor_property = sw_member::property_factory('device_monitor', array('value' => $value));
$condition = sw_member::condition_factory('mod_device_monitor', array('value_id' => $vid));
$condition->set_in('value_id');
$condition->set_property($device_monitor_property);
$device = sw_member::operator_factory('device');
$device->get_operator('monitor')->mod_monitor($condition);
} catch (\swan\exception\sw_exception $e) {
return $this->render_json(null, 10002, $e->getMessage());
}

return $this->render_json(null, 10000, 'mod device monitor success.');
}

// }}}
// {{{ public function action_json()

/**
* 获取设备监控器列表
*
* @access public
* @return string
*/
public function action_json()
{
// 获取设备监控器
$did = $this->__request->get_post('did', '');
$page = $this->__request->get_post('page', 1);
$page_count = $this->__request->get_post('page_count', 10);
$count = 0;
try {
$condition = sw_member::condition_factory('get_device_monitor');
$condition->set_is_count(true);
if ($did) {
$condition->set_in('device_id');
$condition->set_device_id($did);
}
$device = sw_member::operator_factory('device');
$count = $device->get_operator('monitor')->get_monitor($condition);
} catch (\swan\exception\sw_exception $e) {
return $this->render_json(null, 10001, $e->getMessage());
}

if (!$count) {
return $this->render_json(array('count' => $count), 10001, 'no data.');
}

try {
$condition = sw_member::condition_factory('get_device_monitor');
if ($did) {
$condition->set_in('device_id');
$condition->set_device_id($did);
}
$condition->set_is_count(false);
$condition->set_limit_page(array('page' => $page, 'rows_count' => $page_count));
$data = $device->get_operator('monitor')->get_monitor($condition);
} catch (\swan\exception\sw_exception $e) {
return $this->render_json(null, 10001, $e->getMessage());
}

$result = array(
'result' => $data,
'count' => $count,
);

return $this->render_json($result, 10000, 'get device monitor success.');
}

// }}}
// }}}
}
18 changes: 18 additions & 0 deletions test/api/device.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,21 @@ function call($url, $type = 'GET', $params = array())
$rev = call($url, 'POST', array('name' => 'ss21222'));
$rev = json_decode($rev, true);
var_dump($rev);

//// 获取 device
$url = '127.0.0.1:9080/user/?/device.json';
$rev = call($url, 'POST');
$rev = json_decode($rev, true);
var_dump($rev);

// 删除 device
//$url = '127.0.0.1:9080/user/?/device.del';
//$rev = call($url, 'POST', array('did' => 2));
//$rev = json_decode($rev, true);
//var_dump($rev);

// 修改 device
$url = '127.0.0.1:9080/user/?/device.mod';
$rev = call($url, 'POST', array('did' => 3, 'display_name' => 'rerererer'));
$rev = json_decode($rev, true);
var_dump($rev);

0 comments on commit 8f5f2af

Please sign in to comment.