function_helper.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767
  1. <?php
  2. /*
  3. ** 通用函数helper
  4. */
  5. //移除空参数
  6. function remove_null_params($data = array()) {
  7. foreach ($data as $key => $value) {
  8. if ($value === NULL || $value === '') {
  9. unset($data[$key]);
  10. }
  11. }
  12. return $data;
  13. }
  14. // 通过pm2.5获取aqi的值
  15. function get_aqi_and_level_by_pm25($pm25){
  16. if ($pm25 < 35) { // 很好
  17. $aqi = 50 / 35 * $pm25;
  18. }elseif ($pm25 >= 35 && $pm25 < 75) { // 好
  19. $aqi = 50 / 40 * ($pm25 - 35) + 50;
  20. }elseif ($pm25 >= 75 && $pm25 < 115) { // 一般
  21. $aqi = 50 / 40 * ($pm25 - 75) + 100;
  22. }elseif ($pm25 >= 115 && $pm25 < 150) { // 较差
  23. $aqi = 50 / 35 * ($pm25 - 115) + 150;
  24. }elseif ($pm25 >= 150 && $pm25 < 250) { // 差
  25. $aqi = 100 / 100 * ($pm25 - 150) + 200;
  26. }elseif ($pm25 >= 250 && $pm25 < 350) { // 很差
  27. $aqi = 100 / 100 * ($pm25 - 250) + 300;
  28. }elseif ($pm25 >= 350) { // 非常差
  29. $aqi = 100 / 150 * ($pm25 - 350) + 400;
  30. }
  31. $level = get_aqi_str($aqi);
  32. return array('aqi'=>round($aqi),'level'=>$level);
  33. }
  34. function get_aqi_str($aqi){
  35. if ($aqi < 50) { // 很好
  36. $level = 'Very good';
  37. }elseif ($aqi >= 50 && $aqi < 100) { // 好
  38. $level = 'Good';
  39. }elseif ($aqi >= 100 && $aqi < 150) { // 一般
  40. $level = 'Commonly';
  41. }elseif ($aqi >= 150 && $aqi < 200) { // 较差
  42. $level = 'Poor';
  43. }elseif ($aqi >= 200 && $aqi < 300) { // 差
  44. $level = 'Difference';
  45. }elseif ($aqi >= 300 && $aqi < 400) { // 很差
  46. $level = 'Very bad';
  47. }elseif ($aqi >= 400) { // 非常差
  48. $level = 'Very bad';
  49. }
  50. return $level;
  51. }
  52. // modbus crc 16 校验码计算
  53. function crc16($string) {
  54. $crc = 0xFFFF;
  55. for ($x = 0; $x < strlen ($string); $x++) {
  56. $crc = $crc ^ ord($string[$x]);
  57. for ($y = 0; $y < 8; $y++) {
  58. if (($crc & 0x0001) == 0x0001) {
  59. $crc = (($crc >> 1) ^ 0xA001);
  60. } else { $crc = $crc >> 1; }
  61. }
  62. }
  63. $crc = strval(base_convert($crc, 10, 16));
  64. $crc = substr('0000'.$crc, -4);
  65. return substr($crc, -2).substr($crc, 0,2);
  66. }
  67. function crc_result($cmd){
  68. $begin = pack('H*',substr($cmd, 0,2));
  69. for ($i=0; $i < intval(strlen($cmd) / 2) - 1; $i++) {
  70. $begin = $begin ^ pack('H*',substr($cmd, $i*2+2,2));
  71. }
  72. $begin = unpack('H*', $begin);
  73. return substr('00'.$begin[1], -2);
  74. }
  75. function mqttCmd($sendTopic,$backTopic,$sendData,$timeout = 5,$seq = ''){
  76. require_once './application/libraries/Mqtt.php';
  77. // 订阅信息,接收一个信息后退出
  78. $CI =& get_instance();
  79. $server = $CI->config->item('WIFIMqttServer');
  80. $port = $CI->config->item('WIFIMqttPort');
  81. $username = $CI->config->item('WIFIMqttUsername');
  82. $password = $CI->config->item('WIFIMqttPassword');
  83. $client_id = time().rand(100000,999999); // 设置你的连接客户端id
  84. $mqtt = new Mqtt($server, $port, $client_id);
  85. if($mqtt->connect(true, NULL, $username, $password)) { //链接不成功再重复执行监听连接
  86. $mqtt->publish($sendTopic, $sendData, 0);
  87. // $mqtt->publish('/WEGW/ReadIn/865860046894466', pack('H*','010AA00B'), 2);
  88. }else{
  89. return '';
  90. }
  91. $topics[$backTopic] = array("qos" => 0,"function" => "procmsg");
  92. // $topics['/WEGW/ReadOut/865860046894466'] = array("qos" => 2, "function" => "procmsg");
  93. // 订阅主题
  94. $mqtt->subscribe($topics, 0);
  95. $true = 1;
  96. $t = time();
  97. while($true == 1){
  98. $true = $mqtt->proc();
  99. if (!empty($seq) && $true != 1){
  100. $res = unpack('H*', $true['msg']);
  101. if (substr($res[1], 0,4) != strtolower($seq) && substr(substr($res[1], 0,4), -2).substr(substr($res[1], 0,4), 0,2) != strtolower($seq)) $true = 1;
  102. }
  103. if (time() - $t >= $timeout) {
  104. return '';
  105. }
  106. }
  107. $mqtt->close();
  108. return $true;
  109. }
  110. function get_seq(){
  111. $seq = rand(0,100000000);
  112. $seq = base_convert($seq, 10, 16);
  113. return substr('0000'.$seq, -4);
  114. }
  115. function get_check_sum($data){
  116. $sum = 0;
  117. for ($i=0; $i < strlen($data)/2; $i++) {
  118. $sum += base_convert(substr($data, $i*2,2), 16, 10);
  119. }
  120. $sum = base_convert($sum, 10, 16);
  121. $sum = substr('00'.$sum, -2);
  122. return $sum;
  123. }
  124. // 设置时区
  125. function set_timezone($date,$timezone){
  126. $time = strtotime($date) + $timezone*3600;
  127. return date('Y-m-d H:i:s',$time);
  128. }
  129. //小时数
  130. function secToTime($times){
  131. $result = '00';
  132. if ($times>0) {
  133. $hour = floor($times/3600);
  134. $minute = floor(($times-3600 * $hour)/60);
  135. $second = floor((($times-3600 * $hour) - 60 * $minute) % 60);
  136. $result = $hour;
  137. }
  138. return $result;
  139. }
  140. // 发送邮件
  141. function send_email($to,$msg,$title = '维修通知'){
  142. // $this->load->library('Smtp');
  143. require_once FCPATH.'application/libraries/Smtp.php';
  144. //******************** 配置信息 ********************************
  145. $CI =& get_instance();
  146. $config = $CI->config->config['email'];
  147. $smtpserver = $config['smtp_host'];//SMTP服务器
  148. $smtpserverport = $config['smtp_port'];//SMTP服务器端口
  149. $smtpusermail = $config['user_email'];//SMTP服务器的用户邮箱
  150. $smtpemailto = $to;//发送给谁
  151. $smtpuser = $config['smtp_user'];//SMTP服务器的用户帐号,注:部分邮箱只需@前面的用户名
  152. $smtppass = $config['smtp_pass'];//SMTP服务器的用户密码
  153. $mailtitle = $title;//邮件主题
  154. $mailcontent = $msg;//邮件内容
  155. $mailtype = "HTML";//邮件格式(HTML/TXT),TXT为文本邮件
  156. //************************ 配置信息 ****************************
  157. $smtp = new Smtp($smtpserver,$smtpserverport,true,$smtpuser,$smtppass);//这里面的一个true是表示使用身份验证,否则不使用身份验证.
  158. $smtp->debug = false;//是否显示发送的调试信息
  159. return $smtp->sendmail($smtpemailto, $smtpusermail, $mailtitle, $mailcontent, $mailtype);
  160. }
  161. /**
  162. * 获取密码安全等级
  163. * @param string $password 密码
  164. * @return int 0:低,1:中,2:高
  165. */
  166. function get_password_level($password){
  167. if(preg_match('/^([0-9]{6,16})$/',$password)){
  168. return 0;
  169. } else if (preg_match('/^[0-9 a-z]{6,16}$/',$password)){
  170. return 1;
  171. } else if (preg_match('/^[0-9 a-z A-Z !@#$%^&*]{6,16}$/',$password)) {
  172. return 2;
  173. }
  174. return 0;
  175. }
  176. // 天气信息翻译
  177. function weather_translate_en($code){
  178. $data = array(
  179. '100' => 'Sunny/Clear',
  180. '101' => 'Cloudy',
  181. '102' => 'Few Clouds',
  182. '103' => 'Partly Cloudy',
  183. '104' => 'Overcast',
  184. '200' => 'Windy',
  185. '201' => 'Calm',
  186. '202' => 'Light Breeze',
  187. '203' => 'Moderate/Gentle Breeze',
  188. '204' => 'Fresh Breeze',
  189. '205' => 'Strong Breeze',
  190. '206' => 'High Wind, Near Gale',
  191. '207' => 'Gale',
  192. '208' => 'Strong Gale',
  193. '209' => 'Storm',
  194. '210' => 'Violent Storm',
  195. '211' => 'Hurricane',
  196. '212' => 'Tornado',
  197. '213' => 'Tropical Storm',
  198. '300' => 'Shower Rain',
  199. '301' => 'Heavy Shower Rain',
  200. '302' => 'Thundershower',
  201. '303' => 'Heavy Thunderstorm',
  202. '304' => 'Thundershower with hail',
  203. '305' => 'Light Rain',
  204. '306' => 'Moderate Rain',
  205. '307' => 'Heavy Rain',
  206. '308' => 'Extreme Rain',
  207. '309' => 'Drizzle Rain',
  208. '310' => 'Storm',
  209. '311' => 'Heavy Storm',
  210. '312' => 'Severe Storm',
  211. '313' => 'Freezing Rain',
  212. '314' => 'Light to moderate rain',
  213. '315' => 'Moderate to heavy rain',
  214. '316' => 'Heavy rain to storm',
  215. '317' => 'Storm to heavy storm',
  216. '318' => 'Heavy to severe storm',
  217. '399' => 'Rain',
  218. '400' => 'Light Snow',
  219. '401' => 'Moderate Snow',
  220. '402' => 'Heavy Snow',
  221. '403' => 'Snowstorm',
  222. '404' => 'Sleet',
  223. '405' => 'Rain And Snow',
  224. '406' => 'Shower Snow',
  225. '407' => 'Snow Flurry',
  226. '408' => 'Light to moderate snow',
  227. '409' => 'Moderate to heavy snow',
  228. '410' => 'Heavy snow to snowstorm',
  229. '499' => 'Snow',
  230. '500' => 'Mist',
  231. '501' => 'Foggy',
  232. '502' => 'Haze',
  233. '503' => 'Sand',
  234. '504' => 'Dust',
  235. '507' => 'Duststorm',
  236. '508' => 'Sandstorm',
  237. '509' => 'Dense fog',
  238. '510' => 'Strong fog',
  239. '511' => 'Moderate haze',
  240. '512' => 'Heavy haze',
  241. '513' => 'Severe haze',
  242. '514' => 'Heavy fog',
  243. '515' => 'Extra heavy fog',
  244. '900' => 'Hot',
  245. '901' => 'Cold',
  246. '999' => 'Unknown',
  247. );
  248. return isset($data[$code]) ? $data[$code] : '';
  249. }
  250. function weather_translate_cn($code){
  251. $data = array(
  252. '100' => '晴',
  253. '101' => '多云',
  254. '102' => '少云',
  255. '103' => '晴间多云',
  256. '104' => '阴',
  257. '200' => '有风',
  258. '201' => '平静',
  259. '202' => '微风',
  260. '203' => '和风',
  261. '204' => '清风',
  262. '205' => '强风/劲风',
  263. '206' => '疾风',
  264. '207' => '大风',
  265. '208' => '烈风',
  266. '209' => '风暴',
  267. '210' => '狂爆风',
  268. '211' => '飓风',
  269. '212' => '龙卷风',
  270. '213' => '热带风暴',
  271. '300' => '阵雨',
  272. '301' => '强阵雨',
  273. '302' => '雷阵雨',
  274. '303' => '强雷阵雨',
  275. '304' => '雷阵雨伴有冰雹',
  276. '305' => '小雨',
  277. '306' => '中雨',
  278. '307' => '大雨',
  279. '308' => '极端降雨',
  280. '309' => '毛毛雨/细雨',
  281. '310' => '暴雨',
  282. '311' => '大暴雨',
  283. '312' => '特大暴雨',
  284. '313' => '冻雨',
  285. '314' => '小到中雨',
  286. '315' => '中到大雨',
  287. '316' => '大到暴雨',
  288. '317' => '暴雨到大暴雨',
  289. '318' => '大暴雨到特大暴雨',
  290. '399' => '雨',
  291. '400' => '小雪',
  292. '401' => '中雪',
  293. '402' => '大雪',
  294. '403' => '暴雪',
  295. '404' => '雨夹雪',
  296. '405' => '雨雪天气',
  297. '406' => '阵雨夹雪',
  298. '407' => '阵雪',
  299. '408' => '小到中雪',
  300. '409' => '中到大雪',
  301. '410' => '大到暴雪',
  302. '499' => '雪',
  303. '500' => '薄雾',
  304. '501' => '雾',
  305. '502' => '霾',
  306. '503' => '扬沙',
  307. '504' => '浮尘',
  308. '507' => '沙尘暴',
  309. '508' => '强沙尘暴',
  310. '509' => '浓雾',
  311. '510' => '强浓雾',
  312. '511' => '中度霾',
  313. '512' => '重度霾',
  314. '513' => '严重霾',
  315. '514' => '大雾',
  316. '515' => '特强浓雾',
  317. '900' => '热',
  318. '901' => '冷',
  319. '999' => '未知',
  320. );
  321. return isset($data[$code]) ? $data[$code] : '';
  322. }
  323. function weather_translate_ru($code){
  324. $data = array(
  325. '100' => 'чинг',
  326. '101' => 'облачно',
  327. '102' => 'Меньше облако',
  328. '103' => 'Ясная и облачная',
  329. '104' => 'инь',
  330. '200' => 'дует',
  331. '201' => 'спокойствие',
  332. '202' => 'ветерок',
  333. '203' => 'зефир',
  334. '204' => 'Свежий ветер',
  335. '205' => 'Сильный ветер/сильный ветер',
  336. '206' => 'шквал',
  337. '207' => 'ветер',
  338. '208' => 'шторм',
  339. '209' => 'шторм',
  340. '210' => 'Сильный ветер',
  341. '211' => 'ураган',
  342. '212' => 'Торнадо',
  343. '213' => 'Тропический шторм',
  344. '300' => 'ливень',
  345. '301' => 'Сильный душ',
  346. '302' => 'Грозовой дождь',
  347. '303' => 'Сильный грозовой дождь',
  348. '304' => 'Ливень сопровождается градом',
  349. '305' => 'дождик',
  350. '306' => 'Средний дождь',
  351. '307' => 'дождь',
  352. '308' => 'Экстремальный дождь',
  353. '309' => 'Дождик/мелкий дождь',
  354. '310' => 'Проливной дождь',
  355. '311' => 'Сильный дождь',
  356. '312' => 'Сильный ливень',
  357. '313' => 'Ледяной дождь',
  358. '314' => 'Немного дождя',
  359. '315' => 'Под дождём',
  360. '316' => 'Большой до грозы',
  361. '317' => 'От грозы до грозы',
  362. '318' => 'Сильные ливни, сильные ливни',
  363. '399' => 'дождь',
  364. '400' => 'Мелкий снежок',
  365. '401' => 'средний снег',
  366. '402' => 'снегопад',
  367. '403' => 'Снежная буря',
  368. '404' => 'Дождь и снег',
  369. '405' => 'Дождливая и снежная погода',
  370. '406' => 'Дождь и снег',
  371. '407' => 'снежный',
  372. '408' => 'Маленький до снега',
  373. '409' => 'Прямо в снег',
  374. '410' => 'От большой до снежной бури',
  375. '499' => 'снег',
  376. '500' => 'дымка',
  377. '501' => 'туман',
  378. '502' => 'мгла',
  379. '503' => 'янша',
  380. '504' => 'пыль',
  381. '507' => 'Песчаная буря',
  382. '508' => 'Сильная песчаная буря',
  383. '509' => 'Густой туман',
  384. '510' => 'Сильный густой туман',
  385. '511' => 'Умеренный туман',
  386. '512' => 'Тяжелая смола',
  387. '513' => 'Туман',
  388. '514' => 'Сильный туман',
  389. '515' => 'Густой туман',
  390. '900' => 'горячая',
  391. '901' => 'холодно',
  392. '999' => 'неизвестный',
  393. );
  394. return isset($data[$code]) ? $data[$code] : '';
  395. }
  396. //输出json
  397. function json_result($code, $msg, $data = array(),$resType = 0) {
  398. $data = empty($data) && $resType == 0? new stdClass() : $data;
  399. $res = array('code' => $code,'msg' => $msg,'data' => $data);
  400. $CI =& get_instance();
  401. $clientId = $CI->input->post('clientId',true);
  402. if (!empty($clientId)) {
  403. if (is_array($res['data']) && !isset($res['data']['status'])) {
  404. $res['data']['status'] = 2;
  405. }else{
  406. $res['data'] = array('status'=>2);
  407. }
  408. exit(send_websocket($clientId,$res));
  409. }else{
  410. echo json_encode($res);
  411. }
  412. }
  413. // 获取jwt的token值
  414. function get_jwt_token(){
  415. $CI =& get_instance();
  416. $CI->load->library('Jwt');
  417. $payload=array("iss"=>"chirpstack-application-server","aud"=>"chirpstack-application-server","nbf"=>time()-60, "exp"=>time()+7200,'sub'=>'user','username'=>'admin');
  418. return $CI->jwt->getToken($payload);
  419. }
  420. //发送命令到server
  421. function send_cmd($cmd, $timeout = 30, $ms_timeout = 200) {
  422. $CI =& get_instance();
  423. $host = $CI->config->item('cmd_server');
  424. $ip = $host['ip'];
  425. $port = $host['port'];
  426. $client = @stream_socket_client("tcp://{$ip}:{$port}", $errno, $errstr, 30);
  427. if ($client) {
  428. fwrite($client, $cmd);
  429. stream_set_timeout($client, $timeout, $ms_timeout);
  430. $result = fread($client, 8196);
  431. fclose($client);
  432. return $result;
  433. } else {
  434. return false;
  435. }
  436. }
  437. /**
  438. * 模拟post进行url请求
  439. * @param string $url
  440. * @param string $param
  441. */
  442. function http_post($url = '', $data = '',$header=array(),$timeout = 30) {
  443. $tuCurl = curl_init();
  444. //参数
  445. curl_setopt($tuCurl, CURLOPT_URL,$url);
  446. curl_setopt($tuCurl, CURLOPT_POSTFIELDS, $data);
  447. curl_setopt($tuCurl, CURLOPT_SSL_VERIFYPEER, false);
  448. // curl_setopt($tuCurl, CURLOPT_SSL_VERIFYHOST, false);
  449. curl_setopt($tuCurl, CURLOPT_SSLVERSION, 1);
  450. curl_setopt($tuCurl, CURLOPT_RETURNTRANSFER, 1);
  451. curl_setopt($tuCurl, CURLOPT_TIMEOUT, $timeout);
  452. if (!empty($header)) {
  453. curl_setopt($tuCurl, CURLOPT_HTTPHEADER, $header);
  454. }
  455. $tuData = curl_exec($tuCurl);
  456. curl_close($tuCurl);
  457. return $tuData;
  458. }
  459. // 文件下载
  460. function file_download($filePath){
  461. $file_name = basename($filePath); //下载文件名
  462. //检查文件是否存在
  463. if (! file_exists ( $filePath )) {
  464. exit (json_result('0012','找不到文件',array()));
  465. } else {
  466. //打开文件
  467. $file = fopen ( $filePath, "r" );
  468. //输入文件标签
  469. Header ( "Content-type: application/octet-stream" );
  470. Header ( "Accept-Ranges: bytes" );
  471. Header ( "Accept-Length: " . filesize ( $filePath) );
  472. Header ( "Content-Disposition: attachment; filename=" . $file_name );
  473. //输出文件内容
  474. //读取文件内容并直接输出到浏览器
  475. echo fread ( $file, filesize ( $filePath ) );
  476. fclose ( $file );
  477. exit ();
  478. }
  479. }
  480. /**
  481. * 模拟get进行url请求
  482. * @param string $url
  483. */
  484. function http_get($url,$header = array()) {
  485. $ch = curl_init();
  486. curl_setopt($ch, CURLOPT_URL, $url);
  487. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  488. curl_setopt($ch, CURLOPT_HEADER, 0);
  489. if (!empty($header)) {
  490. curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
  491. }
  492. // curl_setopt($ch, CURLOPT_TIMEOUT_MS, 1);
  493. $result = curl_exec($ch);
  494. curl_close($ch);
  495. return $result;
  496. }
  497. //截取字符串
  498. function cut_str($string, $length, $append=true) {
  499. if(strlen($string) <= $length ) {
  500. return $string;
  501. } else {
  502. $i = 0;
  503. while ($i < $length) {
  504. $stringTMP = substr($string,$i,1);
  505. if ( ord($stringTMP) >=224 ) {
  506. $stringTMP = substr($string,$i,3);
  507. $i = $i + 3;
  508. } elseif( ord($stringTMP) >=192 ) {
  509. $stringTMP = substr($string,$i,2);
  510. $i = $i + 2;
  511. } else {
  512. $i = $i + 1;
  513. }
  514. $stringLast[] = $stringTMP;
  515. }
  516. $stringLast = implode("",$stringLast);
  517. if($append) {
  518. $stringLast .= "...";
  519. }
  520. return $stringLast;
  521. }
  522. }
  523. //填充数组不存在的键值对
  524. function fillUnsetData($array, $length) {
  525. for ($i=1; $i <= $length; $i++) {
  526. if (!isset($array[$i])) {
  527. $array[$i] = '';
  528. }
  529. }
  530. return $array;
  531. }
  532. //查找数组第一个元素的key
  533. function findFirstKey($array) {
  534. foreach ($array as $k => $v){
  535. return $k;
  536. }
  537. }
  538. function doAsyncRequest($url, $param=array()){
  539. $urlinfo = parse_url($url);
  540. $host = $urlinfo['host'];
  541. $path = $urlinfo['path'];
  542. $query = isset($param)? http_build_query($param) : '';
  543. $port = 80;
  544. $errno = 0;
  545. $errstr = '';
  546. $timeout = 10;
  547. $fp = fsockopen($host, $port, $errno, $errstr, $timeout);
  548. $out = "POST ".$path." HTTP/1.1\r\n";
  549. $out .= "host:".$host."\r\n";
  550. $out .= "content-length:".strlen($query)."\r\n";
  551. $out .= "content-type:application/x-www-form-urlencoded\r\n";
  552. $out .= "connection:close\r\n\r\n";
  553. $out .= $query;
  554. fputs($fp, $out);
  555. fclose($fp);
  556. }
  557. function send_websocket($clientid,$data = array(),$resType='cmd')
  558. {
  559. if (!empty($clientid)) {
  560. $CI =& get_instance();
  561. $CI->db->insert('message',['client'=>$clientid,'msg'=>json_encode($data),'type'=>'cmd']);
  562. }
  563. }
  564. //token生成算法
  565. function generate_token($username, $password, $client_key) {
  566. $CI =& get_instance();
  567. $salt = $CI->config->config['encryption_key'];
  568. return md5($username.$password.$client_key.$salt);
  569. // return md5($username.$password.date('yyyy').date('mm').$client_key.$salt);
  570. }
  571. //获取汉字拼音的第一个字母
  572. function get_first_char($str) {
  573. $firstchar_ord = ord(strtoupper($str{0}));
  574. if (($firstchar_ord >= 65 and $firstchar_ord <= 91) or ($firstchar_ord >= 48 and $firstchar_ord <= 57)) return $str{0};
  575. $s = iconv("UTF-8", "GBK//TRANSLIT//IGNORE", $str);
  576. $asc = ord($s{0}) * 256 + ord($s{1}) - 65536;
  577. if ($asc >= - 20319 and $asc <= - 20284) return "A";
  578. if ($asc >= - 20283 and $asc <= - 19776) return "B";
  579. if ($asc >= - 19775 and $asc <= - 19219) return "C";
  580. if ($asc >= - 19218 and $asc <= - 18711) return "D";
  581. if ($asc >= - 18710 and $asc <= - 18527) return "E";
  582. if ($asc >= - 18526 and $asc <= - 18240) return "F";
  583. if ($asc >= - 18239 and $asc <= - 17923) return "G";
  584. if ($asc >= - 17922 and $asc <= - 17418) return "H";
  585. if ($asc >= - 17417 and $asc <= - 16475) return "J";
  586. if ($asc >= - 16474 and $asc <= - 16213) return "K";
  587. if ($asc >= - 16212 and $asc <= - 15641) return "L";
  588. if ($asc >= - 15640 and $asc <= - 15166) return "M";
  589. if ($asc >= - 15165 and $asc <= - 14923) return "N";
  590. if ($asc >= - 14922 and $asc <= - 14915) return "O";
  591. if ($asc >= - 14914 and $asc <= - 14631) return "P";
  592. if ($asc >= - 14630 and $asc <= - 14150) return "Q";
  593. if ($asc >= - 14149 and $asc <= - 14091) return "R";
  594. if ($asc >= - 14090 and $asc <= - 13319) return "S";
  595. if ($asc >= - 13318 and $asc <= - 12839) return "T";
  596. if ($asc >= - 12838 and $asc <= - 12557) return "W";
  597. if ($asc >= - 12556 and $asc <= - 11848) return "X";
  598. if ($asc >= - 11847 and $asc <= - 11056) return "Y";
  599. if ($asc >= - 11055 and $asc <= - 10247) return "Z";
  600. return '~';
  601. }
  602. //翻译错误
  603. function transfer_error_tips($msg) {
  604. if(strpos($msg,'project not existed') !== false) {
  605. return '项目不存在';
  606. } else if(strpos($msg,'all of networks in this project are not online') !== false) {
  607. return '当前项目下所有网络都不在线';
  608. } else if(strpos($msg,'error check_code') !== false) {
  609. return '路灯发生未知错误';
  610. } else if(strpos($msg,'cmd not existed') !== false) {
  611. return '未知命令';
  612. } else if(strpos($msg,'address not correct') !== false) {
  613. return '无线模块地址不正确';
  614. } else if(strpos($msg,'current address not online') !== false) {
  615. return '无线模块不在线';
  616. } else if(strpos($msg,'time out no response') !== false) {
  617. return '超时未响应';
  618. } else if(strpos($msg,'PDU error') !== false) {
  619. return 'PDU错误';
  620. } else if(strpos($msg,'not online') !== false) {
  621. return '网络不在线';
  622. } else if(strpos($msg,'send command timeout') !== false) {
  623. return '下发指令超时';
  624. } else if(strpos($msg,'The file you are attempting to upload is larger than the permitted size.') !== false) {
  625. return '上传文件过大';
  626. } else if(strpos($msg,'The filetype you are attempting to upload is not allowed.') !== false) {
  627. return '上传文件类型错误';
  628. }else if($msg == '') {
  629. return '未知错误';
  630. } else {
  631. return '未知错误';
  632. }
  633. }
  634. //俄语翻译错误
  635. function transfer_error_tips_ru($msg) {
  636. if(strpos($msg,'project not existed') !== false) {
  637. return 'Проект не существует';
  638. } else if(strpos($msg,'all of networks in this project are not online') !== false) {
  639. return 'Все сети в рамках текущего проекта не подключены';
  640. } else if(strpos($msg,'error check_code') !== false) {
  641. return 'Неизвестная ошибка уличных фонарей';
  642. } else if(strpos($msg,'cmd not existed') !== false) {
  643. return 'Неизвестная команда';
  644. } else if(strpos($msg,'address not correct') !== false) {
  645. return 'Неверный адрес модуля';
  646. } else if(strpos($msg,'current address not online') !== false) {
  647. return 'беспроводной модуль не подключен';
  648. } else if(strpos($msg,'time out no response') !== false) {
  649. return 'задержка без ответа';
  650. } else if(strpos($msg,'PDU error') !== false) {
  651. return 'ошибка PDU';
  652. } else if(strpos($msg,'not online') !== false) {
  653. return 'сеть не работает';
  654. } else if(strpos($msg,'send command timeout') !== false) {
  655. return 'время ожидания следующей команды';
  656. } else if(strpos($msg,'The file you are attempting to upload is larger than the permitted size.') !== false) {
  657. return 'загружать слишком большой файл';
  658. } else if(strpos($msg,'The filetype you are attempting to upload is not allowed.') !== false) {
  659. return 'Ошибка загрузки типа файла';
  660. }else if($msg == '') {
  661. return 'Неизвестная ошибка';
  662. } else {
  663. return 'Неизвестная ошибка';
  664. }
  665. }
  666. /**
  667. * 把返回的数据集转换成Tree
  668. * @param array $list 要转换的数据集
  669. * @param string $pid parent标记字段
  670. * @param string $level level标记字段
  671. * @return array
  672. * @author 麦当苗儿 <zuojiazi@vip.qq.com>
  673. */
  674. function list_to_tree($list, $pk='id', $pid = 'pid', $child = '_child', $root = 0) {
  675. // 创建Tree
  676. $tree = array();
  677. if(is_array($list)) {
  678. // 创建基于主键的数组引用
  679. $refer = array();
  680. foreach ($list as $key => $data) {
  681. $refer[$data[$pk]] =& $list[$key];
  682. }
  683. foreach ($list as $key => $data) {
  684. // 判断是否存在parent
  685. $parentId = $data[$pid];
  686. if ($root == $parentId) {
  687. $tree[] =& $list[$key];
  688. }else{
  689. if (isset($refer[$parentId])) {
  690. $parent =& $refer[$parentId];
  691. $parent[$child][] =& $list[$key];
  692. }
  693. }
  694. }
  695. }
  696. return $tree;
  697. }
  698. function getTree($data, $pId)
  699. {
  700. $tree = [];
  701. foreach($data as $k => $v)
  702. {
  703. if($v['pid'] == $pId)
  704. {
  705. $v['subList'] = getTree($data, $v['id']);
  706. $tree[] = $v;
  707. unset($data[$k]);
  708. }
  709. }
  710. return $tree;
  711. }
  712. // 编号设置
  713. function set_number($n){
  714. $n = intval($n);
  715. $res = $n + 1000000;
  716. return substr($res, 1);
  717. }
  718. ?>