This commit is contained in:
LORENZO\pacio 2024-10-23 14:24:59 +02:00
parent 1a5f2f06cb
commit 9020c1d58f
12 changed files with 344 additions and 0 deletions

39
.classpath Normal file
View File

@ -0,0 +1,39 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" output="target/classes" path="src/main/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
<attributes>
<attribute name="test" value="true"/>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources">
<attributes>
<attribute name="test" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8">
<attributes>
<attribute name="module" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="target/classes"/>
</classpath>

23
.project Normal file
View File

@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>impercar</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.m2e.core.maven2Builder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
<nature>org.eclipse.m2e.core.maven2Nature</nature>
</natures>
</projectDescription>

30
pom.xml Normal file
View File

@ -0,0 +1,30 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>pi</groupId>
<artifactId>impercar</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<!-- https://mvnrepository.com/artifact/com.squareup.okhttp3/okhttp -->
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>4.9.3</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.google.code.gson/gson -->
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.6</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.24</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,137 @@
package impercar;
import com.google.gson.Gson;
import impercar.classi.Device;
import impercar.classi.DeviceLog;
import impercar.classi.Plant;
import impercar.classi.ValueLast;
import impercar.classi.ValueLastRoot;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
public class ImpercarApiClient {
private String _token, _un, _pw, _apiUrl;
private Gson _gson = new Gson();
public ImpercarApiClient(String un, String pw, String apiUrl) {
this._un = un.trim();
this._pw = pw.trim();
this._apiUrl = apiUrl;
}
public Boolean getToken() throws Exception {
Boolean res = true;
this._token = "";
OkHttpClient client = new OkHttpClient().newBuilder().build();
MediaType mediaType = MediaType.parse("text/plain");
RequestBody body = RequestBody.create(mediaType, "{\"username\":\"" + _un + "\",\"password\":\"" + _pw + "\"}");
Request request = new Request.Builder().url(_apiUrl + "authenticate").method("POST", body)
.addHeader("Content-Type", "text/plain").build();
Response response = null;
try {
response = client.newCall(request).execute();
if (response.code() != 200) {
throw new Exception("response non 200");
}
this._token = _gson.fromJson(response.body().string(), Token.class).token;
response.body().close();
} catch (Exception e) {
throw new Exception("errore response " + e.getMessage());
} finally {
if (response != null) {
response.close();
}
}
System.out.println("getToken " + res);
return res;
}
public Plant[] getImpianti() throws Exception {
Plant[] ret = null;
OkHttpClient client = new OkHttpClient().newBuilder().build();
MediaType mediaType = MediaType.parse("text/plain");
Request request = new Request.Builder().url(_apiUrl + "plants").method("GET", null)
.addHeader("authorization", _token).build();
Response response = client.newCall(request).execute();
if (response.code() != 200) {
throw new Exception("errore response");
}
Plant[] plants = _gson.fromJson(response.body().string(), Plant[].class);
ret = plants;
return ret;
}
public Device[] getDevices(String plantId) throws Exception {
Device[] ret = null;
String url = _apiUrl + "plants/%s/devices";
url = String.format(url, plantId);
OkHttpClient client = new OkHttpClient().newBuilder().build();
MediaType mediaType = MediaType.parse("text/plain");
Request request = new Request.Builder().url(url).method("GET", null).addHeader("authorization", _token).build();
Response response = client.newCall(request).execute();
ret = _gson.fromJson(response.body().string(), Device[].class);
return ret;
}
public DeviceLog[] getDeviceLogs(String plantId, String devId) throws Exception {
DeviceLog[] ret = null;
String url = _apiUrl + "plants/%s/devices/%s/logs";
url = String.format(url, plantId, devId);
OkHttpClient client = new OkHttpClient().newBuilder().build();
MediaType mediaType = MediaType.parse("text/plain");
Request request = new Request.Builder().url(url).method("GET", null).addHeader("authorization", _token).build();
Response response = client.newCall(request).execute();
ret = _gson.fromJson(response.body().string(), DeviceLog[].class);
return ret;
}
public ValueLast[] getValueLast(String plantId, String devId, String logId)
throws Exception {
String url = _apiUrl + "getLastValue/%s/%s/%s/";
url = String.format(url, plantId, devId, logId);
OkHttpClient client = new OkHttpClient().newBuilder().build();
MediaType mediaType = MediaType.parse("text/plain");
Request request = new Request.Builder().url(url).method("GET", null).addHeader("authorization", _token).build();
Response response = client.newCall(request).execute();
ValueLastRoot ret1 = _gson.fromJson(response.body().string(), ValueLastRoot.class);
ValueLast[] ret = new ValueLast[ret1.items.size()];
ret = ret1.items.toArray(ret);
return ret;
}
}

View File

@ -0,0 +1,6 @@
package impercar;
public class Token {
public String token;
}

View File

@ -0,0 +1,24 @@
package impercar.classi;
import lombok.Getter;
public class Device {
@Getter
public String name;
@Getter
public String id;
@Getter
public String description;
@Getter
public String version;
@Getter
public String hwType;
@Getter
public int connectionStatus;
@Getter
public int powerStatus;
@Getter
public String ip;
@Getter
public int port;
}

View File

@ -0,0 +1,15 @@
package impercar.classi;
import lombok.Getter;
public class DeviceLog {
@Getter
public int id;
@Getter
public String name;
@Getter
public int samplingTime;
@Getter
public String tag;
}

View File

@ -0,0 +1,8 @@
package impercar.classi;
public class Log {
public String name;
public int id;
}

View File

@ -0,0 +1,10 @@
package impercar.classi;
import lombok.Getter;
public class Plant {
@Getter
public int id;
@Getter
public String name,description,address,note,timezone,lat,lng;
}

View File

@ -0,0 +1,16 @@
package impercar.classi;
import lombok.Getter;
public class ValueLast {
@Getter
public String name;
@Getter
public int id;
@Getter
public String unit;
@Getter
public String value;
@Getter
public int utc;
}

View File

@ -0,0 +1,9 @@
package impercar.classi;
import java.util.ArrayList;
public class ValueLastRoot {
public Log log;
public ArrayList<ValueLast> items;
}

View File

@ -0,0 +1,27 @@
package impercar;
public class test {
public static void main(String[] args) {
// TODO Auto-generated method stub
ImpercarApiClient a = new ImpercarApiClient("Impercar2022", "Impercar2009?","http://remote.electroelsa.com/api/v1/");
try {
a.getToken();
a.getImpianti();
a.getDevices("368");
a.getDeviceLogs("368", "3394VCQGI38D");
a.getValueLast("368", "3394VCQGI38D", "2001644");
}catch (Exception e) {
System.out.println(e.getMessage());
}
}
}