Tryag File Manager
Home
||
Turbo Force
||
B-F Config_Cpanel
Current Path :
/
home
/
egg.eemo.co.kr
/
public_html
/
Or
Select Your Path :
Upload File :
New :
File
Dir
//home/egg.eemo.co.kr/public_html/nvr_sock_bak.php
<? error_reporting(E_ALL && ~E_NOTICE); /* Allow the script to hang around waiting for connections. */ set_time_limit(0); /* Turn on implicit output flushing so we see what we're getting as it comes in. */ ob_implicit_flush(); $address = '0.0.0.0'; $port = 9080; // create a streaming socket, of type TCP/IP $sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP); socket_set_option($sock, SOL_SOCKET, SO_REUSEADDR, 1); socket_bind($sock, $address, $port); socket_listen($sock); // create a list of all the clients that will be connected to us.. // add the listening socket to this list $clients = array($sock); $clients_wifis = array(); include("super5/dbconn.php"); include("super5/lib.php"); while (true) { // create a copy, so $clients doesn't get modified by socket_select() $read = $clients; $write = null; $except = null; // get a list of all the clients that have data to be read from // if there are no clients with data, go to next iteration if (socket_select($read, $write, $except, 1000) < 1) continue; // check if there is a client trying to connect if (in_array($sock, $read)) { $clients[] = $newsock = socket_accept($sock); //socket_write($newsock, "There are ".(count($clients) - 1)." client(s) connected to the server\n"); echo "now ".(count($clients) - 1)." clients connected\n"; socket_getpeername($newsock, $ip, $port); echo "New client connected: ip : {$ip} , port : {$port}\n"; $key = array_search($sock, $read); unset($read[$key]); } // loop through all the clients that have data to read from foreach ($read as $read_sock) { // read until newline or 1024 bytes // socket_read while show errors when the client is disconnected, so silence the error messages //$data = @socket_read($read_sock, 4096); $numBytes = @socket_recv($read_sock,$buffer,1004096,0); echo "bytes : ".$numBytes."\n"; // check if the client is disconnected if ($numBytes == null || $numBytes == 0) { // remove client for $clients array $key = array_search($read_sock, $clients); unset($clients[$key]); echo "client disconnected. sock id : {$read_sock}\n"; socket_close($read_sock); continue; } //$bufferC=substr($buffer, 1, strlen($buffer)-2); //$tmp_b64buff=trim($bufferC); //$tmp_buff=substr($tmp_b64buff,0,8); $data = trim($buffer); echo "data : ".$data."\n"; if (!empty($data)) { $db=mysql_connect("localhost","root","web!@#") or die(mysql_error()); mysql_select_db("egg") or die(mysql_error()); $rs_main_conf=RS("select * from conf_main "); echo "\n\n"; $dec = simplexml_load_string($data); if($dec===false) { $xml = ''; $xml .= "<root>". "<error>wrong xml data</error>". "</root>"; $response_data=$xml; socket_write($read_sock, $response_data, strlen($response_data)); mysql_close($db); echo "\n\n"; echo "SEND:\n\n"; echo $response_data."\n\n"; } else { $vv_transaction_id=$dec->transaction_id; $vv_nvr_serial=$dec->nvr_serial; $vv_ddns_url=$dec->ddns_url; $vv_lpr_num=$dec->lpr_num; $vv_lpr_num_trim=str_replace(" ","",$vv_lpr_num); if(!$vv_transaction_id) { } else { if ( preg_match('/[^\x{1100}-\x{11FF}\x{3130}-\x{318F}\x{AC00}-\x{D7AF}0-9a-zA-Z]/u',$vv_lpr_num_trim) ) { $xml = ''; $xml .= "<root>". "<transaction_id>".$vv_transaction_id."</transaction_id>". "<error>wrong LPR number</error>". "</root>"; $response_data=$xml; socket_write($read_sock, $response_data, strlen($response_data)); mysql_close($db); echo "\n\n"; echo "SEND:\n\n"; echo $response_data."\n\n"; } else { $rs_nvr=RS("select * from farm_nvr where nvr_sn='$vv_nvr_serial' "); $rs_farm=RS("select * from farm where no='{$rs_nvr[farm_no]}' "); $farm_no=$rs_nvr[farm_no]; $nvr_no=$rs_nvr[no]; echo "select * from farm_nvr where nvr_sn='$vv_nvr_serial' \n"; echo "select * from farm where no='{$rs_nvr[farm_no]}' \n"; if(!$rs_nvr[no]) { $xml = ''; $xml .= "<root>". "<transaction_id>".$vv_transaction_id."</transaction_id>". "<error>no register nvr serial</error>". "</root>"; $response_data=$xml; socket_write($read_sock, $response_data, strlen($response_data)); mysql_close($db); echo "\n\n"; echo "SEND:\n\n"; echo $response_data."\n\n"; } else { if($vv_lpr_num) { mysql_query("insert into farm_lpr_log set farm_no='$farm_no', lpr_num='$vv_lpr_num', regdate=now() ") or die(mysql_error()); mysql_query("update farm_nvr set now_lpr_num='$vv_lpr_num' where no='$nvr_no' ") or die(); } else { mysql_query("update farm_nvr set now_lpr_num='' where no='$nvr_no' ") or die(); } foreach($dec->ipcams->children() as $ak => $av) { $vv_ch_no=$av->chan_num; $vv_cam_ip=$av->cam_ip; $rs_ipcam=RS("select * from farm_ipcamera where nvr_no='$nvr_no' and ca_ip='$vv_cam_ip' "); $cam_url=$rs_ipcam[mac].".dynlink.net:554"; $rtsp_url="rtsp://admin:1234@".$rs_ipcam[mac].".dynlink.net:554/main/ch".$vv_ch_no; $hls_url="http://110.93.133.72:1935/live/".$rs_ipcam[mac]."ch".$vv_ch_no.".stream/playlist.m3u8"; //mysql_query("update farm_ipcamera set ch_no='$vv_ch_no', rtsp_url='$rtsp_url', hls_url='$hls_url' where nvr_no='$nvr_no' and ca_ip='$vv_cam_ip' ") or die(mysql_error()); mysql_query("update farm_ipcamera set ca_ip='$vv_cam_ip', rtsp_url='$rtsp_url', hls_url='$hls_url' where nvr_no='$nvr_no' and ch_no='$vv_ch_no' ") or die(mysql_error()); foreach($av->children() as $akk => $avv) { if($avv->sens_no) { $vv_sens_no=$avv->sens_no; $vv_sens_pos_screen_width=$avv->sens_pos->windows_width; $vv_sens_pos_screen_height=$avv->sens_pos->windows_height; $vv_sens_pos_x=$avv->sens_pos->pos_x; $vv_sens_pos_y=$avv->sens_pos->pos_y; //echo "vv_sens_pos_screen_height : ".$vv_sens_pos_screen_height."\n\n"; mysql_query("update farm_sensor set sens_pos_screen_width='$vv_sens_pos_screen_width', sens_pos_screen_height='$vv_sens_pos_screen_height', sens_pos_x='$vv_sens_pos_x', sens_pos_y='$vv_sens_pos_y' where no='$vv_sens_no' ") or die(mysql_error()); } } $rs_cc=RS("select * from farm_nvr_recieve_ipcam where nvr_no='$nvr_no' and ch_no='$vv_ch_no' "); if(!$rs_cc[no]) { mysql_query("insert into farm_nvr_recieve_ipcam set farm_no='$farm_no', nvr_no='$nvr_no', ip='$vv_cam_ip', ch_no='$vv_ch_no', regdate=now() ") or die(mysql_error()); } else { mysql_query("update farm_nvr_recieve_ipcam set ip='$vv_cam_ip' where no='{$rs_cc[no]}' ") or die(mysql_error()); } } //여기부터 send $xml = '<?xml version=\"1.0\" encoding=\"UTF-8\"?>'; $xml .= "<root>". "<transaction_id>".$vv_transaction_id."</transaction_id>". "<farm_name>".$rs_farm[farm_name]."</farm_name>". "<ipcams>"; $result=mysql_query("select * from farm_ipcamera where farm_no='{$rs_nvr[farm_no]}' and nvr_no='{$rs_nvr[no]}' ") or die(mysql_error()); $n=1; $str=""; while($rs=mysql_fetch_array($result)) { $rs_dong=RS("select * from farm_dong_info where farm_no='{$rs_nvr[farm_no]}' and dong_name='{$rs[dong_info]}' "); if($rs_dong[no]) { $vv_title=str_replace(" ","_",$rs[dong_info])."_".$rs[pos]; } else { $vv_title=$rs[pos]; } $str.= "<ipcam>". "<cam_title>".$vv_title."</cam_title>". "<cam_ip>".$rs[ca_ip]."</cam_ip>". "<dong_no>".$rs_dong[no]."</dong_no>". "<dong_name>".$rs[dong_info]."</dong_name>". "<sensors>"; $dd=""; if($rs_dong[no]) { $result_sens=mysql_query("select * from farm_sensor where farm_no='{$rs_nvr[farm_no]}' and dong_no='{$rs_dong[no]}' ") or die(mysql_error()); while($rs_sens=mysql_fetch_array($result_sens)) { if($rs_sens[no]) { $vv_time=time()-40; $rs_iot=RS("select * from iot_raw where sensor_no='{$rs_sens[no]}' and unix_timestamp(regdate) >= '$vv_time' order by no desc limit 0,1 "); //echo "select * from iot_raw where sensor_no='{$rs_sens[no]}' and unix_timestamp(regdate) >= '$vv_time' order by no desc limit 0,1 \n"; $is_al="N"; $al_color="0,255,0"; if($rs_sens[sensor_type]==1) { if($rs_iot[temp] < $rs_sens[set_temper_min_value]) { $is_al="Y"; $al_color="255,0,0"; } if($rs_iot[temp] > $rs_sens[set_temper_max_value]) { $is_al="Y"; $al_color="255,0,0"; } if($rs_iot[humi] < $rs_sens[set_humi_min_value]) { $is_al="Y"; $al_color="255,0,0"; } if($rs_iot[humi] > $rs_sens[set_humi_max_value]) { $is_al="Y"; $al_color="255,0,0"; } } else if($rs_sens[sensor_type]==2) { if($rs_iot[adc_data] < $rs_sens[set_anal_min_value]) { $is_al="Y"; $al_color="255,0,0"; } if($rs_iot[adc_data] > $rs_sens[set_anal_max_value]) { $is_al="Y"; $al_color="255,0,0"; } } /* if($rs_iot[is_alarm]==1) { $is_al="Y"; $al_color="255,0,0"; echo "ALARM : select * from iot_raw where sensor_no='{$rs_sens[no]}' and unix_timestamp(regdate) >= '$vv_time' order by no desc limit 0,1 \n"; } else { $is_al="N"; $al_color="0,255,0"; } */ $rs_nvr_pop=RS("select * from nvr_popup_notice where farm_no='$farm_no' and is_read='0' order by no desc limit 0,1 "); if($rs_nvr_pop[no]) { $cont=$rs_nvr_pop[msg]; if(strstr($cont,"\r\n")) { $cont=str_replace("\r\n"," ",$cont); } else if(strstr($cont,"\n")) { $cont=str_replace("\n"," ",$cont); } $al_pop="Y"; $al_pop_text=$cont."-".$rs_nvr_pop[regdate]; $al_pop_timeout=$rs_nvr_pop[view_sec]; $al_pop_text_style="NORMAL"; mysql_query("update nvr_popup_notice set is_read='1' where no='{$rs_nvr_pop[no]}' ") or die(); } else { $al_pop="N"; $al_pop_text=""; $al_pop_timeout=$rs_nvr_pop[view_sec]; $al_pop_text_style="NORMAL"; } $dd.= "<sensor>". "<sens_no>".$rs_sens[no]."</sens_no>". "<sens_type>".$rs_sens[sensor_type]."</sens_type>". "<sens_temp_val>".$rs_iot[temp]."</sens_temp_val>". "<sens_humi_val>".$rs_iot[humi]."</sens_humi_val>". "<sens_anal_val>".$rs_iot[adc_data]."</sens_anal_val>". "<sens_alarm>".$is_al."</sens_alarm>". "<sens_alarm_type>".$rs_sens[sensor_type]."</sens_alarm_type>". "<sens_color>".$al_color."</sens_color>". "<sens_pos>". "<windows_width>".$rs_sens[sens_pos_screen_width]."</windows_width>". "<windows_height>".$rs_sens[sens_pos_screen_height]."</windows_height>". "<pos_x>".$rs_sens[sens_pos_x]."</pos_x>". "<pos_y>".$rs_sens[sens_pos_y]."</pos_y>". "</sens_pos>". "<alarm_pop>".$al_pop."</alarm_pop>". "<alarm_pop_timeout>".$al_pop_timeout."</alarm_pop_timeout>". "<alarm_pop_text>".$al_pop_text."</alarm_pop_text>". "<alarm_pop_text_style>".$al_pop_text_style."</alarm_pop_text_style>". "</sensor>"; } } } $str.=$dd; /* $br_days="12"; $br_ipchu="35,000"; $br_now_susu="31,200"; $br_now_perc="17%"; $br_now_dote="15"; $br_now_pesa="9"; $br_tot_feed="31,500kg"; $fan_status="정상"; $heater_status="정상"; */ if($rs_dong[no]) { $fan_status="정상"; $heater_status="정상"; $rs_dong_contract=RS("select a.* from farm_contract a , farm_contract_dong b where a.contract_status='1' and a.ltms_status='5' and a.no=b.farm_contract_no and b.farm_dong_info_no='{$rs_dong[no]}' "); $rs_parts=RS("select * from farm_contract_parts where contract_no='{$rs_dong_contract[no]}' order by no desc limit 0,1 "); $rs_ipchu=RS("select min(jdate) as vdate, sum(susu) as cnt from farm_ipchu_confirm_log where parts_no='{$rs_parts[no]}' and dong_no='{$rs_dong[no]}' "); $sdate = new DateTime($rs_ipchu[vdate]); $edate = new DateTime(date('Y-m-d')); $diff = date_diff($sdate, $edate); $br_days=($diff->days+1); // $br_ipchu=$rs_ipchu[cnt]; // $rs_dote_sum=RS("select sum(dote+pesa) as cnt from farm_dote_input_log where parts_no='{$rs_parts[no]}' and dong_no='{$rs_dong[no]}' "); $br_now_susu= ($rs_ipchu[cnt]-$rs_dote_sum[cnt]) ; // $br_now_perc=sprintf("%.1f", ( ($rs_ipchu[cnt]-$rs_dote_sum[cnt]) / $rs_ipchu[cnt] ) * 100); // $rs_today_dote=RS("select * from farm_dote_input_log where parts_no='{$rs_parts[no]}' and dong_no='{$rs_dong[no]}' and jdate=date(now()) "); $br_now_dote=$rs_today_dote[dote]; // $br_now_pesa=$rs_today_dote[pesa]; // $br_tot_feed=0; // } else { $br_days="0"; $br_ipchu="0"; $br_now_susu="0"; $br_now_perc="0"; $br_now_dote="0"; $br_now_pesa="0"; $br_tot_feed="0"; $fan_status=""; $heater_status=""; } $str.= "</sensors>". "<breed>". "<days>".$br_days."</days>". "<ipchu>".$br_ipchu."</ipchu>". "<now_susu>".$br_now_susu."</now_susu>". "<now_perc>".$br_now_perc."</now_perc>". "<now_dote>".$br_now_dote."</now_dote>". "<now_pesa>".$br_now_pesa."</now_pesa>". "<tot_feed>".$br_tot_feed."</tot_feed>". "</breed>". "<fanheat>". "<fan_status>".$fan_status."</fan_status>". "<heater_status>".$heater_status."</heater_status>". "</fanheat>". "</ipcam>"; } $xml.=$str; $xml.= "</ipcams></root>"; //--여기부터 send // do sth.. // send some message to listening socket $response_data=$xml; socket_write($read_sock, $response_data, strlen($response_data)); mysql_close($db); echo "\n\n"; echo "SEND:\n\n"; echo $response_data."\n\n"; } } } } } } // end of reading foreach } // close the listening socket socket_close($sock); ?>