This commit is contained in:
AndrewTrieu
2023-04-28 10:06:31 +03:00
parent 2c7fb72aaf
commit 9f81308583

View File

@@ -6,14 +6,16 @@ import scala.io.Source
val API_KEY = Source.fromFile("apiKey.txt").getLines().mkString val API_KEY = Source.fromFile("apiKey.txt").getLines().mkString
def collectData( object Main {
def collectData(
dataSources: Map[Int, String] dataSources: Map[Int, String]
): Unit = { ): Unit = {
val now = (java.time.LocalDateTime.now) val now = (java.time.LocalDateTime.now)
.truncatedTo(java.time.temporal.ChronoUnit.SECONDS) .truncatedTo(java.time.temporal.ChronoUnit.SECONDS)
val startTime = now.minusMonths(3).toString().concat("Z") val startTime = now.minusMonths(3).toString().concat("Z")
val endTime = now.toString.concat("Z") val endTime = now.toString.concat("Z")
println("Collecting data...")
for ((k, v) <- dataSources) { for ((k, v) <- dataSources) {
val url = new URL( val url = new URL(
s"https://api.fingrid.fi/v1/variable/$k/events/csv?start_time=$startTime&end_time=$endTime" s"https://api.fingrid.fi/v1/variable/$k/events/csv?start_time=$startTime&end_time=$endTime"
@@ -42,24 +44,21 @@ def collectData(
) )
} }
} }
} println("Collection completed")
}
def readData(filePath: String): Unit = { def readData(filePath: String): Unit = {
println("Reading data...")
val bufferedSource = io.Source.fromFile(filePath) val bufferedSource = io.Source.fromFile(filePath)
for (line <- bufferedSource.getLines) { for (line <- bufferedSource.getLines) {
val cols = line.split(",").map(_.trim) val cols = line.split(",").map(_.trim)
println(s"${cols(0)}\t${cols(1)}\t${cols(2)}") println(s"${cols(0)}\t${cols(1)}\t${cols(2)}")
} }
bufferedSource.close bufferedSource.close
}
def checkSources(filePath: String): Unit = {
val bufferedSource = io.Source.fromFile(filePath)
for (line <- bufferedSource.getLines) {
val cols = line.split(",").map(_.trim)
println(s"${cols(0)}\t${cols(1)}\t${cols(2)}")
} }
bufferedSource.close
def modifySources(filePath: String): Unit = {
readData(filePath)
print("Enter your choice:\n1) Modify\n2) Exit\n") print("Enter your choice:\n1) Modify\n2) Exit\n")
val choice = readLine() val choice = readLine()
@@ -69,52 +68,36 @@ def checkSources(filePath: String): Unit = {
"Choose the source to modify:\n1) Wind\n2) Hydro\n3) Nuclear\n4) Exit\n" "Choose the source to modify:\n1) Wind\n2) Hydro\n3) Nuclear\n4) Exit\n"
) )
val choice2 = readLine() val choice2 = readLine()
print("Enter the new value: ")
val newValue = readLine()
val lines = Source.fromFile(filePath).getLines.toList
val temp = lines(1).split(",")
val pw = new java.io.PrintWriter(filePath)
choice2 match { choice2 match {
case "1" => case "1" =>
print("Enter the new value: ")
val newValue = readLine()
val lines = Source.fromFile(filePath).getLines.toList
val temp = lines(1).split(",")
val newLines = lines.updated(1, s"$newValue,${temp(1)},${temp(2)}") val newLines = lines.updated(1, s"$newValue,${temp(1)},${temp(2)}")
val pw = new java.io.PrintWriter(filePath)
newLines.foreach(pw.println) newLines.foreach(pw.println)
pw.close()
println("Value updated")
case "2" => case "2" =>
print("Enter the new value: ")
val newValue = readLine()
val lines = Source.fromFile(filePath).getLines.toList
val temp = lines(1).split(",")
val newLines = lines.updated(1, s"${temp(0)},$newValue,${temp(2)}") val newLines = lines.updated(1, s"${temp(0)},$newValue,${temp(2)}")
val pw = new java.io.PrintWriter(filePath)
newLines.foreach(pw.println) newLines.foreach(pw.println)
pw.close()
println("Value updated")
case "3" => case "3" =>
print("Enter the new value: ")
val newValue = readLine()
val lines = Source.fromFile(filePath).getLines.toList
val temp = lines(1).split(",")
val newLines = lines.updated(1, s"${temp(0)},${temp(1)},$newValue") val newLines = lines.updated(1, s"${temp(0)},${temp(1)},$newValue")
val pw = new java.io.PrintWriter(filePath)
newLines.foreach(pw.println) newLines.foreach(pw.println)
case "4" =>
return
case _ =>
println("Invalid choice")
}
pw.close() pw.close()
println("Value updated") println("Value updated")
case "4" =>
println("Exiting...")
System.exit(0)
case _ =>
println("Invalid choice")
}
case "2" => case "2" =>
println("Exiting...") return
System.exit(0)
case _ => case _ =>
println("Invalid choice") println("Invalid choice")
} }
} }
def main(args: Array[String]): Unit = { def main(args: Array[String]): Unit = {
val now = (java.time.LocalDateTime.now) val now = (java.time.LocalDateTime.now)
.truncatedTo(java.time.temporal.ChronoUnit.SECONDS) .truncatedTo(java.time.temporal.ChronoUnit.SECONDS)
while (true) { while (true) {
@@ -125,25 +108,8 @@ def main(args: Array[String]): Unit = {
choice match { choice match {
case "1" => case "1" =>
// println("Energy sources:") modifySources("sources.csv")
// println("1) Wind\n2) Hydro\n3) Nuclear\n4) All")
// print("Enter your choice: ")
// val choice2 = readLine()
// choice2 match {
// case "1" =>
// println("Wind")
// case "2" =>
// println("Hydro")
// case "3" =>
// println("Nuclear")
// case "4" =>
// println("All")
// case _ =>
// println("Invalid choice")
// }
checkSources("sources.csv")
case "2" => case "2" =>
println("Collecting data...")
collectData( collectData(
Map( Map(
188 -> "nuclear.csv", 188 -> "nuclear.csv",
@@ -152,9 +118,7 @@ def main(args: Array[String]): Unit = {
192 -> "data.csv" 192 -> "data.csv"
) )
) )
println("Collection completed")
case "3" => case "3" =>
println("Viewing data...")
readData("data.csv") readData("data.csv")
case "4" => case "4" =>
println("Analyzing data...") println("Analyzing data...")
@@ -165,4 +129,5 @@ def main(args: Array[String]): Unit = {
println("Invalid choice") println("Invalid choice")
} }
} }
}
} }