Änderungen eingepflegt
This commit is contained in:
parent
286e025067
commit
a45a14febc
@ -0,0 +1,23 @@
|
|||||||
|
package info.peper.metertoinflux;
|
||||||
|
|
||||||
|
public class MeterReadException extends Exception {
|
||||||
|
|
||||||
|
private static final long serialVersionUID = -1413981604296334092L;
|
||||||
|
|
||||||
|
public MeterReadException() {
|
||||||
|
super();
|
||||||
|
}
|
||||||
|
|
||||||
|
public MeterReadException(String message, Throwable cause) {
|
||||||
|
super(message, cause);
|
||||||
|
}
|
||||||
|
|
||||||
|
public MeterReadException(String message) {
|
||||||
|
super(message);
|
||||||
|
}
|
||||||
|
|
||||||
|
public MeterReadException(Throwable cause) {
|
||||||
|
super(cause);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
7
src/main/java/info/peper/metertoinflux/MeterReader.java
Normal file
7
src/main/java/info/peper/metertoinflux/MeterReader.java
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
package info.peper.metertoinflux;
|
||||||
|
|
||||||
|
import java.util.Properties;
|
||||||
|
|
||||||
|
public interface MeterReader {
|
||||||
|
double getCurrentValue() throws MeterReadException;
|
||||||
|
}
|
||||||
@ -0,0 +1,86 @@
|
|||||||
|
package info.peper.metertoinflux;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.Reader;
|
||||||
|
import java.util.Properties;
|
||||||
|
|
||||||
|
import com.google.gson.Gson;
|
||||||
|
import com.google.gson.stream.JsonReader;
|
||||||
|
|
||||||
|
import okhttp3.FormBody;
|
||||||
|
import okhttp3.OkHttpClient;
|
||||||
|
import okhttp3.Request;
|
||||||
|
import okhttp3.RequestBody;
|
||||||
|
import okhttp3.Response;
|
||||||
|
|
||||||
|
public class Shelly3EmCloudReader implements MeterReader {
|
||||||
|
|
||||||
|
private final String authKey;
|
||||||
|
private final String id;
|
||||||
|
private final String cloudServerHostname;
|
||||||
|
private final OkHttpClient httpClient;
|
||||||
|
|
||||||
|
public Shelly3EmCloudReader(final Properties config) {
|
||||||
|
this.authKey = config.getProperty("authKey");
|
||||||
|
this.id = config.getProperty("id");
|
||||||
|
this.cloudServerHostname = config.getProperty("cloudServerHostname");
|
||||||
|
this.httpClient = new OkHttpClient();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public double getCurrentValue() throws MeterReadException {
|
||||||
|
try {
|
||||||
|
final RequestBody requestBody = new FormBody.Builder()
|
||||||
|
.add("auth_key", this.authKey)
|
||||||
|
.add("id", this.id)
|
||||||
|
.build();
|
||||||
|
final Request request = new Request.Builder().url("https://" + this.cloudServerHostname + "/device/status")
|
||||||
|
.post(requestBody)
|
||||||
|
.build();
|
||||||
|
final Response response = httpClient.newCall(request).execute();
|
||||||
|
if (response.isSuccessful()) {
|
||||||
|
double sum = 0.0;
|
||||||
|
final Reader bodyReader = response.body().charStream();
|
||||||
|
final Gson gson = new Gson();
|
||||||
|
final JsonReader jsonReader = gson.newJsonReader(bodyReader);
|
||||||
|
jsonReader.beginObject();
|
||||||
|
int level = 0;
|
||||||
|
int phase = 0;
|
||||||
|
while (jsonReader.hasNext()) {
|
||||||
|
final String name = jsonReader.nextName();
|
||||||
|
if ("data".equals(name) && level == 0) {
|
||||||
|
jsonReader.beginObject();
|
||||||
|
level++;
|
||||||
|
} else if ("device_status".equals(name) && level == 1) {
|
||||||
|
jsonReader.beginObject();
|
||||||
|
level++;
|
||||||
|
} else if ("emeters".equals(name) && level == 2) {
|
||||||
|
jsonReader.beginArray();
|
||||||
|
jsonReader.beginObject();
|
||||||
|
phase = 1;
|
||||||
|
level++;
|
||||||
|
} else if ("power".equals(name) && level == 3) {
|
||||||
|
sum += jsonReader.nextDouble();
|
||||||
|
while (jsonReader.hasNext()) {
|
||||||
|
jsonReader.nextName();
|
||||||
|
jsonReader.skipValue();
|
||||||
|
}
|
||||||
|
jsonReader.endObject();
|
||||||
|
if (jsonReader.hasNext()) {
|
||||||
|
phase++;
|
||||||
|
jsonReader.beginObject();
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
jsonReader.skipValue();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (phase > 0) {
|
||||||
|
return sum;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new MeterReadException("Could not get value from meter.", e);
|
||||||
|
}
|
||||||
|
throw new MeterReadException("Could not get value from meter.");
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,48 @@
|
|||||||
|
package info.peper.metertoinflux;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.Reader;
|
||||||
|
import java.util.Properties;
|
||||||
|
|
||||||
|
import com.google.gson.Gson;
|
||||||
|
import com.google.gson.stream.JsonReader;
|
||||||
|
|
||||||
|
import okhttp3.OkHttpClient;
|
||||||
|
import okhttp3.Request;
|
||||||
|
import okhttp3.Response;
|
||||||
|
|
||||||
|
public class ShellyPlus1PmLocalReader implements MeterReader {
|
||||||
|
|
||||||
|
private final String hostname;
|
||||||
|
private final OkHttpClient httpClient;
|
||||||
|
|
||||||
|
public ShellyPlus1PmLocalReader(final Properties config) {
|
||||||
|
this.hostname = config.getProperty("hostname");
|
||||||
|
this.httpClient = new OkHttpClient();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public double getCurrentValue() throws MeterReadException {
|
||||||
|
try {
|
||||||
|
final Request request = new Request.Builder().url("http://" + hostname + "/rpc/Switch.GetStatus?id=0").build();
|
||||||
|
final Response response = httpClient.newCall(request).execute();
|
||||||
|
if (response.isSuccessful()) {
|
||||||
|
final Reader bodyReader = response.body().charStream();
|
||||||
|
final Gson gson = new Gson();
|
||||||
|
final JsonReader jsonReader = gson.newJsonReader(bodyReader);
|
||||||
|
jsonReader.beginObject();
|
||||||
|
while (jsonReader.hasNext()) {
|
||||||
|
final String name = jsonReader.nextName();
|
||||||
|
if ("apower".equals(name)) {
|
||||||
|
return jsonReader.nextDouble();
|
||||||
|
} else {
|
||||||
|
jsonReader.skipValue();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new MeterReadException("Could not get value from meter.", e);
|
||||||
|
}
|
||||||
|
throw new MeterReadException("Could not get value from meter.");
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,15 @@
|
|||||||
|
package info.peper.metertoinflux;
|
||||||
|
|
||||||
|
import java.util.Properties;
|
||||||
|
|
||||||
|
public class TestMeterReaderMain {
|
||||||
|
|
||||||
|
public static void main(String[] args) throws Exception {
|
||||||
|
final Properties propsLocal = new Properties();
|
||||||
|
propsLocal.setProperty("hostname", "photovoltaik");
|
||||||
|
final MeterReader readerLocal = new ShellyPlus1PmLocalReader(propsLocal);
|
||||||
|
System.out.println(readerLocal.getCurrentValue());
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user