doccam-pi/main.js

94 lines
3.7 KiB
JavaScript
Raw Permalink Normal View History

2017-04-18 17:23:59 +12:00
/**
2017-08-08 01:13:18 +02:00
* @module Main
* @description The main module and entry point.
2017-04-18 17:23:59 +12:00
*/
2017-08-08 01:13:18 +02:00
const redux = require('redux');
const ReduxThunk = require('redux-thunk').default;
const communicator = require('./src/communicator.js');
const commander = require('./src/commander.js');
const logger = require('./src/logger.js');
const ssh = require('./src/ssh.js');
2016-09-07 14:45:15 +12:00
2017-04-18 17:23:59 +12:00
///////////////////////////////////////////////////////////////////////////////
2017-08-08 01:13:18 +02:00
// Redux //
2017-04-18 17:23:59 +12:00
///////////////////////////////////////////////////////////////////////////////
2016-09-07 15:15:25 +12:00
2017-08-08 01:13:18 +02:00
/**
* Inital State for the app.
* @property { Object } stream Stream Status.
* @property { bool } stream.running Indicates wether the stream is running.
* @property { number | bool } stream.error Error code. Any number outside the range of the Error array in @see ffmpeg-command.js will be treated as non error. Most conveniently set: false.
* @property { bool | string } stream.errorHandling Indicates wether the program tries to handle an error.
* @property { bool } stream.restaring Indicades wether the stream is currently being restarted.
* @property { bool } stream.takingSnapshot Indicades wether a Snapshot is being taken.
* @property { Object } config Configuration of the camera.
* @property { Object } ssh The SSH tunnel status.
* @property { bool } ssh.enabled
* @property { bool } ssh.willReconnect Indicates if the SSH-Manager will try to reconnect as soon as the connection to the manager is regained.
* @property { string } ssh.status Status of the SSH Connection. Can either be DISABLED | DISCONNECTED |DISCONNECTING | CONNECTING | CONNECTED
* @property { number } ssh.sshForwardport SSH remote endpoint port for the ssh reverse tunnel.
* @property { number } ssh.camForwardPort SSH remote endpoint port for the reverse tunnel to the control panel of the IP Camera.
* @property { string } ssh.error The SSH Error, if there is any. You might check ssh.willReconnect as well, to see if a retry is sceduled as soon as the connection to the manager is regained.
*/
let initialState = {
stream: {
running: 'STOPPED',
error: false,
reconnecting: false,
handleError: false,
restaring: false,
snapshot: {
taking: false,
failed: false
2016-08-26 14:35:18 +12:00
}
2017-08-08 01:13:18 +02:00
},
ssh: {
status: 'DISCONNECTED', // TODO: CD // TODO: Implement in WEBIF
2017-08-16 11:20:06 +02:00
camForwardPort: false,
2017-08-08 01:13:18 +02:00
sshForwardPort: false,
willReconnect: false,
error: false
},
connected: false,
config: false
2017-04-18 17:23:59 +12:00
};
2016-08-26 14:35:18 +12:00
2017-08-08 01:13:18 +02:00
// Reducer
const reducer = require('./src/reducers');
2016-08-26 14:35:18 +12:00
2017-08-08 01:13:18 +02:00
// Reference to the store of the application state.
const store = redux.createStore(reducer, initialState, redux.applyMiddleware(ReduxThunk, logger.middleware, communicator.middleware, ssh.middleware, commander.middleware));
2017-04-22 12:12:16 +12:00
2017-08-08 01:13:18 +02:00
// The dispatch function for the state.
const dispatch = store.dispatch;
2016-08-26 14:35:18 +12:00
2017-08-08 01:13:18 +02:00
// The function to get the state.
const getState = store.getState;
2016-08-26 14:35:18 +12:00
2017-08-08 01:13:18 +02:00
// A helper to get the config.
const getConfig = () => store.getState().config;
2016-08-26 14:35:18 +12:00
2017-08-08 01:13:18 +02:00
// Simple Action creators
const creators = require('./src/actions.js').creators;
2016-08-26 14:35:18 +12:00
2017-08-08 01:13:18 +02:00
///////////////////////////////////////////////////////////////////////////////
// Load //
///////////////////////////////////////////////////////////////////////////////
2016-09-01 23:28:19 +12:00
2017-08-08 01:13:18 +02:00
// Code the Config
dispatch(creators.updateConfig());
2016-08-29 17:10:01 +12:00
2017-08-08 01:13:18 +02:00
// Instanciate the Commander
const Commander = new commander(getState, getConfig, dispatch);
2016-08-29 17:10:01 +12:00
2017-08-08 01:13:18 +02:00
// The server Communication
const Communicator = new communicator(getState, getConfig, dispatch, Commander);
2016-08-29 17:10:01 +12:00
2017-08-08 01:13:18 +02:00
// Start the Stream
dispatch(Commander.start()).then().catch(); // TODO: Better Handling
2016-08-29 17:10:01 +12:00
2017-08-08 01:13:18 +02:00
/**
* We're good to go from here!
*/