initial upload
[apps/native/smart-surveillance-camera.git] / dashboard / server.js
1  /*\r
2  * Copyright (c) 2018 Samsung Electronics Co., Ltd.\r
3  *\r
4  * Licensed under the Apache License, Version 2.0 (the "License");\r
5  * you may not use this file except in compliance with the License.\r
6  * You may obtain a copy of the License at\r
7  *\r
8  * http://www.apache.org/licenses/LICENSE-2.0\r
9  *\r
10  * Unless required by applicable law or agreed to in writing, software\r
11  * distributed under the License is distributed on an AS IS BASIS,\r
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
13  * See the License for the specific language governing permissions and\r
14  * limitations under the License.\r
15  */\r
16 \r
17 var fs = require('fs');\r
18 var http = require('http');\r
19 \r
20 function extractPath(url) {\r
21   var urlParts = url.split('/'),\r
22     i = 0,\r
23     l = urlParts.length,\r
24     result = [];\r
25   for (; i < l; ++i) {\r
26     if (urlParts[i].length > 0) {\r
27       result.push(urlParts[i]);\r
28     }\r
29   }\r
30   return result;\r
31 }\r
32 \r
33 http.createServer(function(req, res) {\r
34   req.on('end', function() {\r
35     var path = extractPath(req.url);\r
36     console.log(req.url)\r
37     // var last = path[path.length - 1];\r
38     if (path[0] === undefined) {\r
39       res.writeHead(200);\r
40       res.end(fs.readFileSync('public/index.html'));\r
41     } else if (path[0] == 'test') {\r
42       res.writeHead(200);\r
43       res.end(fs.readFileSync('public/test.html'));\r
44     } else if (req.url == '/js/app.js') {\r
45       res.writeHead(200);\r
46       res.end(fs.readFileSync('public/js/app.js'));\r
47     } else if (req.url == '/css/style.css') {\r
48       res.writeHead(200);\r
49       res.end(fs.readFileSync('public/css/style.css'));\r
50     } else {\r
51       res.setHeader('Location', 'http://download.tizen.online/smart-surveillance' + req.url);\r
52       res.writeHead(302);\r
53       res.end();\r
54       console.log('33333');\r
55     }\r
56   });\r
57 }).listen(9090);\r
58 \r
59 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////\r
60 \r
61 var ENABLE_WEBSOCKET = true;\r
62 \r
63 if (ENABLE_WEBSOCKET) {\r
64 \r
65   var websocket = require('websocket');\r
66 \r
67   var options = {\r
68     port: 8888\r
69   }\r
70 \r
71   var server = new websocket.Server(options, Listener);\r
72 \r
73   function Listener(ws) {\r
74     console.log('Client connected: handshake done!');\r
75     ws.ack = true;\r
76     ws.on('message', function (msg) {\r
77       console.log('Message received: %s', msg.toString());\r
78       // ws.send(msg.toString(), {mask: true, binary: false}); //echo\r
79       // ws.send('Received: ' + msg.toString()); //echo\r
80       // server.close();\r
81       ws.ack = true;\r
82     });\r
83     ws.on('ping', function (msg) {\r
84       console.log('Ping received: %s', msg.toString());\r
85     });\r
86     ws.on('error', function (msg) {\r
87       console.log('Error: %s', msg.toString());\r
88     });\r
89 \r
90     var i = 0;\r
91     var prev = 0;\r
92     var timeout = setInterval(function() {\r
93       if (!ws.ack)\r
94         return false;\r
95       var now = Date.now();\r
96       var data;\r
97       try {\r
98         data = fs.readFileSync('/tmp/latest.jpg');\r
99       } catch (err) {\r
100         data = fs.readFileSync('/opt/home/dashboard/default.gif');\r
101       }\r
102       ws.send(data, {mask: false, binary: true});\r
103       console.log(`Sending frame(${i++}), interval(${now - prev} ms)`);\r
104       // server.broadcast(data, {mask: false, binary: true});\r
105       // server.broadcast(`HELLO TO ALL FROM IoT.js!!! (${i++}, ${now - prev})`);\r
106       prev = now;\r
107       ws.ack = false;\r
108     }, 1000 / 15.0);\r
109 \r
110     ws.on('close', function (msg) {\r
111       console.log('Client close: ' + msg.reason + ' (' + msg.code + ')');\r
112       clearInterval(timeout);\r
113     });\r
114   };\r
115 \r
116   server.on('error', function (msg) {\r
117     console.log('Error: %s', msg.toString());\r
118   });\r
119 \r
120   server.on('close', function (msg) {\r
121     console.log('Server close: ' + msg.reason + ' (' + msg.code + ')');\r
122   });\r
123 \r
124 }\r