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

187 lines
3.5 KiB
Markdown

# 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:
```bash
npm install
```
2. Link the entity package:
```bash
cd ../entity
npm install
cd ../svc-hcm-crawler
```
3. Build the project:
```bash
npm run build
```
## Configuration
Edit `config/default.json` or `config/prod.json`:
```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:
```bash
npm run dev
```
## Deployment with PM2
1. Build the project:
```bash
npm run build
```
2. Start with PM2:
```bash
npm run pm2:start
```
Or manually:
```bash
pm2 start ecosystem.config.js
```
3. Check status:
```bash
pm2 list
pm2 logs svc-hcm-crawler
```
4. Stop the service:
```bash
npm run pm2:stop
# or
pm2 stop svc-hcm-crawler
```
5. Restart the service:
```bash
npm run pm2:restart
# or
pm2 restart svc-hcm-crawler
```
6. Delete from PM2:
```bash
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:
```bash
export NODE_ENV=prod
```
Or use PM2 ecosystem config which sets it automatically.
## License
ISC