Skip to content
This repository has been archived by the owner on Mar 11, 2024. It is now read-only.

Commit

Permalink
test cases added.
Browse files Browse the repository at this point in the history
  • Loading branch information
ekkelenkamp committed Apr 26, 2015
1 parent b83dc3f commit 108493c
Show file tree
Hide file tree
Showing 10 changed files with 309 additions and 194 deletions.
13 changes: 13 additions & 0 deletions .idea/libraries/Maven__org_mockito_mockito_all_1_10_19.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions netatmo2wow.iml
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,6 @@
<orderEntry type="library" name="Maven: junit:junit:4.10" level="project" />
<orderEntry type="library" name="Maven: org.hamcrest:hamcrest-core:1.1" level="project" />
<orderEntry type="library" name="Maven: commons-cli:commons-cli:1.2" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.mockito:mockito-all:1.10.19" level="project" />
</component>
</module>
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@
<artifactId>commons-cli</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>1.10.19</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>

Expand Down
17 changes: 6 additions & 11 deletions src/main/java/com/ekkelenkamp/netatmo2wow/Cli.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,22 @@

public class Cli {

final static org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger(NetatmoHttpClient.class);
final static org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger(NetatmoHttpClientImpl.class);

private static final Logger log = Logger.getLogger(Cli.class.getName());
private String[] args = null;
private Options options = new Options();
CommandLineParser parser = new BasicParser();
CommandLine cmd = null;
private CommandLineParser parser = new BasicParser();
private CommandLine cmd = null;
private NetatmoHttpClient netatmoHttpClient = new NetatmoHttpClientImpl();

public Cli(String[] args) {

this.args = args;

Option option = new Option("h", "help", false, "show help.");
option.setRequired(false);
options.addOption(option);


OptionGroup group = new OptionGroup();
option = new Option("c", "clientid", true, "Client id of netatmo application. See: https://dev.netatmo.com/dev/createapp");
option.setRequired(false);
Expand Down Expand Up @@ -61,14 +61,9 @@ public Cli(String[] args) {

public void parse() {



try {
cmd = parser.parse(options, args);

if (cmd.hasOption("h")) help();


run();

} catch (ParseException e) {
Expand All @@ -85,7 +80,7 @@ private void help() {
}

private void run() {
NetatmoDownload download = new NetatmoDownload();
NetatmoDownload download = new NetatmoDownload(netatmoHttpClient);
WowUpload upload = new WowUpload();
try {
List<Measures> measures = download.downloadMeasures(cmd.getOptionValue("e"), cmd.getOptionValue("p"), cmd.getOptionValue("c"), cmd.getOptionValue("s"), cmd.getOptionValue("t"));
Expand Down
63 changes: 21 additions & 42 deletions src/main/java/com/ekkelenkamp/netatmo2wow/NetatmoDownload.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,15 @@
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.util.*;

public class NetatmoDownload {

private NetatmoHttpClient netatmoHttpClient;

final static Logger logger = Logger.getLogger(NetatmoDownload.class);

// API URLs that will be used for requests, see: http://dev.netatmo.com/doc/restapi.
Expand All @@ -25,17 +23,19 @@ public class NetatmoDownload {
protected final String URL_GET_DEVICES_LIST = URL_BASE + "/api/devicelist";
protected final String URL_GET_MEASURES_LIST = URL_BASE + "/api/getmeasure";

public NetatmoDownload(NetatmoHttpClient netatmoHttpClient) {
this.netatmoHttpClient = netatmoHttpClient;
}

public List<Measures> downloadMeasures(String username, String password, String clientId, String clientSecret, String timespan) throws IOException {
String url = URL_REQUEST_TOKEN;
String token = login(username, password, clientId, clientSecret);
logger.debug("Token: " + token);
String measureTypes = "Temperature,Humidity";
String measureTypes = "Temperature,Humidity,Rain";
Device device = getDevices(token);
List<Measures> measures = new ArrayList<Measures>();
Map<String, List<String>> devices = device.getDevices();
for (String dev : devices.keySet()) {

List<String> modules = devices.get(dev);
for (String module : modules) {
logger.debug("Device: " + device);
Expand All @@ -46,12 +46,10 @@ public List<Measures> downloadMeasures(String username, String password, String
long currentDate = ((new java.util.Date().getTime()) / 1000) - timePeriod;
logger.debug("start time: " + new Date(currentDate * 1000));
logger.debug("start time seconds: " + currentDate);

measures.addAll(getMeasures(token, dev, module, measureTypes, scale, currentDate));
}
}
return measures;

}

public List<Measures> getMeasures(String token, String device, String module, String measureTypes, String scale, long dateBegin) {
Expand All @@ -67,7 +65,7 @@ public List<Measures> getMeasures(String token, String device, String module, St
List<Measures> measuresList = new ArrayList<Measures>();
try {
JSONParser parser = new JSONParser();
String result = NetatmoHttpClient.post(new URL(URL_GET_MEASURES_LIST), params);
String result = netatmoHttpClient.post(new URL(URL_GET_MEASURES_LIST), params);
Object obj = parser.parse(result);
JSONObject jsonResult = (JSONObject) obj;
if (!(jsonResult.get("body") instanceof JSONObject)) {
Expand All @@ -82,18 +80,20 @@ public List<Measures> getMeasures(String token, String device, String module, St
Measures measures = new Measures();
measures.setTimestamp(Long.parseLong(timeStamp) * 1000);
measures.setTemperature(Double.parseDouble("" + valuesArray.get(0)));
measures.setHumidity(Double.parseDouble("" + valuesArray.get(1)));
if (valuesArray.size() > 1 && valuesArray.get(1) != null) {
measures.setHumidity(Double.parseDouble("" + valuesArray.get(1)));
}
if (valuesArray.size() > 2 && valuesArray.get(2) != null) {
measures.setRain(Double.parseDouble("" + valuesArray.get(2)));
}
if (valuesArray.size() > 3 && valuesArray.get(3) != null) {
measures.setWind(Double.parseDouble("" + valuesArray.get(3)));
}
measuresList.add(measures);
}
Collections.sort(measuresList);
return measuresList;
} catch (IOException e) {
throw new RuntimeException(e);
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e);
} catch (KeyManagementException e) {
throw new RuntimeException(e);
} catch (ParseException e) {
} catch (Exception e) {
throw new RuntimeException(e);
}
}
Expand All @@ -105,7 +105,7 @@ public Device getDevices(String token) {
List<String> devicesList = new ArrayList<String>();
try {
JSONParser parser = new JSONParser();
String result = NetatmoHttpClient.post(new URL(URL_GET_DEVICES_LIST), params);
String result = netatmoHttpClient.post(new URL(URL_GET_DEVICES_LIST), params);
Object obj = parser.parse(result);
JSONObject jsonResult = (JSONObject) obj;
JSONObject body = (JSONObject) jsonResult.get("body");
Expand All @@ -115,21 +115,12 @@ public Device getDevices(String token) {
String moduleId = (String) module.get("_id");
String deviceId = (String) module.get("main_device");
device.addModuleToDevice(deviceId, moduleId);

}


return device;
} catch (IOException e) {
throw new RuntimeException(e);
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e);
} catch (KeyManagementException e) {
throw new RuntimeException(e);
} catch (ParseException e) {
} catch (Exception e) {
throw new RuntimeException(e);
}

}


Expand All @@ -145,27 +136,15 @@ public String login(String email, String password, String clientId, String clien
params.put("client_secret", clientSecret);
params.put("username", email);
params.put("password", password);

try {
JSONParser parser = new JSONParser();

String result = NetatmoHttpClient.post(new URL(URL_REQUEST_TOKEN), params);
String result = netatmoHttpClient.post(new URL(URL_REQUEST_TOKEN), params);
Object obj = parser.parse(result);
JSONObject jsonResult = (JSONObject) obj;
String token = (String) jsonResult.get("access_token");
return token;
} catch (ParseException e) {
throw new RuntimeException(e);
} catch (KeyManagementException e) {
throw new RuntimeException(e);
} catch (MalformedURLException e) {
throw new RuntimeException(e);
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e);
} catch (IOException e) {
} catch (Exception e) {
throw new RuntimeException(e);
}

}

}
Loading

0 comments on commit 108493c

Please sign in to comment.