Skip to content

Commit

Permalink
Added shinobi backend
Browse files Browse the repository at this point in the history
  • Loading branch information
KodeStar authored and KodeStar committed Jul 3, 2017
1 parent 61f52df commit 0ab20b5
Show file tree
Hide file tree
Showing 6 changed files with 171 additions and 77 deletions.
67 changes: 67 additions & 0 deletions app/Helpers/Shinobi.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

namespace App\Helpers;

use App\Helpers\Contracts\CameraContract;
use GuzzleHttp\Exception\GuzzleException;
use GuzzleHttp\Client;
use App\Camera as Camera;
use App\Setting as Setting;

class Shinobi implements CameraContract
{
/**
* Gets JSON feed from backend and turns into PHP array
* @return array
*/
public function getFeed()
{
$backend = Setting::value('backend_location');
$api_key = Setting::value('api_key');
$group = Setting::value('group');

$client = new Client(); //GuzzleHttp\Client
$response = $client->get(
$backend.'/shinobi/api/monitors.json',
[
//'auth' => ['user', 'pass']
]
);
if($response->getStatusCode() === 200) {
$body = $response->getBody();
return json_decode($body);
}

return [];
}

/**
* List all cameras
* @return array Returns an array of Camera objects
*/
public function list()
{
$demo = env('DEMO', false);
$output = [];

$cameras = $this->getFeed();

foreach($cameras->monitors as $camera) {
//print_r($camera);
$backend = Setting::value('backend_location');
$preview = $backend.'/zm/cgi-bin/nph-zms?mode=jpeg&scale=100&maxfps=4&monitor='.$camera->Monitor->Id;
if($demo) $preview = '/img/camera1.png';
$output[] = [
'key' => $camera->Monitor->Id,
'name' => $camera->Monitor->Name,
'feed' => $camera->Monitor->Path,
'preview' => $preview,
'status' => $camera->Monitor->Enabled
];

}

return $output;
}
public function single() {}
}
4 changes: 2 additions & 2 deletions app/Helpers/Zoneminder.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class Zoneminder implements CameraContract
public function getFeed()
{
$demo = env('DEMO', false);
$backend = env('BACKEND_LOCATION');
$backend = Setting::value('backend_location');

if($demo) { // return early with a demo JSON file
return json_decode(file_get_contents('../resources/assets/camera.json'));
Expand Down Expand Up @@ -51,7 +51,7 @@ public function list()

foreach($cameras->monitors as $camera) {
//print_r($camera);
$backend = Setting::where('key', 'backend_location')->first()->value;
$backend = Setting::value('backend_location');
$preview = $backend.'/zm/cgi-bin/nph-zms?mode=jpeg&scale=100&maxfps=4&monitor='.$camera->Monitor->Id;
if($demo) $preview = '/img/camera1.png';
$output[] = [
Expand Down
29 changes: 23 additions & 6 deletions app/Providers/CameraServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,24 @@
use Illuminate\Support\ServiceProvider;
// List all created backends here
use App\Helpers\Zoneminder;
use App\Setting as Setting;

class CameraServiceProvider extends ServiceProvider
{

public $available = [
'zoneminder' => 'Zoneminder',
'shinobi' => 'Shinobi'
];

/**
* Bootstrap the application services.
*
* @return void
*/
public function boot()
{
//
view()->share('available_backends', $this->available);
}
/**
* Register the application services.
Expand All @@ -25,13 +32,23 @@ public function boot()
public function register()
{
$this->app->bind('App\Helpers\Contracts\CameraContract', function(){
$backend = env('BACKEND', 'zoneminder');
switch($backend)
{
case 'zoneminder': return new Zoneminder();
default: return new Zoneminder();

$path = base_path().'/database/database.sqlite';

$backend = 'zoneminder';

if(file_exists($path) && filesize($path) > 0) {
$backend = Setting::value('backend');
} else {
$request = app(\Illuminate\Http\Request::class);
if(null !== $request->input('backend')) $backend = $request->input('backend');
}

$available = $this->available;
$interface = isset($available[$backend]) ? $available[$backend] : 'Zoneminder';
$interface = "\App\Helpers\\".$interface;

return new $interface();
});
}

Expand Down
9 changes: 9 additions & 0 deletions app/Setting.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
namespace App;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;

class Setting extends Model
{
Expand All @@ -20,4 +21,12 @@ class Setting extends Model
*/
public $timestamps = false;

public static function value($key)
{
if (DB::connection()->getDatabaseName()) {
return self::where('key', $key)->first()->value;
}
return false;
}

}
Loading

0 comments on commit 0ab20b5

Please sign in to comment.