This commit is contained in:
Sage The DM 2024-11-25 22:25:35 +01:00
parent db6b2a2a6e
commit 58a45463d4
38 changed files with 512 additions and 173 deletions

View file

@ -0,0 +1,7 @@
{
"java.project.sourcePaths": ["src"],
"java.project.outputPath": "bin",
"java.project.referencedLibraries": [
"lib/**/*.jar"
]
}

View file

@ -0,0 +1,18 @@
## Getting Started
Welcome to the VS Code Java world. Here is a guideline to help you get started to write Java code in Visual Studio Code.
## Folder Structure
The workspace contains two folders by default, where:
- `src`: the folder to maintain sources
- `lib`: the folder to maintain dependencies
Meanwhile, the compiled output files will be generated in the `bin` folder by default.
> If you want to customize the folder structure, open `.vscode/settings.json` and update the related settings there.
## Dependency Management
The `JAVA PROJECTS` view allows you to manage your dependencies. More details can be found [here](https://github.com/microsoft/vscode-java-dependency#manage-dependencies).

View file

@ -0,0 +1,22 @@
public static void main(String[] args) {
try {
// Example input for generating the URL
String channelId = "123456"; // Replace with your actual channel ID
String apiKey = "YOUR_READ_API_KEY"; // Replace with your actual API key
int results = 50; // Fetch 50 records
String start = "2024-11-01 00:00:00"; // Start date-time
String end = "2024-11-01 23:59:59"; // End date-time
String timezone = "Europe/Zurich"; // Timezone
// Generate the dynamic URL using the method
String url = createThingSpeakURL(channelId, apiKey, results, start, end, timezone);
// Output the generated URL
System.out.println("Generated ThingSpeak URL: ");
System.out.println(url);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
}

View file

@ -0,0 +1,87 @@
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class ThingSpeakURLGenerator {
// Constants for Room channel numbers
private static final int ROOM_39_NUMBER = 1521262;
private static final int ROOM_38_NUMBER = 1364580;
private static final int ROOM_37_NUMBER = 1521263;
// Example Dates for fetching data, dynamically generated
private static final LocalDateTime START_DATE = LocalDateTime.of(2024, 11, 1, 0, 0);
private static final LocalDateTime END_DATE = LocalDateTime.of(2024, 11, 1, 23, 59);
// Room URLs
private static final Map<Integer, String> ROOM_URLS = new HashMap<>();
static {
ROOM_URLS.put(39, createUrl(ROOM_39_NUMBER));
ROOM_URLS.put(38, createUrl(ROOM_38_NUMBER));
ROOM_URLS.put(37, createUrl(ROOM_37_NUMBER));
}
// Method to create the ThingSpeak API URL dynamically
public static String createUrl(int channelNumber) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String startDate = encodeUrlParam(START_DATE.format(formatter));
String endDate = encodeUrlParam(END_DATE.format(formatter));
return String.format("https://api.thingspeak.com/channels/%d/feeds.csv?start=%s&end=%s",
channelNumber, startDate, endDate);
}
// Helper method to URL encode the date strings
private static String encodeUrlParam(String value) {
try {
return URLEncoder.encode(value, "UTF-8");
} catch (UnsupportedEncodingException e) {
System.err.println("Error encoding URL parameter: " + e.getMessage());
return value;
}
}
// Method to calculate the average of a specific number of data points (e.g.,
// CO2 levels)
public static double calculateAverage(List<Co2Data> dataList) {
if (dataList == null || dataList.isEmpty()) {
return 0.0;
}
double sum = 0.0;
for (Co2Data data : dataList) {
sum += data.getCo2Level(); // Assuming Co2Data has a method getCo2Level() that returns the CO2 level
}
return sum / dataList.size();
}
public static void main(String[] args) {
try {
// Print out the generated URLs for each room
for (Map.Entry<Integer, String> entry : ROOM_URLS.entrySet()) {
System.out.println("Room " + entry.getKey() + " URL: " + entry.getValue());
}
// Example: Calculate and print the average CO2 level for each room (assuming
// Co2Data.getData() method exists)
List<Co2Data> room39Data = Co2Data.getData(ROOM_URLS.get(39));
List<Co2Data> room38Data = Co2Data.getData(ROOM_URLS.get(38));
List<Co2Data> room37Data = Co2Data.getData(ROOM_URLS.get(37));
double avgRoom39 = calculateAverage(room39Data);
double avgRoom38 = calculateAverage(room38Data);
double avgRoom37 = calculateAverage(room37Data);
System.out.println("Average CO2 Level in Room 39: " + avgRoom39);
System.out.println("Average CO2 Level in Room 38: " + avgRoom38);
System.out.println("Average CO2 Level in Room 37: " + avgRoom37);
} catch (Exception e) {
e.printStackTrace();
}
}
}