Ejemplo n.º 1
0
 /**
  * Remove the specified resource from storage.
  *
  * @param  int  $id
  * @return Response
  */
 public function getDelete($id)
 {
     $gateway = Gateway::find($id);
     $gateway->delete();
     Event::fire('logger', array(array('gateway_remove', array('id' => $id, 'gateway_name' => $gateway->gateway_name), 2)));
     return Output::push(array('path' => 'gateway', 'messages' => array('success' => _('Gateway has been deleted'))));
 }
Ejemplo n.º 2
0
 public static function updateGatewayStatusFromInflux($since = null, $interval = '1h')
 {
     $config = DI::getDefault()->getConfig();
     $database = DI::getDefault()->getInfluxdb();
     if (is_null($since)) {
         if (!preg_match('/\\d{1,10}[smhd]{1}/', $interval)) {
             echo 'Malformed interval';
             return false;
         }
         $result = $database->query("SELECT * FROM gateway_status WHERE time > (NOW() - {$interval}) GROUP BY eui;");
     } else {
         $localTimezone = new DateTimeZone($config->monitor->localTimezone);
         $localSince = new DateTime($since, $localTimezone);
         $maxSince = self::getCurrentLocalDateTime();
         $maxSince->modify($config->monitor->maxSinceOffset);
         // make sure we're not querying too much in the past, which would result in many entries returned and either influxdb quitting or php crashing
         if ($localSince < $maxSince) {
             $localSince = $maxSince;
         }
         $influxTimezone = new DateTimeZone($config->monitor->influxTimezone);
         $influxSince = clone $localSince;
         $influxSince = $influxSince->setTimezone($influxTimezone);
         $result = $database->query("SELECT * FROM gateway_status WHERE time > '" . $influxSince->format('Y-m-d H:i:s.u') . "' GROUP BY eui;");
     }
     $now = self::getCurrentLocalDateTime();
     $gatewaysProcessed = array();
     foreach ($result->getSeries() as $serie) {
         $eui = $serie['tags']['eui'];
         $numberOfValues = count($serie['values']);
         $lastValue = $serie['values'][$numberOfValues - 1];
         $valueIndex = array_flip($serie['columns']);
         $gateway = Gateway::findFirstByEui($eui);
         $gateway->last_seen = self::prepareForDb(self::convertToLocalTimezone($lastValue[$valueIndex['time']]));
         $gateway->location = $lastValue[$valueIndex['latitude']] . ',' . $lastValue[$valueIndex['longitude']];
         $gateway->updated_at = self::prepareForDb($now);
         $statusUpdate = new StatusUpdate();
         $statusUpdate->update_time = self::prepareForDb($now);
         if (is_null($since)) {
             $statusUpdate->interval = $interval;
         } else {
             $statusUpdate->since_time = self::prepareForDb($localSince);
         }
         $statusUpdate->entries_seen = $numberOfValues;
         $gateway->StatusUpdate = $statusUpdate;
         if (!$gateway->save()) {
             foreach ($gateway->getMessages() as $message) {
                 echo $message;
             }
         } else {
             $gatewaysProcessed[] = $eui;
         }
     }
     // make sure that gateways that did not send a recent message to influx get updated
     $allGateways = Gateway::find();
     foreach ($allGateways as $gateway) {
         if (!in_array($gateway->eui, $gatewaysProcessed)) {
             $gateway->updated_at = self::prepareForDb($now);
             $gateway->save();
         }
     }
 }
Ejemplo n.º 3
0
    if (is_null($team)) {
        return App::abort(404);
    } else {
        return $team;
    }
});
Route::bind('donation', function ($value) {
    $donation = Donation::find($value);
    if (is_null($donation)) {
        return App::abort(404);
    } else {
        return $donation;
    }
});
Route::bind('gateway', function ($value) {
    $gateway = Gateway::find($value);
    if (is_null($gateway)) {
        return App::abort(404);
    } else {
        return $gateway;
    }
});
/*
|--------------------------------------------------------------------------
| Legacy URLs
|--------------------------------------------------------------------------
|
| These are the old URLs for original vataware. When possible it should be
| a 301 (permanent redirect). This is not always possible though.
|
*/
Ejemplo n.º 4
0
 * Local variables
 * @var \Phalcon\Mvc\Micro $app
 */
/**
 * Add your routes here
 */
$app->get('/', function () use($app) {
    $gateways = Gateway::find();
    $lastEntry = Gateway::getLastEntry();
    // this isn't 100% correct: if all gateways are down, the last update datetime won't be the same as the actual update time. Otherwise the update datetime could be up to gateway-update-interval off
    echo $app['view']->render('index', array('gateways' => $gateways, 'lastUpdate' => $lastEntry->last_seen));
});
$app->get('/gateways', function () use($app) {
    $response = new Phalcon\Http\Response();
    $response->setContentType('application/json');
    $gateways = Gateway::find();
    $gatewaysArray = array();
    foreach ($gateways as $gateway) {
        $gatewayArray = $gateway->toArray();
        unset($gatewayArray['ID']);
        $gatewaysArray[] = $gatewayArray;
    }
    // Pass the content of a file
    $response->setContent(json_encode($gatewaysArray));
    return $response;
});
$app->get('/gateways/{eui}', function ($eui) use($app) {
    $response = new Phalcon\Http\Response();
    $response->setContentType('application/json');
    $gateway = Gateway::findFirstByEui($eui);
    if (empty($gateway)) {