<commit_before>package brewcontrol

import akka.actor._
import akka.io.IO
import akka.pattern.ask
import akka.util.Timeout
import brewcontrol.History.Item
import com.typesafe.scalalogging.LazyLogging
import rx._
import rx.core.Reactor
import rx.ops.{AkkaScheduler, _}
import slick.jdbc.JdbcBackend.Database
import spray.can.Http

import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.duration._

trait AbstractBrewApp extends App with LazyLogging {

  implicit def temperatureConnection: TemperatureConnection
  implicit def gpio: GpioConnection
  def host = "0.0.0.0"
  def port = 8080
  def jdbcUrl = "jdbc:sqlite:/mnt/lfs/brewcontrol/data.sqlite"

  logger.info(s"Hi from BrewControl")
  sys.addShutdownHook(logger.info("Shutting down"))

  Class.forName("org.sqlite.JDBC")
  logger.info(s"Using Database $jdbcUrl")
  val database = Database.forURL(jdbcUrl)
  implicit val db = new DB(database)
  db.init()

  implicit val system = akka.actor.ActorSystem()
  implicit val scheduler = system.scheduler
  implicit val rxScheduler = new AkkaScheduler(system)
  implicit val clock = new Clock
  implicit val config = new Config()

  val temperatureReader = new TemperatureReaderImpl()

  var observers = List[Reactor[_]]()

  observers = persistTemperatureReadings() :: observers

  val relayController = new RelayController()
  observers = persistRelayStates().toList ::: observers

  val pidController = new PidController(config.targetTemperature, temperatureReader.Cooler.temperature, 10 seconds)

  // We use some tolerance to create a deadband in which neither relay is turned on
  // This is to avoid switching too quickly between heating and cooling
  val temperatureTolerance = 0.5f

  observers = pidController.output.map { output =>
    relayController.Cooler.value() = config.coolerEnabled() && output < -temperatureTolerance
    relayController.Heater.value() = config.heaterEnabled() && output > temperatureTolerance
  } :: observers

  startWebServer()
  logger.info("Startup complete")
  // No need to do anything else - the daemon threads are loose!

  def persistTemperatureReadings(): Obs = {
    temperatureReader.currentReadings.foreach(
      _.foreach(reading => History.addItem(reading.sensorId, "double", Item(reading.timestamp, reading.value)))
    )
  }

  def persistRelayStates(): Seq[Obs] = {
    relayController.relays.map(r =>
      r.value.foreach(v => History.addItem(r.name, "binary", Item(clock.now.getMillis, if (v) 1 else 0)))
    )
  }

  def startWebServer() = {<commit_msg>user sensor name instead of id for history<commit_after>package brewcontrol

import akka.actor._
import akka.io.IO
import akka.pattern.ask
import akka.util.Timeout
import brewcontrol.History.Item
import com.typesafe.scalalogging.LazyLogging
import rx._
import rx.core.Reactor
import rx.ops.{AkkaScheduler, _}
import slick.jdbc.JdbcBackend.Database
import spray.can.Http

import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.duration._

trait AbstractBrewApp extends App with LazyLogging {

  implicit def temperatureConnection: TemperatureConnection
  implicit def gpio: GpioConnection
  def host = "0.0.0.0"
  def port = 8080
  def jdbcUrl = "jdbc:sqlite:/mnt/lfs/brewcontrol/data.sqlite"

  logger.info(s"Hi from BrewControl")
  sys.addShutdownHook(logger.info("Shutting down"))

  Class.forName("org.sqlite.JDBC")
  logger.info(s"Using Database $jdbcUrl")
  val database = Database.forURL(jdbcUrl)
  implicit val db = new DB(database)
  db.init()

  implicit val system = akka.actor.ActorSystem()
  implicit val scheduler = system.scheduler
  implicit val rxScheduler = new AkkaScheduler(system)
  implicit val clock = new Clock
  implicit val config = new Config()

  val temperatureReader = new TemperatureReaderImpl()

  var observers = List[Reactor[_]]()

  observers = persistTemperatureReadings() :: observers

  val relayController = new RelayController()
  observers = persistRelayStates().toList ::: observers

  val pidController = new PidController(config.targetTemperature, temperatureReader.Cooler.temperature, 10 seconds)

  // We use some tolerance to create a deadband in which neither relay is turned on
  // This is to avoid switching too quickly between heating and cooling
  val temperatureTolerance = 0.5f

  observers = pidController.output.map { output =>
    relayController.Cooler.value() = config.coolerEnabled() && output < -temperatureTolerance
    relayController.Heater.value() = config.heaterEnabled() && output > temperatureTolerance
  } :: observers

  startWebServer()
  logger.info("Startup complete")
  // No need to do anything else - the daemon threads are loose!

  def persistTemperatureReadings(): Obs = {
    temperatureReader.currentReadings.foreach(
      _.foreach(reading => History.addItem(reading.name, "double", Item(reading.timestamp, reading.value)))
    )
  }

  def persistRelayStates(): Seq[Obs] = {
    relayController.relays.map(r =>
      r.value.foreach(v => History.addItem(r.name, "binary", Item(clock.now.getMillis, if (v) 1 else 0)))
    )
  }

  def startWebServer() = {
{'fraction_non_alphanumeric': 0.09990205680705191, 'fraction_numerical': 0.005484818805093046, 'mean_word_length': 4.55, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2484257', 'n_tokens_mistral': 1599, 'n_tokens_neox': 1520, 'n_words': 397}