summaryrefslogtreecommitdiff
path: root/server.js
blob: f8dcd8aa0e31ee19ba7518c9a32cd1504407e6e0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
'use strict';

var express = require('express');
var fs = require('fs');
var request = require('request');
var MjpegProxy = require('mjpeg-proxy').MjpegProxy;

var app = express();

var cameras = [
	{
		baseurl: 'http://admin:123456@192.168.0.33',
		model: 'conceptronic'
	},
	{
		baseurl: 'http://admin:123456@192.168.0.117',
		model: 'conceptronic'
	},
	{
		url: 'http://admin:123456@192.168.0.178',
		model: 'conceptronic'
	}
];

var still_frames = [
	fs.readFileSync(__dirname + '/noise.jpg'),
	fs.readFileSync(__dirname + '/noise2.jpg'),
];

function SimpleProxy(url) {
	var self = this;

	self.url = url;
	self.handle_request = function(req, res) {
		console.log('Got request. Forwarding to ' + self.url);
		request(self.url).pipe(res);
	};
}

for (var camera_idx in cameras) {
	var camera = cameras[camera_idx];
	var urls = {};

	if (camera.model == 'conceptronic') {
		urls.mjpeg = camera.baseurl + '/videostream.cgi?rate=1';
		urls.down = camera.baseurl
			  + '/decoder_control.cgi?onestep=1&command=0';
		urls.up = camera.baseurl
			+ '/decoder_control.cgi?onestep=1&command=2';
		urls.right = camera.baseurl
			   + '/decoder_control.cgi?onestep=1&command=4';
		urls.left = camera.baseurl
			  + '/decoder_control.cgi?onestep=1&command=6';
		urls.snapshot = camera.baseurl + '/snapshot.cgi';
	} else if (camera.model == 'mjpegplexer') {
		urls.mjpeg = camera.baseurl + '/stream.mjpeg';
		urls.down = camera.baseurl + '/control/down';
		urls.up = camera.baseurl + '/control/up';
		urls.right = camera.baseurl + '/control/right';
		urls.left = camera.baseurl + '/control/left';
		urls.snapshow = camera.baseurl + '/snapshot.jpg';
	};

	camera.proxy = new MjpegProxy(urls.mjpeg, still_frames);
	app.get('/cam/' + camera_idx + '/stream.mjpeg', camera.proxy.proxyRequest);
	for (var url in urls) {
		if (url == 'mjpeg' || url == 'snapshot')
			continue;
		app.get('/cam/' + camera_idx + '/control/' + url,
				new SimpleProxy(urls[url]).handle_request);
	}
	app.get('/cam/' + camera_idx + '/snapshot.jpg',
			new SimpleProxy(urls.snapshot).handle_request);
	app.use('/cam/' + camera_idx + '/browse',
			express.static(__dirname + '/cam_public'));
}

app.get('/blackout', function(req, res) {
	for (var camera_idx in cameras) {
		var camera = cameras[camera_idx];

		camera.proxy.on_air = !camera.proxy.on_air;
	}
	res.send('OK');
});


app.listen(8080);