summaryrefslogtreecommitdiff
path: root/smarttv-client/Javascript/Epg.js
blob: 87325a829d8b9f089418cf0d868bb0187be2f3c2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/*
 * Principle: One monitor for each channel
 * Nur max 20
 *  Oder einer:
 *  Search once through all entries and look for the lowest expiry.
 *  Set a timer to update the entry
 *  check, whether the entry is "on screen"
 * 
*/

var Epg = {
	timeoutObj : null,
	isActive : false,
	guidErrors : {},
	guidTitle : {},
	curGuid : "",
	lastGuid :"",
	sameGuidCount : 0,
	globalCount :0
};

Epg.stopUpdates = function () {
	if (this.isActive == false )
		return;

	window.clearTimeout(this.timeoutObj);
	this.isActive = false;
	this.curGui = "";
};

Epg.startEpgUpdating = function() {
	Main.log("");
	Main.log("**** Epg.startEpgUpdating ****");
	
	if (this.isActive == true)
		return;
		
/*	if (this.globalCount == 30) {
		Main.log ("stopping");
		return;
	}
	*/
	this.globalCount ++;
	if (Main.state != Main.eLIVE) {
		Main.logToServer("ERROR in Epg.startEpgUpdating: I am NOT in state Main.eLive...!!");
		return;
	}
	this.isActive = true;
	var res = Data.findEpgUpdateTime(); 
	
	var now = Display.GetEpochTime();
	var delay = Math.round(res.min - now) *1000;
	this.curGuid = res.guid;

	
	if (this.lastGuid == res.guid) {
		// lastGuid is only changed, when the last response was successful
		// number of repetitive errors are checked in the error response procedure
		this.sameGuidCount ++;
//		Main.logToServer("WARNING in Epg.startEpgUpdating: again same Guid guid= " + res.guid + "(" +Epg.guidTitle[res.guid] +") (sameGuidCount= " +this.sameGuidCount+"). OrgDelay= " + (delay/1000.0) + "sec. Delaying...");
		delay = 1000;
		if (this.sameGuidCount > 60) {
			Epg.insertFakeEntry(res.guid);
			Main.logToServer("WARNING in EPG.updateEPG: insertFakeEntry for guid= " + res.guid + " (" + Epg.guidTitle[res.guid] +")");
			Main.log("WARNING in EPG.updateEPG: insertFakeEntry for guid= " + res.guid + " (" + Epg.guidTitle[res.guid] +")");
			Epg.isActive = false;
			Epg.startEpgUpdating();
//			Main.logToServer("ERROR in EPG.updateEPG: 10x same guid !!! - Please file a bug report! - Bitte eine Fehlermeldung posten");
//			Display.showPopup("EPG.updateEPG: 10x same guid !!!<br>Please file a bug report!<br>Bitte eine Fehlermeldung posten");
			return;
		}
	}
	else {
		Main.log("Epg.startEpgUpdating: Next update for GUID= " + res.guid + " with Min= " + res.min + " delay= " + (delay/1000.0) + "sec"); 
		this.sameGuidCount = 0;
	}

	if (delay <0) {
		delay = 300; //bit delay in msec
	}
	if (!(res.guid in this.guidErrors)) {
		this.guidErrors[res.guid] = 0;
	};

/*	Main.log("Iter over this.guidErrors");
	for (var prop in this.guidErrors) {
		Main.log(" " +prop + " == " + this.guidErrors[prop]);
	}
	Main.log("Iter Done");
*/	
	if (this.guidErrors[res.guid] == 0) {
		Main.log("Epg.startEpgUpdating: next Update for guid= " + res.guid + " (" +Epg.guidTitle[res.guid] +") guidErrors= "+this.guidErrors[res.guid]+ " in " + (delay/1000.0) +"sec");
		Main.logToServer("Epg.startEpgUpdating: next Update for guid= " + res.guid + " (" +Epg.guidTitle[res.guid]+ ") guidErrors= "+this.guidErrors[res.guid]+ " in " + (delay/1000.0) +"sec");
//	Main.logToServer("Epg.startEpgUpdating: next Update for guid= " + res.guid + " in " + (delay/1000.0) +"sec");
	}
	
	this.timeoutObj = window.setTimeout(function() { Epg.updateEpg(res.guid); }, delay);
};

Epg.updateEpg = function (guid) {
	var url = Config.serverUrl + "/epg.xml?id=" + guid;
	
	if (Epg.guidErrors[guid] >2) {
//		Main.logToServer("WARNING in Epg.updateEpg: guid= " + guid + " uses mode=nodesc" );
		url = url +"&mode=nodesc";
	}
	Main.log("Epg.updateEpg: Prep for guid= " + guid + " with ErrorCount= " + Epg.guidErrors[guid] +" and url= " + url);
	$.ajax({
        type: "GET",
        async: true,
        url: url,
        success: Epg.parseResponse,
		error: Epg.handleError
    });	
};

Epg.handleError = function (XHR, textStatus, errorThrown) {
	
	Epg.guidErrors[Epg.curGuid] += 1;
//	Main.log("EPG.updateEPG Error ("+XHR.status +": " +XHR.responseText+")EPG.curGuid= "+Epg.curGuid +" Epg.guidErrors[Epg.curGuid]= " +Epg.guidErrors[Epg.curGuid]);
//	Main.logToServer("EPG.updateEPG Error Response("+ XHR.status + ") EPG.curGuid= "+Epg.curGuid +" Epg.guidErrors[Epg.curGuid]= " +Epg.guidErrors[Epg.curGuid]);

	Epg.isActive = false;
//	Main.log("--------------------------------------------");
	if (Epg.guidErrors[Epg.curGuid] < 6) {
		Epg.startEpgUpdating();
	}
	else {
		Epg.insertFakeEntry(Epg.curGuid);
		Main.logToServer("ERROR in EPG.updateEPG: insertingFakeEntry for guid= " + Epg.curGuid + " (" + Epg.guidTitle[Epg.curGuid] +") with Epg.guidErrors= " +Epg.guidErrors[Epg.curGuid]);
		Epg.startEpgUpdating();
//		Display.showPopup("EPG.updateEPG: stop updating !!!<br>Please file a bug report!<br>Bitte eine Fehlermeldung posten");
	}
	return;
};

Epg.insertFakeEntry = function (guid) {
//	Main.logToServer("Epg.insertFakeEntry for guid= " + guid + " (" + Epg.guidErrors[guid] +")");
	var now = Display.GetEpochTime();

	entry={};
	entry.prog = "Unknown";
	entry.desc = "Empty";
	entry.start = now;
	entry.dur = 120;

	Data.updateEpg(guid, entry);
};

Epg.parseResponse = function (message,text, XHR) {

	$(message).find("programme").each(function(){
		var guid = $(this).find("guid").text();
		if (guid != Epg.curGuid) {
			Main.logToServer("ERROR in Epg.parseResponse : Guid (="+ guid + ") != Epg.curGuid (=" + Epg.curGuid+ ") " );
		}
		
		Epg.guidErrors[Epg.curGuid] = 0;
		
		entry={};
		entry.prog = $(this).find("title").text();
		entry.desc = $(this).find("desc").text();
		entry.start = parseInt($(this).find("start").text());
		entry.eventid= parseInt($(this).find("eventid").text());
		entry.dur = (parseInt($(this).find("end").text()) - parseInt($(this).find("start").text()));

		Main.log("Epg.parseResponse : Guid= "+ guid + " title= " + entry.prog+ " start= " +entry.start + " dur= " + entry.dur);
//		Main.logToServer("Epg.parseResponse : Guid= "+ guid + " title= " + entry.prog+ " start= " +entry.start + " dur= " + entry.dur);

		Data.updateEpg(guid, entry);
		
		//trigger Display refresh
		if (Data.getCurrentItem().childs[Main.selectedVideo].payload.guid == guid) {
			// the updated record is either playing or in Menu
			if (Player.state != Player.STOPPED) {
				Main.logToServer("Updating Progress Bar");
				Display.updateOlForLive (entry.start, entry.dur, Display.GetEpochTime()); // Updates the progress bar
			}
			else {
				Main.logToServer("Updating Right Half");
				Display.handleDescription(Main.selectedVideo);
			}
		}

		Epg.lastGuid = guid;
		Epg.isActive = false;
//		Main.log("--------------------------------------------");
		Epg.startEpgUpdating();
	});
};

Epg.getCurrentEvent4Rec = function (guid) {
	var url = Config.serverUrl + "/epg.xml?id=" + guid;
	
	Main.log("Epg.getCurrentEpg: guid= " + guid + " and url= " + url);
	$.ajax({
        type: "GET",
        async: true,
        url: url,
        success: function(data, status, XHR ) {
        	$(message).find("programme").each(function(){
        		var guid = $(this).find("guid").text();
        		if (guid != Epg.curGuid) {
        			Main.logToServer("ERROR in Epg.parseResponse : Guid (="+ guid + ") != Epg.curGuid (=" + Epg.curGuid+ ") " );
        		}
        		
        		entry={};
        		entry.prog = $(this).find("title").text();
        		entry.desc = $(this).find("desc").text();
        		entry.start = parseInt($(this).find("start").text());
        		entry.eventid= parseInt($(this).find("eventid").text());
        		entry.dur = (parseInt($(this).find("end").text()) - parseInt($(this).find("start").text()));

        		Main.log("Epg.getCurrentEpg : Guid= "+ guid + " title= " + entry.prog+ " start= " +entry.start + " dur= " + entry.dur);

        		// thlo: here I should invoke the rec command
        		// then I should switch to recording playback (new state?).
        		
        	});
        	
        },
		error: function (jqXHR, status, error) {
			
		}
    });	
};