• Das Erstellen neuer Accounts wurde ausgesetzt. Bei berechtigtem Interesse bitte Kontaktaufnahme über die üblichen Wege. Beste Grüße der Admin

[GELÖST] Javascript SSE und JSON parsen - Optimierungsideen gesucht

Communicate

New member
Hallo,

ich bekomme von einem Server diverse Nachrichten per SSE geschickt. Das ganze ist dann immer ein JSON-Satz.

Ich habe mir einen funktionierenden Code zusammengebaut - wenn ich ehrlich bin, sieht das aber für mich nicht so aus, als ob das die perfekte Lösung so ist. Daher meine Bitte an Euch, mir Tips zu geben, was / wie ich besser machen könnte.

Die Rohdaten des SSE-JSON sehen so aus:

Code:
event: message
data: {"topic":"smarthome/items/GEG_HZ_Ist/state","payload":"{\"type\":\"DecimalType\",\"value\":\"21.30\"}","type":"ItemStateEvent"}

Hier der Code:

Code:
<script>

var eventSource = new EventSource("http://192.168.178.52:8080/rest/events");
eventSource.addEventListener('message', function (eventPayload) {
var event = JSON.parse(eventPayload.data);
    
  
if (event.type == 'ItemStateEvent')  
console.log(event.topic)
{  
    
    
switch(event.topic)

{
   
        
    	case "smarthome/items/gFenster/state":
    	var gFenster = JSON.parse(event.payload);
    	break;
    	
    	case "smarthome/items/KEG_Fenster/state":
    	var KEG_Fenster = JSON.parse(event.payload);
    	break;
    	
    	case "smarthome/items/GEG_Fenster/state":
    	var GEG_Fenster = JSON.parse(event.payload);
    	break;
    	
    	case "smarthome/items/WEG_Fenster_V/state":
    	var WEG_Fenster_V = JSON.parse(event.payload);
    	break;
    	
    	case "smarthome/items/WEG_Fenster_L/state":
    	var WEG_Fenster_L = JSON.parse(event.payload);
    	break;
    	
    	case "smarthome/items/BOG_Fenster/state":
    	var BOG_Fenster = JSON.parse(event.payload);
    	break;
    	
    	case "smarthome/items/SOG_Fenster/state":
    	var SOG_Fenster = JSON.parse(event.payload);
    	break;
    	
    	case "smarthome/items/LOG_Fenster/state":
    	var LOG_Fenster = JSON.parse(event.payload);
    	break;
    	
    	case "smarthome/items/AOG_Fenster/state":
    	var AOG_Fenster = JSON.parse(event.payload);
    	break;
    	
    	case "smarthome/items/KUG_Fenster/state":
    	var KUG_Fenster = JSON.parse(event.payload);
    	break;
    	
    	case "smarthome/items/FensterO/state":
    	var FensterO = JSON.parse(event.payload);
    	break;
    	
    	case "smarthome/items/FensterG/state":
    	var FensterG = JSON.parse(event.payload);
    	break;
    	
    	case "smarthome/items/GEG_HZ_Ist/state":
    	var GEG_HZ_Ist = JSON.parse(event.payload);
    	break;
    	
    	case "smarthome/items/GEG_HZ_Soll/state":
    	var GEG_HZ_Soll = JSON.parse(event.payload);
    	break;
    	
    	case "smarthome/items/EG_Garderobe_Beleuchtung/state":
    	var EG_Garderobe_Beleuchtung = JSON.parse(event.payload);
    	break;
    	
    	case "smarthome/items/FEG_HZ_Ist/state":
    	var FEG_HZ_Ist = JSON.parse(event.payload);
    	break;
    	
    	case "smarthome/items/FEG_HZ_Soll/state":
    	var FEG_HZ_Soll = JSON.parse(event.payload);
    	break;
    	
    	case "smarthome/items/GEG_Rollo/state":
    	var GEG_Rollo = JSON.parse(event.payload);
    	break;
    	
    	case "smarthome/items/KEG_Beleuchtung/state":
    	var KEG_Beleuchtung = JSON.parse(event.payload);
    	break;
    	
    	case "smarthome/items/KEG_Lueftung/state":
    	var KEG_Lueftung = JSON.parse(event.payload);
    	break;
    	
    	case "smarthome/items/KEG_Rollo/state":
    	var KEG_Rollo = JSON.parse(event.payload);
    	break;
    	
    	case "smarthome/items/WEG_Rollo_V/state":
    	var WEG_Rollo_V = JSON.parse(event.payload);
    	break;
    	
    	case "smarthome/items/WEG_Rollo_R/state":
    	var WEG_Rollo_R = JSON.parse(event.payload);
    	break;
    	
    	case "smarthome/items/WEG_Rollo_L/state":
    	var WEG_Rollo_L = JSON.parse(event.payload);
    	break;
    	
    	case "smarthome/items/WEG_HZ_Ist/state":
    	var WEG_HZ_Ist = JSON.parse(event.payload);
    	break;
    	
    	case "smarthome/items/WEG_HZ_Soll/state":
    	var WEG_HZ_Soll = JSON.parse(event.payload);
    	break;
    	
    	case "smarthome/items/WEG_HZ_Luft/state":
    	var WEG_HZ_Luft = JSON.parse(event.payload);
    	break;
    	
    	case "smarthome/items/BOG_HZ_Ist/state":
    	var BOG_HZ_Ist = JSON.parse(event.payload);
    	break;
    	
    	case "smarthome/items/BOG_HZ_Soll/state":
    	var BOG_HZ_Soll = JSON.parse(event.payload);
    	break;
    	
    	case "smarthome/items/BOG_HZ_Luft/state":
    	var BOG_HZ_Luft = JSON.parse(event.payload);
    	break;
    	
    	case "smarthome/items/BOG_Rollo/state":
    	var BOG_Rollo = JSON.parse(event.payload);
    	break;
    	
    	case "smarthome/items/SOG_HZ_Ist/state":
    	var SOG_HZ_Ist = JSON.parse(event.payload);
    	break;
    	
    	case "smarthome/items/SOG_HZ_Soll/state":
    	var SOG_HZ_Soll = JSON.parse(event.payload);
    	break;
    	
    	case "smarthome/items/SOG_HZ_Luft/state":
    	var SOG_HZ_Luft = JSON.parse(event.payload);
    	break;
    	
    	case "smarthome/items/SOG_Rollo/state":
    	var SOG_Rollo = JSON.parse(event.payload);
    	break;
    	
    	case "smarthome/items/LOG_HZ_Ist/state":
    	var LOG_HZ_Ist = JSON.parse(event.payload);
    	break;
    	
    	case "smarthome/items/LOG_HZ_Soll/state":
    	var LOG_HZ_Soll = JSON.parse(event.payload);
    	break;
    	
    	case "smarthome/items/LOG_HZ_Luft/state":
    	var LOG_HZ_Luft = JSON.parse(event.payload);
    	break;
    	
    	case "smarthome/items/LOG_Rollo/state":
    	var LOG_Rollo = JSON.parse(event.payload);
    	break;
    	
    	case "smarthome/items/AOG_HZ_Ist/state":
    	var AOG_HZ_Ist = JSON.parse(event.payload);
    	break;
    	
    	case "smarthome/items/AOG_HZ_Soll/state":
    	var AOG_HZ_Soll = JSON.parse(event.payload);
    	break;
    	
    	case "smarthome/items/AOG_Rollo/state":
    	var AOG_Rollo = JSON.parse(event.payload);
    	break;
        
        
  
}

}
    
    
    
    
    
});

Ich würde mich über Feedback sehr freuen :)
 
Zuletzt bearbeitet von einem Moderator:
Hallo,

Das ganze erfolgt im Rahmen einer Steuerung meiner vernetzten Hauselemente (also z.B. Fensterstatus, aktuelle Position der Rollläden, Soll Temperatur der Heizung, etc.).

Für jedes Element habe ich eine Variable angelegt. Mein Ziel ist es, diese dann in eine HTML Seite einzubauen. Z.B. Um ein Dashboard für alle Fenster zu erstellen. Es wird dann quasi angezeigt:
Fenster Küche: auf
Fenster Bad: geschlossen
Etc.

Auf und geschlossen sind in diesem Fall dann die Inhalte der jeweiligen Variablen.

Ich hoffe es ist klar geworden, was ich vor habe:)
 
Hallo,

Danke für die Rückmeldung. Hast du ein paar ergänzende Worte für mich oder gar ein Beispiel? Verstehe ich dich richtig das ich zuerst den json Stream parse dann den Teilstream (payload in meinem Fall) nehme und in ein Objekt stecke (wie auch immer ich das mache) und dann in HTML Teil immer nur eine Eigenschaft des Objekts aufrufe?
 
Du nimmst eine Variable (das Objekt) anstatt der vielen. Dann vereinfacht sich das ganze ungemein:

Code:
let name = event.topic.split('/')[2];
items[name] = JSON.parse(event.payload);
 
Du nimmst eine Variable (das Objekt) anstatt der vielen. Dann vereinfacht sich das ganze ungemein:

Code:
let name = event.topic.split('/')[2];
items[name] = JSON.parse(event.payload);

Vielen Dank, das klingt logisch :)

Ein kleines Problem habe ich leider noch:

ein
Code:
console.log(items[name])
bringt mir in Firebug nur ein

ReferenceError: items is not defined

Was mache ich falsch?
 
ReferenceError: items is not defined sagt dir nur, das die Zuweisung an die Variable items nicht gelungen ist. Zeig doch mal deinen Code wie du ihn jetzt umgeändert hast.
 
Hey,

hier mein Code:

Code:
<script>

var eventSource = new EventSource("http://192.168.178.52:8080/rest/events");
eventSource.addEventListener('message', function (eventPayload) {
var event = JSON.parse(eventPayload.data);

 
  
if (event.type == 'ItemStateEvent')  

{  
	let name = event.topic.split('/')[2];
	items[name] = JSON.parse(event.payload);

	console.log(items[name])
	
}
    
    
    
    
    
});


</script>

ein console.log (name) bringt mir FEG_HZ_Soll.

:

- - - Aktualisiert - - -

Das sieht ja zunächst einmal gut aus. Die Zuweisung an das Array scheint aber nicht zu klappen. Die Herausforderung ist jetzt das ich nicht an den "value" wert herankommen...

- - - Aktualisiert - - -

Ich bin noch nicht so tief bewandert in JS. Kann ich irgendwie die Objekte bzw. die Elemente ausgeben lassen, damit ich so vielleicht besser raus bekomme, warum die Zuweisung nicht klappt ?

Gruß
Dominic

- - - Aktualisiert - - -
 
Zuletzt bearbeitet:
Du musst eine Variable items definieren, in der der Inhalt dann gespeichert wird:
Code:
var eventSource = new EventSource("http://192.168.178.52:8080/rest/events");
var items = {};
eventSource.addEventListener('message', function(eventPayload){
	var event = JSON.parse(eventPayload.data);
	if (event.type === "ItemStateEvent"){  
		var name = event.topic.split('/')[2];
		items[name] = JSON.parse(event.payload);
		console.log(name, items[name])
	}	
});
 
Hallo kkapsner,

das hat geholfen - vielen lieben Dank.

Wie das immer so ist, steht man nach dem Problem vor dem nächsten problem :)

Durch SSE bekommen die einzelnen items Ihren Wert ja nur wenn ein entsprechendes Event auftritt.
Um dieses Problem zu lösen würde ich gerne initialwerte mitgeben. Diese bekomme ich ebenfalls per JSON:

Code:
[{"members":[],"groupType":"Number","function":{"name":"SUM"},"link":"http://192.168.178.52:8080/rest/items/gFenster","state":"0","type":"Group","name":"gFenster","tags":[],"groupNames":[]},{"link":"http://192.168.178.52:8080/rest/items/KEG_Fenster","state":"CLOSED","stateDescription":{"readOnly":true,"options":[]},"type":"Contact","name":"KEG_Fenster","label":"Fenster","tags":[],"groupNames":["gFenster"]},{"link":"http://192.168.178.52:8080/rest/items/GEG_Fenster","state":"CLOSED","stateDescription":{"readOnly":true,"options":[]},"type":"Contact","name":"GEG_Fenster","label":"Fenster","tags":[],"groupNames":["gFenster"]},{"link":"http://192.168.178.52:8080/rest/items/WEG_Fenster_V","state":"CLOSED","stateDescription":{"readOnly":true,"options":[]},"type":"Contact","name":"WEG_Fenster_V","label":"Fenster","tags":[],"groupNames":["gFenster"]},{"link":"http://192.168.178.52:8080/rest/items/WEG_Fenster_L","state":"CLOSED","stateDescription":{"readOnly":true,"options":[]},"type":"Contact","name":"WEG_Fenster_L","label":"Fenster","tags":[],"groupNames":["gFenster"]},{"link":"http://192.168.178.52:8080/rest/items/BOG_Fenster","state":"CLOSED","stateDescription":{"readOnly":true,"options":[]},"type":"Contact","name":"BOG_Fenster","label":"Fenster","tags":[],"groupNames":["gFenster"]},{"link":"http://192.168.178.52:8080/rest/items/SOG_Fenster","state":"CLOSED","stateDescription":{"readOnly":true,"options":[]},"type":"Contact","name":"SOG_Fenster","label":"Fenster","tags":[],"groupNames":["gFenster"]},{"link":"http://192.168.178.52:8080/rest/items/LOG_Fenster","state":"CLOSED","stateDescription":{"readOnly":true,"options":[]},"type":"Contact","name":"LOG_Fenster","label":"Fenster","tags":[],"groupNames":["gFenster"]},{"link":"http://192.168.178.52:8080/rest/items/AOG_Fenster","state":"CLOSED","stateDescription":{"readOnly":true,"options":[]},"type":"Contact","name":"AOG_Fenster","label":"Fenster","tags":[],"groupNames":["gFenster"]},{"link":"http://192.168.178.52:8080/rest/items/KUG_Fenster","state":"NULL","type":"Contact","name":"KUG_Fenster","label":"Fenster","tags":[],"groupNames":["gFenster"]},{"link":"http://192.168.178.52:8080/rest/items/FensterO","state":"NULL","type":"Number","name":"FensterO","tags":[],"groupNames":[]},{"link":"http://192.168.178.52:8080/rest/items/FensterG","state":"8","type":"Number","name":"FensterG","tags":[],"groupNames":[]},{"link":"http://192.168.178.52:8080/rest/items/GEG_HZ_Ist","state":"20.40","stateDescription":{"pattern":"%.1f °C","readOnly":false,"options":[]},"type":"Number","name":"GEG_HZ_Ist","label":"Temperatur Ist","tags":[],"groupNames":[]},{"link":"http://192.168.178.52:8080/rest/items/GEG_HZ_Soll","state":"19.00","stateDescription":{"pattern":"%.1f °C","readOnly":false,"options":[]},"type":"Number","name":"GEG_HZ_Soll","label":"Temperatur Soll","tags":[],"groupNames":[]},{"link":"http://192.168.178.52:8080/rest/items/EG_Garderobe_Beleuchtung","state":"NULL","type":"Switch","name":"EG_Garderobe_Beleuchtung","label":"Garderobe Beleuchtung","category":"light","tags":[],"groupNames":["EG_KG","Corridor","Lights"]},{"link":"http://192.168.178.52:8080/rest/items/FEG_HZ_Ist","state":"21.30","stateDescription":{"pattern":"%.1f °C","readOnly":false,"options":[]},"type":"Number","name":"FEG_HZ_Ist","label":"Temperatur Ist","tags":[],"groupNames":[]},{"link":"http://192.168.178.52:8080/rest/items/FEG_HZ_Soll","state":"20.00","stateDescription":{"pattern":"%.1f °C","readOnly":false,"options":[]},"type":"Number","name":"FEG_HZ_Soll","label":"Temperatur Soll","tags":[],"groupNames":[]},{"link":"http://192.168.178.52:8080/rest/items/GEG_Rollo","state":"NULL","type":"Rollershutter","name":"GEG_Rollo","label":"Rollo","tags":[],"groupNames":[]},{"link":"http://192.168.178.52:8080/rest/items/KEG_Beleuchtung","state":"NULL","type":"Switch","name":"KEG_Beleuchtung","label":"Arbeitsplatte Beleuchtung","category":"light","tags":[],"groupNames":["EG_KG","Corridor","Lights"]},{"link":"http://192.168.178.52:8080/rest/items/KEG_Lueftung","state":"NULL","type":"Switch","name":"KEG_Lueftung","label":"Dunstabzug","category":"light","tags":[],"groupNames":["EG_KG","Corridor","Lights"]},{"link":"http://192.168.178.52:8080/rest/items/KEG_Rollo","state":"NULL","type":"Rollershutter","name":"KEG_Rollo","label":"Rollo","tags":[],"groupNames":[]},{"link":"http://192.168.178.52:8080/rest/items/WEG_Rollo_V","state":"0","stateDescription":{"minimum":0.0,"maximum":1.0,"step":0.1,"pattern":"%.2f %%","readOnly":false,"options":[]},"type":"Rollershutter","name":"WEG_Rollo_V","label":"Rollo vorn","tags":[],"groupNames":[]},{"link":"http://192.168.178.52:8080/rest/items/WEG_Rollo_R","state":"0","stateDescription":{"minimum":0.0,"maximum":1.0,"step":0.1,"pattern":"%.2f %%","readOnly":false,"options":[]},"type":"Rollershutter","name":"WEG_Rollo_R","label":"Rollo rechts","tags":[],"groupNames":[]},{"link":"http://192.168.178.52:8080/rest/items/WEG_Rollo_L","state":"0","stateDescription":{"minimum":0.0,"maximum":1.0,"step":0.1,"pattern":"%.2f %%","readOnly":false,"options":[]},"type":"Rollershutter","name":"WEG_Rollo_L","label":"Rollo links","tags":[],"groupNames":[]},{"link":"http://192.168.178.52:8080/rest/items/WEG_HZ_Ist","state":"NULL","stateDescription":{"pattern":"%.1f °C","readOnly":false,"options":[]},"type":"Number","name":"WEG_HZ_Ist","label":"Temperatur Ist","tags":[],"groupNames":[]},{"link":"http://192.168.178.52:8080/rest/items/WEG_HZ_Soll","state":"NULL","stateDescription":{"pattern":"%.1f °C","readOnly":false,"options":[]},"type":"Number","name":"WEG_HZ_Soll","label":"Temperatur Soll","tags":[],"groupNames":[]},{"link":"http://192.168.178.52:8080/rest/items/WEG_HZ_Luft","state":"NULL","type":"Number","name":"WEG_HZ_Luft","label":"Luftfeuchtigkeit","category":"humidity","tags":[],"groupNames":[]},{"link":"http://192.168.178.52:8080/rest/items/BOG_HZ_Ist","state":"NULL","stateDescription":{"pattern":"%.1f °C","readOnly":false,"options":[]},"type":"Number","name":"BOG_HZ_Ist","label":"Temperatur Ist","tags":[],"groupNames":[]},{"link":"http://192.168.178.52:8080/rest/items/BOG_HZ_Soll","state":"NULL","stateDescription":{"pattern":"%.1f °C","readOnly":false,"options":[]},"type":"Number","name":"BOG_HZ_Soll","label":"Temperatur Soll","tags":[],"groupNames":[]},{"link":"http://192.168.178.52:8080/rest/items/BOG_HZ_Luft","state":"NULL","type":"Number","name":"BOG_HZ_Luft","label":"Luftfeuchtigkeit","category":"humidity","tags":[],"groupNames":[]},{"link":"http://192.168.178.52:8080/rest/items/BOG_Rollo","state":"NULL","type":"Rollershutter","name":"BOG_Rollo","label":"Rollo","tags":[],"groupNames":[]},{"link":"http://192.168.178.52:8080/rest/items/SOG_HZ_Ist","state":"NULL","stateDescription":{"pattern":"%.1f °C","readOnly":false,"options":[]},"type":"Number","name":"SOG_HZ_Ist","label":"Temperatur Ist","tags":[],"groupNames":[]},{"link":"http://192.168.178.52:8080/rest/items/SOG_HZ_Soll","state":"NULL","stateDescription":{"pattern":"%.1f °C","readOnly":false,"options":[]},"type":"Number","name":"SOG_HZ_Soll","label":"Temperatur Soll","tags":[],"groupNames":[]},{"link":"http://192.168.178.52:8080/rest/items/SOG_HZ_Luft","state":"NULL","type":"Number","name":"SOG_HZ_Luft","label":"Luftfeuchtigkeit","category":"humidity","tags":[],"groupNames":[]},{"link":"http://192.168.178.52:8080/rest/items/SOG_Rollo","state":"NULL","type":"Rollershutter","name":"SOG_Rollo","label":"Rollo","tags":[],"groupNames":[]},{"link":"http://192.168.178.52:8080/rest/items/LOG_HZ_Ist","state":"NULL","stateDescription":{"pattern":"%.1f °C","readOnly":false,"options":[]},"type":"Number","name":"LOG_HZ_Ist","label":"Temperatur Ist","tags":[],"groupNames":[]},{"link":"http://192.168.178.52:8080/rest/items/LOG_HZ_Soll","state":"NULL","stateDescription":{"pattern":"%.1f °C","readOnly":false,"options":[]},"type":"Number","name":"LOG_HZ_Soll","label":"Temperatur Soll","tags":[],"groupNames":[]},{"link":"http://192.168.178.52:8080/rest/items/LOG_HZ_Luft","state":"NULL","type":"Number","name":"LOG_HZ_Luft","label":"Luftfeuchtigkeit","category":"humidity","tags":[],"groupNames":[]},{"link":"http://192.168.178.52:8080/rest/items/LOG_Rollo","state":"NULL","type":"Rollershutter","name":"LOG_Rollo","label":"Rollo","tags":[],"groupNames":[]},{"link":"http://192.168.178.52:8080/rest/items/AOG_HZ_Ist","state":"NULL","stateDescription":{"pattern":"%.1f °C","readOnly":false,"options":[]},"type":"Number","name":"AOG_HZ_Ist","label":"Temperatur Ist","tags":[],"groupNames":[]},{"link":"http://192.168.178.52:8080/rest/items/AOG_HZ_Soll","state":"NULL","stateDescription":{"pattern":"%.1f °C","readOnly":false,"options":[]},"type":"Number","name":"AOG_HZ_Soll","label":"Temperatur Soll","tags":[],"groupNames":[]},{"link":"http://192.168.178.52:8080/rest/items/AOG_HZ_Luft","state":"NULL","stateDescription":{"pattern":"%.0f %%","readOnly":false,"options":[]},"type":"Number","name":"AOG_HZ_Luft","label":"Luftfeuchtigkeit","category":"humidity","tags":[],"groupNames":[]},{"link":"http://192.168.178.52:8080/rest/items/AOG_Rollo","state":"NULL","type":"Rollershutter","name":"AOG_Rollo","label":"Rollo","tags":[],"groupNames":[]},{"link":"http://192.168.178.52:8080/rest/items/KEG_Fenster","state":"CLOSED","stateDescription":{"readOnly":true,"options":[]},"type":"Contact","name":"KEG_Fenster","label":"State","category":"Contact","tags":[],"groupNames":[]},{"link":"http://192.168.178.52:8080/rest/items/GEG_Fenster","state":"CLOSED","stateDescription":{"readOnly":true,"options":[]},"type":"Contact","name":"GEG_Fenster","label":"State","category":"Contact","tags":[],"groupNames":[]},{"link":"http://192.168.178.52:8080/rest/items/WEG_Fenster_V","state":"CLOSED","stateDescription":{"readOnly":true,"options":[]},"type":"Contact","name":"WEG_Fenster_V","label":"State","category":"Contact","tags":[],"groupNames":[]},{"link":"http://192.168.178.52:8080/rest/items/WEG_Fenster_L","state":"CLOSED","stateDescription":{"readOnly":true,"options":[]},"type":"Contact","name":"WEG_Fenster_L","label":"State","category":"Contact","tags":[],"groupNames":[]},{"link":"http://192.168.178.52:8080/rest/items/BOG_Fenster","state":"CLOSED","stateDescription":{"readOnly":true,"options":[]},"type":"Contact","name":"BOG_Fenster","label":"State","category":"Contact","tags":[],"groupNames":[]},{"link":"http://192.168.178.52:8080/rest/items/SOG_Fenster","state":"CLOSED","stateDescription":{"readOnly":true,"options":[]},"type":"Contact","name":"SOG_Fenster","label":"State","category":"Contact","tags":[],"groupNames":[]},{"link":"http://192.168.178.52:8080/rest/items/LOG_Fenster","state":"CLOSED","stateDescription":{"readOnly":true,"options":[]},"type":"Contact","name":"LOG_Fenster","label":"State","category":"Contact","tags":[],"groupNames":[]},{"link":"http://192.168.178.52:8080/rest/items/AOG_Fenster","state":"CLOSED","stateDescription":{"readOnly":true,"options":[]},"type":"Contact","name":"AOG_Fenster","label":"State","category":"Contact","tags":[],"groupNames":[]},{"link":"http://192.168.178.52:8080/rest/items/GEG_HZ_Ist","state":"20.50","stateDescription":{"pattern":"%.1f °C","readOnly":false,"options":[]},"type":"Number","name":"GEG_HZ_Ist","label":"Actual Temperature","category":"Temperature","tags":[],"groupNames":[]},{"link":"http://192.168.178.52:8080/rest/items/GEG_HZ_Soll","state":"19.00","stateDescription":{"pattern":"%.1f °C","readOnly":false,"options":[]},"type":"Number","name":"GEG_HZ_Soll","label":"Set Temperature","category":"Temperature","tags":[],"groupNames":[]}]

Ich habe mir dafür eine entsprechende Abfrage gebaut in JavaScript:

Code:
function getJSON(url) {
    var resp ;
    var xmlHttp ;

    resp  = '' ;
    xmlHttp = new XMLHttpRequest();

    if(xmlHttp != null)
    {
        xmlHttp.open( "GET", url, false );
        xmlHttp.send( null );
        resp = xmlHttp.responseText;
    }

    return resp ;
}



var gjson ;
gjson = getJSON('http://192.168.178.52:8080/rest/items?recursive=false') ;
a=JSON.parse(gjson);

window.alert(a);

Als Rückmeldung bekomme ich jetzt in Firebug folgendes:

Code:
0Objectmembers[]groupType"Number"functionObjectlink"http://192.168.178.52:8080/rest/items/gFenster"state"0"type"Group"name"gFenster"tags[]groupNames[]

1Objectlink"http://192.168.178.52:8080/rest/items/KEG_Fenster"state"CLOSED"stateDescriptionObjecttype"Contact"name"KEG_Fenster"label"Fenster"tags[]groupNames[1]
2Object
3Object
4Object
5Object
6Object

Habt Ihr noch einen Tip für mich, wie ich meine items die ich aus dem Post von kkapsner bekomme mit den initialwerten aus diesen JSON versorgen kann?

Vielen Dank für Eure Unterstützung :)
 
Hallo zusammen,

Das hat mir sehr geholfen.

Eine hoffentlich letzte Frage habe ich noch.

Wenn ich die einzelnen Elemente aus dem items Array in den html Teil eingebunden habe ist die Seite ja noch statisch. Sprich, der html Teil bekommt nicht mit, wenn die Elemente sich aktualisieren. Wie kann ich denn dafür sorgen, dass immer bei Aktualisierung meines Arrays auch der html Teil aktualisiert wird?

Vielen Dank für Eure tolle Unterstützung
 
Hallo Dormilich,
Der Ansatz klingt gut. Nur wie kann ich denn aus einer js Funktion genau die Teile im html aktualisieren die meine Werte aus dem Array anzeigen?

Ich bin leider in JS noch nicht so bewandert, aber wie "kennzeichne" ich denn die entsprechenden Stellen, so dass ich sie aktualisieren kann? Jeder stelle eine einzelne ID zuweisen, wird ja auch wieder unübersichtlich oder?
 
Hi,

Hmmm, dann habe ich im Zweifel den Datensatz falsch eingebunden. Ich habe in html einen script tag eingebunden und dann einfach nur " items[FEG_HZ_SOLL ].value" reingeschrieben.

Hätte ich das anders einbinden müssen?
 
Zu dem Zeitpunkt hast du ja noch gar keine Daten ...

wie findest du denn das HTML-Element, das upgedated werden soll?
 
Hi, bin noch nicht vorm Rechner, daher nur theoretisch. Wenn ich mich recht erinnere dann darf ich doch immer nur ein eindeutige IDs vergeben. Bei z.B. 20 Stellen benötige ich also 20 IDs- richtig?
 
Zurück
Oben