Commit 537eec59 authored by 冯超鹏's avatar 冯超鹏

提交报警

parent 2cd1f183
<?php
namespace App\Console\Commands;
use App\Http\Controllers\Auth\SwooleCommandController;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Log;
class alarm extends Command
{
public $ws;
private $token = 'zxc123456789ZXC';
public $time = 10;
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'swoolealarm {action?}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'swoolealarm';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$action = $this->argument('action');
switch ($action) {
case 'close':
break;
default:
$this->start();
break;
}
}
//开启WebSocket
public function start()
{
$url = config('public.swoolwebsocketurl');
$por = config('public.swoolwebsocketurlpor');
$this->ws = new \swoole_websocket_server('0.0.0.0', 9601); //创建一个端口
$this->ws->set(array(
'reactor_num' => 2, //reactor线程数
'worker_num' => 4, //worker进程数
'backlog' => 128, //Listen队列长度
'max_request' => 10,//最大连接
'daemonize'=>0,//守护进程
));
$this->ws->on('open', function ($ws, $request) {
// //链接成功
$userdata = new SwooleCommandController();
$this->ws->push($request->fd,$userdata->alarm(),1);
swoole_timer_tick(6000, function($timerId) use ($ws, $request) {
$userdata = new SwooleCommandController();
$this->ws->push($request->fd,$userdata->alarm(),1);
});
});
//监听WebSocket消息事件
$this->ws->on('message', function ($ws, $frame) {
});
$this->ws->on('request', function ($request, $response,$from_id,$fd) {
});
//监听WebSocket连接关闭事件
$this->ws->on('close', function ($ws, $fd) {
echo "client:{$fd} is closed\n";
swoole_timer_clear($this->time);
});
$this->ws->start();
}
}
......@@ -56,4 +56,20 @@ class SwooleCommandController extends Controller
return $this->jsonSuccessData(['devicelist' => $devicelist, 'count' => $count]);
}
//监测是否设备报警
public function alarm() {
$isalarm = DB::table('reportpolice')
->where('status','=','1')
->where('police','=','1')
->groupBy('id')
->select('devicenumber')
->get()->toArray();
if($isalarm){
return $this->jsonSuccessData($isalarm);
}else{
return $this->jsonErrorData(105,'当前无报警');
}
}
}
\ No newline at end of file
......@@ -48,12 +48,11 @@ class HomepageController extends Controller
->groupBy('date')
->get()
->toArray();
$policedata = Users::whereBetween('created_at', [$year_start, $endThismonth])
->selectRaw('from_unixtime(created_at,"%Y-%m") as date,COUNT(id) as value')
$policedata = Users::whereBetween('created_at', [date('Y-m-d H:i:s',$year_start), date('Y-m-d H:i:s',$endThismonth)])
->selectRaw('from_unixtime(unix_timestamp(created_at),"%Y-%m") as date,COUNT(id) as value')
->groupBy('date')
->get()
->toArray();
$devicepolice = Device::whereBetween('deviceaddtime', [$year_start, $endThismonth])
->where('devicepolice', '>', 1)
->selectRaw('from_unixtime(deviceaddtime,"%Y-%m") as date,COUNT(id) as value')
......
const baseURL = 'ws://127.0.0.1:9502';
const chemicalsUrl = 'ws://127.0.0.1:9507';
const fireUrl = 'ws://127.0.0.1:9508';
const alarmUrl = 'ws://127.0.0.1:9601';
const mapkey = '2719fe261fee06a08dcb4980990879da';
const mapurl = 'https://webapi.amap.com/maps?v=1.4.15&key=';
export default {
......@@ -9,4 +10,5 @@ export default {
mapurl,
chemicalsUrl,
fireUrl,
alarmUrl,
};
......@@ -38,7 +38,7 @@
import ScrollPane from './ScrollPane';
import { generateTitle } from '@/utils/i18n';
import path from 'path';
import websocketurl from '@/api/configurl';
export default {
components: { ScrollPane },
data() {
......@@ -74,6 +74,7 @@ export default {
mounted() {
this.initTags();
this.addTags();
this.initWebSocket(); // 返回报警
},
methods: {
generateTitle, // generateTitle by vue-i18n
......@@ -117,6 +118,84 @@ export default {
}
return false;
},
initWebSocket() {
const wsuri = websocketurl.alarmUrl;
this.websock = new WebSocket(wsuri);
this.websock.onmessage = this.websocketonmessage;
this.websock.onopen = this.websocketonopen;
this.websock.onerror = this.websocketonerror;
this.websock.onclose = this.websocketclose;
},
websocketonopen(){ // 连接建立之后执行send方法发送数据
console.log('连接成功');
},
websocketonerror(){ // 连接建立失败重连
this.$notify({
title: '警告',
message: '建立链接失败,正在重连',
type: 'warning',
});
this.initWebSocket();
},
websocketonmessage(e){ // 数据接收
const redata = JSON.parse(e.data);
if (redata.code === 200) {
localStorage.removeItem('isalam');
if (localStorage.getItem('isalam') === null || localStorage.getItem('isalam') === undefined) { // 第一次没有的话
localStorage.setItem('isalam', JSON.stringify(redata.data));
for (let i = 0; i < redata.data.length; i++) {
setTimeout(() => {
this.$notify.error({
title: '报警提示!点击X可消警',
dangerouslyUseHTMLString: true,
duration: 0,
message: redata.data[i].devicenumber + '正在报警请尽快处理',
onClose: function(){
console.log(redata.data[i].devicenumber);
},
});
}, 3);
}
} else {
for (let j = 0; j < redata.data.length; j++) {
console.log(redata.data[j].devicenumber, JSON.parse(localStorage.getItem('isalam')));
if (!this.isarray(redata.data[j].devicenumber, JSON.parse(localStorage.getItem('isalam')))) {
setTimeout(() => {
this.$notify.error({
title: '报警提示!点击X可消警',
dangerouslyUseHTMLString: true,
duration: 0,
message: redata.data[j].devicenumber + '正在报警请尽快处理',
onClose: function(){
console.log(redata.data[j].devicenumber);
},
});
}, 3);
}
}
}
}
},
websocketsend(){ // 数据发送
this.websock.send(JSON.stringify(this.type));
},
websocketclose(e){ // 关闭
console.log('断开连接', e);
this.$notify({
title: '警告',
message: '监测报警长连接已断开',
type: 'warning',
});
},
isarray(needle, arr) { // data长连接返回 adata本地储
var has = false;
for (let i = 0; i < arr.length; i++) {
if (arr[i].devicenumber === needle) {
return true;
}
}
return has;
},
moveToCurrentTag() {
const tags = this.$refs.tag;
this.$nextTick(() => {
......
......@@ -144,7 +144,7 @@ export default {
console.log('断开连接', e);
this.$notify({
title: '警告',
message: '长连接已断开',
message: '危化监测长连接已断开',
type: 'warning',
});
},
......
......@@ -144,7 +144,7 @@ export default {
console.log('断开连接', e);
this.$notify({
title: '警告',
message: '长连接已断开',
message: '消防监测长连接已断开',
type: 'warning',
});
},
......
......@@ -146,7 +146,7 @@ export default {
console.log('断开连接', e);
this.$notify({
title: '警告',
message: '长连接已断开',
message: '用户统计长连接已断开',
type: 'warning',
});
},
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment