* camera images ordered by mtime

* display image by camera is now second to last, not the last one
* indent changes in javascript code
* js code should now be more roboust if there are camera folders getting deleted
This commit is contained in:
Dávid Danyi 2018-01-25 15:44:39 +01:00
parent 07a95f5c4b
commit 139116d76f
8 changed files with 46 additions and 34 deletions

View File

@ -13,7 +13,7 @@ jQuery(document).ready(function () {
function refreshCameraImages() {
jQuery.get(cameraImageApiUrl, function (data) {
cameraImages = data;
}).always(function() {
}).always(function () {
window.setTimeout(refreshCameraImages, refreshInterval);
});
}
@ -27,7 +27,7 @@ jQuery(document).ready(function () {
// change displayed image
window.setInterval(function () {
cameraPointer = cameraPointer === cameraImages.length - 1
cameraPointer = cameraPointer >= cameraImages.length - 1
? 0
: cameraPointer + 1;
changeImage(cameraPointer);

View File

@ -31,7 +31,10 @@ class CameraPictureManagerService
$cameraImageFolders = array_diff(scandir($cameraImageBaseFolder), ['.', '..']);
foreach ($cameraImageFolders as $cameraImageFolder) {
$cameraImages[] = $this->getLatestImageInDirectory("{$cameraImageBaseFolder}/{$cameraImageFolder}");
$theImage = $this->getLatestCompleteImageInDirectory("{$cameraImageBaseFolder}/{$cameraImageFolder}");
if (null != $theImage) {
$cameraImages[] = $theImage;
}
}
return $cameraImages;
@ -66,13 +69,21 @@ class CameraPictureManagerService
* @param string $directory
* @return CameraImage
*/
private function getLatestImageInDirectory(string $directory): CameraImage
private function getLatestCompleteImageInDirectory(string $directory): ?CameraImage
{
$cameraImage = new CameraImage();
$cameraImage->setCamera(basename($directory));
$allImages = glob("{$directory}/*.{png,jpeg,jpg}", GLOB_NOSORT | GLOB_BRACE);
$latestImage = array_pop($allImages);
if (count($allImages) < 2) {
return null;
}
$imageModificationTimes = array_map('filemtime', $allImages);
array_multisort($imageModificationTimes, SORT_DESC, $allImages);
$latestImage = $allImages[1];
unset($allImages);
$cameraImage->setImageName(pathinfo($latestImage, PATHINFO_BASENAME))
->setCreatedAt(new \DateTime(sprintf("@%s", filemtime($latestImage))));

View File

@ -12,7 +12,7 @@ class CameraPictureManagerServiceTest extends TestCase
{
const EXISTING_CAM_DIR = 'cam1';
const EXISTING_IMAGE = 'img1.png';
const MISSING_IMAGE = 'img9.png';
const MISSING_IMAGE = 'imgXX.png';
const TEST_DATA_DIR = 'test/data';
/** @var Config */

BIN
test/data/cam1/img9.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.4 KiB

BIN
test/data/cam2/img9.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 KiB

BIN
test/data/cam3/img9.jpeg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 KiB

BIN
test/data/cam5/img1.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.4 KiB

View File

@ -0,0 +1 @@
img1