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/iot_sock.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 = 19091; // 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("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]); $obj=(object) $c; $obj->sock=$newsock; $obj->jtime=time(); $clients_wifis[]=$obj; } // 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; } //$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)) { $exp=explode(",",$data); $temper=$exp[0]; $humi=$exp[1]; $mac=$exp[2]; $ip=$exp[3]; $rst=array(); $rst['TRAID']=$mac; $rst['MSG']="OK"; $enc=json_encode($rst); echo $enc."\n"; // do sth.. // send some message to listening socket $response_data=$enc; socket_write($read_sock, $response_data, strlen($response_data)); fn_iot($temper,$humi,$mac,$ip); //socket_close($read_sock); // 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 */ /* foreach($clients_wifis as $key => $obj) { if($obj->sock == $read_sock) { socket_close($obj->sock); unset($clients_wifis[$key]); } if($obj->jtime < (time()-60)) { socket_close($obj->sock); unset($clients_wifis[$key]); } } */ mysql_close($db); } } // end of reading foreach } // close the listening socket socket_close($sock); function fn_iot($temper,$humi,$mac,$ip="") { if(!$mac) {return;} $div_hh=date('H'); $jdate=date('Y-m-d'); $query="insert into tmp_nodemcu set sensor_no='$sensor_no', wifi_no='$wifi_no', temp='$temper', badak_temp='$badak_temp', humi='$humi', gas_no2='$gas_no2', gas_co='$gas_co', gas_nh3='$gas_nh3', giap='$giap', lux='$lux', co2='$co2', voc='$voc', mac='$mac', ip='$ip', regdate=now() "; mysql_query($query) or die(mysql_error()); $rs_sens=RS("select * from farm_sensor where mac='$mac' "); $rs_farm=RS("select * from farm where no='{$rs_sens[farm_no]}' "); $rs_dong=RS("select * from farm_dong_info where no='{$rs_sens[dong_no]}' "); if($rs_sens[no]) { $query="insert into iot_raw set traid='', repid='', logid='', tstamp='', bat='', rssi='', snr='', power='1', temp='$temper', humi='$humi', adc_data='', firm='1.0', err_code='', farm_code='', farm_no='{$rs_sens[farm_no]}', wifi_code='', wifi_no='', dong_code='', dong_no='{$rs_sens[dong_no]}', sensor_code='', sensor_no='{$rs_sens[no]}', sensor_type='{$rs_sens[sensor_type]}', blackout_data='1', jtime=now(), regdate=now() "; mysql_query($query) or die(mysql_error()); $iot_raw_no=mysql_insert_id(); $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()); } if($rs_sens[sensor_type]==1) { $query="update farm_sensor set now_temper_value='$temper', now_humi_value='$humi', sens_uptime=now() where no='{$rs_sens[no]}' "; mysql_query($query) or die(mysql_error()); } //알람 $rs_raw=RS("select * from iot_raw where no='$iot_raw_no' "); if($rs_sens[sensor_type]==1) { if($rs_raw[temp] < $rs_sens[set_temper_min_value]) { $title="알람발생"; $cont="<b>".$rs_dong[dong_name]." 알람발생</b><br><br>".$rs_sens[pos_no]." 온도가 ".$rs_raw[temp]." 도로<br> 기준치 ".$rs_sens[set_temper_min_value]." 도 이하로 떨어졌습니다."; $tts_cont=$rs_dong[dong_name]." 알람발생 , ".$rs_sens[pos_no]." 온도가 ".$rs_raw[temp]." 도로 기준치 ".$rs_sens[set_temper_min_value]." 도 이하로 떨어졌습니다 "; $event_msg=$rs_farm[farm_name]." , ".$tts_cont; $mode="warning"; mysql_query("insert into farm_msg_alarm set farm_no='{$rs_dong[farm_no]}' , farm_code='{$rs_farm[farm_code]}', msg_gubun='긴급알림', title='$title', cont='$tts_cont', sensor_type='1', regdate=now() ") or die(mysql_error()); mysql_query("update iot_raw set is_alarm='1', alarm_type='1', alarm_pushed_date=now() where no='{$rs_raw[no]}' ") or die(); mysql_query("insert into control_center_event set farm_no='{$rs_sens[farm_no]}' , dong_no='{$rs_sens[no]}', ch_no='{$rs_ipcam[ch_no]}', event_type='tmp', tmp_value='{$rs_raw[temp]}', hum_value='{$rs_raw[humi]}', blackout_value='{$rs_raw[adc_data]}', event_msg='$event_msg', register_date=now() ") or die(mysql_error()); if($rs_dong[set_alarm]==1 && $rs_farm[set_alarm]==1) { $result_mem=mysql_query("select * from member where farm_no='{$rs_dong[farm_no]}' and fcm_token is not null ") or die(); while($rs_mem=mysql_fetch_array($result_mem)) { $tokens = array(); $tokens[]=$rs_mem[fcm_token]; $istts=$rs_farm[set_alarm_tts]; $is_sound=$rs_farm[set_alarm_tts]==1?"0":$rs_farm[set_alarm_sound]; $isvib=$rs_farm[alarm_vibrate]; $ispopup=$rs_farm[alarm_popup]; $message = array("message" => $cont,"title" => $title,"mode" => $mode,"sound" => $is_sound=="1"?"1":'-1',"tts" => $tts_cont,"istts" => $istts=="1"?"1":'-1',"isvib" => $isvib=="1"?"1":'-1',"ispopup" => $ispopup=="1"?"1":'-1'); $rst=send_notification($tokens, $message); if($rst=="success") { $str_val=implode("&",$tokens); mysql_query("insert into push_send_log set arr_mem_no='{$rs_mem[no]}', title='$title', cont='$cont', tts_cont='$tts_cont', is_tts='$istts', mode='$mode', is_sound='$is_sound', regdate=now(), mode2='2' ") or die(mysql_error()); } else { } } } } if($rs_raw[temp] > $rs_sens[set_temper_max_value]) { $title="알람발생"; $cont="<b>".$rs_dong[dong_name]." 알람발생</b><br><br>".$rs_sens[pos_no]." 온도가 ".$rs_raw[temp]." 도로<br> 기준치 ".$rs_sens[set_temper_max_value]." 도 이상으로 올라갔습니다."; $tts_cont=$rs_dong[dong_name]." 알람발생 , ".$rs_sens[pos_no]." 온도가 ".$rs_raw[temp]." 도로 기준치 ".$rs_sens[set_temper_max_value]." 도 이상으로 올라갔습니다 "; $event_msg=$rs_farm[farm_name]." , ".$tts_cont; $mode="warning"; mysql_query("insert into farm_msg_alarm set farm_no='{$rs_dong[farm_no]}' , farm_code='{$rs_farm[farm_code]}', msg_gubun='긴급알림', title='$title', cont='$tts_cont', sensor_type='1', regdate=now() ") or die(mysql_error()); mysql_query("update iot_raw set is_alarm='1', alarm_type='1', alarm_pushed_date=now() where no='{$rs_raw[no]}' ") or die(); mysql_query("insert into control_center_event set farm_no='{$rs_sens[farm_no]}' , dong_no='{$rs_sens[no]}', ch_no='{$rs_ipcam[ch_no]}', event_type='tmp', tmp_value='{$rs_raw[temp]}', hum_value='{$rs_raw[humi]}', blackout_value='{$rs_raw[adc_data]}', event_msg='$tts_cont', register_date=now() ") or die(mysql_error()); if($rs_dong[set_alarm]==1 && $rs_farm[set_alarm]==1) { $result_mem=mysql_query("select * from member where farm_no='{$rs_dong[farm_no]}' and fcm_token is not null ") or die(); while($rs_mem=mysql_fetch_array($result_mem)) { $tokens = array(); $tokens[]=$rs_mem[fcm_token]; $istts=$rs_farm[set_alarm_tts]; $is_sound=$rs_farm[set_alarm_tts]==1?"0":$rs_farm[set_alarm_sound]; $isvib=$rs_farm[alarm_vibrate]; $ispopup=$rs_farm[alarm_popup]; $message = array("message" => $cont,"title" => $title,"mode" => $mode,"sound" => $is_sound=="1"?"1":'-1',"tts" => $tts_cont,"istts" => $istts=="1"?"1":'-1',"isvib" => $isvib=="1"?"1":'-1',"ispopup" => $ispopup=="1"?"1":'-1'); $rst=send_notification($tokens, $message); if($rst=="success") { $str_val=implode("&",$tokens); mysql_query("insert into push_send_log set arr_mem_no='{$rs_mem[no]}', title='$title', cont='$cont', tts_cont='$tts_cont', is_tts='$istts', mode='$mode', is_sound='$is_sound', regdate=now(), mode2='2' ") or die(mysql_error()); } else { } } } } if($rs_raw[humi] < $rs_sens[set_humi_min_value]) { $title="알람발생"; $cont="<b>".$rs_dong[dong_name]." 알람발생</b><br><br>".$rs_sens[pos_no]." 습도가 ".$rs_raw[humi]." %로<br> 기준치 ".$rs_sens[set_humi_min_value]." % 이하로 떨어졌습니다."; $tts_cont=$rs_dong[dong_name]." 알람발생 , ".$rs_sens[pos_no]." 습도가 ".$rs_raw[humi]." 퍼센트로 기준치 ".$rs_sens[set_humi_min_value]." 퍼센트 이하로 떨어졌습니다 "; $event_msg=$rs_farm[farm_name]." , ".$tts_cont; $mode="warning"; mysql_query("insert into farm_msg_alarm set farm_no='{$rs_dong[farm_no]}' , farm_code='{$rs_farm[farm_code]}', msg_gubun='긴급알림', title='$title', cont='$tts_cont', sensor_type='2', regdate=now() ") or die(mysql_error()); mysql_query("update iot_raw set is_alarm='1', alarm_type='1', alarm_pushed_date=now() where no='{$rs_raw[no]}' ") or die(); mysql_query("insert into control_center_event set farm_no='{$rs_sens[farm_no]}' , dong_no='{$rs_sens[no]}', ch_no='{$rs_ipcam[ch_no]}', event_type='hum', tmp_value='{$rs_raw[temp]}', hum_value='{$rs_raw[humi]}', blackout_value='{$rs_raw[adc_data]}', event_msg='$event_msg', register_date=now() ") or die(mysql_error()); if($rs_dong[set_alarm]==1 && $rs_farm[set_alarm]==1) { $result_mem=mysql_query("select * from member where farm_no='{$rs_dong[farm_no]}' and fcm_token is not null ") or die(); while($rs_mem=mysql_fetch_array($result_mem)) { $tokens = array(); $tokens[]=$rs_mem[fcm_token]; $istts=$rs_farm[set_alarm_tts]; $is_sound=$rs_farm[set_alarm_tts]==1?"0":$rs_farm[set_alarm_sound]; $isvib=$rs_farm[alarm_vibrate]; $ispopup=$rs_farm[alarm_popup]; $message = array("message" => $cont,"title" => $title,"mode" => $mode,"sound" => $is_sound=="1"?"1":'-1',"tts" => $tts_cont,"istts" => $istts=="1"?"1":'-1',"isvib" => $isvib=="1"?"1":'-1',"ispopup" => $ispopup=="1"?"1":'-1'); $rst=send_notification($tokens, $message); if($rst=="success") { $str_val=implode("&",$tokens); mysql_query("insert into push_send_log set arr_mem_no='{$rs_mem[no]}', title='$title', cont='$cont', tts_cont='$tts_cont', is_tts='$istts', mode='$mode', is_sound='$is_sound', regdate=now(), mode2='2' ") or die(mysql_error()); } else { } } } } if($rs_raw[humi] > $rs_sens[set_humi_max_value]) { $title="알람발생"; $cont="<b>".$rs_dong[dong_name]." 알람발생</b><br><br>".$rs_sens[pos_no]." 습도가 ".$rs_raw[humi]." %로<br> 기준치 ".$rs_sens[set_humi_max_value]." % 이상으로 올라갔습니다."; $tts_cont=$rs_dong[dong_name]." 알람발생 , ".$rs_sens[pos_no]." 습도가 ".$rs_raw[humi]." 퍼센트로 기준치 ".$rs_sens[set_humi_max_value]." 퍼센트 이상으로 올라갔습니다 "; $event_msg=$rs_farm[farm_name]." , ".$tts_cont; $mode="warning"; mysql_query("insert into farm_msg_alarm set farm_no='{$rs_dong[farm_no]}' , farm_code='{$rs_farm[farm_code]}', msg_gubun='긴급알림', title='$title', cont='$tts_cont', sensor_type='2', regdate=now() ") or die(mysql_error()); mysql_query("update iot_raw set is_alarm='1', alarm_type='1', alarm_pushed_date=now() where no='{$rs_raw[no]}' ") or die(); mysql_query("insert into control_center_event set farm_no='{$rs_sens[farm_no]}' , dong_no='{$rs_sens[no]}', ch_no='{$rs_ipcam[ch_no]}', event_type='hum', tmp_value='{$rs_raw[temp]}', hum_value='{$rs_raw[humi]}', blackout_value='{$rs_raw[adc_data]}', event_msg='$event_msg', register_date=now() ") or die(mysql_error()); if($rs_dong[set_alarm]==1 && $rs_farm[set_alarm]==1) { $result_mem=mysql_query("select * from member where farm_no='{$rs_dong[farm_no]}' and fcm_token is not null ") or die(); while($rs_mem=mysql_fetch_array($result_mem)) { $tokens = array(); $tokens[]=$rs_mem[fcm_token]; $istts=$rs_farm[set_alarm_tts]; $is_sound=$rs_farm[set_alarm_tts]==1?"0":$rs_farm[set_alarm_sound]; $isvib=$rs_farm[alarm_vibrate]; $ispopup=$rs_farm[alarm_popup]; $message = array("message" => $cont,"title" => $title,"mode" => $mode,"sound" => $is_sound=="1"?"1":'-1',"tts" => $tts_cont,"istts" => $istts=="1"?"1":'-1',"isvib" => $isvib=="1"?"1":'-1',"ispopup" => $ispopup=="1"?"1":'-1'); $rst=send_notification($tokens, $message); if($rst=="success") { $str_val=implode("&",$tokens); mysql_query("insert into push_send_log set arr_mem_no='{$rs_mem[no]}', title='$title', cont='$cont', tts_cont='$tts_cont', is_tts='$istts', mode='$mode', is_sound='$is_sound', regdate=now(), mode2='2' ") or die(mysql_error()); } else { } } } } } //--알람 } } function send_notification($tokens, $message) { $url = 'https://fcm.googleapis.com/fcm/send'; $fields = array( 'registration_ids' => $tokens, 'data' => $message ); $key = "AIzaSyCG6QsJex8nT-CVCMaDQL7vKxclJl3_XwI"; $headers = array( 'Authorization:key =' . $key, 'Content-Type: application/json' ); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields)); $response = curl_exec($ch); curl_close($ch); $json=json_decode($response,true); //NotRegistered = 앱삭제 if($json["success"]) { return "success"; } else { return $response; } } ?>