74 lines
2.7 KiB
PHP
74 lines
2.7 KiB
PHP
<?php
|
|
|
|
namespace app\controller;
|
|
|
|
use think\worker\Server;
|
|
use Workerman\Lib\Timer;
|
|
use think\facade\Db;
|
|
class Push extends Server
|
|
{
|
|
protected $socket = 'http://0.0.0.0:2346'; //端口自行修改
|
|
|
|
protected static $heartbeat_time = 60*60*24;
|
|
|
|
public function onWorkerStart($worker)
|
|
{
|
|
Timer::add(3, function () use ($worker) {
|
|
|
|
$time_now = time();
|
|
$map1 = [
|
|
['status', '=',255],
|
|
['is_pushed','=',1],
|
|
];
|
|
|
|
$map2 = [
|
|
['status','=', 0],
|
|
['is_pushed','=',1],
|
|
];
|
|
$hasfault = Db::table('charge_pile')->whereOr([$map1, $map2])->order('charge_pile_id desc')->count('charge_pile_id');
|
|
|
|
if ($hasfault) {
|
|
$case1 = [
|
|
['charge_pile.status', '=',255],
|
|
['is_pushed','=',1],
|
|
];
|
|
$case2 = [
|
|
['charge_pile.status', '=',0],
|
|
['is_pushed','=',1],
|
|
];
|
|
$faultInfo = Db::table('charge_pile,charge_station')
|
|
->whereOr([$case1, $case2])
|
|
->where('charge_pile.charge_station_id = charge_station.charge_station_id')
|
|
->order('charge_pile_id desc')
|
|
->field('charge_station.charge_station_id,charge_station_name,charge_pile.charge_pile_number,charge_station.area,charge_station.street,charge_pile.status,charge_pile.no')
|
|
->find();
|
|
$faultInfo['code'] = 1;
|
|
foreach ($worker->connections as $connection) {
|
|
if (empty($connection->lastMessageTime)) {
|
|
$connection->lastMessageTime = $time_now;
|
|
}
|
|
|
|
if ($time_now - $connection->lastMessageTime > self::$heartbeat_time) {
|
|
$connection->close();
|
|
}
|
|
|
|
$connection->send(json_encode($faultInfo));
|
|
}
|
|
Db::table('charge_pile')->where('charge_pile_number',$faultInfo['charge_pile_number'])->save(['is_pushed'=>2]);
|
|
} else {
|
|
foreach ($worker->connections as $connection) {
|
|
if (empty($connection->lastMessageTime)) {
|
|
$connection->lastMessageTime = $time_now;
|
|
continue;
|
|
}
|
|
if ($time_now - $connection->lastMessageTime > self::$heartbeat_time) { //连接超时
|
|
$connection->close();
|
|
}
|
|
$connection->send(json_encode(['code'=>0,'msg'=>'暂时没有信息']));
|
|
}
|
|
|
|
}
|
|
});
|
|
}
|
|
}
|