Skip to content

Commit

Permalink
fix testing
Browse files Browse the repository at this point in the history
  • Loading branch information
szigyi committed Mar 19, 2021
1 parent 4e45fd2 commit 8cb6ed3
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 26 deletions.
34 changes: 15 additions & 19 deletions src/main/scala/hu/szigyi/ettl/app/CliEttlApp.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import cats.effect.{ExitCode, IO, IOApp}
import com.typesafe.scalalogging.StrictLogging
import hu.szigyi.ettl.hal.{DummyCamera, GCameraImpl}
import hu.szigyi.ettl.model.Model.SettingsCameraModel
import hu.szigyi.ettl.service.{EttlApp, SchedulerImpl}
import hu.szigyi.ettl.service.{DirectoryService, EttlApp, SchedulerImpl}
import org.rogach.scallop.{ScallopConf, ScallopOption}

import java.nio.file.{Path, Paths}
Expand All @@ -13,7 +13,6 @@ import java.util.concurrent.TimeUnit
import scala.concurrent.duration._
import scala.util.{Failure, Success, Try}


// 1: Can capture image and download it so can view it from computer
// 2: Can capture consecutive images without camera failure or black screen or camera lag
// 3: Can successfully change settings of camera before capturing the image
Expand Down Expand Up @@ -55,21 +54,18 @@ object CliEttlApp extends IOApp with StrictLogging {
).as(ExitCode.Success)
}

private def runApp(dummyCamera: Boolean,
basePath: String,
setSettings: Boolean,
numberOfCaptures: Int,
intervalSeconds: Double,
rawFileExtension: String): IO[Try[Seq[Path]]] = {
val clock = Clock.systemDefaultZone()
val appConfig = AppConfiguration(Paths.get(basePath), if (dummyCamera) "JPG" else rawFileExtension)
private def runApp(dummyCamera: Boolean, basePath: String, setSettings: Boolean, numberOfCaptures: Int, intervalSeconds: Double, rawFileExtension: String): IO[Try[Seq[Path]]] = {
val clock = Clock.systemDefaultZone()
val appConfig = AppConfiguration(Paths.get(basePath), if (dummyCamera) "JPG" else rawFileExtension)
val schedulerAwakingFrequency = 100.milliseconds
val setting = if (setSettings) Some(SettingsCameraModel(Some(1d / 100d), Some(400), Some(2.8))) else None
val interval = Duration(intervalSeconds, TimeUnit.SECONDS)
val setting = if (setSettings) Some(SettingsCameraModel(Some(1d / 100d), Some(400), Some(2.8))) else None
val interval = Duration(intervalSeconds, TimeUnit.SECONDS)

val scheduler = new SchedulerImpl(clock, schedulerAwakingFrequency)
val dir = new DirectoryService
val ettl =
if (dummyCamera) new EttlApp(appConfig, new DummyCamera, new SchedulerImpl(clock, schedulerAwakingFrequency), clock.instant())
else new EttlApp(appConfig, new GCameraImpl, new SchedulerImpl(clock, schedulerAwakingFrequency), clock.instant())
if (dummyCamera) new EttlApp(appConfig, new DummyCamera, scheduler, dir, clock.instant())
else new EttlApp(appConfig, new GCameraImpl, scheduler, dir, clock.instant())

logger.info(s" Clock: $clock")
logger.info(s" Dummy Camera: $dummyCamera")
Expand All @@ -90,11 +86,11 @@ object CliEttlApp extends IOApp with StrictLogging {
}

class Conf(args: Seq[String]) extends ScallopConf(args) {
val dummyCamera: ScallopOption[Boolean] = opt[Boolean](name = "dummyCamera", required = false, descr = "Use dummy camera so can simulate capturing photos")
val imagesBasePath: ScallopOption[String] = opt[String](name = "imagesBasePath", required = true, descr = "Folder where the captured images will be stored")
val setSettings: ScallopOption[Boolean] = opt[Boolean](name = "setSettings", descr = "Do you want the app to override the camera settings before capturing an image?")
val numberOfCaptures: ScallopOption[Int] = opt[Int](name = "numberOfCaptures", required = true, descr = "How many photos do you want to take?")
val intervalSeconds: ScallopOption[Double] = opt[Double](name = "intervalSeconds", required = true, descr = "Seconds between two captures", validate = _ > 0.1)
val dummyCamera: ScallopOption[Boolean] = opt[Boolean](name = "dummyCamera", required = false, descr = "Use dummy camera so can simulate capturing photos")
val imagesBasePath: ScallopOption[String] = opt[String](name = "imagesBasePath", required = true, descr = "Folder where the captured images will be stored")
val setSettings: ScallopOption[Boolean] = opt[Boolean](name = "setSettings", descr = "Do you want the app to override the camera settings before capturing an image?")
val numberOfCaptures: ScallopOption[Int] = opt[Int](name = "numberOfCaptures", required = true, descr = "How many photos do you want to take?")
val intervalSeconds: ScallopOption[Double] = opt[Double](name = "intervalSeconds", required = true, descr = "Seconds between two captures", validate = _ > 0.1)
val rawFileExtension: ScallopOption[String] = opt[String](name = "rawFileExtension", required = true, descr = "Extension of your Raw file ie: CR2, NEF")
verify()
}
Expand Down
16 changes: 16 additions & 0 deletions src/main/scala/hu/szigyi/ettl/service/DirectoryService.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package hu.szigyi.ettl.service

import java.nio.file.Path
import scala.util.{Failure, Success, Try}

class DirectoryService {

def createFolder(folderPath: Path): Try[Unit] =
folderPath.toFile.mkdirs() match {
case true =>
Success(())
case false =>
Failure(new Exception(s"Could not create folder: ${folderPath}"))
}

}
4 changes: 2 additions & 2 deletions src/main/scala/hu/szigyi/ettl/service/EttlApp.scala
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ import java.time.format.DateTimeFormatter
import scala.concurrent.duration.Duration
import scala.util.{Failure, Success, Try}

class EttlApp(appConfig: AppConfiguration, camera: GCamera, scheduler: Scheduler, now: Instant) extends StrictLogging {
class EttlApp(appConfig: AppConfiguration, camera: GCamera, scheduler: Scheduler, dir: DirectoryService, now: Instant) extends StrictLogging {

def execute(setting: Option[SettingsCameraModel], numberOfCaptures: Int, interval: Duration): Try[Seq[Path]] =
for {
sessionFolderName <- Try(nowToSessionId(now))
_ <- createSessionFolder(sessionFolderName)
_ <- dir.createFolder(appConfig.imageBasePath.resolve(sessionFolderName))
config <- connectToCamera(camera, ShellKill.killGPhoto2Processes())
imagePaths <- scheduler.schedule(numberOfCaptures, interval, capture(camera, config, numberOfCaptures, setting, sessionFolderName))
_ <- config.close
Expand Down
16 changes: 11 additions & 5 deletions src/test/scala/hu/szigyi/ettl/service/EttlAppSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ import org.scalatest.freespec.AnyFreeSpec
import org.scalatest.matchers.should.Matchers

import java.time.Instant.{parse => instant}

import java.nio.file.Paths
import java.nio.file.{Path, Paths}
import scala.concurrent.duration._
import scala.util.{Failure, Success, Try}

Expand All @@ -20,7 +19,10 @@ class EttlAppSpec extends AnyFreeSpec with Matchers {
"Normal Scenario" - {
"set camera then capture images with custom settings" in {
val camera = new DummyCamera(testing = true)
val result = new EttlApp(AppConfiguration(Paths.get("/"), "CR2"), camera, immediateScheduler, instant("2021-03-19T19:00:00Z"))
val result = new EttlApp(
AppConfiguration(Paths.get("/"), "CR2"), camera, immediateScheduler, new DirectoryService() {
override def createFolder(folderPath: Path): Try[Unit] = Try(())
}, instant("2021-03-19T19:00:00Z"))
.execute(Some(SettingsCameraModel(Some(1d / 100d), Some(400), Some(2.8))), 2, 10.millisecond)

result shouldBe a[Success[_]]
Expand All @@ -38,7 +40,9 @@ class EttlAppSpec extends AnyFreeSpec with Matchers {

"set camera then capture images without custom settings" in {
val camera = new DummyCamera(testing = true)
val result = new EttlApp(AppConfiguration(Paths.get("/"), "CR2"), camera, immediateScheduler, instant("2021-03-19T19:00:00Z"))
val result = new EttlApp(AppConfiguration(Paths.get("/"), "CR2"), camera, immediateScheduler, new DirectoryService {
override def createFolder(folderPath: Path): Try[Unit] = Try(())
}, instant("2021-03-19T19:00:00Z"))
.execute(None,2, 10.millisecond)

result shouldBe a[Success[_]]
Expand All @@ -61,7 +65,9 @@ class EttlAppSpec extends AnyFreeSpec with Matchers {
override def newConfiguration: Try[GConfiguration] = throw new UnsupportedOperationException()

override def captureImage: Try[GFile] = throw new UnsupportedOperationException()
}, immediateScheduler, instant("2021-03-19T19:00:00Z")).execute(None, 1, 10.millisecond)
}, immediateScheduler, new DirectoryService {
override def createFolder(folderPath: Path): Try[Unit] = Try(())
}, instant("2021-03-19T19:00:00Z")).execute(None, 1, 10.millisecond)

result shouldBe a[Failure[_]]
result.failed.get.getMessage shouldBe "gp_camera_init failed with GP_ERROR_MODEL_NOT_FOUND #-105: Unknown model"
Expand Down

0 comments on commit 8cb6ed3

Please sign in to comment.