Skip to content

Commit

Permalink
Init wrapper development
Browse files Browse the repository at this point in the history
  • Loading branch information
Giorgio Bertolotti committed Apr 8, 2019
1 parent 7f6d181 commit 64f6438
Show file tree
Hide file tree
Showing 10 changed files with 222 additions and 0 deletions.
20 changes: 20 additions & 0 deletions TadoAPI/.classpath
@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" output="target/classes" path="src">
<attributes>
<attribute name="optional" 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="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 changes: 23 additions & 0 deletions TadoAPI/.project
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>TadoAPI</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.m2e.core.maven2Nature</nature>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
5 changes: 5 additions & 0 deletions TadoAPI/.settings/org.eclipse.jdt.core.prefs
@@ -0,0 +1,5 @@
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
org.eclipse.jdt.core.compiler.compliance=1.8
org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
org.eclipse.jdt.core.compiler.source=1.8
4 changes: 4 additions & 0 deletions TadoAPI/.settings/org.eclipse.m2e.core.prefs
@@ -0,0 +1,4 @@
activeProfiles=
eclipse.preferences.version=1
resolveWorkspaceProjects=true
version=1
Binary file added TadoAPI/bin/TadoConnector.class
Binary file not shown.
35 changes: 35 additions & 0 deletions TadoAPI/pom.xml
@@ -0,0 +1,35 @@
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>TadoAPI</groupId>
<artifactId>TadoAPI</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>TadoAPI</name>
<description>Java wrapper for Tado APIs</description>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>3.14.0</version>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20180813</version>
</dependency>
</dependencies>
</project>
134 changes: 134 additions & 0 deletions TadoAPI/src/TadoConnector.java
@@ -0,0 +1,134 @@
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.json.JSONObject;

import okhttp3.FormBody;
import okhttp3.FormBody.Builder;
import okhttp3.Headers;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class TadoConnector {
private String username;
private String password;
private String clientSecret;
private String bearer;
private String refreshToken;
private int homeID;
private int zone;
private OkHttpClient client;
public static final MediaType FORM = MediaType.parse("multipart/form-data");

public TadoConnector(String username, String password) {
this.username = username;
this.password = password;
client = new OkHttpClient();
this.clientSecret = getClientSecret();
getBearerTokens();
this.homeID = getHomeID();
}

public String getUsername() {
return username;
}

public void setUsername(String username) {
this.username = username;
}

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}

public int getHome() {
return homeID;
}

public void setHome(int home) {
this.homeID = home;
}

public int getZone() {
return zone;
}

public void setZone(int zone) {
this.zone = zone;
}

private String getClientSecret() {
try {
String jsonResponse = doGetRequest("https://my.tado.com/webapp/env.js", null);
JSONObject json = new JSONObject(jsonResponse);
return json.getJSONObject("config").getJSONObject("oauth").getString("clientSecret");
} catch (IOException e) {
e.printStackTrace();
return null;
}
}

private void getBearerTokens() {
Map<String, String> body = new HashMap<>();
body.put("client_id", "tado-web-app");
body.put("grant_type", "password");
body.put("scope", "home.user");
body.put("username", this.username);
body.put("password", this.password);
body.put("client_secret", this.clientSecret);
try {
String response = doPostRequest("https://auth.tado.com/oauth/token", body, null);
JSONObject json = new JSONObject(response);
this.bearer = json.getString("access_token");
this.refreshToken = json.getString("refresh_token");
} catch (IOException e) {
e.printStackTrace();
}
}

private Integer getHomeID() {
try {
Map<String, String> headers = new HashMap<>();
headers.put("Authorization", "Bearer " + this.bearer);
String jsonResponse = doGetRequest("https://my.tado.com/api/v2/me", headers);
JSONObject json = new JSONObject(jsonResponse);
return json.getInt("homeId");
} catch (IOException e) {
e.printStackTrace();
return null;
}
}

private String doGetRequest(String url, Map<String, String> headers) throws IOException {
Request request;
if (headers != null)
request = new Request.Builder().url(url).headers(Headers.of(headers)).build();
else
request = new Request.Builder().url(url).build();
Response response = client.newCall(request).execute();
return response.body().string();
}

private String doPostRequest(String url, Map<String, String> body, Map<String, String> headers) throws IOException {
Builder builder = new FormBody.Builder();
for (Map.Entry<String, String> entry : body.entrySet()) {
builder.add(entry.getKey(), entry.getValue());
}
RequestBody formBody = builder.build();
Request request;
if (headers != null)
request = new Request.Builder().url(url).post(formBody).headers(Headers.of(headers)).build();
else
request = new Request.Builder().url(url).post(formBody).build();
Response response = client.newCall(request).execute();
return response.body().string();
}
}
Binary file added TadoAPI/target/classes/TadoConnector.class
Binary file not shown.
Empty file.
@@ -0,0 +1 @@
D:\Documenti\Sviluppo\TadoAPI\TadoAPI\src\TadoConnector.java

0 comments on commit 64f6438

Please sign in to comment.