A full-stack web application that allows users to interact with and control virtual devices (Light and Fan) in a sandbox environment. Built with React (TypeScript) frontend and PHP backend with MySQL database.
- Drag & Drop Interface - Intuitive device placement on canvas
- Real-time Device Control
- Light: Power toggle, brightness slider (0-100%), color temperature (warm, neutral, cool, pink)
- Fan: Power toggle, speed control (0-100%)
- Preset Management - Save, load and delete device configurations
- Responsive Design - Works seamlessly on desktop and mobile devices
- Touch Support - Full mobile touch interaction support
- Persistent Storage - All data saved to MySQL database
- Clean Architecture - Modular components with proper separation of concerns
- React 19 with TypeScript
- Vite - Build tool and dev server
- Tailwind CSS 4.1 - Styling
- React DnD - Drag and drop functionality
- React DnD Touch-Backend - Touch support for tablet and mobile devices
- Axios - HTTP client
- Context API - State management
- PHP 8.4 - Server-side logic
- MySQL 8.0+ - Database
- Apache - Web server
- Custom MVC Architecture
- PSR-4 Autoloading
- RESTful API design
- Trait-based validation
- Singleton database pattern
Ensure you have the following installed on your system:
- PHP >= 8.1
- MySQL >= 8.0
- Apache >= 2.4 with
mod_rewriteenabled - Node.js >= 18.0 (includes npm)
- Git (for cloning the repository)
- PHP: https://windows.php.net/download/
- MySQL: https://dev.mysql.com/downloads/installer/
- Apache: https://www.apachelounge.com/download/
- Node.js: https://nodejs.org/
- XAMPP (All-in-one): https://www.apachefriends.org/
# Using Homebrew
brew install [email protected]
brew install mysql
brew install httpd
brew install node# Add PHP repository
sudo add-apt-repository ppa:ondrej/php
sudo apt update
# Install dependencies
sudo apt install php8.4 php8.4-mysql php8.4-cli php8.4-json php8.4-mbstring
sudo apt install mysql-server
sudo apt install apache2
sudo apt install nodejs npmgit clone https://github.com/Mesbah-Tonmoy/Device-Sandbox-Simulator.git
cd Device-Sandbox-SimulatorOption A: Using MySQL Command Line
mysql -u root -pThen in MySQL prompt:
CREATE DATABASE device_sandbox CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
EXIT;Option B: Using phpMyAdmin
- Open phpMyAdmin (usually at
http://localhost/phpmyadmin) - Click "New" in the sidebar
- Database name:
device_sandbox - Collation:
utf8mb4_unicode_ci - Click "Create"
Option A: Command Line
mysql -u root -p device_sandbox < database/schema.sqlOption B: phpMyAdmin
- Select
device_sandboxdatabase - Click "Import" tab
- Choose
database/schema.sqlfile - Click "Go"
Edit backend/.env:
# Database Configuration
DB_HOST=localhost
DB_NAME=device_sandbox
DB_USER=root
DB_PASS=your_password_here
DB_CHARSET=utf8mb4
# Environment
ENV=developmentImportant: Replace your_password_here with your actual MySQL root password.
Windows (XAMPP)
- Edit
C:\xampp\apache\conf\httpd.conf - Find
DocumentRootand update:DocumentRoot "C:/xampp/htdocs" <Directory "C:/xampp/htdocs">
macOS (Homebrew)
- Edit
/usr/local/etc/httpd/httpd.conf - Update DocumentRoot to your projects folder
Linux
- Edit
/etc/apache2/sites-available/000-default.conf - Update DocumentRoot
Windows (XAMPP)
# Copy backend folder
cp -r backend C:/xampp/htdocs/device-sandbox-simulator/macOS/Linux
# Copy backend folder
sudo cp -r backend /var/www/html/device-sandbox-simulator/
sudo chown -R www-data:www-data /var/www/html/device-sandbox-simulator/Windows (XAMPP)
- Edit
httpd.conf - Uncomment:
LoadModule rewrite_module modules/mod_rewrite.so
macOS
sudo a2enmod rewrite
sudo apachectl restartLinux
sudo a2enmod rewrite
sudo systemctl restart apache2macOS/Linux
sudo chmod -R 755 /var/www/html/device-sandbox-simulator/
sudo chmod -R 777 /var/www/html/device-sandbox-simulator/backend/logs/Open browser and navigate to:
http://localhost/device-sandbox-simulator/backend/api/devices/get.php
Expected response:
{
"success": true,
"message": "No device on canvas"
}cd frontend
npm installEdit frontend/src/utils/constants.ts:
// Update this line with your backend URL
export const API_BASE_URL =
"http://localhost/device-sandbox-simulator/backend/api";npm run devThe application will start at http://localhost:5173 (or another available port).
- Open
http://localhost:5173in your browser - You should see the Device Sandbox Simulator interface
- Try dragging a Light or Fan device to the canvas
- Test device controls (power, brightness/speed)
- Save a preset and reload the page to verify persistence
- Drag from Sidebar - Click and drag a Light or Fan icon from the left sidebar
- Drop on Canvas - Drop it anywhere on the "Testing Canvas" area
- Device appears centered - The device will be placed in the center with its control panel
- Power Toggle - Click to turn the light on/off
- Brightness Slider - Adjust brightness from 0-100%
- Color Temperature - Choose between Warm, Neutral, Cool, or Pink
- Power Toggle - Click to turn the fan on/off
- Speed Slider - Adjust fan speed from 0-100%
-
Save Preset
- Configure a device with desired settings
- Click "Save Preset" button
- Enter a unique name
- Click "Save"
-
Load Preset
- Drag a saved preset from the sidebar
- Drop on canvas
- Device will load with saved settings
-
Delete Preset
- Click the trash icon on any preset in the sidebar
- Click "OK" to confirm deletion
- Open Sidebar - Tap the hamburger menu icon (☰)
- Drag Devices - Long-press and drag items
- Touch Controls - Tap toggles, slide controls work with touch
- Close Sidebar - Tap outside or the X button
device-sandbox-simulator/
├── backend/
│ ├── api/
│ │ ├── devices/
│ │ │ ├── delete.php
│ │ │ ├── get.php
│ │ │ └── save.php
│ │ └── presets/
│ │ ├── delete.php
│ │ ├── get.php
│ │ ├── list.php
│ │ └── save.php
│ ├── config/
│ │ ├── Autoloader.php
│ │ ├── cors.php
│ │ └── env.php
│ ├── DeviceSandbox/
│ │ ├── Config/
│ │ │ ├── Database.php
│ │ │ └── Response.php
│ │ ├── Controllers/
│ │ │ └── BaseController.php
│ │ ├── Interfaces/
│ │ │ └── ModelInterface.php
│ │ ├── Models/
│ │ │ ├── BaseModel.php
│ │ │ ├── Device.php
│ │ │ └── Preset.php
│ │ └── Traits/
│ │ └── ValidatesDeviceSettings.php
│ ├── .env
│ ├── .env.example
│ └── .htaccess
│
├── database/
│ └── schema.sql
│
└── frontend/
├── src/
│ ├── components/
│ │ ├── Canvas/
│ │ ├── Controls/
│ │ ├── Devices/
│ │ ├── Icons/
│ │ ├── Modals/
│ │ ├── Notification/
│ │ └── Sidebar/
│ ├── context/
│ │ └── DeviceContext.tsx
│ ├── services/
│ │ └── api.ts
│ ├── types/
│ │ └── index.ts
│ ├── utils/
│ │ └── constants.ts
│ ├── App.tsx
│ ├── index.css
│ └── main.tsx
├── eslint.config.js
├── index.html
├── package.json
├── tsconfig.json
└── vite.config.ts
- MVC Pattern - Separation of concerns
- PSR-4 Autoloading - Modern PHP class loading
- RESTful API - Standard HTTP methods and status codes
- Singleton Pattern - Single database connection
- Trait-based Validation - Reusable validation logic
- CORS Handling - Secure cross-origin requests
- Component-based - Reusable React components
- Context API - Centralized state management
- TypeScript - Type-safe development
- Custom Hooks - Reusable logic (useDevice)
- Service Layer - API abstraction
- Constants - Centralized configuration
- devices table - Stores current device on canvas (only one)
- presets table - Stores saved device configurations
- JSON columns - Flexible settings storage
- Proper indexing - Optimized queries
- Constraints - Data integrity
http://localhost/device-sandbox-simulator/backend/api
GET /devices/get.phpResponse:
{
"success": true,
"data": {
"id": 1,
"type": "light",
"settings": {
"power": true,
"brightness": 75,
"colorTemp": "cool"
}
}
}POST /devices/save.php
Content-Type: application/json
{
"type": "light",
"settings": {
"power": false,
"brightness": 0,
"colorTemp": "warm"
}
}DELETE /devices/delete.phpGET /presets/list.phpPOST /presets/save.php
Content-Type: application/json
{
"name": "Bright Cool Light",
"device_type": "light",
"device_settings": {
"power": true,
"brightness": 100,
"colorTemp": "cool"
}
}DELETE /presets/delete.php/{id}Problem: "Database connection failed"
Solution:
1. Check MySQL is running
2. Verify credentials in backend/.env
3. Ensure database 'device_sandbox' exists
Problem: "404 Not Found" on API endpoints
Solution:
1. Verify backend is in Apache htdocs directory
2. Check .htaccess file exists
3. Ensure mod_rewrite is enabled
4. Restart Apache
Problem: "CORS policy" errors in browser console
Solution:
1. Check backend/config/cors.php includes your frontend URL
2. Verify Apache mod_headers is enabled
3. Clear browser cache
Problem: "Permission denied" errors
Solution (Linux/macOS):
sudo chmod -R 755 /path/to/backend
sudo chown -R www-data:www-data /path/to/backend
Problem: "Cannot connect to server"
Solution:
1. Verify backend URL in constants.ts
2. Check backend is running and accessible
3. Test API directly in browser
Problem: "Module not found" errors
Solution:
cd frontend
rm -rf node_modules package-lock.json
npm install
- ✅ Input sanitization in backend
- ✅ SQL injection prevention (prepared statements)
- ✅ CORS configuration
- ✅ XSS protection headers
- ✅ Directory browsing disabled
- ✅ .env file protection
⚠️ Note: This is a development setup. For production:- Use environment-specific .env files
- Enable HTTPS
- Implement authentication
- Add rate limiting
- Use production database credentials
This project is created for interview purposes and educational use.
Created by Md. Mesbah Hossain
For issues or questions:
- Check the Troubleshooting section above
- Review API documentation
- Contact: [email protected]
