[apps/tidep0084.git] / tutorials / generic_sensor_tutorial / final / SensorToCloud / example / iot-gateway / webserver / webserver.js
1 /******************************************************************************
3 @file webserver.js
5 @brief webserver implementation
7 Group: WCS LPC
8 $Target Devices: Linux: AM335x, Embedded Devices: CC1310, CC1350$
10 ******************************************************************************
11 $License: BSD3 2016 $
13 Copyright (c) 2015, Texas Instruments Incorporated
14 All rights reserved.
16 Redistribution and use in source and binary forms, with or without
17 modification, are permitted provided that the following conditions
18 are met:
20 * Redistributions of source code must retain the above copyright
21 notice, this list of conditions and the following disclaimer.
23 * Redistributions in binary form must reproduce the above copyright
24 notice, this list of conditions and the following disclaimer in the
25 documentation and/or other materials provided with the distribution.
27 * Neither the name of Texas Instruments Incorporated nor the names of
28 its contributors may be used to endorse or promote products derived
29 from this software without specific prior written permission.
31 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
32 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
33 THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
34 PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
35 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
36 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
37 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
38 OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
39 WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
40 OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
41 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42 ******************************************************************************
43 $Release Name: TI-15.4Stack Linux x64 SDK ENG$
44 $Release Date: Nov 21, 2016 (2.00.01.09)$
45 *****************************************************************************/
47 var express = require('express');
48 var events = require("events");
49 var socket = require("socket.io")
50 var http = require("http");
53 /* Webserver Instance */
54 var webserverInstance;
56 /*!
57 * @brief Constructor for web-server
58 *
59 * @param none
60 *
61 * @retun none
62 */
63 function Webserver() {
65 /* There should be only one app client */
66 if (typeof webserverInstance !== "undefined") {
67 return webserverInstance;
68 }
70 /* Set up to emit events */
71 events.EventEmitter.call(this);
72 webserverInstance = this;
74 var port = 1350;
76 /* Set up webserver */
77 var app = express();
78 var server = http.createServer(app);
79 webserverInstance.io = socket.listen(server);
80 server.listen( port, '0.0.0.0');
81 var path = require('path');
82 app.use(express.static(path.join(__dirname, '/public')));
83 console.log("localhost port=",port);
84 app.get('/', function(req, res){
85 res.sendFile(__dirname + '/collectorApp.html');
86 });
88 /* Handle socket events */
89 webserverInstance.io.sockets.on('connection', function (socket) {
91 socket.on('setJoinPermit', function (data) {
92 webserverInstance.emit('setJoinPermitReq', data);
93 });
95 socket.on('getDevArrayReq', function (data) {
96 webserverInstance.emit('getDevArrayReq', data);
97 });
99 socket.on('getNwkInfoReq', function (data) {
100 webserverInstance.emit('getNwkInfoReq', data);
101 });
103 socket.on('sendConfig', function (data) {
104 webserverInstance.emit('sendConfig', data);
105 });
107 socket.on('sendToggle', function (data) {
108 webserverInstance.emit('sendToggle', data);
109 });
111 socket.on('sendGeneric', function (data) {
112 webserverInstance.emit('sendGeneric', data);
113 });
115 });
117 /**********************************************************************
118 Public method to send Update Messages to the client
119 ***********************************************************************/
120 webserverInstance.webserverSendToClient = function(msgType, data){
121 webserverInstance.io.sockets.emit(msgType, data);
122 };
123 webserverInstance.cloudAdapter_sendDeviceInfoMsg = function(data, extAddr){
124 webserverInstance.io.sockets.emit('connDevInfoUpdate', data);
125 };
126 webserverInstance.cloudAdapter_sendNetworkInfoMsg = function(data){
127 webserverInstance.io.sockets.emit('nwkUpdate', data);
128 };
129 }
131 Webserver.prototype.__proto__ = events.EventEmitter.prototype;
133 module.exports = Webserver;