|
| 1 | +/* Copyright 2018-present Samsung Electronics Co., Ltd. and other contributors |
| 2 | + * |
| 3 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | + * you may not use this file except in compliance with the License. |
| 5 | + * You may obtain a copy of the License at |
| 6 | + * |
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * |
| 9 | + * Unless required by applicable law or agreed to in writing, software |
| 10 | + * distributed under the License is distributed on an "AS IS" BASIS |
| 11 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | + * See the License for the specific language governing permissions and |
| 13 | + * limitations under the License. |
| 14 | + */ |
| 15 | + |
| 16 | +var console = require('console'); |
| 17 | + |
| 18 | +var config = require('./config.js'); |
| 19 | +var st = require('./smartthings.js'); |
| 20 | +var sthook = require('./stwebhook.js'); |
| 21 | +var fs = require('fs'); |
| 22 | + |
| 23 | +var config_ok = true; |
| 24 | +if (!config.authToken) { |
| 25 | + console.log('ERROR: No personal authToken specified in the config file'); |
| 26 | + console.log('Please visit the https://account.smartthings.com/tokens page'); |
| 27 | + console.log('To generate an authToken'); |
| 28 | + console.log(''); |
| 29 | + config_ok = false; |
| 30 | +} |
| 31 | + |
| 32 | +if (!config.targetUrl) { |
| 33 | + console.log('ERROR: No targetUrl specified in the config file'); |
| 34 | + console.log('The targetUrl should be a publicly accessible https url'); |
| 35 | + console.log(''); |
| 36 | + config_ok = false; |
| 37 | +} |
| 38 | + |
| 39 | +if (!config.appName) { |
| 40 | + console.log('ERROR: No appName specified in the config file'); |
| 41 | + console.log('The appName should be a globally unique name'); |
| 42 | + console.log(''); |
| 43 | + config_ok = false; |
| 44 | +} |
| 45 | + |
| 46 | +if (!config_ok) { |
| 47 | + process.exit(-1); |
| 48 | +} |
| 49 | + |
| 50 | +var demoApp = new sthook.STWebHook(); |
| 51 | + |
| 52 | +demoApp.on('CONFIGURATION', function(message, response) { |
| 53 | + console.log(' Got CONFIGURATION lifecycle'); |
| 54 | + var data = {}; |
| 55 | + |
| 56 | + if (message.configurationData.phase === 'INITIALIZE') { |
| 57 | + console.log(' Performing SmartApp initialization'); |
| 58 | + this._installedAppId = message.configurationData.installedAppId; |
| 59 | + var initData = { |
| 60 | + name: 'Example IoT.js SmartC2C app', |
| 61 | + description: data.name, |
| 62 | + id: 'demoappc2c', |
| 63 | + permissions: [ |
| 64 | + 'l:devices', |
| 65 | + 'r:devices:*', |
| 66 | + 'x:devices:*', |
| 67 | + 'r:schedules', |
| 68 | + 'w:schedules', |
| 69 | + 'i:deviceprofiles', |
| 70 | + 'r:locations:*', |
| 71 | + ], |
| 72 | + firstPageId: '1', |
| 73 | + }; |
| 74 | + |
| 75 | + data = { initialize: initData }; |
| 76 | + } else if (message.configurationData.phase === 'PAGE') { |
| 77 | + var pageData = { |
| 78 | + pageId: '1', |
| 79 | + name: 'Example IoT.js SmartC2C app', |
| 80 | + nextPageId: null, |
| 81 | + previousPageId: null, |
| 82 | + complete: true, |
| 83 | + sections: [ |
| 84 | + { |
| 85 | + name: 'C-2-C config', |
| 86 | + settings: [ |
| 87 | + { |
| 88 | + id: 'magicKeySettings', |
| 89 | + name: 'Magic key', |
| 90 | + description: 'Magic key to configure something', |
| 91 | + type: 'TEXT', |
| 92 | + required: false, |
| 93 | + defaultValue: '42', |
| 94 | + }, |
| 95 | + { |
| 96 | + id: 'debugLogging', |
| 97 | + name: 'Use debug logging?', |
| 98 | + description: '', |
| 99 | + type: 'BOOLEAN', |
| 100 | + required: true, |
| 101 | + defaultValue: 'true', |
| 102 | + }, |
| 103 | + ], |
| 104 | + } |
| 105 | + ], |
| 106 | + }; |
| 107 | + |
| 108 | + data.page = pageData; |
| 109 | + } |
| 110 | + |
| 111 | + return { statusCode: 200, configurationData: data }; |
| 112 | +}); |
| 113 | + |
| 114 | + |
| 115 | +demoApp.on('INSTALL', function(message, response) { |
| 116 | + console.log(' Got INSTALL lifecycle'); |
| 117 | + console.log(' Got authToken: %s', message.installData.authToken); |
| 118 | + |
| 119 | + this._installData = message.installData; |
| 120 | + fs.writeFileSync(config.paths.lastInstall, JSON.stringify(message)); |
| 121 | + |
| 122 | + var deviceProfile = JSON.parse(fs.readFileSync(config.paths.deviceProfile)); |
| 123 | + |
| 124 | + if (config.schedule) { |
| 125 | + var options = { |
| 126 | + authToken: message.installData.authToken, |
| 127 | + installedAppId: message.installData.installedApp.installedAppId, |
| 128 | + } |
| 129 | + console.log(' Trying to install a schedule'); |
| 130 | + st.schedules_add(options, config.schedule, function(data, status_code) { |
| 131 | + if (status_code == 200) { |
| 132 | + console.log(' Schedule install OK'); |
| 133 | + } else { |
| 134 | + console.log(' Schedule install failed'); |
| 135 | + } |
| 136 | + }); |
| 137 | + } |
| 138 | + |
| 139 | + var deviceData = { |
| 140 | + label: "Test dev", |
| 141 | + locationId: this._installData.installedApp.locationId, |
| 142 | + app: { |
| 143 | + profileId: deviceProfile.id, |
| 144 | + installedAppId: this._installData.installedApp.installedAppId, |
| 145 | + externalId: 'IOTJSD0001', |
| 146 | + } |
| 147 | + }; |
| 148 | + st.device_install({authToken: this._installData.authToken}, deviceData, function(data) { |
| 149 | + console.log("Device installed:"); |
| 150 | + console.log(data); |
| 151 | + fs.writeFileSync(config.paths.deviceInstall, JSON.stringify(data)); |
| 152 | + }); |
| 153 | + |
| 154 | + return { statusCode: 200, installData: {} }; |
| 155 | +}); |
| 156 | + |
| 157 | +demoApp.on('UPDATE', function(message, response) { |
| 158 | + console.log(' Got UPDATE lifecycle'); |
| 159 | + |
| 160 | + fs.writeFileSync(config.path.newConfig, JSON.stringify(message)); |
| 161 | + |
| 162 | + return { statusCode: 200, updateData: {} }; |
| 163 | +}); |
| 164 | + |
| 165 | +demoApp.on('EVENT', function(message, response) { |
| 166 | + console.log(' Got EVENT lifecycle'); |
| 167 | + |
| 168 | + var events = message.eventData.events; |
| 169 | + |
| 170 | + for (var idx = 0; idx < events.length; idx++) { |
| 171 | + var event = events[idx]; |
| 172 | + var eventData = {}; |
| 173 | + switch (event.eventType) { |
| 174 | + case 'DEVICE_COMMANDS_EVENT': { |
| 175 | + console.log(' DEVICE Event triggered:') |
| 176 | + eventData = event.deviceCommandsEvent; |
| 177 | + break; |
| 178 | + } |
| 179 | + case 'DEVICE_EVENT': { |
| 180 | + console.log(' DEVICE Event triggered:') |
| 181 | + eventData = event.deviceEvent; |
| 182 | + break; |
| 183 | + } |
| 184 | + case 'TIMER_EVENT': { |
| 185 | + console.log(' TIMER Event triggered:') |
| 186 | + eventData = event.timerEvent; |
| 187 | + break; |
| 188 | + } |
| 189 | + } |
| 190 | + |
| 191 | + console.log(eventData); |
| 192 | + } |
| 193 | + |
| 194 | + return { statusCode: 200, eventData: {} }; |
| 195 | +}); |
| 196 | + |
| 197 | +var server = new sthook.STWebHookServer(config, demoApp); |
| 198 | +server.createServer(); |
| 199 | +server.listen(); |
0 commit comments