Skip to content

Commit ece76f0

Browse files
committed
Improve codebase accessibility for newcomers and contributors
- Add comprehensive JSDoc documentation to main classes (CgateWebBridge, CBusEvent, CBusCommand) - Create detailed CONTRIBUTING.md with setup guide, architecture overview, and development workflows - Add inline comments explaining complex C-Bus domain logic and addressing concepts - Create extensive settings.js.example with detailed configuration examples and C-Bus reference - Enhance package.json with improved scripts for development, testing, and setup workflows - Document C-Bus addressing format (network/application/group), level conversions, and MQTT mappings These changes significantly reduce the learning curve for new contributors by providing clear documentation, practical examples, and comprehensive development guidance.
1 parent 065461c commit ece76f0

File tree

7 files changed

+702
-15
lines changed

7 files changed

+702
-15
lines changed

CONTRIBUTING.md

Lines changed: 239 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,239 @@
1+
# Contributing to CGateWeb
2+
3+
Welcome! This guide will help you get started contributing to CGateWeb, a Node.js bridge that connects Clipsal C-Bus automation systems to MQTT for Home Assistant integration.
4+
5+
## Getting Started
6+
7+
### Prerequisites
8+
9+
- **Node.js 14+**: This project uses modern JavaScript features
10+
- **C-Gate Server**: Clipsal's C-Bus automation gateway software
11+
- **MQTT Broker**: Like Mosquitto or built into Home Assistant
12+
- **Git**: For version control and contributing
13+
14+
### Development Setup
15+
16+
1. **Clone the repository**
17+
```bash
18+
git clone <repository-url>
19+
cd cgateweb
20+
```
21+
22+
2. **Install dependencies**
23+
```bash
24+
npm install
25+
```
26+
27+
3. **Copy and configure settings**
28+
```bash
29+
cp settings.js.example settings.js
30+
# Edit settings.js with your C-Gate and MQTT broker details
31+
```
32+
33+
4. **Run tests**
34+
```bash
35+
npm test
36+
```
37+
38+
5. **Run the application**
39+
```bash
40+
npm start
41+
```
42+
43+
## Understanding the Codebase
44+
45+
### Architecture Overview
46+
47+
CGateWeb acts as a bidirectional bridge:
48+
49+
```
50+
C-Bus Devices ←→ C-Gate Server ←→ CGateWeb ←→ MQTT Broker ←→ Home Assistant
51+
```
52+
53+
### Key Components
54+
55+
- **`CgateWebBridge`** (`src/cgateWebBridge.js`): Main orchestration class
56+
- **`CBusEvent`** (`src/cbusEvent.js`): Parses events from C-Gate
57+
- **`CBusCommand`** (`src/cbusCommand.js`): Parses MQTT commands for C-Gate
58+
- **`MqttManager`** (`src/mqttManager.js`): Handles MQTT connections
59+
- **`CgateConnection`** (`src/cgateConnection.js`): Manages C-Gate socket connections
60+
- **`HaDiscovery`** (`src/haDiscovery.js`): Home Assistant device discovery
61+
- **`ThrottledQueue`** (`src/throttledQueue.js`): Rate-limited message processing
62+
63+
### C-Bus Concepts
64+
65+
Understanding C-Bus addressing is essential:
66+
67+
- **Network**: Typically 254 (default network)
68+
- **Application**: Device type (56 = lighting, 36 = triggers, etc.)
69+
- **Group**: Individual device/channel number
70+
- **Level**: 0-255 brightness for lights (0 = off, 255 = full brightness)
71+
72+
Example: `254/56/4` = Network 254, Lighting Application, Group 4
73+
74+
### MQTT Message Flow
75+
76+
**Incoming Commands (MQTT → C-Gate):**
77+
- MQTT: `cbus/write/254/56/4/switch` payload `ON`
78+
- C-Gate: `on //PROJECT/254/56/4`
79+
80+
**Outgoing Events (C-Gate → MQTT):**
81+
- C-Gate: `lighting on 254/56/4`
82+
- MQTT: `cbus/read/254/56/4/state` payload `ON`
83+
84+
## Development Guidelines
85+
86+
### Code Style
87+
88+
- Use **descriptive variable names** that explain C-Bus concepts
89+
- Follow **existing patterns** in the codebase
90+
- Add **JSDoc comments** for public methods and complex logic
91+
- Use **ES6+ features** (const/let, arrow functions, destructuring)
92+
- Keep functions **focused and small**
93+
94+
### Testing Requirements
95+
96+
**Before submitting any changes:**
97+
98+
1. **Write tests first** for new functionality
99+
2. **Run all tests**: `npm test`
100+
3. **Ensure 100% test pass rate** (currently 134 tests)
101+
4. **Update tests** when modifying existing functionality
102+
103+
### Test Structure
104+
105+
Tests use **Jest** with extensive mocking:
106+
107+
```javascript
108+
// Example test pattern
109+
describe('ClassName', () => {
110+
let mockDependency;
111+
112+
beforeEach(() => {
113+
mockDependency = {
114+
method: jest.fn()
115+
};
116+
});
117+
118+
it('should handle specific scenario', () => {
119+
// Arrange
120+
const instance = new ClassName(mockDependency);
121+
122+
// Act
123+
const result = instance.method();
124+
125+
// Assert
126+
expect(result).toBe(expectedValue);
127+
expect(mockDependency.method).toHaveBeenCalledWith(expectedArgs);
128+
});
129+
});
130+
```
131+
132+
### Common Development Tasks
133+
134+
#### Adding New C-Bus Command Types
135+
136+
1. Add command constant to `src/constants.js`
137+
2. Update `CBusCommand._parsePayload()` method
138+
3. Add handler method in `CgateWebBridge`
139+
4. Write tests for the new command type
140+
141+
#### Adding New C-Gate Response Types
142+
143+
1. Add response code constant to `src/constants.js`
144+
2. Update `CgateWebBridge._processCommandResponse()` switch statement
145+
3. Implement response handler method
146+
4. Write tests for the new response type
147+
148+
#### Debugging Connection Issues
149+
150+
Enable detailed logging in `settings.js`:
151+
```javascript
152+
logging: true // Shows all MQTT and C-Gate messages
153+
```
154+
155+
## Submitting Changes
156+
157+
### Pull Request Process
158+
159+
1. **Create feature branch**: `git checkout -b feature/your-feature-name`
160+
2. **Make changes** with tests
161+
3. **Verify tests pass**: `npm test`
162+
4. **Commit with clear messages**: Focus on the "why" not just "what"
163+
5. **Push and create PR**
164+
165+
### Commit Message Format
166+
167+
Use clear, descriptive commit messages:
168+
169+
```
170+
Add support for C-Bus trigger commands
171+
172+
- Implement MQTT command parsing for trigger devices
173+
- Add trigger command handlers in bridge
174+
- Include comprehensive tests for trigger functionality
175+
176+
Resolves #123
177+
```
178+
179+
### Code Review Checklist
180+
181+
- [ ] All tests pass
182+
- [ ] New functionality has tests
183+
- [ ] JSDoc comments added for public methods
184+
- [ ] No breaking changes to existing API
185+
- [ ] Error handling for edge cases
186+
- [ ] Logging for debugging
187+
- [ ] Settings validation if applicable
188+
189+
## Getting Help
190+
191+
### Understanding C-Bus
192+
193+
- **C-Gate Manual**: Essential reading for C-Bus concepts
194+
- **C-Bus Toolkit**: GUI tool for testing C-Gate connections
195+
- **Existing tests**: Great examples of expected behavior
196+
197+
### Common Issues
198+
199+
**"Connection refused to C-Gate"**
200+
- Verify C-Gate is running and accessible
201+
- Check firewall settings
202+
- Confirm port numbers (typically 20023/20024)
203+
204+
**"MQTT publish failed"**
205+
- Verify MQTT broker is running
206+
- Check authentication credentials
207+
- Test with MQTT client tools
208+
209+
**"Tests failing after changes"**
210+
- Run `npm test` to see specific failures
211+
- Check if you broke existing functionality
212+
- Update test expectations if behavior intentionally changed
213+
214+
### Project Structure
215+
216+
```
217+
cgateweb/
218+
├── src/ # Main source code
219+
│ ├── cgateWebBridge.js # Main bridge class
220+
│ ├── cbusEvent.js # C-Bus event parsing
221+
│ ├── cbusCommand.js # MQTT command parsing
222+
│ ├── constants.js # Shared constants
223+
│ └── ...
224+
├── tests/ # Jest test files
225+
├── settings.js.example # Configuration template
226+
├── index.js # Application entry point
227+
└── package.json # Dependencies and scripts
228+
```
229+
230+
## Contributing Philosophy
231+
232+
This project aims to be:
233+
234+
- **Reliable**: Robust error handling and comprehensive testing
235+
- **Maintainable**: Clear code structure and documentation
236+
- **Accessible**: Easy for newcomers to understand and contribute
237+
- **Stable**: Changes shouldn't break existing integrations
238+
239+
Thank you for contributing to CGateWeb! Your help makes C-Bus automation more accessible to the open source community.

package.json

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
{
22
"name": "cgateweb",
33
"version": "0.0.1",
4+
"description": "Node.js bridge connecting Clipsal C-Bus automation systems to MQTT for Home Assistant integration",
5+
"keywords": ["cbus", "clipsal", "mqtt", "home-assistant", "automation", "smart-home", "cgate", "bridge"],
46
"private": true,
57
"dependencies": {
68
"mqtt": "^5.11.0",
@@ -10,7 +12,16 @@
1012
"jest": "^29.7.0"
1113
},
1214
"scripts": {
15+
"start": "node index.js",
1316
"test": "jest",
14-
"start": "node index.js"
17+
"test:watch": "jest --watch",
18+
"test:coverage": "jest --coverage",
19+
"dev": "node index.js",
20+
"dev:debug": "node --inspect index.js",
21+
"install-service": "node install-service.js",
22+
"uninstall-service": "node uninstall-service.js",
23+
"setup": "cp settings.js.example settings.js && echo 'Settings file created! Edit settings.js to configure your C-Gate and MQTT settings.'",
24+
"lint": "echo 'No linter configured. Consider adding ESLint for code quality.'",
25+
"validate-settings": "node -e \"try { require('./settings.js'); console.log('Settings file is valid!'); } catch(e) { console.error('Settings file error:', e.message); process.exit(1); }\""
1526
}
1627
}

0 commit comments

Comments
 (0)