Files
svc-hcm-crawler/README.md
Sony Surahmn e5baaf003a Initial Commit
2025-12-10 19:00:39 +07:00

3.5 KiB

SVC-HCM-CRAWLER

Hikvision Device Crawling Service for HCM Bridge

This service crawls Hikvision access control devices and sends events to the svc-hcmbridge service.

Features

  • Automatically crawls Hikvision devices configured for crawling (flag=1 or iscrawling=1)
  • Fetches device MAC address from device info endpoint
  • Handles HTTP Digest authentication
  • Duplicate event detection
  • Sends events to svc-hcmbridge at /api/dooraccess/logs
  • PM2 deployment ready

Prerequisites

  • Node.js 20+
  • MySQL database (same as svc-hcmbridge)
  • PM2 (for deployment)
  • entity package (from parent directory)

Installation

  1. Install dependencies:
npm install
  1. Link the entity package:
cd ../entity
npm install
cd ../svc-hcm-crawler
  1. Build the project:
npm run build

Configuration

Edit config/default.json or config/prod.json:

{
  "database": {
    "hcm": {
      "host": "127.0.0.1",
      "port": "3306",
      "username": "root",
      "password": "your_password",
      "database": "tt_hcm"
    }
  },
  "bridge": {
    "host": "127.0.0.1",
    "port": "3000",
    "endpoint": "/api/dooraccess/logs"
  },
  "hikvision": {
    "defaultUsername": "admin",
    "defaultPassword": "Passwordhik_1",
    "defaultPort": 80
  },
  "crawler": {
    "interval": "*/1 * * * *"
  }
}

Development

Run in development mode:

npm run dev

Deployment with PM2

  1. Build the project:
npm run build
  1. Start with PM2:
npm run pm2:start

Or manually:

pm2 start ecosystem.config.js
  1. Check status:
pm2 list
pm2 logs svc-hcm-crawler
  1. Stop the service:
npm run pm2:stop
# or
pm2 stop svc-hcm-crawler
  1. Restart the service:
npm run pm2:restart
# or
pm2 restart svc-hcm-crawler
  1. Delete from PM2:
npm run pm2:delete
# or
pm2 delete svc-hcm-crawler

PM2 Commands

  • npm run pm2:start - Start the service
  • npm run pm2:stop - Stop the service
  • npm run pm2:restart - Restart the service
  • npm run pm2:delete - Delete from PM2
  • npm run pm2:logs - View logs

How It Works

  1. The service runs on a cron schedule (default: every 1 minute)
  2. It queries tbl_deviceinfo for devices with:
    • brand = 'HIKVISION'
    • flag = 1 OR iscrawling = 1
    • isdeleted = 0
  3. For each device:
    • Fetches MAC address from device info endpoint
    • Makes authenticated requests to /ISAPI/AccessControl/AcsEvent
    • Maps events to webhook format
    • Checks for duplicates
    • Sends events to svc-hcmbridge at /api/dooraccess/logs

Logs

Logs are stored in:

  • PM2 logs: ./logs/pm2-out.log and ./logs/pm2-error.log
  • Console output (when running with npm start)

Troubleshooting

  1. Database connection issues:

    • Check database credentials in config
    • Ensure database is accessible
    • Check if entity package is properly linked
  2. Bridge service not responding:

    • Ensure svc-hcmbridge is running
    • Check bridge host/port in config
    • Verify endpoint path is correct
  3. Device connection issues:

    • Check device IP addresses in tbl_deviceinfo
    • Verify device credentials
    • Check network connectivity to devices
  4. MAC address not being saved:

    • Check logs for XML parsing errors
    • Verify device info endpoint is accessible
    • Check device returns valid XML

Environment Variables

Set NODE_ENV to prod for production:

export NODE_ENV=prod

Or use PM2 ecosystem config which sets it automatically.

License

ISC