diff --git a/.classpath b/.classpath new file mode 100644 index 0000000..5869b87 --- /dev/null +++ b/.classpath @@ -0,0 +1,39 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/.project b/.project new file mode 100644 index 0000000..5cbe537 --- /dev/null +++ b/.project @@ -0,0 +1,23 @@ + + + impercar + + + + + + org.eclipse.jdt.core.javabuilder + + + + + org.eclipse.m2e.core.maven2Builder + + + + + + org.eclipse.jdt.core.javanature + org.eclipse.m2e.core.maven2Nature + + diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..1743477 --- /dev/null +++ b/pom.xml @@ -0,0 +1,30 @@ + + 4.0.0 + pi + impercar + 0.0.1-SNAPSHOT + + + + + com.squareup.okhttp3 + okhttp + 4.9.3 + + + + + com.google.code.gson + gson + 2.8.6 + + + + org.projectlombok + lombok + 1.18.24 + provided + + + + \ No newline at end of file diff --git a/src/main/java/impercar/ImpercarApiClient.java b/src/main/java/impercar/ImpercarApiClient.java new file mode 100644 index 0000000..2305c3a --- /dev/null +++ b/src/main/java/impercar/ImpercarApiClient.java @@ -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; + } +} diff --git a/src/main/java/impercar/Token.java b/src/main/java/impercar/Token.java new file mode 100644 index 0000000..bd1c2ac --- /dev/null +++ b/src/main/java/impercar/Token.java @@ -0,0 +1,6 @@ +package impercar; + +public class Token { + + public String token; +} diff --git a/src/main/java/impercar/classi/Device.java b/src/main/java/impercar/classi/Device.java new file mode 100644 index 0000000..8cec95a --- /dev/null +++ b/src/main/java/impercar/classi/Device.java @@ -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; +} diff --git a/src/main/java/impercar/classi/DeviceLog.java b/src/main/java/impercar/classi/DeviceLog.java new file mode 100644 index 0000000..564474e --- /dev/null +++ b/src/main/java/impercar/classi/DeviceLog.java @@ -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; +} diff --git a/src/main/java/impercar/classi/Log.java b/src/main/java/impercar/classi/Log.java new file mode 100644 index 0000000..ba69378 --- /dev/null +++ b/src/main/java/impercar/classi/Log.java @@ -0,0 +1,8 @@ +package impercar.classi; + +public class Log { + + public String name; + public int id; + +} diff --git a/src/main/java/impercar/classi/Plant.java b/src/main/java/impercar/classi/Plant.java new file mode 100644 index 0000000..a520bff --- /dev/null +++ b/src/main/java/impercar/classi/Plant.java @@ -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; +} diff --git a/src/main/java/impercar/classi/ValueLast.java b/src/main/java/impercar/classi/ValueLast.java new file mode 100644 index 0000000..018aaaa --- /dev/null +++ b/src/main/java/impercar/classi/ValueLast.java @@ -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; +} diff --git a/src/main/java/impercar/classi/ValueLastRoot.java b/src/main/java/impercar/classi/ValueLastRoot.java new file mode 100644 index 0000000..4da6158 --- /dev/null +++ b/src/main/java/impercar/classi/ValueLastRoot.java @@ -0,0 +1,9 @@ +package impercar.classi; + +import java.util.ArrayList; + +public class ValueLastRoot { + + public Log log; + public ArrayList items; +} diff --git a/src/main/java/impercar/test.java b/src/main/java/impercar/test.java new file mode 100644 index 0000000..0053b5c --- /dev/null +++ b/src/main/java/impercar/test.java @@ -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()); + } + } + +}