feat: add customer hooks plugin system (v1.1.0)

New plugin architecture for customer-specific business logic:
- hooks/base.py: CustomerHooks base class with 12 hook points
  (on_alarm_created, on_alarm_resolved, on_energy_data_received,
   on_device_status_changed, on_quota_exceeded, on_work_order_created,
   on_work_order_completed, on_inspection_completed, on_report_generated,
   calculate_custom_kpis, on_charging_order_created/completed)
- hooks/loader.py: Dynamic loader that imports from customers/{CUSTOMER}/hooks/
- alarm_checker.py: calls on_alarm_created and on_alarm_resolved hooks
- quota_checker.py: calls on_quota_exceeded hook

Customers override hooks by creating customers/{name}/hooks/__init__.py
without modifying core code. Scales to 10-20+ customers.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
Du Wenbo
2026-04-04 18:30:53 +08:00
parent 26d2731d3e
commit 2b9797d61b
6 changed files with 265 additions and 1 deletions

View File

@@ -6,6 +6,7 @@ from sqlalchemy.ext.asyncio import AsyncSession
from app.models.quota import EnergyQuota, QuotaUsage
from app.models.alarm import AlarmEvent
from app.models.energy import EnergyDailySummary
from app.hooks import get_hooks
logger = logging.getLogger("quota_checker")
@@ -34,6 +35,7 @@ async def check_quotas(session: AsyncSession):
)
quotas = result.scalars().all()
hooks = get_hooks()
for quota in quotas:
period_start, period_end = _get_period_range(quota.period, now)
@@ -121,4 +123,10 @@ async def check_quotas(session: AsyncSession):
f"quota={quota.quota_value:.1f} rate={usage_rate_pct:.1f}%"
)
# Customer hook: on_quota_exceeded
try:
await hooks.on_quota_exceeded(quota, usage_record, session)
except Exception as _he:
logger.error(f"Hook on_quota_exceeded error: {_he}")
await session.flush()