
//웹소켓 오프젝트 생성..
var webSocket = new WebSocket("ws://61.108.209.27:7902");


webSocket.onopen    = function(message) { wsOpen(message);}
webSocket.onmessage = function(message) { wsOnMessage(message);}
webSocket.onclose   = function(message) { wsClose(message);}
webSocket.onerror   = function(message) { wsError(message);}

function wsOpen(message) {
	console.log('wsOpen!');
	//wsSendMessage(message);
};

function wsCloseConnection() {
	console.log('close!');
    webSocket.close();
};

function wsSendMessage(message) {
    //webSocket.send(message);
	/*waitForSocketConnection(webSocket, function() {
        webSocket.send(message);
    });*/
};

/*function waitForSocketConnection(socket, callback) {
    setTimeout(function() {
	    if (socket.readyState === 1)
	    {
	        if (callback != null) callback();
	    }
	    else
	    {
	        console.log("wait for connection...")
	        waitForSocketConnection(socket, callback);
	    }
	}, 100 * 5);
}*/

function wsOnMessage(message) {
	//console.log(message);
	if(_Level < 5)
	{
		//데이터 수신
	    var data     = message.data.split('|');
	    var dataType = data[0];
	    var dataCnt  = Number(data[1]);
	    var signal   = null;
	    //var findCnt = 0;
	    
	    if(dataCnt > 0)
	    {
	    	for(var ii = 0; ii < dataCnt; ii++)
	        {
	    		if(data[2+ii] != null)
	    		{
	    			var sigData  = data[2+ii].split(',');
	            	var regionId = sigData[0];
	            	var intNo    = sigData[1];

	            	signal = signalInfo[regionId+"."+intNo];

	            	if (typeof signal == 'undefined')
	            	{
	            		continue;
	            	}

	            	//findCnt++;
	        		var mode;
	        		//var status;
	        	/*    		    	var phase_a1, phase_a2, phase_a3, phase_a4, phase_a5, phase_a6, phase_a7, phase_a8,
	        	    		    		phase_b1, phase_b2, phase_b3, phase_b4, phase_b5, phase_b6, phase_b7, phase_b8;
	        	*/
	        		if(dataType == 'A')
	        		{
	        			if(Number(sigData[6]) != 0 || Number(sigData[6]) != 1) continue;
	        			signal.groupNo  		= Number(sigData[2]);
	        			mode     				= Number(sigData[3]);
	        			signal.ring_a   		= Number(sigData[4]);
	        			signal.ring_b   		= Number(sigData[5]);
	        			signal.commOnOffFlag   	= Number(sigData[6]);
	        	    	signal.intSigCycleCnt   = Number(sigData[7]);
	        	    	signal.intSigCycleLen   = Number(sigData[8]);
	        	/*    		        	phase_a1 = sigData[9];
	        	    		        	phase_a2 = sigData[10];
	        	    		        	phase_a3 = sigData[11];
	        	    		        	phase_a4 = sigData[12];
	        	    		        	phase_a5 = sigData[13];
	        	    		        	phase_a6 = sigData[14];
	        	    		        	phase_a7 = sigData[15];
	        	    		        	phase_a8 = sigData[16];
	        	    		        	phase_b1 = sigData[17];
	        	    		        	phase_b2 = sigData[18];
	        	    		        	phase_b3 = sigData[19];
	        	    		        	phase_b4 = sigData[20];
	        	    		        	phase_b5 = sigData[21];
	        	    		        	phase_b6 = sigData[22];
	        	    		        	phase_b7 = sigData[23];
	        	    		        	phase_b8 = sigData[24];
	        	*/    		        	
	        	    	$('#groupNo_'+regionId+'_'+intNo).empty().html(groupNo);
	        		}
	        		else
	        		{
	        			if(Number(sigData[5]) != 0 || Number(sigData[5]) != 1) continue;
	        			mode            		= Number(sigData[2]);
	        			signal.ring_a   		= Number(sigData[3]);
	        			signal.ring_b   		= Number(sigData[4]);
	        			signal.commOnOffFlag    = Number(sigData[5]);
	        	    	signal.intSigCycleCnt   = Number(sigData[6]);
	        	    	signal.intSigCycleLen   = Number(sigData[7]);
	        		}
	        		
	        		//console.log('dataType : ' + dataType + ' , regionId : ' + regionId + ' , intNo : ' + intNo + ' , ring_a : ' + signal.ring_a + ' , ring_b : ' + signal.ring_b + ' , dataCnt: ' + dataCnt + ' , findCnt: ' + findCnt);
	        		
	        		switch(Number(signal.ring_a))      
	        		{
	        		case  0: signal.aRingDeg = aRingPhaseArr[signal.signalIdx].ring0; break;
	        		case  1: signal.aRingDeg = aRingPhaseArr[signal.signalIdx].ring1; break;
	        		case  2: signal.aRingDeg = aRingPhaseArr[signal.signalIdx].ring2; break;
	        		case  3: signal.aRingDeg = aRingPhaseArr[signal.signalIdx].ring3; break;
	        		case  4: signal.aRingDeg = aRingPhaseArr[signal.signalIdx].ring4; break;
	        		case  5: signal.aRingDeg = aRingPhaseArr[signal.signalIdx].ring5; break;
	        		case  6: signal.aRingDeg = aRingPhaseArr[signal.signalIdx].ring6; break;
	        		case  7: signal.aRingDeg = aRingPhaseArr[signal.signalIdx].ring7; break;
	        		default: signal.aRingDeg = ''; break;
	        		}
	        		switch(Number(signal.ring_b))      
	        		{
	        		case  0: signal.bRingDeg = bRingPhaseArr[signal.signalIdx].ring0; break;
	        		case  1: signal.bRingDeg = bRingPhaseArr[signal.signalIdx].ring1; break;
	        		case  2: signal.bRingDeg = bRingPhaseArr[signal.signalIdx].ring2; break;
	        		case  3: signal.bRingDeg = bRingPhaseArr[signal.signalIdx].ring3; break;
	        		case  4: signal.bRingDeg = bRingPhaseArr[signal.signalIdx].ring4; break;
	        		case  5: signal.bRingDeg = bRingPhaseArr[signal.signalIdx].ring5; break;
	        		case  6: signal.bRingDeg = bRingPhaseArr[signal.signalIdx].ring6; break;
	        		case  7: signal.bRingDeg = bRingPhaseArr[signal.signalIdx].ring7; break;
	        		default: signal.bRingDeg = ''; break;
	        		}
	        		
	        		signal.setPhase(signal.aRingDeg, 
	        						signal.bRingDeg, 
	        				        signal.regionCd, 
	        				        signal.regionId,
	        				        signal.regionNm,
	        				        signal.intNo,
	        				        signal.intNm,
	        				        signal.intSigCycleCnt, 
	        				        signal.intSigCycleLen, 
	        				        signal.commOnOffFlag, 
	        				        signal.groupNo, 
	        				        signal.mapNo,
	        				        signal.mainIntNo, 
	        				        signal.sigunguCd, 
	        				        signal.intLcType, 
	        				        signal.intLampType,
	        				        signal.intMainphase);
	        	
	        		//제어기 클릭 이벤트 시그널인포 팝업창 적용..
	        		if(siginfoPhaseDetailWin != null) siginfoPhaseDetailWin.getSignalInfo('sigPop');
	        		
	        		$('#iframeBottomList').contents().find('#bottomInfo #contrlrOperModeCd_'+regionId+'_'+intNo).empty().html(contrlrOperModeCdFnc(mode));
	        		$('#iframeBottomList').contents().find('#bottomInfo #aRingPhaseVal_'+regionId+'_'+intNo).text((Number(signal.ring_a) + 1));
	        		$('#iframeBottomList').contents().find('#bottomInfo #bRingPhaseVal_'+regionId+'_'+intNo).text((Number(signal.ring_b) + 1));
	        		$('#iframeBottomList').contents().find('#bottomInfo #commOnOffFlag_'+regionId+'_'+intNo).empty().html('<img src="/images/comm_'+signal.commOnOffFlag+'.png" />');
	        		$('#iframeBottomList').contents().find('#bottomInfo #intSigCycleCnt_'+regionId+'_'+intNo).text(signal.intSigCycleCnt);
	        		$('#iframeBottomList').contents().find('#bottomInfo #intSigCycleLen_'+regionId+'_'+intNo).text(signal.intSigCycleLen);
	    		}
	        }
	    }
	}
}

function wsClose(message) {
   console.log("Disconnect ... ");
   //wsSendMessage('ws')
}

function wsError(message) {
    //console.log("Error ... ");
	console.log(message);
}