iot-agent/README.md
2025-08-03 23:07:33 +05:30

337 lines
8.5 KiB
Markdown

# AI Agent Backend - IoT Dashboard
A comprehensive Node.js Express backend for IoT device management with AI-powered analytics, real-time streaming, self-healing capabilities, and intelligent suggestions.
## Features
### 🔐 Authentication & Authorization
- JWT-based authentication with Redis session management
- Role-based access control (Admin, Operator, Viewer)
- Password hashing with bcrypt
- Rate limiting for security
### 📡 Real-time Data Streaming
- Apache StreamPipes integration for IoT data ingestion
- WebSocket support for real-time communication
- Kafka/MQTT support for message queuing
- Redis caching for high-performance data access
### 🤖 AI Agent Capabilities
- Intelligent anomaly detection
- Predictive maintenance suggestions
- Performance optimization recommendations
- Self-healing automation
- Machine learning model management
### 🚨 Alert Management
- Real-time alert generation
- Configurable alert rules and thresholds
- Multi-channel notifications (Email, SMS, WebSocket)
- Alert acknowledgment and resolution tracking
### 🔧 Self-Healing System
- Automated problem detection and resolution
- Device restart and configuration management
- Load balancing and circuit breaker patterns
- Maintenance scheduling
### 📊 Analytics & Insights
- Real-time dashboard metrics
- Performance analytics
- Device health monitoring
- Historical data analysis
### 📱 Notification System
- Multi-channel notifications (Email, SMS, In-app)
- Configurable notification preferences
- Notification history and status tracking
## Tech Stack
- **Runtime**: Node.js 16+
- **Framework**: Express.js
- **Database**: MySQL 8.0+
- **Cache**: Redis 6.0+
- **Authentication**: JWT + bcrypt
- **Real-time**: Socket.IO
- **Streaming**: Apache StreamPipes, Kafka, MQTT
- **Notifications**: Nodemailer, Twilio
- **Logging**: Winston
- **Validation**: Express-validator
## Prerequisites
- Node.js 16+
- MySQL 8.0+
- Redis 6.0+
- Apache StreamPipes (optional)
- Kafka (optional)
- MQTT Broker (optional)
## Installation
1. **Clone the repository**
```bash
git clone <repository-url>
cd ai-agent-backend
```
2. **Install dependencies**
```bash
npm install
```
3. **Environment Configuration**
```bash
cp env.example .env
```
Edit `.env` with your configuration:
```env
# Database
DB_HOST=localhost
DB_PORT=3306
DB_USER=root
DB_PASSWORD=your_password
DB_NAME=ai_agent_iot
# JWT
JWT_SECRET=your_super_secret_jwt_key_here
# Redis
REDIS_HOST=localhost
REDIS_PORT=6379
# StreamPipes
STREAMPIPES_HOST=localhost
STREAMPIPES_PORT=8080
STREAMPIPES_USERNAME=admin
STREAMPIPES_PASSWORD=admin
# Notifications
SMTP_HOST=smtp.gmail.com
SMTP_USER=your_email@gmail.com
SMTP_PASS=your_app_password
TWILIO_ACCOUNT_SID=your_twilio_account_sid
TWILIO_AUTH_TOKEN=your_twilio_auth_token
```
4. **Database Setup**
```bash
# Run migrations
npm run migrate
# Seed initial data (optional)
npm run seed
```
5. **Start the server**
```bash
# Development
npm run dev
# Production
npm start
```
## API Endpoints
### Authentication
- `POST /api/auth/register` - Register new user
- `POST /api/auth/login` - User login
- `POST /api/auth/logout` - User logout
- `GET /api/auth/profile` - Get user profile
- `PUT /api/auth/profile` - Update user profile
- `POST /api/auth/refresh` - Refresh JWT token
### Devices
- `GET /api/devices` - Get all devices
- `GET /api/devices/:deviceId` - Get device details
- `POST /api/devices` - Create new device
- `PUT /api/devices/:deviceId` - Update device
- `DELETE /api/devices/:deviceId` - Delete device
- `GET /api/devices/:deviceId/data` - Get device data
- `POST /api/devices/:deviceId/command` - Send command to device
- `GET /api/devices/:deviceId/stats` - Get device statistics
### Alerts
- `GET /api/alerts` - Get all alerts
- `GET /api/alerts/:alertId` - Get alert details
- `POST /api/alerts/:alertId/acknowledge` - Acknowledge alert
- `POST /api/alerts/:alertId/resolve` - Resolve alert
- `GET /api/alerts/stats/overview` - Get alert statistics
### Analytics
- `GET /api/analytics/dashboard` - Get dashboard overview
- `GET /api/analytics/performance` - Get performance metrics
### Healing
- `GET /api/healing` - Get healing actions
### Suggestions
- `GET /api/suggestions` - Get AI suggestions
### Notifications
- `GET /api/notifications` - Get user notifications
### StreamPipes
- `GET /api/streampipes/health` - Check StreamPipes health
- `GET /api/streampipes/streams` - Get data streams
## WebSocket Events
### Client to Server
- `subscribe_device` - Subscribe to device updates
- `unsubscribe_device` - Unsubscribe from device updates
- `subscribe_alerts` - Subscribe to alerts
- `unsubscribe_alerts` - Unsubscribe from alerts
- `device_control` - Send device control command
- `acknowledge_alert` - Acknowledge alert
### Server to Client
- `device_data_update` - Device data update
- `new_alert` - New alert notification
- `alert_acknowledged` - Alert acknowledged
- `healing_approved` - Healing action approved
- `suggestion_feedback_received` - Suggestion feedback
- `new_notification` - New notification
- `dashboard_update` - Dashboard update
## Database Schema
### Core Tables
- `users` - User accounts and authentication
- `devices` - IoT device information
- `device_data` - Raw device data storage
- `alerts` - Alert records
- `notifications` - User notifications
### AI & Analytics Tables
- `ai_suggestions` - AI-generated suggestions
- `ai_analysis_results` - AI analysis results
- `healing_actions` - Self-healing actions
- `device_controls` - Device control commands
## Services Architecture
### Core Services
- **StreamPipesService** - Handles Apache StreamPipes integration
- **AIAgentService** - Manages AI analysis and suggestions
- **AlertService** - Handles alert generation and management
- **HealingService** - Manages self-healing actions
- **NotificationService** - Handles multi-channel notifications
### Configuration
- **Database** - MySQL connection and query management
- **Redis** - Caching and session management
- **Logger** - Structured logging with Winston
## Development
### Project Structure
```
├── config/ # Configuration files
├── middleware/ # Express middleware
├── routes/ # API route handlers
├── services/ # Business logic services
├── socket/ # WebSocket handlers
├── utils/ # Utility functions
├── scripts/ # Database scripts
├── logs/ # Application logs
├── server.js # Main application file
└── package.json # Dependencies and scripts
```
### Available Scripts
- `npm start` - Start production server
- `npm run dev` - Start development server with nodemon
- `npm run migrate` - Run database migrations
- `npm run seed` - Seed database with initial data
- `npm test` - Run tests
### Environment Variables
See `env.example` for all available configuration options.
## Deployment
### Production Setup
1. Set `NODE_ENV=production`
2. Configure production database and Redis
3. Set up SSL certificates
4. Configure reverse proxy (nginx)
5. Set up process manager (PM2)
### Docker Deployment
```bash
# Build image
docker build -t ai-agent-backend .
# Run container
docker run -p 5000:5000 --env-file .env ai-agent-backend
```
## Monitoring & Logging
### Health Checks
- `GET /health` - Application health status
- Service-specific health checks available
### Logging
- Structured JSON logging
- Log rotation and archiving
- Different log levels (error, warn, info, debug)
### Metrics
- Request/response metrics
- Database query performance
- Service health metrics
- Custom business metrics
## Security
### Authentication
- JWT tokens with configurable expiration
- Redis-based session management
- Password hashing with bcrypt
### Authorization
- Role-based access control
- Route-level permissions
- Device-level access control
### Security Headers
- Helmet.js for security headers
- CORS configuration
- Rate limiting
- Input validation
## Contributing
1. Fork the repository
2. Create a feature branch
3. Make your changes
4. Add tests
5. Submit a pull request
## License
MIT License - see LICENSE file for details
## Support
For support and questions:
- Create an issue in the repository
- Check the documentation
- Review the API examples
## Roadmap
- [ ] Advanced ML model integration
- [ ] GraphQL API support
- [ ] Microservices architecture
- [ ] Kubernetes deployment
- [ ] Advanced analytics dashboard
- [ ] Mobile app support
- [ ] Multi-tenant support
- [ ] Advanced security features