Токен-аутентификация Sanctum (login/logout по устройствам), эндпоинты /api/v1 для зон/устройств/правил автоматизации с тем же RBAC (owner/viewer), что и в веб-версии — контроллеры переиспользуют существующие Policy и FormRequest. Устройства отдают живой статус из Device Shadow (Redis) и историю телеметрии из ClickHouse, плюс управление (turn-on/turn-off/set-level) через device-control-service.
67 lines
1.9 KiB
PHP
67 lines
1.9 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Api\V1;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Http\Requests\AutomationRuleRequest;
|
|
use App\Http\Resources\AutomationRuleResource;
|
|
use App\Models\AutomationRule;
|
|
|
|
class AutomationRuleController extends Controller
|
|
{
|
|
public function index()
|
|
{
|
|
$this->authorize('viewAny', AutomationRule::class);
|
|
|
|
$rules = AutomationRule::with(['zone', 'targetDevice', 'conditionSourceDevice'])
|
|
->orderByDesc('id')
|
|
->get();
|
|
|
|
return AutomationRuleResource::collection($rules);
|
|
}
|
|
|
|
public function store(AutomationRuleRequest $request)
|
|
{
|
|
$rule = new AutomationRule($this->mapActionParams($request->validated()));
|
|
$rule->user()->associate($request->user());
|
|
$rule->save();
|
|
|
|
return (new AutomationRuleResource($rule))->response()->setStatusCode(201);
|
|
}
|
|
|
|
public function update(AutomationRuleRequest $request, AutomationRule $automation_rule)
|
|
{
|
|
$automation_rule->update($this->mapActionParams($request->validated()));
|
|
|
|
return new AutomationRuleResource($automation_rule);
|
|
}
|
|
|
|
public function destroy(AutomationRule $automation_rule)
|
|
{
|
|
$this->authorize('delete', $automation_rule);
|
|
|
|
$automation_rule->delete();
|
|
|
|
return response()->noContent();
|
|
}
|
|
|
|
/**
|
|
* Mirrors AutomationRuleController@mapActionParams on the web side —
|
|
* "level" is a friendlier stand-in for action_params over the wire too,
|
|
* so mobile clients don't need to know the {"level": ...} shape.
|
|
*
|
|
* @param array<string, mixed> $data
|
|
* @return array<string, mixed>
|
|
*/
|
|
private function mapActionParams(array $data): array
|
|
{
|
|
$data['action_params'] = $data['action_type'] === 'set_level'
|
|
? ['level' => $data['level']]
|
|
: [];
|
|
|
|
unset($data['level']);
|
|
|
|
return $data;
|
|
}
|
|
}
|