/old/src/main/java/ch/weirich/solview/Client.java

https://gitlab.com/rgwch/solview · Java · 104 lines · 88 code · 10 blank · 6 comment · 4 complexity · 0fcfa173ee86a1bb1f40c9808c83673a MD5 · raw file

  1. package ch.weirich.solview;
  2. import ch.rgw.tools.TimeTool;
  3. import com.mongodb.client.MongoCollection;
  4. import com.mongodb.client.model.FindOneAndReplaceOptions;
  5. import io.vertx.core.AbstractVerticle;
  6. import io.vertx.core.http.HttpClient;
  7. import io.vertx.core.http.HttpClientOptions;
  8. import io.vertx.core.json.JsonObject;
  9. import org.bson.Document;
  10. /**
  11. * Created by gerry on 26.10.15.
  12. *
  13. */
  14. public class Client extends AbstractVerticle {
  15. static final String API = "/solar_api/v1/";
  16. HttpClient client;
  17. @Override
  18. public void start() {
  19. HttpClientOptions options = new HttpClientOptions().setDefaultHost(Runner.cfg.getString("datalogger"));
  20. Runner.vertx.eventBus().consumer("client.catchup", msg ->{
  21. JsonObject body= (JsonObject) msg.body();
  22. TimeTool tt=new TimeTool(body.getString("date"));
  23. TimeTool today=new TimeTool();
  24. today.chop(2);
  25. while(tt.isBefore(today)){
  26. loadStringDCPower(tt.toString(TimeTool.DATE_ISO));
  27. tt.addDays(1);
  28. }
  29. });
  30. client = Runner.vertx.createHttpClient(options);
  31. Runner.vertx.setPeriodic(3600000L, time -> {
  32. // Runner.vertx.setTimer(100L,time ->{
  33. TimeTool tt = new TimeTool();
  34. Runner.log.info("loading data for "+tt.toString(TimeTool.DATE_GER));
  35. loadStringDCPower(tt.toString(TimeTool.DATE_ISO));
  36. loadInverterRealtimeData();
  37. });
  38. }
  39. void loadStringDCPower(String date) {
  40. StringBuilder sb = new StringBuilder(API);
  41. sb.append("GetArchiveData.cgi?Scope=System&StartDate=").append(date)
  42. .append("&EndDate=").append(date)
  43. .append("&Channel=Current_DC_String_1&Channel=Current_DC_String_2&Channel=Voltage_DC_String_1")
  44. .append("&Channel=Voltage_DC_String_2&Channel=EnergyReal_WAC_Sum_Produced&Channel=Temperature_Powerstage");
  45. loadAndStore(sb.toString(), "archive", "DC_Power", date);
  46. Runner.log.info("loadStringDCPower");
  47. }
  48. void loadStringDCVolt(String date){
  49. }
  50. void loadMeterData(String date){
  51. StringBuilder sb=new StringBuilder(API);
  52. sb.append("GetMeterRealtimeData.cgi?Scope=System");
  53. loadAndStore(sb.toString(),"powerdata","RealWAC",date);
  54. Runner.log.info("loadMeterData");
  55. }
  56. void loadInverterRealtimeData() {
  57. StringBuilder sb = new StringBuilder(API).append("GetInverterRealtimeData.cgi?Scope=System");
  58. loadAndStore(sb.toString(), "realtime", "inverterSummary", new TimeTool().toString(TimeTool.DATE_ISO));
  59. sb = new StringBuilder(API).append("GetInverterRealtimeData.cgi?Scope=Device&DeviceId=1&DataCollection=MinMaxInverterData");
  60. loadAndStore(sb.toString(), "realtime", "inverterExt", new TimeTool().toString(TimeTool.DATE_ISO));
  61. Runner.log.info("loadInverterRealtimeData ");
  62. }
  63. void loadAndStore(String request, final String collection, final String datatype, final String querydate) {
  64. client.getNow(request, response -> {
  65. System.out.print(response.statusCode() + ", " + response.statusMessage());
  66. if (response.statusCode() == 200) {
  67. response.bodyHandler(buffer -> {
  68. try {
  69. String jss = buffer.toString();
  70. MongoCollection coll = Runner.db.getCollection(collection);
  71. // JsonObject jos=new JsonObject(jss);
  72. Document bss = Document.parse(jss);
  73. Document head = (Document) bss.get("Head");
  74. TimeTool ts = new TimeTool(querydate);
  75. bss.put("date", ts.toString(TimeTool.DATE_COMPACT));
  76. bss.put("type", datatype);
  77. FindOneAndReplaceOptions frop = new FindOneAndReplaceOptions().upsert(true);
  78. Object result=coll.findOneAndReplace(new Document().append("date", ts.toString(TimeTool.DATE_COMPACT)).append("type", datatype), bss, frop);
  79. if(result==null){
  80. Runner.log.info("inserted (Document not found) "+bss.toString());
  81. }else {
  82. Runner.log.info("updated " + datatype+" "+bss.toString());
  83. }
  84. }catch(Throwable ex){
  85. Runner.log.severe(ex.getMessage());
  86. ex.printStackTrace();
  87. }
  88. });
  89. } else {
  90. Runner.log.severe(response.statusCode() + ", " + response.statusMessage() + ": " + Runner.cfg.getString("datalogger") + request);
  91. }
  92. });
  93. }
  94. }