Clean up
This commit is contained in:
@@ -6,163 +6,128 @@ 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 {
|
||||||
dataSources: Map[Int, String]
|
def collectData(
|
||||||
): Unit = {
|
dataSources: Map[Int, String]
|
||||||
val now = (java.time.LocalDateTime.now)
|
): Unit = {
|
||||||
.truncatedTo(java.time.temporal.ChronoUnit.SECONDS)
|
val now = (java.time.LocalDateTime.now)
|
||||||
val startTime = now.minusMonths(3).toString().concat("Z")
|
.truncatedTo(java.time.temporal.ChronoUnit.SECONDS)
|
||||||
val endTime = now.toString.concat("Z")
|
val startTime = now.minusMonths(3).toString().concat("Z")
|
||||||
|
val endTime = now.toString.concat("Z")
|
||||||
|
|
||||||
for ((k, v) <- dataSources) {
|
println("Collecting data...")
|
||||||
val url = new URL(
|
for ((k, v) <- dataSources) {
|
||||||
s"https://api.fingrid.fi/v1/variable/$k/events/csv?start_time=$startTime&end_time=$endTime"
|
val url = new URL(
|
||||||
)
|
s"https://api.fingrid.fi/v1/variable/$k/events/csv?start_time=$startTime&end_time=$endTime"
|
||||||
|
)
|
||||||
|
|
||||||
Try(url.openConnection().asInstanceOf[HttpURLConnection]) match {
|
Try(url.openConnection().asInstanceOf[HttpURLConnection]) match {
|
||||||
case util.Success(connection) =>
|
case util.Success(connection) =>
|
||||||
connection.setRequestMethod("GET")
|
connection.setRequestMethod("GET")
|
||||||
connection.setRequestProperty("Accept", "text/csv")
|
connection.setRequestProperty("Accept", "text/csv")
|
||||||
connection.setRequestProperty("x-api-key", API_KEY)
|
connection.setRequestProperty("x-api-key", API_KEY)
|
||||||
connection.connect()
|
connection.connect()
|
||||||
|
|
||||||
Try(
|
Try(
|
||||||
scala.io.Source.fromInputStream(connection.getInputStream).mkString
|
scala.io.Source.fromInputStream(connection.getInputStream).mkString
|
||||||
) match {
|
) match {
|
||||||
case util.Success(response) =>
|
case util.Success(response) =>
|
||||||
Files.write(Paths.get(v), response.getBytes("UTF-8"))
|
Files.write(Paths.get(v), response.getBytes("UTF-8"))
|
||||||
case util.Failure(e) =>
|
case util.Failure(e) =>
|
||||||
System.err.println(
|
System.err.println(
|
||||||
s"An error occurred while reading the response: ${e.getMessage}"
|
s"An error occurred while reading the response: ${e.getMessage}"
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
case util.Failure(e) =>
|
case util.Failure(e) =>
|
||||||
System.err.println(
|
System.err.println(
|
||||||
s"An error occurred while connecting to the API endpoint: ${e.getMessage}"
|
s"An error occurred while connecting to the API endpoint: ${e.getMessage}"
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
println("Collection completed")
|
||||||
|
}
|
||||||
|
|
||||||
|
def readData(filePath: String): Unit = {
|
||||||
|
println("Reading data...")
|
||||||
|
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")
|
||||||
|
val choice = readLine()
|
||||||
|
choice match {
|
||||||
|
case "1" =>
|
||||||
|
print(
|
||||||
|
"Choose the source to modify:\n1) Wind\n2) Hydro\n3) Nuclear\n4) Exit\n"
|
||||||
)
|
)
|
||||||
|
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 {
|
||||||
|
case "1" =>
|
||||||
|
val newLines = lines.updated(1, s"$newValue,${temp(1)},${temp(2)}")
|
||||||
|
newLines.foreach(pw.println)
|
||||||
|
case "2" =>
|
||||||
|
val newLines = lines.updated(1, s"${temp(0)},$newValue,${temp(2)}")
|
||||||
|
newLines.foreach(pw.println)
|
||||||
|
case "3" =>
|
||||||
|
val newLines = lines.updated(1, s"${temp(0)},${temp(1)},$newValue")
|
||||||
|
newLines.foreach(pw.println)
|
||||||
|
case "4" =>
|
||||||
|
return
|
||||||
|
case _ =>
|
||||||
|
println("Invalid choice")
|
||||||
|
}
|
||||||
|
pw.close()
|
||||||
|
println("Value updated")
|
||||||
|
case "2" =>
|
||||||
|
return
|
||||||
|
case _ =>
|
||||||
|
println("Invalid choice")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
def readData(filePath: String): Unit = {
|
def main(args: Array[String]): Unit = {
|
||||||
val bufferedSource = io.Source.fromFile(filePath)
|
val now = (java.time.LocalDateTime.now)
|
||||||
for (line <- bufferedSource.getLines) {
|
.truncatedTo(java.time.temporal.ChronoUnit.SECONDS)
|
||||||
val cols = line.split(",").map(_.trim)
|
while (true) {
|
||||||
println(s"${cols(0)}\t${cols(1)}\t${cols(2)}")
|
|
||||||
}
|
|
||||||
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
|
|
||||||
|
|
||||||
print("Enter your choice:\n1) Modify\n2) Exit\n")
|
|
||||||
val choice = readLine()
|
|
||||||
choice match {
|
|
||||||
case "1" =>
|
|
||||||
print(
|
print(
|
||||||
"Choose the source to modify:\n1) Wind\n2) Hydro\n3) Nuclear\n4) Exit\n"
|
"REPS management system:\n1) Check energy sources\n2) Collect data\n3) View data\n4) Analyze data\n5) Exit\nEnter your choice: "
|
||||||
)
|
)
|
||||||
val choice2 = readLine()
|
val choice = readLine()
|
||||||
choice2 match {
|
|
||||||
|
choice match {
|
||||||
case "1" =>
|
case "1" =>
|
||||||
print("Enter the new value: ")
|
modifySources("sources.csv")
|
||||||
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 pw = new java.io.PrintWriter(filePath)
|
|
||||||
newLines.foreach(pw.println)
|
|
||||||
pw.close()
|
|
||||||
println("Value updated")
|
|
||||||
case "2" =>
|
case "2" =>
|
||||||
print("Enter the new value: ")
|
collectData(
|
||||||
val newValue = readLine()
|
Map(
|
||||||
val lines = Source.fromFile(filePath).getLines.toList
|
188 -> "nuclear.csv",
|
||||||
val temp = lines(1).split(",")
|
191 -> "hydro.csv",
|
||||||
val newLines = lines.updated(1, s"${temp(0)},$newValue,${temp(2)}")
|
181 -> "wind.csv",
|
||||||
val pw = new java.io.PrintWriter(filePath)
|
192 -> "data.csv"
|
||||||
newLines.foreach(pw.println)
|
)
|
||||||
pw.close()
|
)
|
||||||
println("Value updated")
|
|
||||||
case "3" =>
|
case "3" =>
|
||||||
print("Enter the new value: ")
|
readData("data.csv")
|
||||||
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 pw = new java.io.PrintWriter(filePath)
|
|
||||||
newLines.foreach(pw.println)
|
|
||||||
pw.close()
|
|
||||||
println("Value updated")
|
|
||||||
case "4" =>
|
case "4" =>
|
||||||
|
println("Analyzing data...")
|
||||||
|
case "5" =>
|
||||||
println("Exiting...")
|
println("Exiting...")
|
||||||
System.exit(0)
|
System.exit(0)
|
||||||
case _ =>
|
case _ =>
|
||||||
println("Invalid choice")
|
println("Invalid choice")
|
||||||
}
|
}
|
||||||
case "2" =>
|
|
||||||
println("Exiting...")
|
|
||||||
System.exit(0)
|
|
||||||
case _ =>
|
|
||||||
println("Invalid choice")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
def main(args: Array[String]): Unit = {
|
|
||||||
val now = (java.time.LocalDateTime.now)
|
|
||||||
.truncatedTo(java.time.temporal.ChronoUnit.SECONDS)
|
|
||||||
while (true) {
|
|
||||||
print(
|
|
||||||
"REPS management system:\n1) Check energy sources\n2) Collect data\n3) View data\n4) Analyze data\n5) Exit\nEnter your choice: "
|
|
||||||
)
|
|
||||||
val choice = readLine()
|
|
||||||
|
|
||||||
choice match {
|
|
||||||
case "1" =>
|
|
||||||
// println("Energy sources:")
|
|
||||||
// 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" =>
|
|
||||||
println("Collecting data...")
|
|
||||||
collectData(
|
|
||||||
Map(
|
|
||||||
188 -> "nuclear.csv",
|
|
||||||
191 -> "hydro.csv",
|
|
||||||
181 -> "wind.csv",
|
|
||||||
192 -> "data.csv"
|
|
||||||
)
|
|
||||||
)
|
|
||||||
println("Collection completed")
|
|
||||||
case "3" =>
|
|
||||||
println("Viewing data...")
|
|
||||||
readData("data.csv")
|
|
||||||
case "4" =>
|
|
||||||
println("Analyzing data...")
|
|
||||||
case "5" =>
|
|
||||||
println("Exiting...")
|
|
||||||
System.exit(0)
|
|
||||||
case _ =>
|
|
||||||
println("Invalid choice")
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user