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(); } } }
$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)) { $response->setContent('Gateway not found'); $response->setStatusCode(404, "Not Found"); return $response; } $gatewayArray = $gateway->toArray(); $gatewayArray['entries_in_last_24h'] = '<to be implemented>'; unset($gatewayArray['ID']); // Pass the content of a file $response->setContent(json_encode($gatewayArray)); return $response; }); /** * Not found handler */