$BASE = dirname(__FILE__); // Result or error responses require_once $BASE . '/lib/response.class.php'; // Checks CORS and pulls Dataporten info from headers require_once $BASE . '/lib/dataporten.class.php'; $feide_config = json_decode(file_get_contents($DATAPORTEN_CONFIG_PATH), true); $feide = new Dataporten($feide_config); // http://altorouter.com require_once $BASE . '/lib/router.class.php'; $router = new Router(); // $router->addMatchTypes(array('userlist' => '[0-9A-Za-z\[\]@.,%]++')); $router->setBasePath($API_BASE_PATH); // Relay API require_once $BASE . '/lib/relay.class.php'; $relay_config = json_decode(file_get_contents($RELAY_CONFIG_PATH), true); $relay = new Relay($relay_config); // ---------------------- DEFINE ROUTES ---------------------- /** * GET all REST routes */ $router->map('GET', '/', function () { global $router; Response::result($router->getRoutes()); }, 'Routes listing'); /** * GET TechSmith Relay version */ $router->map('GET', '/version/', function () { global $relay; Response::result($relay->getRelayVersion()); }, 'TechSmith Relay version');
public function updateSiteRelay($site_id, $relay_id, $status) { $site = Site::find($site_id); if (!$site->Zone) { $site_relays = $site->Relays; if (!$site_relays->count()) { for ($i = 0; $i < 6; $i++) { Relay::create(array('site_id' => $site_id, 'relay_id' => $i, 'status' => 'False')); } } $site_relay = Relay::withSiteAndRelay($site_id, $relay_id)->get()->first(); $relay = Relay::find($site_relay->id); if ($status == 0) { $statusCommand = 0; $statusString = 'Off'; $status = 'False'; } else { $statusCommand = 1; $statusString = 'On'; $status = 'True'; } $relay->status = $status; $relay->save(); $entry = new Record(); $entry->site_id = $site_id; $entry->site_name = $site->name; $entry->switch = $relay->relay_id; $entry->status = $statusString; $entry->command = $statusCommand; $entry->save(); } else { $message = 'This site is zoned. Please update changes to the zone!'; Session::put('message', $message); } }
public function OffCommand($zone_id, $relay_id) { $zone = Zone::find($zone_id); $zone_sites = $zone->Sites; $zone_relays = $zone->Relays; if (!$zone_relays->count()) { for ($i = 0; $i < 6; $i++) { ZoneRelay::create(array('zone_id' => $zone_id, 'relay_id' => $i, 'status' => 'False')); } } $zone_relay = ZoneRelay::withZoneAndRelay($zone_id, $relay_id)->get()->first(); $relay = ZoneRelay::find($zone_relay->id); $relay->status = 'False'; $relay->save(); $entry = new ZoneRecord(); $entry->zone_id = $zone->id; $entry->zone_name = $zone->name; $entry->switch = $relay->relay_id; $entry->status = 'Off'; $entry->command = 0; $entry->admin_id = Auth::admin()->get()->id; $entry->save(); foreach ($zone_sites as $zone_site) { $site_relays = $zone_site->Relays; if (!$site_relays->count()) { for ($i = 0; $i < 6; $i++) { Relay::create(array('site_id' => $zone_site->id, 'relay_id' => $i, 'status' => 'False')); } } $site_relay = Relay::withSiteAndRelay($zone_site->id, $zone_relay->relay_id)->get()->first(); $relay = Relay::find($site_relay->id); $relay->status = 'False'; $relay->save(); $entry = new Record(); $entry->site_id = $zone_site->id; $entry->site_name = $zone_site->name; $entry->switch = $relay->relay_id; $entry->status = 'Off'; $entry->command = 0; $entry->save(); } return Redirect::to('zone/' . $zone_id); }
public function closeDoor($id) { $site = Site::find($id); $door = Relay::withSiteAndRelay($id, 0)->get()->first(); if (!$door) { for ($i = 0; $i < 6; $i++) { Relay::create(array('site_id' => $id, 'relay_id' => $i, 'status' => 'False')); } $door = Relay::withSiteAndRelay($id, 0)->get()->first(); } else { $door->status = 'False'; $door->save(); } $timestamp = time() - 2 * 60; $entry = new Record(); $entry->site_id = $id; $entry->site_name = $site->name; $entry->switch = 0; $entry->status = 'Off'; $entry->command = 0; $entry->created_at = $timestamp; $entry->save(); return '#'; }
public function update() { $zone = Zone::find(Input::get('zone_id')); $sites = Site::all(); $zone_sites = $zone->Sites; $zone_relays = $zone->Relays; //add sites to zone foreach ($sites as $key => $value) { $selected_sites = Input::get('selected_sites_' . $value->id); if ($selected_sites) { $value->zone_id = $zone->id; $value->save(); } else { // $value->zone_id = null; // $value->save(); } } $zone = Zone::find(Input::get('zone_id')); $zone_sites = $zone->Sites; $zone_relays = $zone->Relays; //create new zone relays if not present if (!$zone_relays->count()) { for ($i = 0; $i < 6; $i++) { ZoneRelay::create(array('zone_id' => $zone->id, 'relay_id' => $i, 'status' => 'False')); } } //for all sites of the zone $zone_sites = $zone->Sites; foreach ($zone_sites as $site) { //update site relays $zone_relays = $zone->Relays; foreach ($zone_relays as $relay) { $site_relays = $site->Relays; //create new site relays if not present if (!$site_relays->count()) { Relay::create(array('site_id' => $site->id, 'relay_id' => $relay->relay_id, 'status' => $relay->status)); } $site_relay = Relay::where('site_id', '=', $site->id)->where('relay_id', '=', $relay->relay_id)->get()->first(); $relay_status = $site_relay->status; //when zone relay is true if ($relay->status == 'True') { $site_relay->status = 'True'; $site_relay->save(); //log this change $entry = new Record(); $entry->site_id = $site->id; $entry->site_name = $site->name; $entry->switch = $relay->relay_id; $entry->status = 'On'; $entry->command = 1; $entry->save(); //when zone relay is false } else { $site_relay->status = 'False'; $site_relay->save(); //log this change $entry = new Record(); $entry->site_id = $site->id; $entry->site_name = $site->name; $entry->switch = $relay->relay_id; $entry->status = 'Off'; $entry->command = 0; $entry->save(); } } } $users = User::all(); foreach ($users as $user) { if ($zone->GivesAccessToUser($user->rfid) == 'Granted') { DB::table('user_zone')->insert(array('user_id' => $user->rfid, 'zone_id' => $zone->id)); foreach ($zone_sites as $zone_site) { DB::table('site_user')->insert(array('user_id' => $user->rfid, 'site_id' => $zone_site->id)); } } else { DB::table('user_zone')->where('user_id', '=', $user->rfid)->where('zone_id', '=', $zone->id)->delete(); foreach ($zone_sites as $zone_site) { DB::table('site_user')->where('user_id', '=', $user->rfid)->where('site_id', '=', $zone_site->id)->delete(); } } } return Redirect::to('zoneSite/' . $zone->id); }