|
| 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 ws = require('websocket'); |
| 17 | + |
| 18 | +var websocket = new ws.Websocket(); |
| 19 | + |
| 20 | +var device_id = "DEVICE ID"; |
| 21 | +var device_token = "DEVICE TOKEN"; |
| 22 | + |
| 23 | +function getTimeMillis(){ |
| 24 | + return parseInt(Date.now().toString()); |
| 25 | +} |
| 26 | + |
| 27 | +function register() { |
| 28 | + var reg_object = { |
| 29 | + type: "register", |
| 30 | + sdid: device_id, |
| 31 | + Authorization: "bearer " + device_token, |
| 32 | + cid: getTimeMillis(), |
| 33 | + }; |
| 34 | + console.log('Sending register message ' + JSON.stringify(reg_object)); |
| 35 | + websocket.send(JSON.stringify(reg_object), {mask: true}); |
| 36 | +} |
| 37 | + |
| 38 | +function sendTemp(temp) { |
| 39 | + var data_object = { |
| 40 | + sdid: device_id, |
| 41 | + ts: getTimeMillis(), |
| 42 | + data: { |
| 43 | + temp: temp, |
| 44 | + }, |
| 45 | + cid: getTimeMillis(), |
| 46 | + }; |
| 47 | + console.log('Sending temperature data ' + JSON.stringify(data_object)); |
| 48 | + websocket.send(JSON.stringify(data_object), {mask: true}); |
| 49 | +} |
| 50 | + |
| 51 | +websocket.on('message', function(msg) { |
| 52 | + console.log('Received message: ' + msg); |
| 53 | + var parsed_msg = JSON.parse(msg); |
| 54 | +}); |
| 55 | + |
| 56 | +websocket.on('connect', function() { |
| 57 | + console.log('Websocket connection is open ....'); |
| 58 | + register(); |
| 59 | + sendTemp(73); |
| 60 | +}); |
| 61 | + |
| 62 | +websocket.connect('wss://api.artik.cloud', 443, '/v1.1/websocket?ack=true'); |
0 commit comments