Tryag File Manager
Home
||
Turbo Force
||
B-F Config_Cpanel
Current Path :
/
home
/
egg.eemo.co.kr
/
public_html
/
super5
/
Or
Select Your Path :
Upload File :
New :
File
Dir
/home/egg.eemo.co.kr/public_html/super5/test_sock7.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 = 9091; // 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_once("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,4096,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; $key2 = array_search($read_sock, $clients_wifis); $query="insert into iot_err_log set traid='-2', iot_raw_no='0', farm_no='', wifi_device_id='$key2', sensor_id='', err_code='', is_chk='0', status='중계기 접속끊어짐', regdate=now() "; mysql_query($query) or die(mysql_error()); } //$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"; $db=mysql_connect("localhost","root","web!@#") or die(mysql_error()); mysql_select_db("egg") or die(mysql_error()); if (!empty($data)) { $arr_err_code=array(); $arr_err_code['A']="온습도 센서 통신 불량"; $arr_err_code['B']="아날로그 센서 연결 안됨"; $arr_err_code['C']="배터리 저전압"; $arr_err_code['D']="SD 카드 상태 이상"; $arr_err_code['E']="중계기 무선 모듈 불량"; echo " receive data : {$data} , ".date('Y-m-d H:i:s').", from sock id : $read_sock\n\n\n"; $json_dec=json_decode($data); //echo $json_dec->TRAID."\n"; /* if (!array_key_exists($json_dec->REPID, $clients_wifis)) { $clients_wifis[$json_dec->REPID]=$read_sock; } else { $clients_wifis[$json_dec->REPID]=$read_sock; } */ if($json_dec->REPID) { $clients_wifis[$json_dec->REPID]=$read_sock; } //$rs_wifi=RS("select * from wifi_device where vcode='{$json_dec->REPID}' "); $rs_sens=RS("select * from farm_sensor where vcode='{$json_dec->LOGID}' "); $rs_wifi=RS("select * from wifi_device where vcode='{$rs_sens[wifi_vcode]}' "); $rs_dong=RS("select * from farm_dong_info where no='{$rs_sens[dong_no]}' "); $rs_farm=RS("select * from farm where no='{$rs_sens[farm_no]}' "); if(!$rs_farm[no]) { $rs_wifi=RS("select * from wifi_device where vcode='{$json_dec->REPID}' "); $rs_farm=RS("select * from farm where no='{$rs_wifi[farm_no]}' "); } $div_hh=substr($json_dec->TIME,0,2); $div_ii=substr($json_dec->TIME,2,2); $div_ss=substr($json_dec->TIME,4,2); //$jtime=date('Y-m-d')." ".$div_hh.":".$div_ii.":".$div_ss; //$jtime=date('Y-m-d H:i:s'); if(intval(date('H')) < intval($div_hh)) { $jtime=date('Y-m-d',strtotime("-1 days"))." ".$div_hh.":".$div_ii.":".$div_ss; $jdate=date('Y-m-d',strtotime("-1 days")); } else { $jdate=date('Y-m-d'); $jtime=date('Y-m-d')." ".$div_hh.":".$div_ii.":".$div_ss; } //echo "select * from farm_sensor where vcode='{$json_dec->LOGID}' \n"; //echo "select * from wifi_device where vcode='{$rs_sens[wifi_vcode]}' \n"; //echo "select * from farm_dong_info where no='{$rs_sens[dong_no]}' \n"; //echo "select * from farm where no='{$rs_sens[farm_no]}' \n"; //echo "farm_code : ".$rs_wifi[farm_code]."\n"; if($json_dec->TIME=='') { continue; } $query="insert into iot_raw set traid='{$json_dec->TRAID}', repid='{$json_dec->REPID}', logid='{$json_dec->LOGID}', tstamp='{$json_dec->TIME}', bat='{$json_dec->BAT}', rssi='{$json_dec->RSSI}', snr='{$json_dec->SNR}', power='{$json_dec->POWER}', temp='{$json_dec->TEMP}', humi='{$json_dec->HUMI}', adc_data='{$json_dec->ADC}', firm='{$json_dec->FIRM}', err_code='{$json_dec->ERR}', farm_code='{$rs_farm[farm_code]}', farm_no='{$rs_farm[no]}', wifi_code='{$rs_sens[wifi_vcode]}', wifi_no='{$rs_wifi[no]}', dong_code='{$rs_dong[dong_code]}', dong_no='{$rs_dong[no]}', sensor_code='{$rs_sens[sensor_code]}', sensor_no='{$rs_sens[no]}', sensor_type='{$rs_sens[sensor_type]}', blackout_data='{$json_dec->POWER}', jtime='$jtime', regdate=now() "; mysql_query($query) or die(mysql_error()); $iot_raw_no=mysql_insert_id(); //echo $query."\n"; $rs_tot=RS("select avg(temp) as tem , avg(humi) as hum , avg(adc_data) as anl, max(bat) as vbat from iot_raw where sensor_no='{$rs_sens[no]}' and date(jtime)='$jdate' and LPAD(hour(jtime),2,'0')='$div_hh' "); $rs_hour=RS("select * from iot_hour where farm_no='{$rs_farm[no]}' and dong_no='{$rs_dong[no]}' and sensor_no='{$rs_sens[no]}' and jdate='$jdate' and jhour='$div_hh' "); if(!$rs_hour[no]) { $query="insert into iot_hour set farm_no='{$rs_farm[no]}', dong_no='{$rs_dong[no]}', sensor_no='{$rs_sens[no]}', temp='{$rs_tot[tem]}', humi='{$rs_tot[hum]}', anal='{$rs_tot[anl]}', bat='{$rs_tot[vbat]}', jdate='$jdate', jhour='$div_hh', uptime=now() "; if($rs_farm[no]) { mysql_query($query) or die(mysql_error()); } } else { $query="update iot_hour set temp='{$rs_tot[tem]}', humi='{$rs_tot[hum]}', anal='{$rs_tot[anl]}', bat='{$rs_tot[vbat]}', uptime=now() where no='{$rs_hour[no]}' "; mysql_query($query) or die(mysql_error()); } $rs_tot2=RS("select avg(temp) as tem , avg(humi) as hum , avg(adc_data) as anl, max(bat) as vbat from iot_raw where sensor_no='{$rs_sens[no]}' and date(jtime)='$jdate' "); $rs_date=RS("select * from iot_date where farm_no='{$rs_farm[no]}' and dong_no='{$rs_dong[no]}' and sensor_no='{$rs_sens[no]}' and jdate='$jdate' "); if(!$rs_date[no]) { $query="insert into iot_date set farm_no='{$rs_farm[no]}', dong_no='{$rs_dong[no]}', sensor_no='{$rs_sens[no]}', temp='{$rs_tot2[tem]}', humi='{$rs_tot2[hum]}', anal='{$rs_tot2[anl]}', bat='{$rs_tot2[vbat]}', jdate='$jdate', uptime=now() "; if($rs_farm[no]) { mysql_query($query) or die(mysql_error()); } } else { $query="update iot_date set temp='{$rs_tot2[tem]}', humi='{$rs_tot2[hum]}', anal='{$rs_tot2[anl]}', bat='{$rs_tot2[vbat]}', uptime=now() where no='{$rs_date[no]}' "; mysql_query($query) or die(mysql_error()); } $logfile="/home/nonga.eemo.co.kr/public_html/www/sock_log/".date('Y-m-d_H').".txt"; $fp=fopen($logfile,"a+"); $qrystr="\nENCOODE : ".$data. " [".date('Y-m-d H:i:s')."]\nQUERY : \ntraid='{$json_dec->TRAID}',repid='{$json_dec->REPID}',logid='{$json_dec->LOGID}',tstamp='{$json_dec->TIME}',bat='{$json_dec->BAT}',rssi='{$json_dec->RSSI}',snr='{$json_dec->SNR}',power='{$json_dec->POWER}',temp='{$json_dec->TEMP}',humi='{$json_dec->HUMI}',adc_data='{$json_dec->ADC}',firm='{$json_dec->FIRM}',err_code='{$json_dec->ERR}',farm_code='{$rs_farm[farm_code]}',farm_no='{$rs_farm[no]}',wifi_code='{$rs_wifi[wifi_code]}',wifi_no='{$rs_wifi[no]}',dong_code='{$rs_dong[dong_code]}',dong_no='{$rs_dong[no]}',sensor_code='{$rs_sens[sensor_code]}',sensor_no='{$rs_sens[no]}',sensor_type='{$rs_sens[sensor_type]}',blackout_data='{$json_dec->POWER}'\n\n"; fwrite($fp,$qrystr); fclose($fp); if($json_dec->TRAID) { if($json_dec->TRAID=="-") { //초기 장비 설치시 $query="insert into iot_err_log set traid='{$json_dec->TRAID}', iot_raw_no='$iot_raw_no', farm_no='{$rs_farm[no]}', wifi_device_id='{$json_dec->REPID}', sensor_id='{$json_dec->LOGID}', err_code='{$json_dec->ERR}', is_chk='0', status='중계기에서 수집할 로거가 결정안된상태', regdate=now() "; mysql_query($query) or die(mysql_error()); } else if($json_dec->ERR) { $status=$arr_err_code[$json_dec->ERR]; $query="insert into iot_err_log set traid='{$json_dec->TRAID}', iot_raw_no='$iot_raw_no', farm_no='{$rs_farm[no]}', wifi_device_id='{$json_dec->REPID}', sensor_id='{$json_dec->LOGID}', err_code='{$json_dec->ERR}', is_chk='0', status='$status', regdate=now() "; mysql_query($query) or die(mysql_error()); } else { if($rs_sens[sensor_type]==1) { $query="update farm_sensor set now_temper_value='{$json_dec->TEMP}', now_humi_value='{$json_dec->HUMI}', sens_uptime=now() where no='{$rs_sens[no]}' "; mysql_query($query) or die(mysql_error()); } else { $query="update farm_sensor set now_anal_value='{$json_dec->ADC}', sens_uptime=now() where no='{$rs_sens[no]}' "; mysql_query($query) or die(mysql_error()); } } } $rst=array(); $rst['TRAID']=$json_dec->TRAID; $rst['MSG']="OK"; $enc=json_encode($rst); // do sth.. // send some message to listening socket $response_data=$enc; socket_write($read_sock, $response_data, strlen($response_data)); // send this to all the clients in the $clients array (except the first one, which is a listening socket) foreach ($clients as $send_sock) { if ($send_sock == $sock) continue; /*$response_data2="abcdef"; socket_write($send_sock, $response_data2, strlen($response_data2)); echo "we send sock id is {$send_sock}, message : {$response_data2} \n"; */ } // end of broadcast foreach $result_chk=mysql_query("select * from wifi_device where chk_send='1' ") or die(); while($rs_chk=mysql_fetch_array($result_chk)) { $arr_logerid=array(); $result_ss=mysql_query("select * from farm_sensor where wifi_vcode='{$rs_chk[vcode]}' order by vcode asc ") or die(); while($rs_ss=mysql_fetch_array($result_ss)) { $arr_logerid[]=$rs_ss[vcode]; } if(sizeof($arr_logerid) > 0) { $str_logerid=implode(",",$arr_logerid); foreach($clients_wifis as $sk => $s_sock) { if($sk==$rs_chk[vcode]) { $rst=array(); $rst['LOGID']=$str_logerid; $enc=json_encode($rst); echo "socket send : ".$enc." sock id : ".$s_sock." , ".date('Y-m-d H:i:s')."\n\n"; $response_data=$enc; socket_write($s_sock, $response_data, strlen($response_data)); mysql_query("update wifi_device set chk_send='0', chk_send_okdate=now() where no='{$rs_chk[no]}' ") or die(); $enc2=addslashes($enc); $query="insert into iot_send_log set farm_no='{$rs_chk[farm_no]}', wifi_device_id='{$rs_chk[vcode]}', write_msg='$enc2', status='중계기에게 로거 수집 변경요청', regdate=now() "; mysql_query($query) or die(mysql_error()); } } } } mysql_close($db); } } // end of reading foreach } // close the listening socket socket_close($sock); ?>