Project

General

Profile

index.html

Владимир Матвеев, 06/04/2020 02:35 PM

Download (7.02 KB)

 
1
<script>
2

3
var config = {
4
        //"updateIfNotContains":        "wrt",
5

6
        "askImageUpdate":                false,
7
        "askBootstrapUpdate":        false,
8
        "redirect":                                "http://mag.smotreshka.tv",
9

10
        "logo": {
11
                "url":                                        "http://wrtech.local/update/WR-330/logo.bmp.gz",
12
                "md5":                                        "91bc87fa0bd1f2ebae21dcb86ab79467"
13
        },
14

15
        "envs": {
16
                "update_url":                                "http://wrtech.local/update/WR-330/imageupdate",
17
                "bootstrap_url":                        "http://wrtech.local/update/WR-330/imageupdate_bootstrap",
18
                "TZ":                                                "Asia/Yekaterinburg",
19
                "ntpurl":                                        "pool.ntp.org",
20
        },
21
        
22
        "iptvPlaylist": [
23
                {"time":"0","name":"Channel 1","type":81,"url":"udp://@239.255.11.1:1234","tsOn":true},
24
                {"time":"0","name":"Channel 2","type":81,"url":"udp://@239.255.11.2:1234","tsOn":true},
25
                {"time":"0","name":"Channel 3","type":81,"url":"udp://@239.255.11.3:1234","tsOn":true}
26
        ]
27
};
28

29
function wrtImageUpdate(args) {
30
        if (location.search.indexOf("skipupdate") == -1) { //если портал не был открыт с опцией пропуска обновления, обновление прошивки
31
                stbUpdate.startCheck(args.url);
32
                if (stbUpdate.getStatus() == 21) { // файл прошивки доступен и заголовок прочитан
33
                        console.log("image is available, header parsed");
34
                        if(typeof(args.imageDate) !== "number") {
35
                                args.imageDate = Date.parse(stbUpdate.getImageDateStr());
36
                                console.log("got date from image: " + args.imageDate);
37
                        }
38

39
                        if (args.curDate < args.imageDate) { //файл на сервере новее, чем текущая прошивка или безусловно
40
                                console.log("got new update");
41
                                WRT.UpdateGui( //запуск обновления
42
                                        args.url, //файл прошивки
43
                                        location.origin + location.pathname + (location.search ? location.search + "&" : "?") + "skipupdate", //адрес для возврата в случае отмены или ошибки скачивания (текущий + skipupdate)
44
                                        args.forced //true - принудительно запустить обновление, false - предварительно спросить
45
                                );
46
                        } else {
47
                                console.log("already updated");
48
                        }
49
                }
50
        } else {
51
                console.log("update is skiped");
52
        }
53
}
54

55
function wrtEnvsUpdate(envs) {
56
        for(key in envs) {
57
                if (typeof(envs[key]) === 'string') {
58
                        console.log("key: " + key + "; old: " + WRT.getEnv(key) + " new: " + envs[key]);
59
                        if(WRT.getEnv(key) !== envs[key]) {
60
                                WRT.setEnv(key, envs[key]);
61
                        }
62
                }
63
        }
64
}
65

66
function wrtLogoUpdate(logo) {
67
        var logoPath = "/usr/share/logo.bmp.gz";
68
        var logoUpdateCmd = ". /etc/env.sh && wget '" + logo.url + "' -O " + logoPath + " && flash_eraseall $LOGO_MTDDEV" +
69
                                " && ( cat $LOGO_HEAD ; zcat " + logoPath + " ) | nandwrite -p $LOGO_MTDDEV";
70

71
        if (typeof(logo.md5) === "string") {
72
                if (typeof(currentLogoMd5) !== "string") {
73
                        currentLogoMd5 = WRT.Exec("md5sum " + logoPath).stdout.replace(/ .*/, "").trim();
74
                }
75

76
                if (currentLogoMd5 !== logo.md5) { //сначала размещяется сам файл лого на сервер, затем изменяется-размещяется config.json
77
                        if(WRT.Exec(logoUpdateCmd).status === "\u0000") {
78
                                console.log("logo updated");
79
                                currentLogoMd5 = logo.md5;
80
                        } else {
81
                                console.log("error writing logo"); //может отсутсвовать лого по ссылке (нет на сервере), может быть неправильно написана сама сслыка (в конфиг файле)
82
                        }
83
                }
84
        } else {
85
                var xmlhttp = new XMLHttpRequest();
86
                xmlhttp.onreadystatechange = function() {
87
                        if (4 == xmlhttp.readyState) {
88
                                if (xmlhttp.status >= 200 && xmlhttp.status < 300) {
89
                                        var ETag = xmlhttp.getResponseHeader('ETag');
90
                                        if(typeof(ETag) !== "string") {
91
                                                ETag = xmlhttp.getResponseHeader('Last-Modified');
92
                                                console.log("no ETag Header, using Last-Modified: " + ETag);
93
                                        }
94
                                        if(typeof(ETag) === "string") {
95
                                                console.log("logo ETag: " + ETag);
96
                                                if (typeof(currentLogoETag) === "string" && currentLogoETag === ETag) {
97
                                                        console.log("got same logo ETag");
98
                                                } else {
99
                                                        console.log("got new logo ETag. current ETag: "
100
                                                                + (typeof(currentLogoETag) === "string" ? currentLogoETag : "undefined"));
101
                                                        if(WRT.Exec(logoUpdateCmd).status === "\u0000") {
102
                                                                console.log("logo updated");
103
                                                                currentLogoETag = ETag;
104
                                                        } else {
105
                                                                console.log("error writing logo");
106
                                                        }
107
                                                }
108
                                        } else {
109
                                                console.log("no ETag Header");
110
                                        }
111
                                } else {
112
                                        console.log("http status cause error: " + xmlhttp.status);
113
                                }
114
                        }
115
                };
116

117
                xmlhttp.timeout = 3000;
118
                xmlhttp.open("HEAD", logo.url, true);
119

120
                console.log("requesting head: " + logo.url);
121
                xmlhttp.send(null);
122
        }
123
}
124

125
function wrtServiceListUpdate(serviceList) {
126
        if(typeof(serviceList) === "object") {
127
                serviceListStr = JSON.stringify(serviceList).trim();
128
                if (typeof(serviceListStrOld) !== "string") {
129
                        serviceListStrOld = gSTB.LoadUserData("services.json").trim();
130
                }
131
                if (serviceListStrOld !== serviceListStr) { //eсли список поменялся
132
                        gSTB.SaveUserData("services.json", serviceListStr); serviceListStrOld = serviceListStr;
133
                        if (location.origin + location.pathname === "file:///opt/minibrowser/services.html") {
134
                                return true; //перезпустить, если встроенный портал был выбран и список поменялся
135
                        }
136
                }
137
        }
138
        return false;
139
}
140

141
function wrtIptvPlaylistUpdate(iptvPlaylist) {
142
        if(typeof(iptvPlaylist) === "object") {
143
                iptvPlaylistStr = JSON.stringify(iptvPlaylist).trim();
144
                if (typeof(iptvPlaylistStrOld) !== "string") {
145
                        iptvPlaylistStrOld = gSTB.LoadUserData("iptv.json").trim();
146
                }
147
                if (iptvPlaylistStrOld !== iptvPlaylistStr) {
148
                        gSTB.SaveUserData("iptv.json", iptvPlaylistStr); iptvPlaylistStrOld = iptvPlaylistStr;
149
                }
150
        }
151
        return false;
152
}
153

154
function wrtConfigApply(config) {
155

156
        var oldPortal = WRT.getEnv("portal1");
157
        wrtEnvsUpdate(config.envs);
158

159
        if (typeof(currentLogoMd5) !== "string") {
160
                wrtLogoUpdate(config.logo);
161
        }
162

163
        var goStart = (WRT.getEnv("portal1") !== oldPortal) && (location.origin+location.pathname === oldPortal);
164
        var goEmbedded = wrtServiceListUpdate(config.serviceList);
165

166
        if(typeof(config.updateIfNotContains) !== "undefined" && WRT.GetSoftwareVersion().search(config.updateIfNotContains) === -1) {
167
                wrtImageUpdate({"url": WRT.getEnv("update_url"), "forced": true});
168
        } else {
169
                wrtImageUpdate({"url": WRT.getEnv("update_url"), "curDate": Date.parse(WRT.GetSoftwareDate()), "forced": !config.askImageUpdate});
170
        }
171

172
        wrtImageUpdate({"url": WRT.getEnv("bootstrap_url"), "curDate": Date.parse(WRT.GetKernelBuildDate()), "forced": !config.askBootstrapUpdate});
173

174
        wrtIptvPlaylistUpdate(config.iptvPlaylist);
175

176
        if(config.reloadPortalOnChange) {
177
                if(goStart) {
178
                        BrowserApi.goStart();
179
                }
180
                if(goEmbedded) {
181
                        BrowserApi.goEmbedded();
182
                }
183
        }
184
        if(typeof(config.redirect) === "string"){
185
                location = config.redirect;
186
        }
187
}
188

189

190
wrtConfigApply(config);
191
</script>