commit aefc2baf600ed664c9e6345a7b5c70f0705b3f22 Author: AndrewTrieu Date: Thu Apr 27 13:28:31 2023 +0300 First commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..f89126e --- /dev/null +++ b/.gitignore @@ -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 diff --git a/.scalafmt.conf b/.scalafmt.conf new file mode 100644 index 0000000..755b21e --- /dev/null +++ b/.scalafmt.conf @@ -0,0 +1,2 @@ +version = "3.7.3" +runner.dialect = scala213 \ No newline at end of file diff --git a/build.sbt b/build.sbt new file mode 100644 index 0000000..88b4f7b --- /dev/null +++ b/build.sbt @@ -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 diff --git a/project/build.properties b/project/build.properties new file mode 100644 index 0000000..46e43a9 --- /dev/null +++ b/project/build.properties @@ -0,0 +1 @@ +sbt.version=1.8.2 diff --git a/src/main/scala/Main.scala b/src/main/scala/Main.scala new file mode 100644 index 0000000..0376dfc --- /dev/null +++ b/src/main/scala/Main.scala @@ -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") + } + } +}