<commit_before>package brewcontrol

import scala.io.Source
import scala.util.Try

/** Low level interface to deal with temperature sensors */
class TemperatureConnection {

  def sensorIds(): Try[Set[String]] = {
    val path = "/sys/bus/w1/devices/w1_bus_master1/w1_master_slaves"
    Try(Source.fromFile(path).getLines().toSet)
  }

  def temperature(sensorId: String): Try[Float] = {
    val path = s"/sys/bus/w1/devices/$sensorId/w1_slave"
    Try {
      val lines = Source.fromFile(path).getLines().toList
      val floats = (for {
        line <- lines
        s <- "t=(\\d+)".r.findFirstMatchIn(line).map(_.group(1))
        f = s.toInt / 1000f
      } yield f).toSeq
      floats match {
        case Nil => throw new RuntimeException(s"Failed to parse temperature of sensor: $sensorId, content: '$lines'")
        case v :: _ => v
      }
    }
  }
}
<commit_msg>Make TemperatureConnection more tolerant<commit_after>package brewcontrol

import java.io.File

import scala.io.Source
import scala.util.Try

/** Low level interface to deal with temperature sensors */
class TemperatureConnection {

  private object Paths {
    def w1MasterSlaves: String = "/sys/bus/w1/devices/w1_bus_master1/w1_master_slaves"
    def w1Slave(sensorId: String): String = s"/sys/bus/w1/devices/$sensorId/w1_slave"
  }

  def sensorIds(): Try[Set[String]] = {
    Try {
      val ids = Source.fromFile(Paths.w1MasterSlaves).getLines().toSet
      // Filter out any devices that don't have w1_slave files. Whatever they are, they aren't the sensors we seek
      ids.filter(id => new File(Paths.w1Slave(id)).exists())
    }
  }

  def temperature(sensorId: String): Try[Float] = {
    val path = Paths.w1Slave(sensorId)
    Try {
      val lines = Source.fromFile(path).getLines().toList
      val floats = (for {
        line <- lines
        s <- "t=(\\d+)".r.findFirstMatchIn(line).map(_.group(1))
        f = s.toInt / 1000f
      } yield f).toSeq
      floats match {
        case Nil => throw new RuntimeException(s"Failed to parse temperature of sensor: $sensorId, content: '$lines'")
        case v :: _ => v
      }
    }
  }
}

{'fraction_non_alphanumeric': 0.12988235294117648, 'fraction_numerical': 0.013176470588235295, 'mean_word_length': 2.886654478976234, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, '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': '1367419', 'n_tokens_mistral': 724, 'n_tokens_neox': 687, 'n_words': 166}