First commit

This commit is contained in:
AndrewTrieu
2023-04-27 13:28:31 +03:00
commit aefc2baf60
5 changed files with 226 additions and 0 deletions

9
.gitignore vendored Normal file
View File

@@ -0,0 +1,9 @@
.metals/metals.lock.db
.metals/metals.mv.db
.metals/metals.log
.vscode/settings.json
src/main/scala/data.csv
src/main/scala/hydro.csv
src/main/scala/nuclear.csv
src/main/scala/wind.csv
src/main/scala/apiKey.txt

2
.scalafmt.conf Normal file
View File

@@ -0,0 +1,2 @@
version = "3.7.3"
runner.dialect = scala213

75
build.sbt Normal file
View File

@@ -0,0 +1,75 @@
// The simplest possible sbt build file is just one line:
scalaVersion := "2.13.8"
// That is, to create a valid sbt build, all you've got to do is define the
// version of Scala you'd like your project to use.
// ============================================================================
// Lines like the above defining `scalaVersion` are called "settings". Settings
// are key/value pairs. In the case of `scalaVersion`, the key is "scalaVersion"
// and the value is "2.13.8"
// It's possible to define many kinds of settings, such as:
name := "hello-world"
organization := "ch.epfl.scala"
version := "1.0"
// Note, it's not required for you to define these three settings. These are
// mostly only necessary if you intend to publish your library's binaries on a
// place like Sonatype.
// Want to use a published library in your project?
// You can define other libraries as dependencies in your build like this:
libraryDependencies += "org.scala-lang.modules" %% "scala-parser-combinators" % "2.1.1"
// Here, `libraryDependencies` is a set of dependencies, and by using `+=`,
// we're adding the scala-parser-combinators dependency to the set of dependencies
// that sbt will go and fetch when it starts up.
// Now, in any Scala file, you can import classes, objects, etc., from
// scala-parser-combinators with a regular import.
// TIP: To find the "dependency" that you need to add to the
// `libraryDependencies` set, which in the above example looks like this:
// "org.scala-lang.modules" %% "scala-parser-combinators" % "2.1.1"
// You can use Scaladex, an index of all known published Scala libraries. There,
// after you find the library you want, you can just copy/paste the dependency
// information that you need into your build file. For example, on the
// scala/scala-parser-combinators Scaladex page,
// https://index.scala-lang.org/scala/scala-parser-combinators, you can copy/paste
// the sbt dependency from the sbt box on the right-hand side of the screen.
// IMPORTANT NOTE: while build files look _kind of_ like regular Scala, it's
// important to note that syntax in *.sbt files doesn't always behave like
// regular Scala. For example, notice in this build file that it's not required
// to put our settings into an enclosing object or class. Always remember that
// sbt is a bit different, semantically, than vanilla Scala.
// ============================================================================
// Most moderately interesting Scala projects don't make use of the very simple
// build file style (called "bare style") used in this build.sbt file. Most
// intermediate Scala projects make use of so-called "multi-project" builds. A
// multi-project build makes it possible to have different folders which sbt can
// be configured differently for. That is, you may wish to have different
// dependencies or different testing frameworks defined for different parts of
// your codebase. Multi-project builds make this possible.
// Here's a quick glimpse of what a multi-project build looks like for this
// build, with only one "subproject" defined, called `root`:
// lazy val root = (project in file(".")).
// settings(
// inThisBuild(List(
// organization := "ch.epfl.scala",
// scalaVersion := "2.13.8"
// )),
// name := "hello-world"
// )
// To learn more about multi-project builds, head over to the official sbt
// documentation at http://www.scala-sbt.org/documentation.html

1
project/build.properties Normal file
View File

@@ -0,0 +1 @@
sbt.version=1.8.2

139
src/main/scala/Main.scala Normal file
View File

@@ -0,0 +1,139 @@
import java.io._
import java.net._
import scala.io.StdIn.readLine
import scala.io.Source
val API_KEY = Source.fromFile("apiKey.txt").getLines().mkString
def collectData(
variableId: Int,
startTime: String,
endTime: String,
filePath: String
): Unit = {
try {
// Set the URL for the API endpoint
val url = new URL(
s"https://api.fingrid.fi/v1/variable/$variableId/events/csv?start_time=$startTime&end_time=$endTime"
)
// Open a connection to the API endpoint
val connection =
url.openConnection().asInstanceOf[HttpURLConnection]
// Set the request method to GET
connection.setRequestMethod("GET")
// Set the request headers
connection.setRequestProperty("Accept", "text/csv")
connection.setRequestProperty("x-api-key", API_KEY)
// Send the GET request to the API endpoint
connection.connect()
// Read the response from the API endpoint
val inputStream = connection.getInputStream
val inputStreamReader = new InputStreamReader(inputStream)
val reader = new BufferedReader(inputStreamReader)
val response = new StringBuilder
var line: String = reader.readLine
while (line != null) {
response.append(line + "\n")
line = reader.readLine
}
reader.close()
// Save response to a CSV file
val file = new File(filePath)
val bw = new BufferedWriter(new FileWriter(file))
bw.write(response.toString)
bw.close()
} catch {
case e: IOException =>
System.err.println(
"An error occurred while fetching the details: " + e.getMessage
)
}
}
def readData(filePath: String): Unit = {
val bufferedSource = io.Source.fromFile(filePath)
for (line <- bufferedSource.getLines) {
val cols = line.split(",").map(_.trim)
println(s"${cols(0)}|${cols(1)}|${cols(2)}")
}
bufferedSource.close
}
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) Solar\n3) Hydro\n4) Nuclear\n5) Fossil\n6) All")
print("Enter your choice: ")
val choice2 = readLine()
choice2 match {
case "1" =>
println("Wind")
case "2" =>
println("Solar")
case "3" =>
println("Hydro")
case "4" =>
println("Nuclear")
case "5" =>
println("Fossil")
case "6" =>
println("All")
case _ =>
println("Invalid choice")
}
case "2" =>
println("Collecting data...")
collectData(
188,
now.minusMonths(3).toString().concat("Z"),
now.toString.concat("Z"),
"nuclear.csv"
)
collectData(
191,
now.minusMonths(3).toString().concat("Z"),
now.toString.concat("Z"),
"hydro.csv"
)
collectData(
181,
now.minusMonths(3).toString().concat("Z"),
now.toString.concat("Z"),
"wind.csv"
)
collectData(
192,
now.minusMonths(3).toString().concat("Z"),
now.toString.concat("Z"),
"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")
}
}
}