From 2888142e682bbc9950535d7e5aaef2cd20cda38d Mon Sep 17 00:00:00 2001 From: arjunrc Date: Tue, 10 May 2016 16:55:43 -0400 Subject: [PATCH 1/5] added status command to retrieve alarmed status of monitor in addition to on/of --- web/api/app/Controller/MonitorsController.php | 79 ++++++++++++++++++- 1 file changed, 78 insertions(+), 1 deletion(-) diff --git a/web/api/app/Controller/MonitorsController.php b/web/api/app/Controller/MonitorsController.php index 9ab7461f9..d87b115e8 100644 --- a/web/api/app/Controller/MonitorsController.php +++ b/web/api/app/Controller/MonitorsController.php @@ -97,7 +97,7 @@ public function beforeFilter() { if ($this->Session->Read('systemPermission') != 'Edit') { - throw new UnauthotizedException(__('Insufficient privileges')); + throw new UnauthorizedException(__('Insufficient privileges')); return; } @@ -183,6 +183,83 @@ public function beforeFilter() { )); } + // arm/disarm alarms + // expected format: http(s):/portal-api-url/monitors/alarm/id:M/command:C.json + // where M=monitorId + // where C=on|off|status + public function alarm() + { + $id = $this->request->params['named']['id']; + $cmd = strtolower($this->request->params['named']['command']); + if (!$this->Monitor->exists($id)) { + throw new NotFoundException(__('Invalid monitor')); + } + if ( $cmd != 'on' && $cmd != 'off' && $cmd != 'status') + { + throw new BadRequestException(__('Invalid command')); + } + $zm_path_bin = Configure::read('ZM_PATH_BIN'); + + switch ($cmd) + { + case "on": + $q = '-a'; + break; + case "off": + $q = "-c"; + break; + case "status": + $q = "-s"; + break; + } + + // form auth key based on auth credentials + $this->loadModel('Config'); + $options = array('conditions' => array('Config.' . $this->Config->primaryKey => 'ZM_OPT_USE_AUTH')); + $config = $this->Config->find('first', $options); + $zmOptAuth = $config['Config']['Value']; + + + $options = array('conditions' => array('Config.' . $this->Config->primaryKey => 'ZM_AUTH_RELAY')); + $config = $this->Config->find('first', $options); + $zmAuthRelay = $config['Config']['Value']; + + $auth=""; + if ($zmOptAuth) + { + if ($zmAuthRelay == 'hashed') + { + $options = array('conditions' => array('Config.' . $this->Config->primaryKey => 'ZM_AUTH_HASH_SECRET')); + $config = $this->Config->find('first', $options); + $zmAuthHashSecret = $config['Config']['Value']; + + $time = localtime(); + $ak = $zmAuthHashSecret.$this->Session->Read('username').$this->Session->Read('passwordHash').$time[2].$time[3].$time[4].$time[5]; + $ak = md5($ak); + $auth = " -A ".$ak; + } + elseif ($zmAuthRelay == 'plain') + { + $auth = " -U " .$this->Session->Read('username')." -P ".$this->Session->Read('password'); + + } + elseif ($zmAuthRelay == 'none') + { + $auth = " -U " .$this->Session->Read('username'); + } + } + + $shellcmd = escapeshellcmd("$zm_path_bin/zmu -v -m$id $q $auth"); + $status = exec ($shellcmd); + + $this->set(array( + 'status' => $status, + '_serialize' => array('status'), + )); + + + } + // Check if a daemon is running for the monitor id public function daemonStatus() { $id = $this->request->params['named']['id']; From 8d5f2a8e5dbc946fb652cb530ded953a766a5192 Mon Sep 17 00:00:00 2001 From: arjunrc Date: Tue, 10 May 2016 19:17:09 -0400 Subject: [PATCH 2/5] added iconnor's change back --- web/api/app/Controller/MonitorsController.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/web/api/app/Controller/MonitorsController.php b/web/api/app/Controller/MonitorsController.php index d87b115e8..63849af69 100644 --- a/web/api/app/Controller/MonitorsController.php +++ b/web/api/app/Controller/MonitorsController.php @@ -138,7 +138,8 @@ public function beforeFilter() { '_serialize' => array('message') )); // - restart this monitor after change - $this->daemonControl($this->Monitor->id, 'restart', $this->request->data); + // We don't pass the request data as the monitor object because it may be a subset of the full monitor array + $this->daemonControl( $this->Monitor->id, 'restart' ); } /** From af3cae578e501694e7fc2e15bf668736055a3f3d Mon Sep 17 00:00:00 2001 From: arjunrc Date: Tue, 10 May 2016 20:07:28 -0400 Subject: [PATCH 3/5] fixed verbose handling bug for status command on zmu --- web/api/app/Controller/MonitorsController.php | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/web/api/app/Controller/MonitorsController.php b/web/api/app/Controller/MonitorsController.php index 63849af69..cda5081d7 100644 --- a/web/api/app/Controller/MonitorsController.php +++ b/web/api/app/Controller/MonitorsController.php @@ -138,8 +138,7 @@ public function beforeFilter() { '_serialize' => array('message') )); // - restart this monitor after change - // We don't pass the request data as the monitor object because it may be a subset of the full monitor array - $this->daemonControl( $this->Monitor->id, 'restart' ); + $this->daemonControl($this->Monitor->id, 'restart', $this->request->data); } /** @@ -205,11 +204,14 @@ public function beforeFilter() { { case "on": $q = '-a'; + $verbose = "-v"; break; case "off": $q = "-c"; + $verbose = "-v"; break; case "status": + $verbose = ""; // zmu has a bug - gives incorrect verbose output in this case $q = "-s"; break; } @@ -250,7 +252,7 @@ public function beforeFilter() { } } - $shellcmd = escapeshellcmd("$zm_path_bin/zmu -v -m$id $q $auth"); + $shellcmd = escapeshellcmd("$zm_path_bin/zmu $verbose -m$id $q $auth"); $status = exec ($shellcmd); $this->set(array( From b513bcef821a0423eee2cffe7073b8c58a36a9f1 Mon Sep 17 00:00:00 2001 From: arjunrc Date: Tue, 10 May 2016 20:09:41 -0400 Subject: [PATCH 4/5] icon's gonna kill me --- web/api/app/Controller/MonitorsController.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/web/api/app/Controller/MonitorsController.php b/web/api/app/Controller/MonitorsController.php index cda5081d7..893410ce8 100644 --- a/web/api/app/Controller/MonitorsController.php +++ b/web/api/app/Controller/MonitorsController.php @@ -138,7 +138,8 @@ public function beforeFilter() { '_serialize' => array('message') )); // - restart this monitor after change - $this->daemonControl($this->Monitor->id, 'restart', $this->request->data); + // We don't pass the request data as the monitor object because it may be a subset of the full monitor array + $this->daemonControl( $this->Monitor->id, 'restart' ); } /** From 8cb2692bbbb695421024ff7eebb57dcdb1b5635f Mon Sep 17 00:00:00 2001 From: Isaac Connor Date: Fri, 13 May 2016 11:09:12 -0400 Subject: [PATCH 5/5] pass in the server url into the monitor object to use instead of the portal url --- web/skins/classic/views/js/montage.js | 11 ++++++----- web/skins/classic/views/js/montage.js.php | 3 ++- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/web/skins/classic/views/js/montage.js b/web/skins/classic/views/js/montage.js index 1367bc8b1..a6c459760 100644 --- a/web/skins/classic/views/js/montage.js +++ b/web/skins/classic/views/js/montage.js @@ -1,10 +1,11 @@ var requestQueue = new Request.Queue( { concurrent: 2 } ); -function Monitor( index, id, connKey ) +function Monitor( index, monitorData ) { this.index = index; - this.id = id; - this.connKey = connKey; + this.id = monitorData.id; + this.connKey = monitorData.connKey; + this.server_url = monitorData.server_url; this.status = null; this.alarmState = STATE_IDLE; this.lastAlarmState = STATE_IDLE; @@ -110,7 +111,7 @@ function Monitor( index, id, connKey ) this.streamCmdReq.send( this.streamCmdParms+"&command="+CMD_QUERY ); } - this.streamCmdReq = new Request.JSON( { url: thisUrl, method: 'post', timeout: AJAX_TIMEOUT, onSuccess: this.getStreamCmdResponse.bind( this ), onTimeout: this.streamCmdQuery.bind( this, true ), link: 'cancel' } ); + this.streamCmdReq = new Request.JSON( { url: this.server_url, method: 'get', timeout: AJAX_TIMEOUT, onSuccess: this.getStreamCmdResponse.bind( this ), onTimeout: this.streamCmdQuery.bind( this, true ), link: 'cancel' } ); requestQueue.addRequest( "cmdReq"+this.id, this.streamCmdReq ); } @@ -146,7 +147,7 @@ function initPage() { for ( var i = 0; i < monitorData.length; i++ ) { - monitors[i] = new Monitor( i, monitorData[i].id, monitorData[i].connKey ); + monitors[i] = new Monitor( i, monitorData[i] ); var delay = Math.round( (Math.random()+0.5)*statusRefreshTimeout ); monitors[i].start( delay ); } diff --git a/web/skins/classic/views/js/montage.js.php b/web/skins/classic/views/js/montage.js.php index 80f652fd5..7eaebb75b 100644 --- a/web/skins/classic/views/js/montage.js.php +++ b/web/skins/classic/views/js/montage.js.php @@ -35,7 +35,8 @@ monitorData[monitorData.length] = { 'id': Id() ?>, 'connKey': connKey() ?>, 'width': Width() ?>, - 'height':Height() ?> + 'height':Height() ?>, + 'server_url': 'Server()->Url().$_SERVER['PHP_SELF'] ?>' };