NEWS
ecoflow PowerOcean Script - Hilfe nötig
-
Hallo,
ich habe eine PV-Anlage mit Komponenten von ecoflow PowerOcean (WR, Speicher und Wallbox). Diese würde ich gerne abfragen und zwar alle 1-2 Sekunden um den Energiefluss anzeigen zu lassen. Ich habe das Java-Beispiel zum laufen bekommen und kann Werte abfragen. Allerdings tue ich mich mit dem Javascript sehr schwer. Kenne mich mit HTTP Request leider gar nicht aus
Eine Verbindung kann ich herstellen, allerdings bekomme ich keine Werte.
Hatte es zuerste mit Blockly versucht, aber da scheitere ich schon an der Parameter-Übergabe.
Ich denke mein Primärproblem liegt beim Aufbau des korrekten Headers. Der Body ist das JSON-Objekt.Hier mal mein Script, wobei ich mir viel zusammengeschustert habe
var json_String, json_Objekt; json_String = '"{\\"sn\\":\\"SN-PowerOcean\\",\\"params\\":{\\"quotas\\":[\\"mpptHeartBeat\\"]}}"'; json_Objekt = (function () { try { return JSON.parse(json_String); } catch (e) { return {}; }})(); var myacceskey = "U-Name"; var mysecretkey = "P-Word"; const nonce = "640273"; const timestamp = Date.now(); const param = "params.quotas[0]=mpptHeartBeat&sn=SN-PowerOcean&accessKey=U-Name"; const data = param + "&nonce" + nonce + "×tamp" + timestamp; const CryptoJS = require('crypto'); const signTerm = CryptoJS.createHmac('sha256', mysecretkey).update(Buffer.from(data, 'utf-8')).digest(); const sign = signTerm.toString("base64"); // @ts-ignore httpPost( 'https://api.ecoflow.com/iot-open/sign/device/quota', json_Objekt, { timeout: 2000, basicAuth: { user: 'U-Name', password: 'P-Word', }, headers: { 'Authorization': sign }, }, (error, response) => { if (!error) { console.log(response); console.log("DATA: " + response.data); console.log("HEADERS: " + response.headers); } else { console.error(error); } } );
Und hier die Log-Ausgabe:
javascript.0 07:18:00.394 info Start JavaScript script.js.Test2 (Javascript/js) javascript.0 07:18:00.401 info script.js.Test2: httpPost(config={"method":"post","url":"https://api.ecoflow.com/iot-open/sign/device/quota","responseType":"text","responseEncoding":"utf8","timeout":2000,"auth":{"username":"U-Name","password":"P-Word"},"headers":{"User-Agent":"Mozilla/5.0 (X11; Linux i686; rv:109.0) Gecko/20100101 Firefox/121.0","Authorization":"Auth-Code"},"data":"{\"sn\":\"SN-PowerOcean\",\"params\":{\"quotas\":[\"mpptHeartBeat\"]}}"}, data={"sn":"SN-PowerOcean","params":{"quotas":["mpptHeartBeat"]}}) javascript.0 07:18:00.409 info script.js.Test2: registered 0 subscriptions, 0 schedules, 0 messages, 0 logs and 0 file subscriptions javascript.0 07:18:00.486 info script.js.Test2: { statusCode: 200, data: '{"code":"8524","message":"some parameter is empty: accessKey,nonce,timestamp,sign","eagleEyeTraceId":"Trace-ID","tid":""}', headers: Object [AxiosHeaders] { date: 'Mon, 29 Jul 2024 05:18:00 GMT', 'content-type': 'application/json; charset=UTF-8', 'content-length': '145', connection: 'keep-alive', 'eagleeye-traceid': 'Trace-ID', vary: 'Origin, Access-Control-Request-Method, Access-Control-Request-Headers', 'x-timestamp': 'User-ID' } } javascript.0 07:18:00.487 info script.js.Test2: DATA: {"code":"8524","message":"some parameter is empty: accessKey,nonce,timestamp,sign","eagleEyeTraceId":"Trace-ID","tid":""} javascript.0 07:18:00.487 info script.js.Test2: HEADERS: date: Mon, 29 Jul 2024 05:18:00 GMT content-type: application/json; charset=UTF-8 content-length: 145 connection: keep-alive eagleeye-traceid: Trace-ID vary: Origin, Access-Control-Request-Method, Access-Control-Request-Headers x-timestamp: User-ID javascript.0 07:18:04.819 info Stopping script script.js.Test2
Hier mal ein Auszug aus dem Java Aufruf, wo ich Werte erhalte:
private static void getQuotaPVdata() { String json = "{\"sn\":\"" + SN_WR + "\",\"params\":{\"quotas\":[\"mpptHeartBeat\"]}}"; JSONObject jsonObject = JSONUtil.parseObj(json); String response = HttpUtil.execute(HttpMethod.POST, GET_QUOTA_URL, jsonObject, ACCESS_KEY, SECRET_KEY); System.out.println("response: getQuotaPVData|" + response); } . . . HttpUtil.execute(HttpMethod httpMethod, String url, JSONObject req, String accessKey, String secretKey) { response = httpClient.execute(getHttpUriRequest(httpMethod, url, req, accessKey, secretKey)); } public static HttpUriRequest getHttpUriRequest(HttpMethod httpMethod, String url, JSONObject req, String accessKey, String secretKey) { String nonce = RandomUtils.nextInt(10000, 1000000) + ""; String timestamp = System.currentTimeMillis() + ""; SortedMap<String, Object> sortKeyValueMap = new TreeMap<>(); if (!Objects.isNull(req)) { sortKeyValueMap = MyMapUtil.getMapFromObject(req); } String queryString = MyMapUtil.getKeyValueString(sortKeyValueMap); (Wert: params.quotas[0]=mpptHeartBeat&sn=SN-PowerOcean) String keyValueString = MyMapUtil.appendAccessKey(queryString, accessKey, nonce, timestamp); (Wert: params.quotas[0]=mpptHeartBeat&sn=SN-PowerOcean&accessKey=U-Name&nonce=650037×tamp=1722231467572) String sign = EncryptUtil.encryptHmacSHA256(keyValueString, secretKey); queryString = URLEncoder.createDefault().encode(queryString, StandardCharsets.UTF_8); HttpUriRequest httpUriRequest = null; switch (httpMethod) { case GET: httpUriRequest = new HttpGet(String.format("%s?%s", url, queryString)); break; case PUT: HttpPut httpPut = new HttpPut(url); httpPut.addHeader("Content-Type", "application/json;charset=UTF-8"); buildBody(httpPut, req); httpUriRequest = httpPut; break; case POST: HttpPost httpPost = new HttpPost(url); httpPost.addHeader("Content-Type", "application/json;charset=UTF-8"); buildBody(httpPost, req); httpUriRequest = httpPost; break; case DELETE: httpUriRequest = new HttpDelete(String.format("%s?%s", url, queryString)); break; default: System.out.println(String.format("httMethod not match|url=%s,httpMethod=%s", url, httpMethod)); throw new RuntimeException("httMethod not match"); } httpUriRequest.addHeader(ACCESS_KEY, accessKey); httpUriRequest.addHeader(NONCE, nonce); httpUriRequest.addHeader(TIMESTAMP, timestamp); httpUriRequest.addHeader(SIGN, sign); return httpUriRequest; }
Und so sieht die Antwort in Java aus:
response: getQuotaPVData|{"code":"0","message":"Success","data":{"mpptHeartBeat":[{"mpptPv":[{"vol":478.99057,"amp":0.48060158,"pwr":230.20363},{"vol":429.51254,"amp":7.4760933,"pwr":3211.076}]}]},"eagleEyeTraceId":"Trace-ID","tid":""}
Das Ganze muss ich dann später noch auseinandernehmen. Derzeit wäre ich aber schon froh, wenn ich überhaupt diese Antwort bekäme.