Linux-osok simán összeraknak erre egy scriptet, ezért a windows-osoknak mutatok példát.
A PS parancs amit saját hálózati IP cím és inverter azonosításával módosítani kell:
Out-File -FilePath c:\Users\User\Documents\kl\napelem\kl_blog\java\kl_sol_ps.txt -Encoding ASCII -Width 120 -Append
tim tmp pac sac qac etd eto vac iac vpv ipv err
--- --- --- --- --- --- --- --- --- --- --- --- ---
20230218114622 449 6351 6351 2 131 4354 {2360, 2351, 2342} {90, 90, 90} {4642, 3691} {784, 787} 0
Minden faxni nélkül nekünk most csak a java.exe és javac.exe kell én ezeket a példában az adroind studo-ból használtam.
import java.io.BufferedInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;
import java.nio.channels.Channels;
import java.nio.channels.ReadableByteChannel;
import java.time.format.DateTimeFormatter;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.Month;
import java.util.concurrent.TimeUnit;
public class JsonReader22 {
public static void main(String[] args) {
System.out.println("Első argumentum: "+args[0]);
System.out.println("Második argumentum: "+args[1]);
String url = "http://" + args[0] + ":8484/getdevdata.cgi?device=2&sn=" + args[1];
System.out.println("url: "+url);
String out_file = "kl_kesz";
LocalDate datum = LocalDate.now();
LocalDateTime dt = LocalDateTime.now();
DateTimeFormatter df = DateTimeFormatter.ofPattern("_yyyy-MM-dd"); //HH-mm-ss_");
String date_text = dt.format(df);
DateTimeFormatter df2 = DateTimeFormatter.ofPattern("_yyyy-MM-dd_HH-mm-ss_");
String date_text2 = dt.format(df2);
System.out.println("Formázott dátum : "+ date_text);
try {
downloadUsingNIO(url, out_file + date_text2 + ".json"); // futásonként egy-egy filebe
downloadUsingStream(url, out_file + date_text + ".json"); // napi futás egy file töltése összefűzéssel
} catch (IOException e) {
e.printStackTrace();
}
}
private static void downloadUsingStream(String urlStr, String file) throws IOException{
URL url = new URL(urlStr);
BufferedInputStream bis = new BufferedInputStream(url.openStream());
FileOutputStream fis = new FileOutputStream(file, true);
byte[] buffer = new byte[1024];
int count=0;
while((count = bis.read(buffer,0,1024)) != -1)
{
fis.write(buffer, 0, count);
}
System.out.println(buffer);
String textToAppend = "\n";
byte[] strToBytes = textToAppend.getBytes();
fis.write(strToBytes);
fis.close();
bis.close();
}
private static void downloadUsingNIO(String urlStr, String file) throws IOException {
URL url = new URL(urlStr);
ReadableByteChannel rbc = Channels.newChannel(url.openStream());
FileOutputStream fos = new FileOutputStream(file, true);
fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
fos.close();
rbc.close();
}
}
{"flg":1,"tim":"20230218132702","tmp":407,"fac":5000,"pac":893,"sac":893,"qac":0,"eto":4392,"etd":169,"hto":183,"pf":88,"wan":0,"err":0,"vac":[2311,2315,2305],"iac":[14,14,14],"vpv":[4676,3655],"ipv":[113,98],"str":[]}
{"flg":1,"tim":"20230218132722","tmp":407,"fac":5000,"pac":851,"sac":851,"qac":0,"eto":4392,"etd":169,"hto":183,"pf":88,"wan":0,"err":0,"vac":[2323,2311,2315],"iac":[13,14,13],"vpv":[4652,3633],"ipv":[108,94],"str":[]}
{"flg":1,"tim":"20230218133102","tmp":403,"fac":5000,"pac":1276,"sac":1276,"qac":0,"eto":4393,"etd":170,"hto":183,"pf":93,"wan":0,"err":0,"vac":[2334,2323,2313],"iac":[20,19,19],"vpv":[4728,3720],"ipv":[157,149],"str":[]}
{"flg":1,"tim":"20230218133152","tmp":402,"fac":5001,"pac":1261,"sac":1261,"qac":0,"eto":4393,"etd":170,"hto":183,"pf":93,"wan":0,"err":0,"vac":[2345,2265,2326],"iac":[19,19,19],"vpv":[4739,3699],"ipv":[157,142],"str":[]}
{"flg":1,"tim":"20230218133222","tmp":401,"fac":5000,"pac":1219,"sac":1219,"qac":0,"eto":4393,"etd":170,"hto":183,"pf":93,"wan":0,"err":0,"vac":[2344,2280,2312],"iac":[19,18,18],"vpv":[4741,3721],"ipv":[151,137],"str":[]}
{"flg":1,"tim":"20230218133412","tmp":400,"fac":4998,"pac":1487,"sac":1487,"qac":0,"eto":4393,"etd":170,"hto":183,"pf":95,"wan":0,"err":0,"vac":[2371,2268,2321],"iac":[22,22,22],"vpv":[4861,3968],"ipv":[189,150],"str":[]}
{"flg":1,"tim":"20230218135512","tmp":397,"fac":5000,"pac":3270,"sac":3270,"qac":1,"eto":4400,"etd":177,"hto":184,"pf":99,"wan":0,"err":0,"vac":[2378,2379,2328],"iac":[47,46,46],"vpv":[5130,4067],"ipv":[371,363],"str":[]}
{"flg":1,"tim":"20230218135512","tmp":397,"fac":5000,"pac":3270,"sac":3270,"qac":1,"eto":4400,"etd":177,"hto":184,"pf":99,"wan":0,"err":0,"vac":[2378,2379,2328],"iac":[47,46,46],"vpv":[5130,4067],"ipv":[371,363],"str":[]}
{"flg":1,"tim":"20230218135652","tmp":399,"fac":5001,"pac":3009,"sac":3009,"qac":1,"eto":4401,"etd":178,"hto":184,"pf":98,"wan":0,"err":0,"vac":[2382,2368,2321],"iac":[43,42,43],"vpv":[5075,4055],"ipv":[341,337],"str":[]}
{"flg":1,"tim":"20230218135722","tmp":399,"fac":5000,"pac":2948,"sac":2948,"qac":1,"eto":4402,"etd":179,"hto":184,"pf":98,"wan":0,"err":0,"vac":[2384,2363,2320],"iac":[42,41,42],"vpv":[5120,4024],"ipv":[332,333],"str":[]}
Egy egy állományos eredmény:
{"flg":1,"tim":"20230218135722","tmp":399,"fac":5000,"pac":2948,"sac":2948,"qac":1,"eto":4402,"etd":179,"hto":184,"pf":98,"wan":0,"err":0,"vac":[2384,2363,2320],"iac":[42,41,42],"vpv":[5120,4024],"ipv":[332,333],"str":[]}
És nézzünk egy pythonos megoldást is.
A python program
import urllib.request, json
## beolvasás
with urllib.request.urlopen("http://192.168.0.101:8484/getdevdata.cgi?device=2&sn=SP00%%") as url:
data = json.load(url)
## állományba írás
with open(r"c:\Users\User\Documents\kl\napelem\kl_blog\java\kl_kesz_py_2023-02-18.json", "w") as outfile:
json.dump(data, outfile)
## formázott megjelenítés
print(f"""A mai ({data['tim'][0:4]}.{data['tim'][4:6]}.{data['tim'][6:8]}) termelés: {data['etd']/10:04.1f} KWh
és a pillanatnyi ({data['tim'][8:10]}:{data['tim'][10:12]}:{data['tim'][12:14]} -kor) termelés: {data['pac']} Wh
""")
## csoportos megjelenítés
print('-'*60)
for key, value in data.items():
print(key, value)
Az eredmény
A mai (2023.02.18) termelés: 20.2 KWh
és a pillanatnyi (16:01:32 -kor) termelés: 533 Wh
------------------------------------------------------------
flg 1
tim 20230218160132
tmp 372
fac 5005
pac 533
sac 533
qac 0
eto 4425
etd 202
hto 186
pf 77
wan 0
err 0
vac [2352, 2313, 2270]
iac [9, 9, 10]
vpv [4621, 3642]
ipv [67, 59]
str []
Megjegyzés
Azért programozó guruknak jelzem, hogy feleslegesen ne 'gyúrjanak' rajtam, hogy igen csúnya ez a megoldás, meg ezt nem így kellene megcsinálni....
Ennyi időt szántam rá, mikor működött befejeztetem.
Nincsenek megjegyzések:
Megjegyzés küldése
Megjegyzés: Megjegyzéseket csak a blog tagjai írhatnak a blogba.