|
|
|
@ -127,27 +127,7 @@ object Main extends App { |
|
|
|
new String(console.readPassword()) |
|
|
|
new String(console.readPassword()) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
implicit val garmin: GarminConnect = new GarminConnect(email, password) |
|
|
|
syncGarmin(config, plan, new GarminConnect(email, password)) |
|
|
|
val workouts = plan.workouts.toIndexedSeq |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
garmin.login().flatMap { |
|
|
|
|
|
|
|
case Right(s) => |
|
|
|
|
|
|
|
implicit val session: GarminSession = s |
|
|
|
|
|
|
|
for { |
|
|
|
|
|
|
|
maybeDeleteMessage <- deleteWorkoutsTask(workouts.map(_.name)) |
|
|
|
|
|
|
|
maybeGarminWorkouts <- createWorkoutsTask(workouts) |
|
|
|
|
|
|
|
maybeScheduleMessage <- scheduleTask(maybeGarminWorkouts.fold(Seq.empty[GarminWorkout])(identity)) |
|
|
|
|
|
|
|
} yield { |
|
|
|
|
|
|
|
log.info("\nStatistics:") |
|
|
|
|
|
|
|
maybeDeleteMessage.foreach(msg => log.info(" " + msg)) |
|
|
|
|
|
|
|
maybeGarminWorkouts.foreach(workouts => log.info(s" ${workouts.length} imported")) |
|
|
|
|
|
|
|
maybeScheduleMessage.foreach(msg => log.info(" " + msg)) |
|
|
|
|
|
|
|
log.info("Logging out and closing connection...") |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
case Left(loginFailureMessage) => |
|
|
|
|
|
|
|
log.error(loginFailureMessage) |
|
|
|
|
|
|
|
Future.successful(()) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (plan.invalid().isEmpty) proceedToGarmin() |
|
|
|
if (plan.invalid().isEmpty) proceedToGarmin() |
|
|
|
@ -162,48 +142,69 @@ object Main extends App { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
private def syncGarmin(implicit config: Config, plan: WeeklyPlan, garmin: GarminConnect): Future[Unit] = { |
|
|
|
* Deletes existing workouts with the same names or not |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
private def deleteWorkoutsTask( |
|
|
|
|
|
|
|
workouts: Seq[String])(implicit config: Config, garmin: GarminConnect, session: GarminSession): Future[Option[String]] = { |
|
|
|
|
|
|
|
if (config.delete) |
|
|
|
|
|
|
|
garmin.deleteWorkouts(workouts).map(c => Some(s"$c deleted")) |
|
|
|
|
|
|
|
else |
|
|
|
|
|
|
|
Future.successful(None) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private def createWorkoutsTask( |
|
|
|
def deleteWorkoutsTask(workouts: Seq[String])(implicit config: Config, garmin: GarminConnect, session: GarminSession): Future[Option[String]] = { |
|
|
|
workouts: Seq[WorkoutDef])(implicit config: Config, garmin: GarminConnect, session: GarminSession): Future[Option[Seq[GarminWorkout]]] = { |
|
|
|
if (config.delete) |
|
|
|
if (config.mode.exists(Seq(Modes.`import`, Modes.schedule).contains)) |
|
|
|
garmin.deleteWorkouts(workouts).map(c => Some(s"$c deleted")) |
|
|
|
garmin.createWorkouts(workouts).map(Option.apply) |
|
|
|
else |
|
|
|
else |
|
|
|
Future.successful(None) |
|
|
|
Future.successful(None) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private def scheduleTask(workouts: Seq[GarminWorkout])(implicit config: Config, |
|
|
|
def createWorkoutsTask( |
|
|
|
garmin: GarminConnect, |
|
|
|
workouts: Seq[WorkoutDef])(implicit config: Config, garmin: GarminConnect, session: GarminSession): Future[Option[Seq[GarminWorkout]]] = { |
|
|
|
plan: WeeklyPlan, |
|
|
|
if (config.mode.exists(Seq(Modes.`import`, Modes.schedule).contains)) |
|
|
|
session: GarminSession): Future[Option[String]] = { |
|
|
|
garmin.createWorkouts(workouts).map(Option.apply) |
|
|
|
|
|
|
|
else |
|
|
|
|
|
|
|
Future.successful(None) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (config.mode.contains(Modes.schedule)) { |
|
|
|
def scheduleTask(workouts: Seq[GarminWorkout])(implicit |
|
|
|
|
|
|
|
config: Config, |
|
|
|
|
|
|
|
garmin: GarminConnect, |
|
|
|
|
|
|
|
plan: WeeklyPlan, |
|
|
|
|
|
|
|
session: GarminSession): Future[Option[String]] = { |
|
|
|
|
|
|
|
|
|
|
|
val start = (config.start, config.end) match { |
|
|
|
if (config.mode.contains(Modes.schedule)) { |
|
|
|
case (_, end) if !end.isEqual(LocalDate.MIN) => end.minusDays(plan.get().length - 1) |
|
|
|
|
|
|
|
case (from, _) => from |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
val woMap: Map[String, GarminWorkout] = Map(workouts.map(ga => ga.name -> ga): _*) |
|
|
|
val start = (config.start, config.end) match { |
|
|
|
val spec = plan |
|
|
|
case (_, end) if !end.isEqual(LocalDate.MIN) => end.minusDays(plan.get().length - 1) |
|
|
|
.get() |
|
|
|
case (from, _) => from |
|
|
|
.zipWithIndex |
|
|
|
|
|
|
|
.collect { |
|
|
|
|
|
|
|
case (Some(ref), day) if !start.plusDays(day).isBefore(LocalDate.now()) => start.plusDays(day) -> woMap(ref.name) |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
.to[Seq] |
|
|
|
|
|
|
|
garmin.schedule(spec).map(c => Some(s"$c scheduled")) |
|
|
|
val woMap: Map[String, GarminWorkout] = Map(workouts.map(ga => ga.name -> ga): _*) |
|
|
|
} else |
|
|
|
val spec = plan |
|
|
|
Future.successful(None) |
|
|
|
.get() |
|
|
|
|
|
|
|
.zipWithIndex |
|
|
|
|
|
|
|
.collect { |
|
|
|
|
|
|
|
case (Some(ref), day) if !start.plusDays(day).isBefore(LocalDate.now()) => start.plusDays(day) -> woMap(ref.name) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
.to[Seq] |
|
|
|
|
|
|
|
garmin.schedule(spec).map(c => Some(s"$c scheduled")) |
|
|
|
|
|
|
|
} else |
|
|
|
|
|
|
|
Future.successful(None) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
val workouts = plan.workouts.toIndexedSeq |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
garmin.login().flatMap { |
|
|
|
|
|
|
|
case Right(s) => |
|
|
|
|
|
|
|
implicit val session: GarminSession = s |
|
|
|
|
|
|
|
for { |
|
|
|
|
|
|
|
maybeDeleteMessage <- deleteWorkoutsTask(workouts.map(_.name)) |
|
|
|
|
|
|
|
maybeGarminWorkouts <- createWorkoutsTask(workouts) |
|
|
|
|
|
|
|
maybeScheduleMessage <- scheduleTask(maybeGarminWorkouts.fold(Seq.empty[GarminWorkout])(identity)) |
|
|
|
|
|
|
|
} yield { |
|
|
|
|
|
|
|
log.info("\nStatistics:") |
|
|
|
|
|
|
|
maybeDeleteMessage.foreach(msg => log.info(" " + msg)) |
|
|
|
|
|
|
|
maybeGarminWorkouts.foreach(workouts => log.info(s" ${workouts.length} imported")) |
|
|
|
|
|
|
|
maybeScheduleMessage.foreach(msg => log.info(" " + msg)) |
|
|
|
|
|
|
|
log.info("Logging out and closing connection...") |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
case Left(loginFailureMessage) => |
|
|
|
|
|
|
|
log.error(loginFailureMessage) |
|
|
|
|
|
|
|
Future.successful(()) |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private def shutdown() = Await.result(Http().shutdownAllConnectionPools().flatMap(_ => system.terminate()), 10.minutes) |
|
|
|
private def shutdown() = Await.result(Http().shutdownAllConnectionPools().flatMap(_ => system.terminate()), 10.minutes) |
|
|
|
|