Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var Stimeout, Itimeout; // таймеры для обновления времени и изображения
- var Rtimeout; // таймер запросов
- const Stimeout_rate = 1000, Itimeout_rate = 30000, Rtimeout_rate = 1500;
- var ImageSrc=""; // адрес изображения
- var oldDate = 0; // дата/время на момент последнего обновления (в мс)
- var MskTime, SidTime; // московское и звездное время
- var PanelType;
- const paramsURL = "http://tb.sao.ru/cgi-bin/eddy/bta_print.cgi";
- const MeteoURL = "http://tb.sao.ru/cgi-bin/eddy/tempmon";
- const windURL = "http://ztcs.sao.ru/meteo/wind.png";
- const BTAURL = "http://tb.sao.ru/tcs/ctrl/bta_img.cgi?size=200&mode=";
- function $(Id){
- return document.getElementById(Id);
- }
- function _(Name){
- return document.getElementsByTagName(Name);
- }
- function init(panelName){
- MskTime = new Date();
- SidTime = new Date();
- PanelType = panelName;
- if(panelName == "BTA")
- ImageSrc = "http://tb.sao.ru/webcam/webcam_sky_1_maxi.jpeg";
- else if(panelName == "meteo")
- ImageSrc = "http://zserv.sao.ru/webcam/webcam_zserv_2_maxi.jpeg";
- sendR();
- startTimers();
- }
- function refreshTimer(){
- var d = new Date();
- var diff = d.getTime() - oldDate;
- //oldDate = d.getTime();
- clearTimeout(Stimeout);
- $('Date').innerHTML = formatDate(MskTime);
- if(typeof(M_time) != "undefined"){
- //MskTime.setTime(MskTime.getTime() + diff);
- //$('Mtime').innerHTML = formatTime(MskTime);
- d.setTime(MskTime.getTime() + diff);
- $('Mtime').innerHTML = formatTime(d);
- }
- if(typeof(S_time) != "undefined"){
- //SidTime.setTime(SidTime.getTime() + diff);
- //$('Stime').innerHTML = formatTime(SidTime);
- d.setTime(SidTime.getTime() + diff);
- $('Stime').innerHTML = formatTime(d);
- }
- Stimeout = setTimeout(refreshTimer, Stimeout_rate);
- delete(d);
- }
- function sendR(){
- var request;
- sendRequest(request, paramsURL, "", parceReqStr);
- }
- function refreshImage(){
- var yesterday = oldDate/1000 - 86400;
- var imH = (window.innerHeight-$('header').offsetHeight-20)/4-20;
- if(imH < 100) imH = 100;
- function refreshIMG(id, URL){
- /*var oldIMG = $(id);
- var newIMG = document.createElement("img");
- var parent = oldIMG.parentNode;
- newIMG.style.width = "100%";
- newIMG.src = URL;
- parent.insertBefore(newIMG, oldIMG);
- oldIMG.src = null;
- parent.removeChild(oldIMG);
- delete(oldIMG);
- newIMG.id = id;*/
- $(id).src = URL;
- }
- var params = MeteoURL + "?Tstart=" + yesterday + "&Stat=31&height="+imH + "&Gtype=0&Graph=";
- clearTimeout(Itimeout);
- refreshIMG('BigIMG', ImageSrc + "?" + Math.random());
- if(PanelType == "meteo"){
- // временно вместо облаков и качества изображения пишем давление и влажность
- refreshIMG('clouds', params + "16");
- refreshIMG('temp', params + "7");
- refreshIMG('wind', params + "8");
- refreshIMG('seeing', params + "32");
- refreshIMG('winddir',windURL+ "?" + Math.random());
- }
- else if(PanelType == "BTA"){
- refreshIMG('A', BTAURL + "1&" + Math.random());
- refreshIMG('Z', BTAURL + "2&" + Math.random());
- refreshIMG('P2', BTAURL + "3&" + Math.random());
- }
- Itimeout = setTimeout(refreshImage, Itimeout_rate);
- }
- function handleError(msg) {
- $('reqmsgs').innerHTML = "<b>Ошибка xmlhttprequest:<b>" + msg;
- var tmo = setTimeout(function(){$('reqmsgs').innerHTML = "";}, 5000);
- }
- function sendRequest(request, CGI_PATH, req_STR, fn_OK){
- var timeout_id, str;
- if(window.XMLHttpRequest){
- request = new XMLHttpRequest();
- if(request.overrideMimeType)
- request.overrideMimeType("text/plain;");
- }else{
- if(window.ActiveXObject){
- var sign_a = ['MSXML2.XMLHTTP.6.0',
- 'MSXML2.XMLHTTP.5.0',
- 'MSXML2.XMLHTTP.4.0',
- 'MSXML2.XMLHTTP.3.0',
- 'MSXML2.XMLHTTP',
- 'Microsoft.XMLHTTP'];
- for(var i=0, l=sign_a.length; i<l; i++)
- try{
- request = new ActiveXObject(sign_a[i]);
- i = l;
- }catch(e){}
- }
- }
- if(!request){
- handleError("Браузер не поддерживает WEB-запросы!");
- return;
- }
- request.onreadystatechange=function(){
- if(request.readyState == 4){
- if(request.status == 200){
- clearTimeout(timeout_id);
- fn_OK(request);
- }
- else{
- clearTimeout(timeout_id);
- if(request.status)
- handleError(request.statusText);
- else handleError("Ошибка передачи данных");
- }
- }
- }
- request.open("POST", CGI_PATH, true);
- request.send(req_STR);
- timeout_id = setTimeout(function(){
- request.onreadystatechange = null;request.abort();handleError("Time over");}, 5000);
- }
- function setDate(date, dateStr){
- var tm = dateStr.split(':');
- if(tm.length != 3) return false;
- date.setSeconds(tm[2]);
- date.setMinutes(tm[1]);
- date.setHours(tm[0]);
- return true
- }
- function _2(num){
- var A = num.toString();
- if(A.length > 1) return A;
- else return ("00" + A).slice(-2);
- }
- function formatDate(date){
- if(date.toLocaleFormat) return date.toLocaleFormat("%d.%m.%Y");
- else
- return _2(date.getDate())+"."+_2(1+date.getMonth())+"."+date.getFullYear();
- }
- function formatTime(date){
- if(date.toLocaleFormat) return date.toLocaleFormat("%H:%M:%S");
- else
- return _2(date.getHours())+":"+_2(date.getMinutes())+":"+_2(date.getSeconds());
- }
- function parceReqStr(request){
- var i, l, lines, pair, parms, el;
- clearTimeout(Stimeout);
- clearTimeout(Rtimeout);
- lines = request.responseText.split('\n');
- for(i=0, l=lines.length; i<l; i++)
- if(lines[i].search('=') != -1){
- eval(lines[i]);
- parms = lines[i].split('=');
- if(el = $(parms[0])) el.innerHTML = eval(parms[0]);
- }
- var d = new Date();
- oldDate = d.getTime();
- delete(d);
- $('Date').innerHTML = formatDate(MskTime);
- if(typeof(M_time) != "undefined"){
- if(setDate(MskTime, M_time))
- $('Mtime').innerHTML = formatTime(MskTime);
- }
- if(typeof(S_time) != "undefined"){
- if(setDate(SidTime, S_time))
- $('Stime').innerHTML = formatTime(SidTime);
- }
- Stimeout = setTimeout(refreshTimer, Stimeout_rate);
- Rtimeout = setTimeout(sendR, Rtimeout_rate);
- function showVal(elId, val, bad, good){
- $(elId).innerHTML = Number(val).toFixed(1);
- var classname, absval = Math.abs(val);
- if(absval >= bad) classname = "Red";
- else if(absval < good) classname = "Green";
- else classname = "Yellow";
- $(elId).className = classname + " border";
- }
- if(PanelType == "meteo"){
- showVal("tempdiff", ValTout - ValTmir, 10, 5);
- showVal("windspeed", ValWind, 15, 10);
- if(Blast10 == " ") $('Blast10').innerHTML = "нет";
- if(Blast15 == " ") $('Blast15').innerHTML = "нет";
- }
- else if(PanelType == "BTA"){
- $('ACS_BTA').className = "border "+((ACS_BTA!="On")?"Yellow":"Green");
- $('P2_Mode').className = "border "+((P2_Mode=="Stop")?"Yellow":"Green");
- }
- }
- function stopTimers(){
- clearTimeout(Stimeout);
- clearTimeout(Itimeout);
- clearTimeout(Rtimeout);
- $('stop').innerHTML="Запустить обновление";
- $('stop').onclick=startTimers;
- }
- function startTimers(){
- $('stop').innerHTML="Остановить обновление";
- $('stop').onclick=stopTimers;
- var d = new Date();
- oldDate = d.getTime();
- delete(d);
- refreshImage();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement