Use new Server::SendToApi to reduce duplicated code. If Janus Not enabled, then don't both with Janus_Pin. If Server is remote, use API to get Janus_Pin. Implementing the API side is left as an exercise.

pull/3621/head
Isaac Connor 2022-09-28 17:41:46 -04:00
parent 2a9576371b
commit f5bdbcf0bd
1 changed files with 25 additions and 76 deletions

View File

@ -270,12 +270,23 @@ public static function getStatuses() {
'ArchivedEventDiskSpace' => array('type'=>'integer', 'default'=>null, 'do_not_update'=>1),
);
public function Janus_Pin() {
$cmd = getZmuCommand(' --janus-pin -m '.$this->{'Id'});
$output = shell_exec($cmd);
Debug("Running $cmd output: $output");
return $output ? trim($output) : $output;
if (!$this->{'JanusEnabled'}) return '';
if ((!defined('ZM_SERVER_ID')) or ( property_exists($this, 'ServerId') and (ZM_SERVER_ID==$this->{'ServerId'}) )) {
$cmd = getZmuCommand(' --janus-pin -m '.$this->{'Id'});
$output = shell_exec($cmd);
Debug("Running $cmd output: $output");
return $output ? trim($output) : $output;
} else if ($this->ServerId()) {
$result = $this->Server()->SendToApi('/monitors/'.$this->{'Id'}.'.json');
Debug(print_r($result, true));
$json = json_decode($result, true);
return $json['Janus_Pin'];
} else {
Error('Server not assigned to Monitor in a multi-server setup. Please assign a server to the Monitor.');
}
}
public function Control() {
if (!property_exists($this, 'Control')) {
if ($this->ControlId())
@ -522,31 +533,7 @@ public static function getStatuses() {
}
}
} else if ($this->ServerId()) {
$Server = $this->Server();
$url = $Server->UrlToApi().'/monitors/daemonControl/'.$this->{'Id'}.'/'.$mode.'/zmc.json';
if (ZM_OPT_USE_AUTH) {
if (ZM_AUTH_RELAY == 'hashed') {
$url .= '?auth='.generateAuthHash(ZM_AUTH_HASH_IPS);
} else if (ZM_AUTH_RELAY == 'plain') {
$url .= '?user='.$_SESSION['username'];
$url .= '?pass='.$_SESSION['password'];
} else {
Error('Multi-Server requires AUTH_RELAY be either HASH or PLAIN');
return;
}
}
Debug('sending command to '.$url);
$context = stream_context_create();
try {
$result = file_get_contents($url, false, $context);
if ($result === FALSE) { /* Handle error */
Error("Error restarting zmc using $url");
}
} catch (Exception $e) {
Error("Except $e thrown trying to restart zmc");
}
$result = $this->Server()->SendToApi('/monitors/daemonControl/'.$this->{'Id'}.'/'.$mode.'/zmc.json');
} else {
Error('Server not assigned to Monitor in a multi-server setup. Please assign a server to the Monitor.');
}
@ -727,32 +714,8 @@ public static function getStatuses() {
}
socket_close($socket);
} else if ($this->ServerId()) {
$Server = $this->Server();
$url = $Server->UrlToApi().'/monitors/daemonControl/'.$this->{'Id'}.'/'.$command.'/zmcontrol.pl.json';
if (ZM_OPT_USE_AUTH) {
if (ZM_AUTH_RELAY == 'hashed') {
$url .= '?auth='.generateAuthHash(ZM_AUTH_HASH_IPS);
} else if (ZM_AUTH_RELAY == 'plain') {
$url .= '?user='.$_SESSION['username'];
$url .= '?pass='.$_SESSION['password'];
} else if (ZM_AUTH_RELAY == 'none') {
$url .= '?user='.$_SESSION['username'];
}
}
Debug('sending command to '.$url);
$context = stream_context_create();
try {
$result = file_get_contents($url, false, $context);
if ($result === FALSE) { /* Handle error */
Error("Error sending command using $url");
return false;
}
} catch (Exception $e) {
Error("Exception $e thrown trying to send command to $url");
return false;
}
$result = $this->Server()->SendToApi('/monitors/daemonControl/'.$this->{'Id'}.'/'.$command.'/zmcontrol.pl.json');
return $result;
} else {
Error('Server not assigned to Monitor in a multi-server setup. Please assign a server to the Monitor.');
return false;
@ -820,33 +783,19 @@ public static function getStatuses() {
}
if ($this->ServerId()) {
$Server = $this->Server();
$result = $this->Server()->SendToApi('/monitors/alarm/id:'.$this->{'Id'}.'/command:'.$cmd.'.json');
$url = $Server->UrlToApi().'/monitors/alarm/id:'.$this->{'Id'}.'/command:'.$cmd.'.json';
$auth_relay = get_auth_relay();
if ($auth_relay) $url .= '?'.$auth_relay;
Debug('sending command to '.$url);
$context = stream_context_create();
try {
$result = file_get_contents($url, false, $context);
if ($result === FALSE) { /* Handle error */
Error('Error sending command using '.$url);
return false;
}
Debug('Result '.$result);
$json = json_decode($result, true);
return $json['status'];
} catch (Exception $e) {
Error("Exception $e thrown trying to send command to $url");
if ($result === FALSE) { /* Handle error */
Error('Error sending command using '.$url);
return false;
}
$json = json_decode($result, true);
return $json['status'];
} // end if we are on the recording server
Error('Server not assigned to Monitor in a multi-server setup. Please assign a server to the Monitor.');
return false;
}
function TriggerOn() {
$output = $this->AlarmCommand('on');
if ($output and preg_match('/Alarmed event id: (\d+)$/', $output, $matches)) {