- New iobroker_sources + iobroker_states tables - Source manager with discover, test, CRUD - State poller (10s interval, getBulk endpoint) - REST API routes under /api/iobroker - Frontend page with search + pagination - Sidebar link, app route Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
58 lines
1.7 KiB
TypeScript
58 lines
1.7 KiB
TypeScript
import bcrypt from 'bcryptjs';
|
|
import { initDb } from './db/index';
|
|
import { createApp } from './api/index';
|
|
import { connectAllEnabledBrokers } from './modules/mqtt/broker-manager';
|
|
import { connectAllEnabledSources } from './modules/postgres/source-manager';
|
|
import { startAllQueryTimers } from './modules/postgres/query-scheduler';
|
|
import { startAllPollers } from './modules/iobroker/state-poller';
|
|
|
|
const PORT = process.env.PORT ? parseInt(process.env.PORT) : 4000;
|
|
const DB_PATH = process.env.DB_PATH ?? './display.db';
|
|
|
|
async function main(): Promise<void> {
|
|
// Initialize database
|
|
const db = initDb(DB_PATH);
|
|
|
|
// Create default admin user if no users exist
|
|
const userCount = db
|
|
.prepare('SELECT COUNT(*) as count FROM users')
|
|
.get() as { count: number };
|
|
|
|
if (userCount.count === 0) {
|
|
const hash = bcrypt.hashSync('admin', 10);
|
|
db.prepare(
|
|
`INSERT INTO users (username, password_hash) VALUES (?, ?)`
|
|
).run('admin', hash);
|
|
console.log('Created default admin user (admin/admin)');
|
|
}
|
|
|
|
// Connect all enabled MQTT brokers
|
|
await connectAllEnabledBrokers();
|
|
|
|
// Connect all enabled PostgreSQL sources and start query timers
|
|
await connectAllEnabledSources();
|
|
startAllQueryTimers();
|
|
|
|
// Start ioBroker state pollers for all enabled sources
|
|
startAllPollers();
|
|
|
|
// Create and start Express app
|
|
const app = createApp();
|
|
|
|
app.listen(PORT, () => {
|
|
console.log(`Middleware server running on port ${PORT}`);
|
|
});
|
|
}
|
|
|
|
// Only run when executed directly (not during tests)
|
|
if (require.main === module) {
|
|
main().catch((err) => {
|
|
console.error('Fatal startup error:', err);
|
|
process.exit(1);
|
|
});
|
|
}
|
|
|
|
// Export createApp for tests that import from index
|
|
export { createApp };
|
|
export default createApp();
|