Aktueller Stand

This commit is contained in:
Tobias Peper 2024-07-07 17:50:48 +02:00
parent a45a14febc
commit e61d04cc5a
7 changed files with 112 additions and 6 deletions

Binary file not shown.

BIN
sml-neg.dat Normal file

Binary file not shown.

View File

@ -0,0 +1,10 @@
public class TestCalcMain {
public static void main(String[] args) {
short s = (short) 0xFF47;
System.out.println(s);
}
}

View File

@ -0,0 +1,70 @@
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 ShellyPlus1PmCloudReader implements MeterReader {
private final String authKey;
private final String id;
private final String cloudServerHostname;
private final OkHttpClient httpClient;
public ShellyPlus1PmCloudReader(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()) {
final Reader bodyReader = response.body().charStream();
final Gson gson = new Gson();
final JsonReader jsonReader = gson.newJsonReader(bodyReader);
jsonReader.beginObject();
int level = 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 ("switch:0".equals(name) && level == 2) {
jsonReader.beginObject();
level++;
} else if ("apower".equals(name) && level == 3) {
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.");
}
}

View File

@ -10,6 +10,12 @@ public class TestMeterReaderMain {
final MeterReader readerLocal = new ShellyPlus1PmLocalReader(propsLocal); final MeterReader readerLocal = new ShellyPlus1PmLocalReader(propsLocal);
System.out.println(readerLocal.getCurrentValue()); System.out.println(readerLocal.getCurrentValue());
final Properties propsCloud = new Properties();
propsCloud.setProperty("cloudServerHostname", "shelly-8-eu.shelly.cloud");
propsCloud.setProperty("authKey", "MmQ3ZHVpZA71BA656BE0DBDA1FF163B0F97EB1E902B18B3F3E45EFBCC78AC533A3BF52B91279C42553A06482D6");
propsCloud.setProperty("id", "cc7b5c85a870");
final MeterReader readerCloud = new ShellyPlus1PmCloudReader(propsCloud);
System.out.println(readerCloud.getCurrentValue());
} }
} }

View File

@ -2,18 +2,24 @@ package info.peper.serialtest;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.OutputStream;
public class DecodeSmlMain { public class DecodeSmlMain {
private static final int[] SEQ = new int[] {0x77, 0x07, 0x01, 0x00, 0x10, 0x07, 0x00, 0xff, 0x01, 0x01, 0x62, 0x1b, 0x52, 0x00}; private static final int[] SEQ = new int[] {0x77, 0x07, 0x01, 0x00, 0x10, 0x07, 0x00, 0xff, 0x01, 0x01, 0x62, 0x1b, 0x52, 0x00};
public static void decode(final InputStream is, final NewValueListener listener) throws IOException { public static void decode(final InputStream is, final NewValueListener listener, final OutputStream os) throws IOException {
int curPos = 0; int curPos = 0;
int readStatus = 0; int readStatus = 0;
int value = 0; int value = 0;
int readByte; int readByte;
boolean firstByte = false;
do { do {
readByte = is.read(); readByte = is.read();
if (os != null) {
os.write(readByte);
os.flush();
}
if (readByte != -1) { if (readByte != -1) {
if (readStatus == 0) { if (readStatus == 0) {
if (readByte == SEQ[curPos]) { if (readByte == SEQ[curPos]) {
@ -26,13 +32,21 @@ public class DecodeSmlMain {
readStatus = Integer.MAX_VALUE; readStatus = Integer.MAX_VALUE;
} }
} else if (readStatus == Integer.MAX_VALUE) { } else if (readStatus == Integer.MAX_VALUE) {
System.err.println("Anzahl byte: " + ((readByte & 0x0F) - 1));
if ((readByte & 0xF0) == 80) { if ((readByte & 0xF0) == 80) {
readStatus = readByte & 0x0F - 1; readStatus = (readByte & 0x0F) - 1;
} }
firstByte = true;
} else {
if (firstByte && (readByte & 0x80) == 0x80) {
value = -128 + (readByte & 0x7F);
} else { } else {
value *= 256; value *= 256;
value += readByte; value += readByte;
}
firstByte = false;
readStatus--; readStatus--;
System.err.println("gelesenes Byte: " + (byte)readByte + " / ReadStatus: " + readStatus);
if (readStatus == 0) { if (readStatus == 0) {
listener.newValue(value); listener.newValue(value);
value = 0; value = 0;

View File

@ -1,5 +1,6 @@
package info.peper.serialtest; package info.peper.serialtest;
import java.io.FileOutputStream;
import java.io.InputStream; import java.io.InputStream;
import com.fazecast.jSerialComm.SerialPort; import com.fazecast.jSerialComm.SerialPort;
@ -29,8 +30,13 @@ public class SerialTestMain {
System.out.println(value); System.out.println(value);
} }
}; };
try (InputStream is = serialPort.getInputStream();) { try (InputStream is = serialPort.getInputStream();
DecodeSmlMain.decode(is, listener); FileOutputStream fos = new FileOutputStream("sml.dat")) {
// for (int i = 0; i < 5000; i++) {
// fos.write(is.read());
// System.out.println(i);
// }
DecodeSmlMain.decode(is, listener, fos);
} }
serialPort.closePort(); serialPort.closePort();
} }