summaryrefslogtreecommitdiff
path: root/smarttv-client/Javascript/Config.js
blob: ad0196f9617aa57891dbc09e9729936e7ce8c111 (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
228
229
230
231
232
233
234
235
236
var Config = {
	cfgFileName : curWidget.id + "/config.dta",
	XHRObj : null,
	xmlDocument : null,
	serverUrl : "",  // Will become the main URL for contacting the server
	serverAddr : "",
	serverAddrDefault: "192.168.1.122:8000", 
	format :"has",
	tgtBufferBitrate : 6000, // kbps
	totalBufferDuration : 30, // sec
	initialBuffer : 10, // in percent
	pendingBuffer: 40, // in percent
	initialTimeOut: 3, // sec
	skipDuration : 30, // sec
	noLiveChannels : 30, 
	firstLaunch : false
};


/*
 * Check for First launch
 * Launch Config Menu
 * Write File in Confi Menu
 * Come back here, read the file and continue
 */


Config.init = function () {
    var fileSystemObj = new FileSystem();
    
	if (fileSystemObj.isValidCommonPath(curWidget.id) == 0){
//		Display.showPopup ("First Launch of the Widget --> Launching Config Menu");
		alert("First Launch of the Widget");
		// should switch to the config screen here
		var res = fileSystemObj.createCommonDir(curWidget.id);
		if (res == true) {
			Config.firstLaunch = true;

			Main.init();

			Main.changeState(4);
			return;
		}
		else {
			Main.init();
			Display.showPopup ("ERROR: Cannot create widget folder");
			Main.log("ERROR: Cannot create widget folder curWidget.id= " +curWidget.id);
		}
		return;
	}
	else {
		Config.readContext();
	}

	Config.fetchConfig();
};

Config.fetchConfig = function () {
    if (this.XHRObj == null) {
        this.XHRObj = new XMLHttpRequest();
    }
    
    if (this.XHRObj) {

    	this.XHRObj.onreadystatechange = function() {
    		if (Config.XHRObj.readyState == 4) {
    			Config.processConfig();
                }
            };
            
        this.XHRObj.open("GET", this.serverUrl + "/widget.conf", true);
        this.XHRObj.send(null);
    }
};
	
Config.writeContext = function (addr) {
	var fileSystemObj = new FileSystem();

	var fd = fileSystemObj.openCommonFile(Config.cfgFileName,"w");

    fd.writeLine('serverAddr ' + addr); // SHould be overwritten by Options menue
    fileSystemObj.closeCommonFile(fd);
};

Config.updateContext = function (addr) {
	var fileSystemObj = new FileSystem();

	var fd = fileSystemObj.openCommonFile(Config.cfgFileName,"w");

    fd.writeLine('serverAddr ' + addr); // SHould be overwritten by Options menue
    fileSystemObj.closeCommonFile(fd);

    Config.serverAddr = addr;
    Config.serverUrl = "http://" + Config.serverAddr;
};

Config.readContext = function () {
	var fileSystemObj = new FileSystem();

	try {
		var fd = fileSystemObj.openCommonFile(Config.cfgFileName, "r");

		var line = "";
	    
		while (line = fd.readLine()) {
		    var avp = line.split(" ");
//		    Display.showPopup ("Reading Config: attr= " + avp[0] + " val= " + avp[1]);
		    if (avp.length > 1) {
		    	Config.serverAddr = avp[1];
		    	Config.serverUrl = "http://" + Config.serverAddr;
		    }
		    else {
		    	Display.showPopup ("WARNING: Error in Config File. Try widget restart.");    	
		    	// TODO: I should re-write the config file
		    }
		}
		fileSystemObj.closeCommonFile(fd);	
		
	}
	catch (e) {
		Main.log("Config.readContext: Error while reading: e= " +e);
		var res = fileSystemObj.createCommonDir(curWidget.id);
		if (res == true) {
			Main.log("Config.readContext: Widget Folder created");
			Display.showPopup ("Config Read Error:  Try widget restart");  
		}
		else {
			Main.log("Config.readContext: Widget Folder creation failed");
			Display.showPopup ("Config Read Error:  Try re-installing the widget");  
			alert("-------------- Error: res = false ------------------------");			
		}

		Config.firstLaunch = true;
		Main.init();
		Main.changeState(4);
	}
};


Config.getXmlValue = function (itm) {
	var val = this.xmlDocument.getElementsByTagName(itm);
	var res = 0;
	try {
		res = val[0].firstChild.data;
	}
	catch (e) {
		Main.log("parsing widget.conf: Item= " + itm + " not found" + e);
		alert ("parsing widget.conf: Item= " + itm + " not found e= " + e);
	}
	return res;

};

Config.getXmlString = function (itm) {
	var val = this.xmlDocument.getElementsByTagName(itm);
	
	var res = "";
	try {
		res = val[0].firstChild.data;
	}
	catch (e) {
		Main.log("parsing widget.conf: Item= " + itm + " not found" + e);
		alert ("parsing widget.conf: Item= " + itm + " not found e= " + e);
	};

	return res;
};

Config.processConfig = function () {
	if (this.XHRObj.status != 200) {
    	alert ("Config Server Error");  	
    	Display.showPopup("Config Server Error " + this.XHRObj.status);
    	Main.log("Config Server Error " + this.XHRObj.status);
    }
    else {
    	var xmlResponse = this.XHRObj.responseXML;
    	if (xmlResponse == null) {
    		alert ("xml error");
        	Display.showPopup("Error in XML Config File");
        	Main.log("Error in XML Config File");
            return;
    	}
    	this.xmlDocument = xmlResponse.documentElement;
        if (!this.xmlDocument ) {
            alert("Failed to get valid Config XML");
        	Display.showPopup("Failed to get valid Config XML");
        	Main.log("Failed to get valid Config XML");
            return;
        }
        else  {
        	alert ("Paring config XML now");
        	Main.log("Paring config XML now");
        	this.format = Config.getXmlString("format");
        	var res = Config.getXmlValue("tgtBufferBitrate");
        	if (res != 0)
        		this.tgtBufferBitrate = 1.0 * res;
        	res = Config.getXmlValue("totalBufferDuration");
        	if (res != 0) this.totalBufferDuration = 1.0 * res;

        	res= Config.getXmlValue("initialBuffer");
        	if (res != 0) this.initialBuffer = 1.0 * res;

        	res = Config.getXmlValue("pendingBuffer");
        	if (res != 0) this.pendingBuffer = 1.0 * res;

        	res = Config.getXmlValue("skipDuration");
        	if (res != 0) this.skipDuration= 1.0 * res;

        	res = Config.getXmlValue("initialTimeOut");
        	if (res != 0) this.initialTimeOut = 1.0 * res;

        	res = Config.getXmlValue("liveChannels");
        	if (res != 0) noLiveChannels = res;
        	
        	alert("**** Config ****");
        	alert("serverUrl= " + Config.serverUrl);
        	alert("format= " + Config.format);
        	alert("tgtBufferBitrate= " + Config.tgtBufferBitrate);
        	alert("totalBufferDuration= " + Config.totalBufferDuration);
        	alert("initialBuffer= " + Config.initialBuffer);
        	alert("pendingBuffer= " + Config.pendingBuffer);
        	alert("skipDuration= " + Config.skipDuration);
        	alert("initialTimeOut= " + Config.initialTimeOut);
        	alert("**** /Config ****");      	
        };
    	
    };

    Main.init();
};

// This function cleans up after un-installation
Config.reset = function () {
	var fileSystemObj = new FileSystem();
	fileSystemObj.deleteCommonFile(curWidget.id + "/config.dta");
};