UNPKG

3.53 kBJavaScriptView Raw
1/*
2
3BSD LICENSED
4
5Copyright (c) 2011, Janne Julkunen
6All rights reserved.
7
8Redistribution and use in source and binary forms, with or without modification,
9are permitted provided that the following conditions are met:
10
11* Redistributions of source code must retain the above copyright notice, this
12list of conditions and the following disclaimer.
13
14* Redistributions in binary form must reproduce the above copyright notice,
15this list of conditions and the following disclaimer in the documentation
16and/or other materials provided with the distribution.
17
18* Neither the name of the Enlightened Linux Solutions nor the names of its
19contributors may be used to endorse or promote products derived from this
20software without specific prior written permission.
21
22THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
23ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
24WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
26INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
30OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
31OF THE POSSIBILITY OF SUCH DAMAGE.
32
33*/
34
35var addr = "127.0.0.1";
36var port = 3000;
37
38var net = require('net');
39var dgram = require('dgram');
40var express = require('express');
41var form = require('connect-form');
42
43var loaded = [];
44
45var modules = ["app/banshee", "app/frontrow", "app/itunes", "app/mpd",
46 "app/quicktime", "app/rhythmbox", "app/totem",
47 "sys/1-wire", "sys/input", "sys/sound", "sys/surveillance"];
48
49//
50
51console.log("Home Control server: starting");
52
53var socket = net.createConnection(80, 'www.google.com');
54
55socket.on('connect', function() {
56
57 addr = socket.address().address;
58
59 var ssd_srv = dgram.createSocket("udp4");
60
61 ssd_srv.on("message", function (msg, rinfo) {
62 console.log("Received SSD message: " + rinfo.address + ":" + rinfo.port);
63
64 if(msg.slice(0, 8) == "M-SEARCH") {
65 var message = new Buffer(
66 "HTTP/1.1 200 OK\r\n" +
67 "LOCATION: http://" + addr + ":" + port + "/\r\n" +
68 "SERVER: Home Control\r\n" +
69 "ST: " +
70 "EXT: " +
71 "\r\n"
72 );
73
74 console.log("Sending SSD message: " + addr + ":" + port);
75
76 var client = dgram.createSocket("udp4");
77
78 client.send(message, 0, message.length, rinfo.port, rinfo.address);
79
80 client.close();
81 }
82 });
83
84 ssd_srv.bind(1900);
85
86 try {
87 ssd_srv.addMembership('239.255.255.250');
88 } catch (error) {
89 console.log("Automatic discovery: disabled");
90 }
91
92 socket.end();
93});
94
95var http_srv = express.createServer(
96 form({ keepExtensions: true })
97);
98
99http_srv.get("/modules", function(req, res) {
100 res.send(loaded);
101});
102
103for(var i = 0; i < modules.length; i++) {
104 var module = require("./lib/" + modules[i]);
105
106 module.setup(function(module, moduleID, moduleName, moduleCategory) {
107 if((moduleID) && (moduleName) && (moduleCategory)) {
108 console.log("Loading " + moduleCategory + " module: " + moduleName);
109
110 http_srv.get("/" + moduleID + "/*", module.execute);
111
112 http_srv.post("/" + moduleID + "/*", module.execute);
113
114 loaded.push({id: moduleID, name: moduleName, category: moduleCategory});
115 }
116 }.bind(this, module));
117}
118
119http_srv.listen(port);
120