Skip to content

Commit

Permalink
Fix query ranges
Browse files Browse the repository at this point in the history
  • Loading branch information
lr101 committed Aug 31, 2024
1 parent 6802ca6 commit 48b3519
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 22 deletions.
15 changes: 10 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,13 @@ This service is run via docker:
docker compose up -d
```

## Dev

Ardiono IDE ESP8266 Library: http://arduino.esp8266.com/stable/package_esp8266com_index.json

Tile CSS: https://1linelayouts.glitch.me/
## Arduino

1. Driver installation **(specifically for ESP8266 by AZ-Delivery)**: https://www.arduined.eu/ch340-windows-10-driver-download/
2. Connect your Arduino
3. Add Device Library under *preferences -> Additional Boards managt by URLs*: http://arduino.esp8266.com/stable/package_esp8266com_index.json
4. Select the Board under: *Board -> esp8266 -> NodeMCU 1.0 (ESP-12E Module)*
5. Select your COM-Port (should show up when installed the driver correctly and a usb cable capable of transferring data is used)
6. Install dependencies (look at the *#includes* and search for them)
7. Rename the `config_template.h` file to `config.h` and fill in your WiFi information
8. Upload and test
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,10 @@ import com.influxdb.exceptions.BadRequestException
import de.lrprojects.tempserver.config.InfluxProperties
import de.lrprojects.tempserver.entity.Entry
import de.lrprojects.tempserver.service.api.EntryService
import org.jetbrains.kotlin.preloading.ProfilingInstrumenterExample.e
import org.slf4j.LoggerFactory
import org.springframework.stereotype.Service
import java.sql.Timestamp
import java.time.Instant
import java.time.LocalDateTime
import java.time.OffsetDateTime
import java.time.ZoneOffset
import java.time.format.DateTimeFormatter

@Service
class EntryServiceImpl(
Expand All @@ -24,14 +19,14 @@ class EntryServiceImpl(
): EntryService {

override fun getEntries(sensorId: String, date1: OffsetDateTime?, date2: OffsetDateTime?, limit: Int?, interval: Int?): List<Entry> {
val startDate = date1?.toString() ?: "0"
val stopDate = date2?.toString() ?: "now()"
val toDate = date1?.toString() ?: "now()"
val fromDate = date2?.toString() ?: "0"
val query = if (limit != null) {
getLimit(influxProperties.bucket, sensorId, startDate, stopDate, limit)
getLimit(influxProperties.bucket, sensorId, toDate, fromDate, limit)
} else if (interval != null) {
aggregatedMean(influxProperties.bucket, sensorId, startDate, stopDate, interval)
aggregatedMean(influxProperties.bucket, sensorId, toDate, fromDate, interval)
} else {
getEntries(influxProperties.bucket, sensorId, startDate, stopDate)
getEntries(influxProperties.bucket, sensorId, toDate, fromDate)
}

val queryApi = influxDBClient.queryApi
Expand Down Expand Up @@ -75,23 +70,23 @@ class EntryServiceImpl(
}

companion object {
fun aggregatedMean(bucket: String, sensorId: String, date1: String, date2: String, interval: Int) = """
fun aggregatedMean(bucket: String, sensorId: String, toDate: String, fromDate: String, interval: Int) = """
from(bucket: "$bucket")
|> range(start: $date1, stop: $date2)
|> range(start: $fromDate, stop: $toDate)
|> filter(fn: (r) => r["sensorId"] == "$sensorId")
|> group(columns: ["sensorId", "_field"])
|> aggregateWindow(every: $interval, fn: mean, createEmpty: false)
|> yield(name: "mean")
"""
fun getEntries(bucket: String, sensorId: String, date1: String, date2: String) = """
fun getEntries(bucket: String, sensorId: String, toDate: String, fromDate: String) = """
from(bucket: "$bucket")
|> range(start: $date1, stop: $date2)
|> range(start: $fromDate, stop: $toDate)
|> filter(fn: (r) => r["sensorId"] == "$sensorId")
"""

fun getLimit(bucket: String, sensorId: String, date1: String, date2: String, limit: Int) = """
fun getLimit(bucket: String, sensorId: String, toDate: String, fromDate: String, limit: Int) = """
from(bucket: "$bucket")
|> range(start: $date1, stop: $date2)
|> range(start: $fromDate, stop: $toDate)
|> filter(fn: (r) => r["sensorId"] == "$sensorId")
|> limit(n: $limit)
"""
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/static/javascript/display.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ function getCurrentDate(offset) {

function getDataCurrent() {
const ajax = new XMLHttpRequest();
ajax.open("GET", "/sensors/" + id + "/entry?date2=" + getCurrentDate(10) + "&limit=1" , true);
ajax.open("GET", "/rest/v1/sensors/" + id + "/entry?date2=" + getCurrentDate(10) + "&limit=1" , true);
ajax.send(null);
ajax.onreadystatechange = function() {
if (ajax.readyState === 4) {
Expand Down

0 comments on commit 48b3519

Please sign in to comment.