Skip to content

Commit

Permalink
Fix date queries to influxdb
Browse files Browse the repository at this point in the history
  • Loading branch information
lr101 committed Aug 31, 2024
1 parent 779773c commit 6802ca6
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 59 deletions.
41 changes: 10 additions & 31 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,37 +1,16 @@
services:
temp-server:
container_name: temp-server
image: lr.dev.projects/temp-server:latest
image: lrprojects/tempserver:1.0.0-SNAPSHOT
restart: unless-stopped
ports:
- 8081:8081
- 8080:8080
env_file:
- .env
environment:
DB_HOST: db
EMAIL: lr.dev.projects@gmail.com

# ---- Edit to change default: ----
# PORT: 8081

# ---- From .env file: -------
# POSTGRES_PASSWORD
# POSTGRES_USER
# POSTGRES_DB
# EMAIL_PASSWORD
# SECRET
depends_on:
- db
- influxdb

db:
image: postgres:16
restart: unless-stopped
container_name: stick-it-db
ports:
- 5433:5432
volumes:
- ./postgres-data:/var/lib/postgresql/data
- ./backup:/backup
env_file:
- .env
networks:
- influxdb-network
- postgresql-network
networks:
influxdb-network:
external: true
postgresql-network:
external: true
68 changes: 48 additions & 20 deletions src/main/ardoino/ESP8622_DS18B20_http/ESP8622_DS18B20_http.ino
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,16 @@
#include <DallasTemperature.h>
#include <Arduino_JSON.h>
#include <WiFiClientSecure.h>
#include <WiFiUdp.h>
#include "config.h"
//Define GPIO pin D2 as Data-pin
#define ONE_WIRE_BUS 4

// define time libary
#define NTP_OFFSET 0 // In seconds
#define DAYLIGHT_OFFSET 0 // In miliseconds
#define NTP_ADDRESS "europe.pool.ntp.org"

String convertSensorID(DeviceAddress);

struct sensorStruct {
Expand All @@ -19,7 +25,7 @@ struct sensorStruct {
float* curValue;
};

int avgSleepTime;
int avgSleepTime = 200;

// Setup a oneWire instance to communicate with any OneWire devices
OneWire oneWire(ONE_WIRE_BUS);
Expand Down Expand Up @@ -63,7 +69,14 @@ void setup() {
Serial.print("http://");
Serial.print(WiFi.localIP());
Serial.println("/");


// start time client
configTime(NTP_OFFSET, DAYLIGHT_OFFSET, NTP_ADDRESS);
while (!time(nullptr)) {
Serial.println("Waiting for time");
delay(1000);
}

/*
* Setup Thermometers
* Send Thermometer serial number to server to setup db
Expand All @@ -89,29 +102,31 @@ void setup() {
HTTPClient http;

//Connect to WebServer:
if(http.begin(client, ("http://" + host + "/sensors/id/").c_str())){
if(http.begin(client, ("http://" + host + "/rest/v1/sensors").c_str())){
// we are connected to the host!
http.addHeader("Content-Type", "application/json");
Serial.println(sensor_id);//Send the request
String json = "{\"sensorId\":\"" + sensor_id + "\",\"sensorNick\":\"newSensor\" ,\"id\": -1,\"sensorType\": {"
+ "\"id\": 0, \"sensorType\": \"Default\", \"unit\": \"dUnit\", \"repetitions\": 10,\"sleepTime\": 10"
+ "}}";
int httpCode = http.POST(json);
String json = "{\"sensorId\":\"" + sensor_id + "\",\"sensorNick\":\"newSensor\",\"sensorType\": \"Thermometer\"}";
int httpCode = http.POST(json);
infos[i].repetitions = 10;
infos[i].sensor_id = sensor_id;
infos[i].values = new float[10];
infos[i].curValue = infos[i].values;

if (httpCode > 0) { //Check the returning code
http.end();
//get Information of Sensor
http.begin(client, ("http://" + host + "/sensors/id/" + sensor_id).c_str());
int httpCode = http.GET();
if (httpCode == HTTP_CODE_OK) {
JSONVar myObject = JSON.parse(http.getString());
infos[i].repetitions = (int) myObject["sensorType"]["repetitions"];
avgSleepTime += (int) myObject["sensorType"]["sleepTime"];
infos[i].sensor_id = sensor_id;
infos[i].values = new float[infos[i].repetitions];
infos[i].curValue = infos[i].values;
}
http.end();
//http.begin(client, ("http://" + host + "/sensors/id/" + sensor_id).c_str());
//int httpCode = http.GET();
//if (httpCode == HTTP_CODE_OK) {
// JSONVar myObject = JSON.parse(http.getString());
// infos[i].repetitions = (int) myObject["sensorType"]["repetitions"];
// avgSleepTime += (int) myObject["sensorType"]["sleepTime"];
// infos[i].sensor_id = sensor_id;
// infos[i].values = new float[infos[i].repetitions];
// infos[i].curValue = infos[i].values;
// }
// http.end();
}
} else {
// connection failure
Expand All @@ -131,6 +146,7 @@ void loop() {
**/
sensors.requestTemperatures(); // Send the command to get temperatures


// Loop through each device, print out temperature data
for(int i=0;i<numberOfDevices; i++){
// Search the wire for address
Expand All @@ -150,10 +166,10 @@ void loop() {
HTTPClient http;

//Connect to WebServer:
if (http.begin(client,"http://" + host + "/sensors/post/" + infos[i].sensor_id + "/")){
if (http.begin(client,"http://" + host + "/rest/v1/sensors/" + infos[i].sensor_id + "/entry")){
// we are connected to the host!
http.addHeader("Content-Type", "application/json");
String json = "{\"row_id\":0 ,\"entryValue\":\"" + (String)getAvg(infos[i].values, infos[i].repetitions) + "\",\"date\": null }";
String json = "{\"value\":\"" + (String)getAvg(infos[i].values, infos[i].repetitions) + "\",\"timestamp\": \"" + getISOTime() +"\" }";
Serial.println(json);
int httpCode = http.POST(json); //Send the request
} else {
Expand Down Expand Up @@ -227,3 +243,15 @@ String decToHexa(int n)

return hex;
}

String getISOTime() {
time_t now;
struct tm timeinfo;
char buffer[25];

time(&now);
gmtime_r(&now, &timeinfo); // Convert to UTC time
strftime(buffer, sizeof(buffer), "%Y-%m-%dT%H:%M:%SZ", &timeinfo);

return String(buffer);
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ fun Entry.toDto() = EntryDto().also {

fun EntryDto.toEntity() = Entry().also {
it.value = this.value
it.timestamp = Timestamp.from(this.timestamp.toInstant())
it.timestamp = this.timestamp
}
4 changes: 2 additions & 2 deletions src/main/kotlin/de/lrprojects/tempserver/entity/Entry.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package de.lrprojects.tempserver.entity

import java.sql.Timestamp
import java.time.OffsetDateTime

data class Entry(
var timestamp: Timestamp? = null,
var timestamp: OffsetDateTime? = null,
var value: Double? = null,
)
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ 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
Expand Down Expand Up @@ -35,11 +36,12 @@ class EntryServiceImpl(

val queryApi = influxDBClient.queryApi
try {
log.info("Run getEntries query: $query")
val tables = queryApi.query(query)
return tables.flatMap { table ->
table.records.map {
Entry(
timestamp = it.time?.toEpochMilli()?.let { it1 -> Timestamp(it1) },
timestamp = OffsetDateTime.ofInstant(it.time, ZoneOffset.UTC),
value = it.value as Double,
)
}
Expand All @@ -53,12 +55,12 @@ class EntryServiceImpl(
val point = Point.measurement("entry")
.addTag("sensorId", sensorId)
.addField("value", entry.value)
.time(entry.timestamp?.toInstant(), WritePrecision.NS)
.time(entry.timestamp?.toInstant(), WritePrecision.S)

influxDBClient.writeApiBlocking.writePoint(point)
}

override fun deleteEntries(sensorId: String, date1: OffsetDateTime?, date2: OffsetDateTime?) {
override fun deleteEntries(sensorId: String, date1: OffsetDateTime?, date2: OffsetDateTime?) {
val startDate = date1?.toString()?: "0"
val stopDate = date2?.toString() ?: "now()"

Expand Down Expand Up @@ -93,6 +95,6 @@ class EntryServiceImpl(
|> filter(fn: (r) => r["sensorId"] == "$sensorId")
|> limit(n: $limit)
"""

private val log = LoggerFactory.getLogger(this::class.java)
}
}
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 @@ -112,7 +112,7 @@ function getDataCurrent() {
document.getElementById("current").innerHTML = "No Data available";
} else {
let unit = document.getElementById("current").getAttribute("data-unit");
document.getElementById("current").innerHTML = "Current Value: " + list[list.length - 1].entryValue + unit;
document.getElementById("current").innerHTML = "Current Value: " + list[list.length - 1].value + unit;
}
}
};
Expand Down

0 comments on commit 6802ca6

Please sign in to comment.