MS-PDRLKVHCADMG\Administrator 5 tahun lalu
induk
melakukan
c3e4b4cbf0
2 mengubah file dengan 186 tambahan dan 0 penghapusan
  1. 79 0
      sendCmd/idcol_alarm_info_send.php
  2. 107 0
      websocket/idcolWss.php

+ 79 - 0
sendCmd/idcol_alarm_info_send.php

@@ -0,0 +1,79 @@
+<?php
+// idcol 故障信息推送
+require_once './DB.php';
+date_default_timezone_set('Asia/Shanghai');
+
+$config = [
+    'hostname' => 'rm-wz98r5cn33zq4ou980o.mysql.rds.aliyuncs.com',
+	'username' => 'idcol20',
+	'password' => 'idcol@1234',
+	'dbname' => 'idcol',
+];
+
+// 发送邮件
+function send_email($to,$msg,$title = '维修通知'){
+    // $this->load->library('Smtp');
+    require_once './Smtp.php';
+    $config = array();
+    $config['protocol'] = 'smtp';
+	$config['smtp_host'] = 'smtpdm.aliyun.com';
+	$config['user_email'] = 'admin@lampmind.com';
+	$config['smtp_user'] = 'admin@lampmind.com';
+	$config['smtp_pass'] = 'wecloudsSMTP2018';
+	$config['smtp_port'] = 80;
+	$config['smtp_timeout'] = 30;
+	$config['crlf'] = "\r\n";  
+	$config['newline'] = "\r\n";
+	$config['mailtype'] = "html";
+
+    //******************** 配置信息 ********************************
+    // $config = $config['email'];
+    $smtpserver = $config['smtp_host'];//SMTP服务器
+    $smtpserverport = $config['smtp_port'];//SMTP服务器端口
+    $smtpusermail = $config['user_email'];//SMTP服务器的用户邮箱
+    $smtpemailto = $to;//发送给谁
+    $smtpuser = $config['smtp_user'];//SMTP服务器的用户帐号,注:部分邮箱只需@前面的用户名
+    $smtppass = $config['smtp_pass'];//SMTP服务器的用户密码
+    $mailtitle = $title;//邮件主题
+    $mailcontent = $msg;//邮件内容
+    $mailtype = "HTML";//邮件格式(HTML/TXT),TXT为文本邮件
+    //************************ 配置信息 ****************************
+    $smtp = new Smtp($smtpserver,$smtpserverport,true,$smtpuser,$smtppass);//这里面的一个true是表示使用身份验证,否则不使用身份验证.
+    $smtp->debug = false;//是否显示发送的调试信息
+    return $smtp->sendmail($smtpemailto, $smtpusermail, $mailtitle, $mailcontent, $mailtype);
+}
+
+// 故障信息 
+$batstatusArr = array('1'=>'Overdischarge','2'=>'Over Charge','3'=>'High temperature','4'=>'Battery overtemperature');
+$panelstatusArr = array('1'=>'Less than the optiocally controlled voltage','2'=>'greate than the optiocally controlled voltage','3'=>'greater than the battery voltage');
+$lampstatusArr = array('1'=>'open circuit','2'=>'short circuit','3'=>'overload','4'=>'Lamp overtemperature');
+$tempstatusArr = array('1'=>'Charge high temperature','2'=>'Charge over temperature','3'=>'Discharge high temperature','4'=>'Discharge over temperature');
+
+while (1) {
+	$db = new Db($config);
+
+	$lampList = $db->query('select W.id,W.batstatus,W.panelstatus,W.lampstatus,W.tempstatus,L.projectid,P.projectname,L.number from warning_info_log W left join lampinfo L on W.lampid = L.id left join project P on L.projectid = P.id where W.is_send = 0 and L.id is not null and P.id is not null and W.status = 0');
+	foreach ($lampList as $key => $value) {
+		$userList = $db->query('select email from alarm_send_user_info where is_send = 1 and FIND_IN_SET('.$value['projectid'].',project)');
+
+		// $msg = "The device located in project:{$value['hotelName']}->number:{$value['buildName']} appears <span style='color:red;'>{$value['stralarmtype']}</span>, please confirm as soon as possible. If repaired, please ignore it! Thank you for your cooperation! [login www.iotena-web.com to see details]";
+		$alarmArr = array();
+		if (!empty($value['batstatus']) && isset($batstatusArr[$value['batstatus']])) $alarmArr[] = $batstatusArr[$value['batstatus']];
+		if (!empty($value['panelstatus']) && isset($panelstatusArr[$value['panelstatus']])) $alarmArr[] = $panelstatusArr[$value['panelstatus']];
+		if (!empty($value['lampstatus']) && isset($lampstatusArr[$value['lampstatus']])) $alarmArr[] = $lampstatusArr[$value['lampstatus']];
+		if (!empty($value['tempstatus']) && isset($tempstatusArr[$value['tempstatus']])) $alarmArr[] = $tempstatusArr[$value['tempstatus']];
+		if (!empty($alarmArr)) {
+			$stralarmtype = implode(',',$alarmArr);
+			$msg = "The device located in project:{$value['projectname']}->number:{$value['number']} appears <span style='color:red;'>{$stralarmtype}</span>, please confirm as soon as possible. If repaired, please ignore it! Thank you for your cooperation!";
+			foreach ($userList as $user) {
+				if (!empty($user['email'])) send_email($user['email'],$msg,'Alarm info');
+			}
+		}
+
+		$db->table('warning_info_log')->where('id='.$value['id'])->update(['is_send'=>1]);
+	}
+	$db->close();
+	sleep(60);
+}
+
+?>

+ 107 - 0
websocket/idcolWss.php

@@ -3,6 +3,8 @@ use Workerman\Worker;
 require_once __DIR__ . '/Autoloader.php';
 require_once './Lib/mysql/src/Connection.php';
 use \Workerman\Lib\Timer;
+require_once './Lib/MQTT/src/Client.php';
+require_once './Lib/MQTT/src/Protocols/Mqtt.php';
 
 $ws_worker = new Worker("websocket://0.0.0.0:8844");
 
@@ -13,7 +15,55 @@ $ws_worker->onWorkerStart = function($ws_worker)
 {
     // 将db实例存储在全局变量中(也可以存储在某类的静态成员中)
     global $websocketDB;
+    global $mqtt;
+
     $websocketDB = new \Workerman\MySQL\Connection('rm-wz98r5cn33zq4ou980o.mysql.rds.aliyuncs.com', '3306', 'idcol20', 'idcol@1234', 'idcol');
+    global $devList;
+    $devList = array();
+    global $devUpdateList;
+    $devUpdateList = array();
+
+    $options = array(
+        'username'=>'weclouds',
+        'password'=>'weclouds@1234'
+    );
+    $mqtt = new \Workerman\Mqtt\Client('mqtt://streetserver.weclouds.xyz:1883',$options);
+    $mqtt->onConnect = function($mqtt) {
+
+        $room = array(
+            '/IDCOL/CmdInput/#' => 2,
+            '/IDCOL/CmdOutput/#' => 2,
+        );
+
+        $mqtt->subscribe($room, null, function($exception, $granted){});
+
+
+        $mqtt->onMessage = function($room, $message){
+            global $devUpdateList;
+            global $StrategyReport;
+            $idArr = explode('/', $room);
+            $id = $idArr[count($idArr) - 1];
+            $id = strtolower($id);
+            if (strpos($room,'CmdOutput')) {
+                $msg = unpack('H*',$message);
+                if (isset($devUpdateList[$id]) && !empty($devUpdateList[$id])) {
+                    $temp = $devUpdateList[$id];
+                    $type = substr($msg[1], 0,2);
+                    if ($type == '99') {
+                        $status = substr($msg[1],18,2);
+                        $devUpdateList[$id]['status'] = $status;
+                        if ($status == '00') {
+                            $devUpdateList[$id]['type'] = 1;
+                        }elseif ($status == '01') {
+                            $devUpdateList[$id]['type'] = 2;
+                        }
+                    }
+                }
+            }
+        };
+        
+    };
+    $mqtt->connect();
 
     Timer::add(0.5, function()use($ws_worker){
     	// global $clientArr;
@@ -28,6 +78,39 @@ $ws_worker->onWorkerStart = function($ws_worker)
         	}
         }
     });
+
+    // 下发设备升级指令
+    Timer::add(1, function()use($ws_worker){
+        global $mqtt;
+        global $devUpdateList;
+        global $devList;
+        global $websocketDB;
+
+        $temp = $devList;
+        if (!empty($temp)) {
+            foreach ($temp as $v) {
+                $lamp = $websocketDB->query("select manu,address,id from lampinfo where id = ".$v['lamp_id']);
+                if (empty($lamp) || empty($lamp[0]['id'])) {
+                    # code...
+                }
+            }
+        }
+    });
+
+    // 设备升级进度
+    Timer::add(2, function()use($ws_worker){
+        // global $clientArr;
+        // global $websocketDB;
+        // $connections = $ws_worker->connections;
+        // // var_dump($connections);
+        // $data = $websocketDB->select('id,client,msg')->from('message')->query();
+        // if (!empty($data)) {
+        //     foreach ($data as $v) {
+        //         if (!empty($connections[$v['client']])) $connections[$v['client']]->send($v['msg']); 
+        //         $websocketDB->query("DELETE FROM `message` WHERE id='{$v['id']}'");
+        //     }
+        // }
+    });
 };
 
 // 启动1个进程对外提供服务
@@ -44,7 +127,31 @@ $ws_worker->onConnect = function($connection)
     $connection->send(json_encode($data));
     // $clientArr[$key] = $connection;
 };
+$ws_worker->onMessage = function($connection, $data)
+{
+    
+    global $devList;
+    $res = json_decode($data,true);
 
+    if ($res && isset($res['type'])) {
+        if ($res['type'] == 'updateDev' && isset($res['id']) && !empty($res['id'])){
+            $arr = explode(','$res['id']);
+            if (!empty($arr)) {
+                foreach ($arr as $key) {
+                    if (is_int($key) && !empty($key)) {
+                        $devList[] = array('lamp_id'=>intval($key),'client'=>$connection);
+                    }
+                }
+            }else{
+                $connection->close();
+            }
+        }else{
+            $connection->close();
+        }
+    }else{
+        $connection->close();
+    }
+};
 // 客户端断开连接
 // $ws_worker->onClose = function($connection)
 // {