Skip to content

Commit

Permalink
caching weather data
Browse files Browse the repository at this point in the history
  • Loading branch information
axaluss committed Aug 1, 2017
1 parent d480f49 commit 9d9479d
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 17 deletions.
4 changes: 2 additions & 2 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ version := "1.0"

scalaVersion := "2.12.1"

mainClass := Some("de.ax.uwt.Launscha")

resolvers += Resolver.sonatypeRepo("public")

val circeVersion = "0.8.0"
Expand All @@ -25,5 +27,3 @@ libraryDependencies += "ch.qos.logback" % "logback-classic" % "1.2.3"
libraryDependencies += "com.pi4j" % "pi4j-core" % "1.2-SNAPSHOT"

libraryDependencies += "com.typesafe.scala-logging" %% "scala-logging" % "3.7.2"

mainClass := Some("de.ax.uwt.Launscha")
5 changes: 2 additions & 3 deletions src/main/scala/de/ax/uwt/UWT.scala
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ trait HasHistory {
}
}

trait UWT extends HasHistory with LazyLogging{
trait UWT extends HasHistory with LazyLogging {
implicit def i2p(i: Int): OutputPin

implicit def i2p2(i: Int): InputPin
Expand Down Expand Up @@ -246,8 +246,7 @@ trait UWT extends HasHistory with LazyLogging{
dt.dateTime.isAfter(DateTime.now.minusHours(radius)) || dt.dateTime.isBefore(DateTime.now.plusHours(radius))
})
if (datas.isFailure) {
logger.info("error calculating weather")
datas.failed.foreach(_.printStackTrace())
datas.failed.foreach(e => logger.error("error calculating weather", e))
}
val rainLiters: Double = datas.map {
_.map(dt => dt.precipIntensity * dt.precipProbability).sum
Expand Down
39 changes: 27 additions & 12 deletions src/main/scala/de/ax/uwt/WeatherDataSet.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package de.ax.uwt

import java.io.File
import java.security.Security

import io.circe.generic.auto._
import io.circe.parser._
Expand All @@ -10,7 +9,7 @@ import scala.io.Source
import com.github.nscala_time.time.Imports._
import com.typesafe.scalalogging.LazyLogging

import scala.util.Try
import scala.util.{Failure, Success, Try}

/**
* Created by nyxos on 20.06.17.
Expand All @@ -26,24 +25,40 @@ case class WeatherDataSet(currently: WeatherData, hourly: Entry, daily: Entry) {
}

object WeatherDataSet extends App with LazyLogging {
def getIt(): Try[WeatherDataSet] = {
Try {
import sys.process._
new File("tmpfile.json").delete()
val i: Int = (s"wget --timeout=60 -O tmpfile.json https://api.darksky.net/forecast/d9db5456106658292cbd4a6dc3b6e18a/50.8958998,7.30826,${(System.currentTimeMillis() / 1000).toLong}?lang=de&units=ca" !)
Source.fromFile("tmpfile.json", "UTF8").mkString
}.flatMap { s =>
decode[WeatherDataSet](s) match {
case Left(e) => Failure(e)
case Right(v) => Success(v)
}
}
}

Security.setProperty("jdk.tls.disabledAlgorithms", "")
var lastVal: Option[Try[WeatherDataSet]] = Option.empty
var lastUpdate = System.currentTimeMillis()

def getWeatherData: Try[WeatherDataSet] = Try {
import sys.process._
new File("tmpfile.json").delete()
val i: Int = (s"wget --timeout=60 -O tmpfile.json https://api.darksky.net/forecast/d9db5456106658292cbd4a6dc3b6e18a/50.8958998,7.30826,${(System.currentTimeMillis() / 1000).toLong}?lang=de&units=ca" !)
Source.fromFile("tmpfile.json", "UTF8").mkString
}.flatMap { s =>
Try {
decode[WeatherDataSet](s).toOption.get
def getWeatherData: Try[WeatherDataSet] = {
if (System.currentTimeMillis() - lastUpdate > (1000 * 60 * 60 * 2)) {
lastVal = Option.empty
}
if (lastVal.forall(_.isFailure)) {
lastVal = Some(getIt())
lastUpdate = System.currentTimeMillis()
}
lastVal.getOrElse(Failure(new Exception("no cached weather data")))
}

getWeatherData.map { ds =>
ds.hourly.data.filter(_.precipIntensity > 0).sortBy(_.dateTime).foreach(dt => logger.info(s"${Seq(dt.dateTime, dt.time, dt.precipIntensity, dt.precipIntensity, dt.precipProbability)}"))
logger.info(s"got data count: ${ds.hourly.data.size}")
}
logger.info(s"error: ${getWeatherData.failed.map(t => t.printStackTrace())}")

getWeatherData.failed.foreach(t => logger.error("weatherData Error", t))

logger.info(s"getWeatherData:$getWeatherData")
}

0 comments on commit 9d9479d

Please sign in to comment.