Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private def parseObject(obj: Node, currentPath: String): Either[ParseError,Seq[SensorData]] = {
- val eitherInfoVals: Seq[Either[ParseError, SensorData]] = for {
- infoitem <- obj \ "InfoItem"
- objId = obj \ "id"
- objName = infoitem \ "@name"
- objValue = infoitem \ "value"
- sensorDataEither <- for {
- path: String <- currentPath + "/" + (
- objId.headOption match {
- case Some( path_ ) => Right(path_.text)
- case None =>
- Left( new ParseError( "Broken odf format. No id." ) )
- }
- ) + "/" + objName.text
- value: String <- objValue.headOption match {
- case Some( value_ ) => Right(value_.text)
- case None =>
- Left( new ParseError( "No value element found in infoitem." ) )
- }
- dateTimeAttr = infoitem \ "value" \ "@dateTime"
- dateTime: String <- dateTimeAttr.headOption match { // or Right(...getOrElse(""))
- case Some( value_ ) => Right(value_.text)
- case None => Right("")
- }
- typeAttr = infoitem \ "value" \ "@type"
- typeOfInfo: String <- typeAttr.headOption match {
- case Some( value_ ) => Right(value_.text)
- case None => Right("")
- }
- } yield new SensorData( path, value, dateTime )
- } yield sensorDataEither
- val obObjects = obj \ "Object"
- val eitherDeeperVals =
- for {
- subobj <- obObjects
- objId = obj \ "id"
- nextPath = currentPath + "/" + (
- objId.headOption match {
- case Some( path_ ) => Right(path_.text)
- case None =>
- return Left( new ParseError( "Broken odf format. No id." ) )
- }
- )
- } yield parseObject(subobj, nextPath)
- val allData = eitherInfoVals ++ eitherDeeperVals.flatten
- val rightData = allData.map(
- _ match {
- case Left(err) => return Left(err)
- case Right(r) => r
- }
- )
- return Right(rightData)
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement