同步mysql
PHP Code复制内容到剪贴板
- <?php
- define('WS_PORT', '9506');//websocket端口号
- define('MYSQL_HOST', 'mysql');
- define('MYSQL_PORT', 3306);
- define('MYSQL_USER', 'root');
- define('MYSQL_PASSWORD', 'm123456,');
- define('MYSQL_DATABASE', 'widom-site-jiangbei');
- define('INSERT_COUNT', 40); //获取多少条信息以后开始写入数据库
- define('LIST_KEY', 'xt_door'); // 门禁刷卡的队列名称,可以随意取,但请尽量长以区别redis的其他key
- define('REDIS_IP','redis'); //这里提容器的名称,也可以是127.0.0.1等
- define('REDIS_PORT',6379); //redis端口号
- define('REDIS_CUT_STRING', '||||'); // redis入库时的数据分割符
- class WebsocketTest
- {
- public $server;
- public function __construct()
- {
- $serv = new swoole_websocket_server("0.0.0.0", WS_PORT);
- $serv->set([
- 'daemonize' => false, // 设置守护进程模式
- 'log_file' => '/var/www/html/logs/http_server_9506.log',
- ]);
- $serv->on('open', function (swoole_websocket_server $server, $request) {
- // echo "server: handshake success with fd{$request->fd}\n";
- });
- $serv->on('message', function (swoole_websocket_server $server, $frame) {
- // echo "receive from {$frame->fd}:{$frame->data},opcode:{$frame->opcode},fin:{$frame->finish}\n";
- // $server->push($frame->fd, "this is server");
- });
- $serv->on('close', function ($ser, $fd) {
- // echo "client {$fd} closed\n";
- });
- $serv->on('request', [$this, 'onRequest']);
- $this->server = $serv;
- $serv->start();
- }
- public function onRequest($request, $response){
- $data = $request->get;
- if(!$data){
- return false;
- }
- // 接收http请求从get获取message参数的值,给用户推送
- if(array_key_exists("act",$data)){
- $serv = $this->server;
- $ret = [];
- $push = []; //push给前台
- switch ($data['act']){
- case "heart":
- $ret = $this->handleHeart($data);
- break;
- case "card-status":
- if($res = $this->cardStatus($data)){
- $ret = $res["ret"];
- $push = $res["user"];
- }
- break;
- default:
- break;
- }
- if($push){
- $push = json_encode($push,JSON_UNESCAPED_UNICODE);
- if(!emptyempty($serv->connections)){
- // 给所有连接的设备广播***********
- foreach ($serv->connections as $fd) {
- // 判断是不是websocket连接, 如果是,返回的数组中会有websocket_status
- $info = $serv->connection_info($fd); //
- if(array_key_exists("websocket_status",$info)){
- if($info['websocket_status']){
- // 如果有这项,就表示是websocket连接了
- // 1,连接进入等待握手
- // 2,正在握手
- // 3,已握手成功等待浏览器发送数据帧
- if($info['websocket_status'] == 3){
- $serv->push($fd, $push);
- }
- }
- }
- }
- }
- }
- if($ret){
- $ret = json_encode($ret,JSON_UNESCAPED_UNICODE);
- $response->end($ret);
- }
- }
- }
- protected function cardStatus($data){
- $card = $data['Card']; //卡号
- $type = $data['type']; //数据类型
- $serial = $data['Serial']; //控制器序列号,用于区别设备
- $ID = $data['ID']; //控制器序标识符, 如1F7164
- $reader = $data['Reader']; //读头 0-1,1表示外接读头, 0进,1出
- $status = $data['Status']; //当前门状态, 16进制值字符串
- $rand = $data['Index']; //随机值
- $ydata = json_encode($data);
- if(!isset($card) || !isset($type) || !isset($serial) || !isset($ID) || !isset($reader) || !isset($status) || !isset($rand)){
- echo "数据不正确";
- echo $ydata;
- return [];
- }
- if($reader==1){
- $action = "出场";
- }else if($reader==0){
- $action = "进场";
- }else{
- $action = "";
- }
- $note = "事件:" . $action;
- $db = new Swoole\Coroutine\MySQL();
- $server = [
- 'host' => MYSQL_HOST,
- 'port' => MYSQL_PORT,
- 'user' => MYSQL_USER,
- 'password' => MYSQL_PASSWORD,
- 'database' => MYSQL_DATABASE,
- ];
- $db->connect($server);
- $sql = $db->prepare("select user_id,tit,gender,sfz,phone,dwid,bzi,gzi,area,dpt from `widom_user_profile` where nbid = ?");
- $user = $sql->execute([$card]);
- if($user){
- $user = $user[0];
- $gz = "";
- $gzi = $user['gzi']; //工种id
- if($gzi){
- // 工种
- $sql = $db->prepare("select nme from `widom_work_type_manage` where id=?");
- $r = $sql->execute(array($gzi));
- if($r){
- $gz = $r[0]['nme'];
- }
- }
- $bz = "";
- $bzi = $user['bzi']; //班组id
- if($bzi){
- // 班组
- $sql = $db->prepare("select nme from `widom_team_manage` where id=?");
- $r = $sql->execute(array($bzi));
- if($r){
- $gz = $r[0]['nme'];
- }
- }
- $dpt = "";
- $dpti = $user['dpt']; //部门id
- if($dpti){
- // 部门
- $sql = $db->prepare("select nme from `widom_department_manage` where id=?");
- $r = $sql->execute(array($dpti));
- if($r){
- $dpt = $r[0]['nme'];
- }
- }
- $user["gz_name"] = $gz;
- $user["bz_name"] = $bz;
- $user["dpt_name"] = $dpt;
- }else{
- $user = array(
- "tit" => "未知",
- "gz_name" => "未知",
- "bz_name" => "未知",
- "dpt_name" => "未知",
- "gender" => 0
- );
- }
- $user['card'] = $card;
- $AcsRes = "0";
- if($user){
- $AcsRes = "1"; // 0禁止动作,1允许,2报警
- $voice = $action ." 刷卡成功";
- }else{
- $AcsRes = "2";
- $voice = $action ." 失败";
- }
- $answer = [
- "AcsRes" => $AcsRes,
- "ActIndex" => $reader, //动作位置,进出场, 0-1
- "Time" => "10", //动作时间
- "Name" => $user->tit,
- "Note" => $note,
- "Card" => $card,
- "Voice" => $voice,
- "Systime" => date("Y-m-d H:i:s")
- ];
- $sql = $db->prepare("INSERT INTO `widom_sb_door_data` (`card`, `type`, `serial`, `control_id`, `reader`,`status`,`rand`,`ydata`,`answer_res`,`answer`,`created_at`) VALUES (?, ?, ?, ?, ?,?,?,?,?,?,?)");
- $in = [
- $card,
- $type,
- $serial,
- $ID,
- $reader,
- $status,
- $rand,
- $ydata,
- $AcsRes,
- json_encode($answer),
- time()
- ];
- $stmt2 = $sql->execute($in);
- if ($stmt2 == false)
- {
- var_dump($db->errno, $db->error);
- }
- return ["ret" => $answer,"user" => $user];
- }
- protected function handleHeart($data){
- $serial = $data['Serial']; // 设备的序列号
- $ID = $data['ID']; //设备的自定义标识符, 如1F7164
- $key = $data['Key']; //发送的键值,服务器需要原值返回
- $status = $data['Status'];
- $rand = $data['Index']; //随机值
- // 心跳包一直存log即可
- $file = __DIR__ . '/../logs/door-heart-log.txt';//要写入文件的文件名(可以是任意文件名),如果文件不存在,将会创建一个
- $content = date('Y-m-d H:i:s'). ' _ '. json_encode($data) . PHP_EOL;
- file_put_contents($file, $content,FILE_APPEND);
- return [
- "Key" => $key
- ];
- }
- }
- new WebsocketTest();
PHP Code复制内容到剪贴板
- $sql = $taskdb->query("select * from {$tablename} where code = '{$code}'");
- $stmt = $sql->fetch();
- var_dump($stmt);
PHP Code复制内容到剪贴板
- $sql = $taskdb->query("select * from `users`");
- $stmt = $sql->fetchAll();
- var_dump($stmt);
上一篇 门禁UDP协议-demo1-xt
下一篇 备份