]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - apps/tidep0084.git/blob - example/iot-gateway/iot-gateway.js
Updated TI Linux Sensor To Cloud to the latest TI 15.4-Stack v2.4, now with CC13x2...
[apps/tidep0084.git] / example / iot-gateway / iot-gateway.js
1 /******************************************************************************
3  @file iot-gateway.js
5  @brief IoT gateway
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$
44  $Release Date: July 14, 2016 (2.00.00.30)$
45  *****************************************************************************/
46 var AppClient = require("./appClient/appclient.js");
47 var AwsAdapter = require("./cloudAdapter/awsCloudAdapter.js");
48 var IbmAdapter = require("./cloudAdapter/ibmCloudAdapter.js");
49 var QuickstartAdapter = require("./cloudAdapter/ibmQuickstartAdapter.js");
50 var LocalAdapter = require("./webserver/webserver.js");
51 var isAws = false;
52 var isIbm = false;
53 var isQuickstart = false;
54 var isLocal = false;
56 /*!
57  * @brief      Constructor for IoT gateway
58  *
59  * @param      none
60  *
61  * @return      none
62  */
63 function Gateway() {
64         if (process.argv.length < 3)
65         {
66                 console.log("Usage: node "+__filename+"'cloud service'");
67                 console.log("Cloud service being 'aws' or 'ibm'");
68                 process.exit(1);
69         }
70         var appClient = new AppClient();
71         var cloudAdapter = {};
72         if (process.argv[2].toLowerCase() == "aws"){
73                 console.log("Starting AWS Cloud Adapter");
74                 cloudAdapter = new AwsAdapter();
75                 isAws = true;
77         }
78         else if (process.argv[2].toLowerCase() == "ibm"){
79                 console.log("Starting IBM Cloud Adapter");
80                 cloudAdapter = new IbmAdapter();
81                 isIbm = true;
82         }
83         else if (process.argv[2].toLowerCase() == "quickstart"){
84                 console.log("Starting IBM Quickstart Adapter");
85                 cloudAdapter = new QuickstartAdapter();
86                 isQuickstart = true;
87         }
88         else if (process.argv[2].toLowerCase() == "localhost"){
89                 console.log("Starting Localhost Gateway");
90                 cloudAdapter = new LocalAdapter();
91                 isLocal = true;
92         }
93         else {
94                 console.log(process.argv[2]+" is not a supported cloud service.\nPlease use 'aws' or 'ibm'");
95                 process.exit(1);
96         }
98         /* Used for network startup, wait until we receive a network
99          * info update and a device array before sending the first
100          * Network Information Message Type to the Cloud
101          */
102         var gotNwkUpdate = false;
104         /* This functions checks every second to see if we've got the
105          * network and device information we need and also checks if we are
106          * connected to the cloud yet. Once all of that is true, we send
107          * the first Network Information Message Type to the Cloud and
108          * then clear the interval so the function won't repeat.
109          */
110         var intervalID = setInterval( function() {
111                 if (cloudAdapter.connected && gotNwkUpdate){
112                         var nwkInfo = formatNwkInfoMsg(appClient.nwkInfo, appClient.connectedDeviceList);
113                         cloudAdapter.cloudAdapter_sendNetworkInfoMsg(nwkInfo);
114                         if (isAws){
115                                 console.log('Visit the following url in your web browser to view the dashboard:');
116                                 console.log('iotdash.stackbuilder.us/#/dashboard/home?net=' + nwkInfo.ext_addr);
117                         }
118                         if (isQuickstart){
119                                 console.log("Quickstart DevID="+nwkInfo.ext_addr);
120                         }
121                         clearInterval(intervalID);
122                 }
123         }, 1000);
125         /* Allows the Cloud to open or close the wireless network for new devices joins */
126         cloudAdapter.on('updateNetworkState', function (data) {
127                 appClient.appC_setPermitJoin(data);
128         });
130         /* Allows the Cloud to sent actuation messages to devices in the wireless network */
131         cloudAdapter.on('deviceActuation', function (data) {
132                 appClient.appC_sendDeviceActuation(data);
133         });
135         /* rcvd send config req */
136         cloudAdapter.on('sendConfig', function (data) {
137                 /* send config request */
138                 appClient.appC_sendConfig(data);
139         });
141         /* rcvd getNwkInfoReq */
142         cloudAdapter.on('getNwkInfoReq', function (data) {
143                 /* process the request */
144                 appClient.appC_getNwkInfo();
145         });
147         cloudAdapter.on('setJoinPermitReq', function (data) {
148                 /* process the request */
149                 appClient.appC_setPermitJoin(data);
150         });
152         /* A nwkUpdate (with the updated state) is also sent when the state changes so we don't
153          * need to take any action here
154          */
155         appClient.on('permitJoinCnf', function (data) {
156                 console.log('gateway_permitJoinCnf');
157         });
159         /* Send Device Information Message to Cloud Adapter */
160         appClient.on('connDevInfoUpdate', function (data) {
161                 var devInfo = formatDevInfoMsg(data);
162                 cloudAdapter.cloudAdapter_sendDeviceInfoMsg(devInfo, '0x' + appClient.nwkInfo.panCoord.extAddress.toString(16));
163         });
165         /* Send Network Information Message to Cloud Adapter */
166         appClient.on('nwkInfo', function () {
167                 gotNwkUpdate = true;
168                 var nwkInfo = formatNwkInfoMsg(appClient.nwkInfo, appClient.connectedDeviceList);
169                 console.log("nwkInfo:", nwkInfo);
170                 cloudAdapter.cloudAdapter_sendNetworkInfoMsg(nwkInfo);
171         });
173         /* The Network Information Message Type should be common across Cloud vendors. This function
174          * formats the message in a generic way. The CloudAdapter can make vendor specific
175          * modifications before sending to the Cloud if necessary.
176          */
177         function formatNwkInfoMsg(nwkInfo, conDevs) {
178                 var devices = [];
179                 if (nwkInfo == null || conDevs == null)
180                 {
181                         console.log("formatNwkInfoMsg: invalid parameters");
182                         return null;
183                 }       
184                 var nwkAddress = '0x' + nwkInfo.panCoord.extAddress.toString(16);
185                 for(i = 0; i < conDevs.length; i++) {
186                         var short_addr = '0x' + conDevs[i].shortAddress.toString(16);
187                         var ext_addr = '0x' + conDevs[i].extAddress.toString(16);
188                         var active = conDevs[i].active;
189                         devices.push( {
190                                 "name" : short_addr,
191                                 "active" : conDevs[i].active,
192                                 "rssi" : conDevs[i].rssi,
193                                 "last_reported" : conDevs[i].lastreported,
194                                 "short_addr" : short_addr,
195                                 "ext_addr" : ext_addr,
196                                 "topic" : 'ti_iot_' + nwkAddress + '_' + ext_addr,
197                                 "smart_objects" : conDevs[i].so.dump(function (err, data) {
198                                                 if (!err)
199                                                         return data;
200                                         })
201                         });
202                 }
204                 return {
205                         "name" : '0x' + nwkInfo.panCoord.shortAddress.toString(16),
206                         "channels" : nwkInfo.channel,
207                         "pan_id" : '0x' + nwkInfo.panCoord.panId.toString(16),
208                         "short_addr" : '0x' + nwkInfo.panCoord.shortAddress.toString(16),
209                         "ext_addr" : nwkAddress,
210                         "security_enabled" : nwkInfo.securityEnabled,
211                         "mode" : nwkInfo.networkMode,
212                         "state" : nwkInfo.state,
213                         "devices" : devices
214                 }
215         }
217         /* The Device Information Message Type should be common across Cloud vendors. This function
218          * formats the message in a generic way. The CloudAdapter can make vendor specific
219          * modifications before sending to the Cloud if necessary.
220          */
221         function formatDevInfoMsg(dev) {
222                 return {
223                         "active" : dev.active,
224                         "short_addr": '0x' + dev.shortAddress.toString(16),
225                         "ext_addr" : '0x' + dev.extAddress.toString(16),
226                         "rssi" : dev.rssi,
227                         "last_reported" : dev.lastreported,
228                         "smart_objects" : dev.so.dump(function (err, data) {
229                                                 if (!err)
230                                                         return data;
231                                         })
232                 }
233         }
236 /* create gateway */
237 var gateway = new Gateway();