Commit a1d5943b by banice

ac报警信息缓存至redis,未同步到mysql

parent dbaa101f
......@@ -14,24 +14,54 @@ use App\Providers\RedisServiceProvider;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\ServiceProvider;
use src\box\ac\Alarm;
use src\box\Box;
use Validator;
class AlarmController extends Controller
{
public function post(Request $request)
public function post(Request $request, Box $box, Alarm $alarm)
{
app()->register(RedisServiceProvider::class);
$boxCode = $request->get('box_code');
$payload = $request->get('payload');
Log::debug($request->all());
$redis = app('Redis');
if ($redis instanceof \Redis) {
Log::debug($payload);
$validator = Validator::make($payload, [
'sensor_bean' => 'array',
'sensor_bean_count' => 'required|integer',
'box_num' => 'required|integer',
'sensor_smoke' => 'required|array'
]);
if ($validator->fails() === true) {
Log::error($validator->errors()->first(), $request->all());
return;
}
if ($box->searchByCodeNum($boxCode, $payload['box_num'], ['id']) === false) {
Log::error('ac alarm can not find box_id by code and num', $request->all());
return;
}
$boxId = $box->getValue('id');
$sensorSmoke = $payload['sensor_smoke'];
$sensorBean = $payload['sensor_bean'];
$alarm->setBoxId($boxId);
$alarm->setBlackSmoke($sensorSmoke['black_smoke']);
$alarm->setWhiteSmoke($sensorSmoke['white_smoke']);
$alarm->setHumidity(array_column($sensorBean, 'humidity'));
$alarm->setTemperature(array_column($sensorBean, 'temperature'));
$alarm->setSensorState(array_column($sensorBean, 'sensor_state'));
$alarm->setRetain(array_column($sensorBean, 'retain'));
$alarm->setAcRetain($sensorSmoke['retain']);
$alarm->setDeviceState($sensorSmoke['device_state']);
$alarm->updateAllToRedis();
//todo 数据库更新
}
}
\ No newline at end of file
......@@ -29,6 +29,7 @@ $app->withEloquent();
$app->configure('logging');
$app->configure('database');
$app->configure('redis_key');
/*
......
<?php
/**
* Created by PhpStorm.
* User: banice
* Date: 2018/9/27
* Time: 上午11:23
*/
return [
'AC_ALARM' => 'hdg:ac:alarm:hash'
];
......@@ -3,7 +3,7 @@
## redis 字典
* ac报警信息
### ac报警信息
`key:'hdg:ac:alarm:hash`
......
......@@ -24,10 +24,10 @@ $router->group(['middleware' => ['mqtt']], function () use ($router) {
$router->post('ac/state', 'ac\StateController@post');
$router->post('door/lock/version', 'door\LockController@version');
$router->post('door/state', 'door\StateController@post');
$router->post('ac/alarm/info', 'ac\AlarmController@post');
});
$router->post('ac/alarm/info', 'ac\AlarmController@post');
$router->post('box/init', 'door\InitController@init');
......
......@@ -25,7 +25,20 @@ trait SearchModel
*/
public function searchByCode(string $code, array $parameters = []): bool
{
$this->boxModel = BoxModel::where('code', $code)->first($parameters);
$this->boxModel = BoxModel::where([['code', $code], ['box_type', 0]])->first($parameters);
return $this->boxModel === null ? false : true;
}
/**
* @param string $code
* @param int $num
* @param array $parameters
* @return bool
*/
public function searchByCodeNum(string $code, int $num, array $parameters = []): bool
{
$this->boxModel = BoxModel::where([['code', $code], ['box_type', $num]])->first($parameters);
return $this->boxModel === null ? false : true;
}
......
<?php
/**
* Created by PhpStorm.
* User: banice
* Date: 2018/9/27
* Time: 上午10:52
*/
namespace src\box\ac;
use Illuminate\Support\Facades\Redis;
class Alarm
{
private $boxId;
/**
* @var array 湿度
*/
private $humidity = [];
/**
* @var array 温度
*/
private $temperature = [];
private $sensorState = [];
/**
* @var array 保留
*/
private $retain = [];
/**
* @var int 白烟
*/
private $whiteSmoke = 0;
/**
* @var int 黑烟
*/
private $blackSmoke = 0;
/**
* @var int 设备状态
*/
private $deviceState = 0;
/**
* @var int 预留
*/
private $acRetain = 0;
/**
* 更新所有报警数据至redis
*/
public function updateAllToRedis()
{
$redis = app('Redis');
$update = [];
$i = 1;
foreach ([
'humidity' => $this->humidity,
'temperature' => $this->temperature,
'sensor_state' => $this->sensorState,
'retain' => $this->retain,
] as $key => $items) {
$i = 1;
foreach ($items as $item) {
$update[$key . '_' . $i++] = $item;
}
}
$update['white_smoke'] = $this->whiteSmoke;
$update['black_smoke'] = $this->blackSmoke;
$update['device_state'] = $this->deviceState;
$update['retain'] = $this->acRetain;
if ($redis instanceof \Redis) {
$redis->hMset(config('redis_key.AC_ALARM') . ':' . $this->boxId, $update);
}
}
/**
* @return array
*/
public function getHumidity(): array
{
return $this->humidity;
}
/**
* @param array $humidity
*/
public function setHumidity(array $humidity): void
{
$this->humidity = $humidity;
}
/**
* @return array
*/
public function getTemperature(): array
{
return $this->temperature;
}
/**
* @param array $temperature
*/
public function setTemperature(array $temperature): void
{
$this->temperature = $temperature;
}
/**
* @return array
*/
public function getSensorState(): array
{
return $this->sensorState;
}
/**
* @param array $sensorState
*/
public function setSensorState(array $sensorState): void
{
$this->sensorState = $sensorState;
}
/**
* @return array
*/
public function getRetain(): array
{
return $this->retain;
}
/**
* @param array $retain
*/
public function setRetain(array $retain): void
{
$this->retain = $retain;
}
/**
* @return int
*/
public function getWhiteSmoke(): int
{
return $this->whiteSmoke;
}
/**
* @param int $whiteSmoke
*/
public function setWhiteSmoke(int $whiteSmoke): void
{
$this->whiteSmoke = $whiteSmoke;
}
/**
* @return int
*/
public function getBlackSmoke(): int
{
return $this->blackSmoke;
}
/**
* @param int $blackSmoke
*/
public function setBlackSmoke(int $blackSmoke): void
{
$this->blackSmoke = $blackSmoke;
}
/**
* @return int
*/
public function getDeviceState(): int
{
return $this->deviceState;
}
/**
* @param int $deviceState
*/
public function setDeviceState(int $deviceState): void
{
$this->deviceState = $deviceState;
}
/**
* @return mixed
*/
public function getBoxId()
{
return $this->boxId;
}
/**
* @param mixed $boxId
*/
public function setBoxId($boxId): void
{
$this->boxId = $boxId;
}
/**
* @return int
*/
public function getAcRetain(): int
{
return $this->acRetain;
}
/**
* @param int $acRetain
*/
public function setAcRetain(int $acRetain): void
{
$this->acRetain = $acRetain;
}
}
\ No newline at end of file
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