File size: 5,349 Bytes
2fcec7b | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 | /**
*****************************************************************************
Copyright (c) 2014, 2015 IBM Corporation and other Contributors.
All rights reserved. This program and the accompanying materials
are made available under the terms of the Eclipse Public License v1.0
which accompanies this distribution, and is available at
http://www.eclipse.org/legal/epl-v10.html
Contributors:
Tim-Daniel Jacobi - Initial Contribution
Jeffrey Dare
Lokesh Haralakatta - Added Client Side Certificates Support
*****************************************************************************
*
*/
import format from 'format';
import xhr from 'axios';
import Promise from 'bluebird';
import nodeBtoa from 'btoa';
const btoa = btoa || nodeBtoa; // if browser btoa is available use it otherwise use node module
import { isDefined, isString, isNode } from '../util/util.js';
import { default as BaseClient } from './BaseClient.js';
const WILDCARD_TOPIC = 'iot-2/cmd/+/fmt/+';
const CMD_RE = /^iot-2\/cmd\/(.+)\/fmt\/(.+)$/;
const QUICKSTART_ORG_ID = "quickstart";
export default class DeviceClient extends BaseClient {
constructor(config){
super(config);
if(!isDefined(config.type)){
throw new Error('[DeviceClient:constructor] config must contain type');
}
else if(!isString(config.type)){
throw new Error('[DeviceClient:constructor] type must be a string');
}
if(config.org !== QUICKSTART_ORG_ID){
if(!isDefined(config['auth-method'])){
throw new Error('[DeviceClient:constructor] config must contain auth-method');
}
else if(!isString(config['auth-method'])){
throw new Error('[DeviceClient:constructor] auth-method must be a string');
}
else if(config['auth-method'] !== 'token'){
throw new Error('[DeviceClient:constructor] unsupported authentication method' + config['auth-method']);
}
this.mqttConfig.username = 'use-token-auth';
}
this.org = config.org;
this.typeId = config.type;
this.deviceId = config.id;
this.deviceToken = config['auth-token'];
this.mqttConfig.clientId = "d:" + config.org + ":" + config.type + ":" + config.id;
this.log.info("[DeviceClient:constructor] DeviceClient initialized for organization : " + config.org + " for ID : "+config.id);
}
connect(QoS){
QoS = QoS || 2;
super.connect();
var mqtt = this.mqtt;
this.mqtt.on('connect', () => {
this.isConnected = true;
if(isDefined(this.mqttConfig.servername)){
this.log.info("[DeviceClient:connect] DeviceClient Connected using Client Side Certificates");
}
else {
this.log.info("[DeviceClient:connect] DeviceClient Connected");
}
if(this.retryCount === 0){
this.emit('connect');
} else {
this.emit('reconnect');
}
//reset the counter to 0 incase of reconnection
this.retryCount = 0;
if(!this.isQuickstart){
mqtt.subscribe(WILDCARD_TOPIC, { qos: parseInt(QoS) }, function(){});
}
});
this.mqtt.on('message', (topic, payload) => {
this.log.debug("[DeviceClient:onMessage] Message received on topic : "+ topic + " with payload : "+ payload);
let match = CMD_RE.exec(topic);
if(match){
this.emit('command',
match[1],
match[2],
payload,
topic
);
}
});
}
publish(eventType, eventFormat, payload, qos, callback){
if (!this.isConnected) {
this.log.error("[DeviceClient:publish] Client is not connected");
//throw new Error();
//instead of throwing error, will emit 'error' event.
this.emit('error', "[DeviceClient:publish] Client is not connected");
}
let topic = format("iot-2/evt/%s/fmt/%s", eventType, eventFormat);
let QOS = qos || 0;
if( (typeof payload === 'object' || typeof payload === 'boolean' || typeof payload === 'number') && !Buffer.isBuffer(payload) ) {
// mqtt library does not support sending JSON/boolean/number data. So stringifying it.
// All JSON object, array will be encoded.
payload = JSON.stringify(payload);
}
this.log.debug("[DeviceClient:publish] Publishing to topic "+topic+" with payload "+payload+" with QoS "+QOS);
this.mqtt.publish(topic,payload,{qos: parseInt(QOS)}, callback);
return this;
}
publishHTTPS(eventType, eventFormat, payload){
this.log.debug("[DeviceClient:publishHTTPS] Publishing event of Type: "+ eventType + " with payload : "+payload);
return new Promise((resolve, reject) => {
let uri = format("https://%s/api/v0002/device/types/%s/devices/%s/events/%s", this.mqttServer, this.typeId, this.deviceId, eventType);
let xhrConfig = {
url: uri,
method: 'POST',
data : payload,
headers : {
}
};
if(eventFormat === 'json') {
xhrConfig.headers['Content-Type'] = 'application/json';
} else if(eventFormat === 'xml') {
xhrConfig.headers['Content-Type'] = 'application/xml';
}
if(this.org !== QUICKSTART_ORG_ID) {
xhrConfig.headers['Authorization'] = 'Basic ' + btoa('use-token-auth' + ':' + this.deviceToken);
}
this.log.debug("[DeviceClient:publishHTTPS] "+ xhrConfig);
xhr(xhrConfig).then(resolve, reject);
});
}
}
|