1
0
mirror of https://github.com/mainflux/mainflux.git synced 2025-04-24 13:48:49 +08:00

310 lines
10 KiB
JavaScript
Raw Normal View History

// Copyright (c) Mainflux
MF-571 - Elm UI (#632) * Initial commit Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add user create form Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add Makefile and README Signed-off-by: Ivan Milošević <iva@blokovi.com> Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Remove elm-stuff Signed-off-by: Ivan Milošević <iva@blokovi.com> Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add url parsing Signed-off-by: Ivan Milošević <iva@blokovi.com> Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add channels retrive and remove Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Refactor request f and add f annotations Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add things provision Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add thing retrieve and remove Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Rename Channel.elm methods Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Fix trailing slash and add url constants Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add module User with separate model, view and update Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Modularize channels Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Modularize version and things Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Refactor module methods Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Modularize messaging Signed-off-by: Ivan Milošević <iva@blokovi.com> Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add channel pagination Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Fix initial channel model Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add access control (connect and disconnect things from channel) Signed-off-by: Ivan Milošević <iva@blokovi.com> Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add pagination to Things Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Using Url.Builder in connection and messaging Rename Acces module to Connection Rename attribute in Bootstrap code Remove debug logging Signed-off-by: Ivan Milošević <iva@blokovi.com> Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add login Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add dynamic menu Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Fix empty name channel/thing bug Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Remove menu from login screen and add response helper Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add version, channels, things direct display Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add channels table Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Change var names and add refreshChannelList helper Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add Things list Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Fix response behavior Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Remove version page button Signed-off-by: drasko <drasko.draskovic@gmail.com> Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Move everything to ui folder Signed-off-by: drasko <drasko.draskovic@gmail.com> Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Remove dashflux Signed-off-by: drasko <drasko.draskovic@gmail.com> Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * MF-571 Add things and channels lists to Connection module (#580) * Add thing and channel lists to Connection module Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Fix form reset bug & remember checkbox status in Connection module Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * MF-571 - Add batch things-channels connection modification (#588) * Add batch things-channels connection modification Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Remove unused token arg and reposition buttons Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * NOISSUE - messaging in UI (#578) * Add thing and channel lists to Connection module Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * init commit Signed-off-by: Ivan Milošević <iva@blokovi.com> * list channels and things Signed-off-by: Ivan Milošević <iva@blokovi.com> * list channels with radio buttons Signed-off-by: Ivan Milošević <iva@blokovi.com> * Listing channels for selected thing Signed-off-by: Ivan Milošević <iva@blokovi.com> * fix list channels for selected thing Signed-off-by: Ivan Milošević <iva@blokovi.com> * Use records instead of hardcode values Use genFormField from Helpers Remove dead comments Signed-off-by: Ivan Milošević <iva@blokovi.com> Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * NOISSUE - Add Docker build for UI (#582) * Add Docker build for UI Signed-off-by: Drasko DRASKOVIC <drasko.draskovic@gmail.com> * Fix entrypoint and ports Signed-off-by: Drasko DRASKOVIC <drasko.draskovic@gmail.com> * Add NginX server. Fix port. Signed-off-by: Drasko DRASKOVIC <drasko.draskovic@gmail.com> Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * NOISSUE - Fix layout and add custom CSS (#593) * Fix layout and add custom CSS Signed-off-by: Drasko DRASKOVIC <drasko.draskovic@gmail.com> * Remove comments, add copyright headers Signed-off-by: Drasko DRASKOVIC <drasko.draskovic@gmail.com> * Add newline at the end of the css file Signed-off-by: Drasko DRASKOVIC <drasko.draskovic@gmail.com> Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * NOISSUE -update message module (#599) * Update message module Sending message as selected thing to multiple channels Signed-off-by: Ivan Milošević <iva@blokovi.com> * Rename token to thingkey Signed-off-by: Ivan Milošević <iva@blokovi.com> * Reset model on send message and reset list of channels on selecting thing Signed-off-by: Ivan Milošević <iva@blokovi.com> Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Remove logout button and add logout header (#604) * Remove logout button and add logout header * Removed dead code. Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * MF-571 - Pagination (#606) * Add total # things to Thing model Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add basic pagination for Thing Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add pagination helper function & Channel pagination Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * NOISSUE - Add bootstrap cards in version view (#607) * Add total # things to Thing model Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add basic pagination for Thing Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add bootstrap cards in version view Signed-off-by: Ivan Milošević <iva@blokovi.com> * Add pagination helper function & Channel pagination Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * modify styling Signed-off-by: Ivan Milošević <iva@blokovi.com> * Styling Signed-off-by: Ivan Milošević <iva@blokovi.com> * Mainflux logo Signed-off-by: Ivan Milošević <iva@blokovi.com> Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Revert "NOISSUE - Add bootstrap cards in version view (#607)" This reverts commit a9a4dd7c730de453c45b64b51712c81e9befbeec. Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Pagination (#613) Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * MF-571 - Add dashboard (#614) * Add Dashboard module and simplify Main module Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Remove unused code and Version module Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add helper funcs to Main.elm update method Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Improve Main and Message module organization Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add dashboard Things and Channels buttons Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add Modal to edit thing Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add RemoveThing msg and AwesomeFont Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Unify provision and retrieve request Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Reorganize code by making funcs and code repositioning Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add fon-awesome 5.* support Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add metadata edit Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Remove table header add thing and removed dead code Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * MF-571 - UI modal buttons (#617) * Add Dashboard module and simplify Main module Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Remove unused code and Version module Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add helper funcs to Main.elm update method Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Improve Main and Message module organization Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add dashboard Things and Channels buttons Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add Modal to edit thing Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add RemoveThing msg and AwesomeFont Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Unify provision and retrieve request Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Reorganize code by making funcs and code repositioning Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add fon-awesome 5.* support Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add metadata edit Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Remove table header add thing and removed dead code Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add complete Thing edit modal buttons Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add card style to channels (#618) Add dropdown user menu Add font awesome to main menu Signed-off-by: Ivan Milošević <iva@blokovi.com> Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * MF-571 - UI Thing module with modals (#620) * Add Thing provision modal Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add app/dev dropdown to Thing provision modal Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Refactor Thing to have separate Update and Provision msg handling Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Remove fontAwesome import and fix Dict import Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * MF-571 - UI channel modals (#621) * Add Thing provision modal Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add app/dev dropdown to Thing provision modal Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Refactor Thing to have separate Update and Provision msg handling Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Remove fontAwesome import and fix Dict import Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add provision and edit modals to Channel Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add HttpMF http custom library Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add ModalMF Bootstrap modal custom library Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Moved all http funcs to HttpMF Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Moved expect funcs from Connection and Message to HttpMF Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add cards and change tables (#622) * Change type of tables Add tables to cards Colorize buttons Signed-off-by: Ivan Milošević <iva@blokovi.com> * remove comments Signed-off-by: Ivan Milošević <iva@blokovi.com> Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * MF-571 - Fix user login and refactor User (#623) * Fix user login and refactor User Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Remove redundant Grid.row nesting Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Fix links in README (#624) Signed-off-by: Ivan Milošević <iva@blokovi.com> Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Fix ui/Makefile and ui/README.md Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add copyright header to .elm files Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Fix missing README.md after migration Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Fix typo Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add install options to README.md Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Rename Url to URL and Id to ID Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com>
2019-03-06 16:21:09 +01:00
// SPDX-License-Identifier: Apache-2.0
'use strict';
const version = '0.9.0';
var http = require('http'),
redis = require('redis'),
net = require('net'),
protobuf = require('protobufjs'),
websocket = require('websocket-stream'),
grpc = require('grpc'),
protoLoader = require('@grpc/proto-loader'),
fs = require('fs'),
bunyan = require('bunyan'),
logging = require('aedes-logging');
// pass a proto file as a buffer/string or pass a parsed protobuf-schema object
var config = {
log_level: process.env.MF_MQTT_ADAPTER_LOG_LEVEL || 'error',
instance_id: process.env.MF_MQTT_INSTANCE_ID || '',
event_stream: 'mainflux.mqtt',
mqtt_port: Number(process.env.MF_MQTT_ADAPTER_PORT) || 1883,
ws_port: Number(process.env.MF_MQTT_ADAPTER_WS_PORT) || 8880,
nats_url: process.env.MF_NATS_URL || 'nats://localhost:4222',
redis_port: Number(process.env.MF_MQTT_ADAPTER_REDIS_PORT) || 6379,
redis_host: process.env.MF_MQTT_ADAPTER_REDIS_HOST || 'localhost',
redis_pass: process.env.MF_MQTT_ADAPTER_REDIS_PASS || 'mqtt',
redis_db: Number(process.env.MF_MQTT_ADAPTER_REDIS_DB) || 0,
message_ttl: Number(process.env.MF_MQTT_ADAPTER_MESSAGE_TTL) || 60, // in seconds
es_port: Number(process.env.MF_MQTT_ADAPTER_ES_PORT) || 6379,
es_host: process.env.MF_MQTT_ADAPTER_ES_HOST || 'localhost',
es_pass: process.env.MF_MQTT_ADAPTER_ES_PASS || 'mqtt',
es_db: Number(process.env.MF_MQTT_ADAPTER_ES_DB) || 0,
client_tls: (process.env.MF_MQTT_ADAPTER_CLIENT_TLS == 'true') || false,
ca_certs: process.env.MF_MQTT_ADAPTER_CA_CERTS || '',
concurrency: Number(process.env.MF_MQTT_CONCURRENT_MESSAGES) || 100,
auth_url: process.env.MF_THINGS_URL || 'localhost:8181',
schema_dir: process.argv[2] || '.',
},
logger = bunyan.createLogger({
name: 'mqtt',
level: config.log_level
}),
packageDefinition = protoLoader.loadSync(
config.schema_dir + '/internal.proto', {
keepCase: true,
longs: String,
enums: String,
defaults: true,
oneofs: true
}
),
protoDescriptor = grpc.loadPackageDefinition(packageDefinition),
thingsSchema = protoDescriptor.mainflux,
messagesSchema = new protobuf.Root().loadSync(config.schema_dir + '/message.proto'),
RawMessage = messagesSchema.lookupType('mainflux.RawMessage'),
nats = require('nats').connect({
servers: [config.nats_url],
preserveBuffers: true,
}),
aedesRedis = require('aedes-persistence-redis')({
port: config.redis_port,
host: config.redis_host,
password: config.redis_pass,
db: config.redis_db,
packetTTL: function (packet) {
return config.message_ttl; // in seconds
}
}),
mqRedis = require('mqemitter-redis')({
port: config.redis_port,
host: config.redis_host,
password: config.redis_pass,
db: config.redis_db
}),
aedes = require('aedes')({
id: config.instance_id ? config.instance_id : undefined,
mq: mqRedis,
persistence: aedesRedis,
concurrency: config.concurrency
}),
things = (function () {
var certs;
if (config.client_tls) {
certs = grpc.credentials.createSsl(config.ca_certs);
} else {
certs = grpc.credentials.createInsecure();
}
return new thingsSchema.ThingsService(config.auth_url, certs);
})(),
esclient = redis.createClient({
port: config.es_port,
host: config.es_host,
password: config.es_pass,
db: config.es_db
}),
servers = [
startMqtt(),
startWs()
];
logging({
instance: aedes,
servers: servers,
pinoOptions: {
level: 30
}
});
logger.level(config.log_level);
esclient.on('error', function (err) {
logger.warn('error on redis connection: %s', err.message);
});
// MQTT over WebSocket
function startWs() {
var server = http.createServer();
server.on('request', (req, res) => {
res.setHeader('Content-Type', 'application/json');
if (req.url === '/version') {
res.statusCode = 200;
res.end(`{"service":"mqtt-adapter","version":"${version}"}`);
}
res.statusCode = 404;
res.end('{"service":"mqtt-adpater", "message": "not found"}')
});
websocket.createServer({
server: server
}, aedes.handle);
server.listen(config.ws_port);
return server;
}
function startMqtt() {
return net.createServer(aedes.handle).listen(config.mqtt_port);
}
nats.subscribe('channel.>', {
'queue': 'mqtts'
}, function (msg) {
var m = RawMessage.decode(msg),
packet, subtopic, ct;
if (m && m.protocol !== 'mqtt') {
MF-596 - Add subtopic to RawMessage (#642) * Commit for mainflux-596 Modified and tested: - cli - http - mqtt - normalizer - all readers - sdk messages - all writers - ws Missing: - coap - lora Signed-off-by: ale <ale@metaverso.org> * - fix subtopic name in, when starting with dot, http/ws/mqtt - add some test on readers Signed-off-by: ale <ale@metaverso.org> * - fix string concatenation - update http/transport regexp to match subtopic names with only \w- - update ws/transport regexp like http ones with also the wildcard * and > Signed-off-by: ale <ale@metaverso.org> * added subtopic support to coap adapter Signed-off-by: ale <ale@metaverso.org> * - update replace functions with replaceall when needed - renamed getDestChannel to fmtSubject - update api/transport and ws/transport route to be more readable - fix mqtt syntax - renamed func andQuery to query as suggested by @anovakovic01 - have a nice we :) Signed-off-by: ale <ale@metaverso.org> * - fix error declaration on ws/nat/publisher - fix regexp added missing allowed chars - and _ on coap/api/transport - fix subtopic clean suffix / if present on coap/api/transport - improve regexp on http and ws /api/transport, now does not accept url that do not strictly match - add some ws subtopic tests Signed-off-by: ale <ale@metaverso.org> * - enabled wildcard chars on coap/api/transport - allow use special chars on http and ws api/transport Signed-off-by: ale <ale@metaverso.org> * - use strings.Replace() insted ReplaceAll() Signed-off-by: ale <ale@metaverso.org> * - allow every chars on subtopics - fix replace error on mqtt Signed-off-by: ale <ale@metaverso.org> * fix cassandra test Signed-off-by: ale <ale@metaverso.org> * fix ws test with invalid subtopic Signed-off-by: ale <ale@metaverso.org> * fix invalid GOCACHE in go1.12, replaced by -count 1, see https://golang.org/doc/go1.10#test Signed-off-by: ale <ale@metaverso.org> * - improve regexp on http/ws api/transport - minor changes Signed-off-by: ale <ale@metaverso.org> * - add generic function parseSubtopic on ws/http adapters Signed-off-by: ale <ale@metaverso.org> * - add generic function fmtSubtopic on coap adapter Signed-off-by: ale <ale@metaverso.org>
2019-03-15 18:38:07 +01:00
subtopic = m.subtopic !== '' ? '/' + m.subtopic.replace(/\./g, '/') : '';
ct = (m.contentType) ? ('/ct/' + m.contentType.replace('/', '_').replace('+', '-')) : '';
MF-596 - Add subtopic to RawMessage (#642) * Commit for mainflux-596 Modified and tested: - cli - http - mqtt - normalizer - all readers - sdk messages - all writers - ws Missing: - coap - lora Signed-off-by: ale <ale@metaverso.org> * - fix subtopic name in, when starting with dot, http/ws/mqtt - add some test on readers Signed-off-by: ale <ale@metaverso.org> * - fix string concatenation - update http/transport regexp to match subtopic names with only \w- - update ws/transport regexp like http ones with also the wildcard * and > Signed-off-by: ale <ale@metaverso.org> * added subtopic support to coap adapter Signed-off-by: ale <ale@metaverso.org> * - update replace functions with replaceall when needed - renamed getDestChannel to fmtSubject - update api/transport and ws/transport route to be more readable - fix mqtt syntax - renamed func andQuery to query as suggested by @anovakovic01 - have a nice we :) Signed-off-by: ale <ale@metaverso.org> * - fix error declaration on ws/nat/publisher - fix regexp added missing allowed chars - and _ on coap/api/transport - fix subtopic clean suffix / if present on coap/api/transport - improve regexp on http and ws /api/transport, now does not accept url that do not strictly match - add some ws subtopic tests Signed-off-by: ale <ale@metaverso.org> * - enabled wildcard chars on coap/api/transport - allow use special chars on http and ws api/transport Signed-off-by: ale <ale@metaverso.org> * - use strings.Replace() insted ReplaceAll() Signed-off-by: ale <ale@metaverso.org> * - allow every chars on subtopics - fix replace error on mqtt Signed-off-by: ale <ale@metaverso.org> * fix cassandra test Signed-off-by: ale <ale@metaverso.org> * fix ws test with invalid subtopic Signed-off-by: ale <ale@metaverso.org> * fix invalid GOCACHE in go1.12, replaced by -count 1, see https://golang.org/doc/go1.10#test Signed-off-by: ale <ale@metaverso.org> * - improve regexp on http/ws api/transport - minor changes Signed-off-by: ale <ale@metaverso.org> * - add generic function parseSubtopic on ws/http adapters Signed-off-by: ale <ale@metaverso.org> * - add generic function fmtSubtopic on coap adapter Signed-off-by: ale <ale@metaverso.org>
2019-03-15 18:38:07 +01:00
packet = {
cmd: 'publish',
qos: 2,
topic: 'channels/' + m.channel + '/messages' + subtopic + ct,
payload: m.payload,
retain: false
};
aedes.publish(packet);
}
});
MF-596 - Add subtopic to RawMessage (#642) * Commit for mainflux-596 Modified and tested: - cli - http - mqtt - normalizer - all readers - sdk messages - all writers - ws Missing: - coap - lora Signed-off-by: ale <ale@metaverso.org> * - fix subtopic name in, when starting with dot, http/ws/mqtt - add some test on readers Signed-off-by: ale <ale@metaverso.org> * - fix string concatenation - update http/transport regexp to match subtopic names with only \w- - update ws/transport regexp like http ones with also the wildcard * and > Signed-off-by: ale <ale@metaverso.org> * added subtopic support to coap adapter Signed-off-by: ale <ale@metaverso.org> * - update replace functions with replaceall when needed - renamed getDestChannel to fmtSubject - update api/transport and ws/transport route to be more readable - fix mqtt syntax - renamed func andQuery to query as suggested by @anovakovic01 - have a nice we :) Signed-off-by: ale <ale@metaverso.org> * - fix error declaration on ws/nat/publisher - fix regexp added missing allowed chars - and _ on coap/api/transport - fix subtopic clean suffix / if present on coap/api/transport - improve regexp on http and ws /api/transport, now does not accept url that do not strictly match - add some ws subtopic tests Signed-off-by: ale <ale@metaverso.org> * - enabled wildcard chars on coap/api/transport - allow use special chars on http and ws api/transport Signed-off-by: ale <ale@metaverso.org> * - use strings.Replace() insted ReplaceAll() Signed-off-by: ale <ale@metaverso.org> * - allow every chars on subtopics - fix replace error on mqtt Signed-off-by: ale <ale@metaverso.org> * fix cassandra test Signed-off-by: ale <ale@metaverso.org> * fix ws test with invalid subtopic Signed-off-by: ale <ale@metaverso.org> * fix invalid GOCACHE in go1.12, replaced by -count 1, see https://golang.org/doc/go1.10#test Signed-off-by: ale <ale@metaverso.org> * - improve regexp on http/ws api/transport - minor changes Signed-off-by: ale <ale@metaverso.org> * - add generic function parseSubtopic on ws/http adapters Signed-off-by: ale <ale@metaverso.org> * - add generic function fmtSubtopic on coap adapter Signed-off-by: ale <ale@metaverso.org>
2019-03-15 18:38:07 +01:00
function parseTopic(topic) {
// Topics are in the form `channels/<channel_id>/messages`
// Subtopic's are in the form `channels/<channel_id>/messages/<subtopic>`
MF-596 - Add subtopic to RawMessage (#642) * Commit for mainflux-596 Modified and tested: - cli - http - mqtt - normalizer - all readers - sdk messages - all writers - ws Missing: - coap - lora Signed-off-by: ale <ale@metaverso.org> * - fix subtopic name in, when starting with dot, http/ws/mqtt - add some test on readers Signed-off-by: ale <ale@metaverso.org> * - fix string concatenation - update http/transport regexp to match subtopic names with only \w- - update ws/transport regexp like http ones with also the wildcard * and > Signed-off-by: ale <ale@metaverso.org> * added subtopic support to coap adapter Signed-off-by: ale <ale@metaverso.org> * - update replace functions with replaceall when needed - renamed getDestChannel to fmtSubject - update api/transport and ws/transport route to be more readable - fix mqtt syntax - renamed func andQuery to query as suggested by @anovakovic01 - have a nice we :) Signed-off-by: ale <ale@metaverso.org> * - fix error declaration on ws/nat/publisher - fix regexp added missing allowed chars - and _ on coap/api/transport - fix subtopic clean suffix / if present on coap/api/transport - improve regexp on http and ws /api/transport, now does not accept url that do not strictly match - add some ws subtopic tests Signed-off-by: ale <ale@metaverso.org> * - enabled wildcard chars on coap/api/transport - allow use special chars on http and ws api/transport Signed-off-by: ale <ale@metaverso.org> * - use strings.Replace() insted ReplaceAll() Signed-off-by: ale <ale@metaverso.org> * - allow every chars on subtopics - fix replace error on mqtt Signed-off-by: ale <ale@metaverso.org> * fix cassandra test Signed-off-by: ale <ale@metaverso.org> * fix ws test with invalid subtopic Signed-off-by: ale <ale@metaverso.org> * fix invalid GOCACHE in go1.12, replaced by -count 1, see https://golang.org/doc/go1.10#test Signed-off-by: ale <ale@metaverso.org> * - improve regexp on http/ws api/transport - minor changes Signed-off-by: ale <ale@metaverso.org> * - add generic function parseSubtopic on ws/http adapters Signed-off-by: ale <ale@metaverso.org> * - add generic function fmtSubtopic on coap adapter Signed-off-by: ale <ale@metaverso.org>
2019-03-15 18:38:07 +01:00
return /^channels\/(.+?)\/messages\/?.*$/.exec(topic);
}
aedes.authorizePublish = function (client, packet, publish) {
var channel = parseTopic(packet.topic);
if (!channel) {
var err = new Error('unknown topic');
logger.warn(err);
publish(err); // Bad username or password
return;
}
var channelId = channel[1],
accessReq = {
token: client.password,
MF-475 - Replace increment ID with UUID (#490) * Update increment ID to UUID in things service Update increment ID to UUID for things and channels in things service and proto files. Also, update ID type from uint to string. Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update increment ID to UUID in http adapter Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update increment ID to UUID in ws adapter Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update increment ID to UUID in CoAP adapter Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update increment ID to UUID in normalizer service Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update increment ID to UUID in writer services Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update increment ID to UUID in reader services Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update increment ID to UUID in SDK Update increment ID to UUID in SDK. Update id type to string. Update tests. Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update increment ID to UUID in mqtt adapter Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Remove unnecessary case from influxdb reader Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update tests in order to increase code coverage Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update lora adapter to use string ID instead of unsigned int Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com>
2018-12-05 13:09:25 +01:00
chanID: channelId
},
// Parse unlimited subtopics
baseLength = 3, // First 3 elements which represents the base part of topic.
isEmpty = function (value) {
return value !== '';
},
MF-757 - Add support for explicit content type (#771) * Add format to raw message and content type mapping Signed-off-by: Aleksandar Novakovic <anovakovic01@gmail.com> * Add format as part of content type header Signed-off-by: Aleksandar Novakovic <anovakovic01@gmail.com> * Add format to content-type header for WS adapter Add format as part of content-type and add support for content-type header and query parameter. Signed-off-by: Aleksandar Novakovic <anovakovic01@gmail.com> * Add support for format and content-type in MQTT adapter Signed-off-by: Aleksandar Novakovic <anovakovic01@gmail.com> * Add format and content-type to CoAP adapter Signed-off-by: Aleksandar Novakovic <anovakovic01@gmail.com> * Add documentation for content type and format Signed-off-by: Aleksandar Novakovic <anovakovic01@gmail.com> * Add comments to MQTT code Signed-off-by: Aleksandar Novakovic <anovakovic01@gmail.com> * Fix SenML JSON CoAP code type Signed-off-by: Aleksandar Novakovic <anovakovic01@gmail.com> * Remove format from the adapters Signed-off-by: Aleksandar Novakovic <anovakovic01@gmail.com> * Remove format from the docs Signed-off-by: Aleksandar Novakovic <anovakovic01@gmail.com> * Remove format field from MQTT adapter Signed-off-by: Aleksandar Novakovic <anovakovic01@gmail.com> * Update content type to be optional Signed-off-by: Aleksandar Novakovic <anovakovic01@gmail.com> * Add support for CBOR content type in the normalizer Signed-off-by: Aleksandar Novakovic <anovakovic01@gmail.com> * Update docs for MQTT content type Signed-off-by: Aleksandar Novakovic <anovakovic01@gmail.com>
2019-07-25 02:22:28 +02:00
parts = packet.topic.split('/'),
elements = parts.slice(baseLength).join('.').split('.').filter(isEmpty),
baseTopic = 'channel.' + channelId;
// Remove empty elements
for (var i = 0; i < elements.length; i++) {
if (elements[i].length > 1 && (elements[i].includes('*') || elements[i].includes('>'))) {
var err = new Error('invalid subtopic');
logger.warn(err);
publish(err);
return;
}
}
MF-757 - Add support for explicit content type (#771) * Add format to raw message and content type mapping Signed-off-by: Aleksandar Novakovic <anovakovic01@gmail.com> * Add format as part of content type header Signed-off-by: Aleksandar Novakovic <anovakovic01@gmail.com> * Add format to content-type header for WS adapter Add format as part of content-type and add support for content-type header and query parameter. Signed-off-by: Aleksandar Novakovic <anovakovic01@gmail.com> * Add support for format and content-type in MQTT adapter Signed-off-by: Aleksandar Novakovic <anovakovic01@gmail.com> * Add format and content-type to CoAP adapter Signed-off-by: Aleksandar Novakovic <anovakovic01@gmail.com> * Add documentation for content type and format Signed-off-by: Aleksandar Novakovic <anovakovic01@gmail.com> * Add comments to MQTT code Signed-off-by: Aleksandar Novakovic <anovakovic01@gmail.com> * Fix SenML JSON CoAP code type Signed-off-by: Aleksandar Novakovic <anovakovic01@gmail.com> * Remove format from the adapters Signed-off-by: Aleksandar Novakovic <anovakovic01@gmail.com> * Remove format from the docs Signed-off-by: Aleksandar Novakovic <anovakovic01@gmail.com> * Remove format field from MQTT adapter Signed-off-by: Aleksandar Novakovic <anovakovic01@gmail.com> * Update content type to be optional Signed-off-by: Aleksandar Novakovic <anovakovic01@gmail.com> * Add support for CBOR content type in the normalizer Signed-off-by: Aleksandar Novakovic <anovakovic01@gmail.com> * Update docs for MQTT content type Signed-off-by: Aleksandar Novakovic <anovakovic01@gmail.com>
2019-07-25 02:22:28 +02:00
var contentType = '',
MF-757 - Add support for explicit content type (#771) * Add format to raw message and content type mapping Signed-off-by: Aleksandar Novakovic <anovakovic01@gmail.com> * Add format as part of content type header Signed-off-by: Aleksandar Novakovic <anovakovic01@gmail.com> * Add format to content-type header for WS adapter Add format as part of content-type and add support for content-type header and query parameter. Signed-off-by: Aleksandar Novakovic <anovakovic01@gmail.com> * Add support for format and content-type in MQTT adapter Signed-off-by: Aleksandar Novakovic <anovakovic01@gmail.com> * Add format and content-type to CoAP adapter Signed-off-by: Aleksandar Novakovic <anovakovic01@gmail.com> * Add documentation for content type and format Signed-off-by: Aleksandar Novakovic <anovakovic01@gmail.com> * Add comments to MQTT code Signed-off-by: Aleksandar Novakovic <anovakovic01@gmail.com> * Fix SenML JSON CoAP code type Signed-off-by: Aleksandar Novakovic <anovakovic01@gmail.com> * Remove format from the adapters Signed-off-by: Aleksandar Novakovic <anovakovic01@gmail.com> * Remove format from the docs Signed-off-by: Aleksandar Novakovic <anovakovic01@gmail.com> * Remove format field from MQTT adapter Signed-off-by: Aleksandar Novakovic <anovakovic01@gmail.com> * Update content type to be optional Signed-off-by: Aleksandar Novakovic <anovakovic01@gmail.com> * Add support for CBOR content type in the normalizer Signed-off-by: Aleksandar Novakovic <anovakovic01@gmail.com> * Update docs for MQTT content type Signed-off-by: Aleksandar Novakovic <anovakovic01@gmail.com>
2019-07-25 02:22:28 +02:00
st = elements;
if (elements.length > 1 && elements[elements.length - 2] === 'ct') {
// If there is ct prefix, read and decode content type.
contentType = elements[elements.length - 1].replace('_', '/').replace('-', '+');
st = elements.slice(0, elements.length - 2);
}
var channelTopic = st.length ? baseTopic + '.' + st.join('.') : baseTopic,
onAuthorize = function (err, res) {
var rawMsg;
if (!err) {
rawMsg = RawMessage.encode({
publisher: client.thingId,
channel: channelId,
MF-757 - Add support for explicit content type (#771) * Add format to raw message and content type mapping Signed-off-by: Aleksandar Novakovic <anovakovic01@gmail.com> * Add format as part of content type header Signed-off-by: Aleksandar Novakovic <anovakovic01@gmail.com> * Add format to content-type header for WS adapter Add format as part of content-type and add support for content-type header and query parameter. Signed-off-by: Aleksandar Novakovic <anovakovic01@gmail.com> * Add support for format and content-type in MQTT adapter Signed-off-by: Aleksandar Novakovic <anovakovic01@gmail.com> * Add format and content-type to CoAP adapter Signed-off-by: Aleksandar Novakovic <anovakovic01@gmail.com> * Add documentation for content type and format Signed-off-by: Aleksandar Novakovic <anovakovic01@gmail.com> * Add comments to MQTT code Signed-off-by: Aleksandar Novakovic <anovakovic01@gmail.com> * Fix SenML JSON CoAP code type Signed-off-by: Aleksandar Novakovic <anovakovic01@gmail.com> * Remove format from the adapters Signed-off-by: Aleksandar Novakovic <anovakovic01@gmail.com> * Remove format from the docs Signed-off-by: Aleksandar Novakovic <anovakovic01@gmail.com> * Remove format field from MQTT adapter Signed-off-by: Aleksandar Novakovic <anovakovic01@gmail.com> * Update content type to be optional Signed-off-by: Aleksandar Novakovic <anovakovic01@gmail.com> * Add support for CBOR content type in the normalizer Signed-off-by: Aleksandar Novakovic <anovakovic01@gmail.com> * Update docs for MQTT content type Signed-off-by: Aleksandar Novakovic <anovakovic01@gmail.com>
2019-07-25 02:22:28 +02:00
subtopic: st.join('.'),
contentType: contentType,
protocol: 'mqtt',
payload: packet.payload
}).finish();
nats.publish(channelTopic, rawMsg);
publish(null);
} else {
logger.warn('unauthorized publish: %s', err.message);
publish(err); // Bad username or password
}
};
things.CanAccess(accessReq, onAuthorize);
};
aedes.authorizeSubscribe = function (client, packet, subscribe) {
MF-596 - Add subtopic to RawMessage (#642) * Commit for mainflux-596 Modified and tested: - cli - http - mqtt - normalizer - all readers - sdk messages - all writers - ws Missing: - coap - lora Signed-off-by: ale <ale@metaverso.org> * - fix subtopic name in, when starting with dot, http/ws/mqtt - add some test on readers Signed-off-by: ale <ale@metaverso.org> * - fix string concatenation - update http/transport regexp to match subtopic names with only \w- - update ws/transport regexp like http ones with also the wildcard * and > Signed-off-by: ale <ale@metaverso.org> * added subtopic support to coap adapter Signed-off-by: ale <ale@metaverso.org> * - update replace functions with replaceall when needed - renamed getDestChannel to fmtSubject - update api/transport and ws/transport route to be more readable - fix mqtt syntax - renamed func andQuery to query as suggested by @anovakovic01 - have a nice we :) Signed-off-by: ale <ale@metaverso.org> * - fix error declaration on ws/nat/publisher - fix regexp added missing allowed chars - and _ on coap/api/transport - fix subtopic clean suffix / if present on coap/api/transport - improve regexp on http and ws /api/transport, now does not accept url that do not strictly match - add some ws subtopic tests Signed-off-by: ale <ale@metaverso.org> * - enabled wildcard chars on coap/api/transport - allow use special chars on http and ws api/transport Signed-off-by: ale <ale@metaverso.org> * - use strings.Replace() insted ReplaceAll() Signed-off-by: ale <ale@metaverso.org> * - allow every chars on subtopics - fix replace error on mqtt Signed-off-by: ale <ale@metaverso.org> * fix cassandra test Signed-off-by: ale <ale@metaverso.org> * fix ws test with invalid subtopic Signed-off-by: ale <ale@metaverso.org> * fix invalid GOCACHE in go1.12, replaced by -count 1, see https://golang.org/doc/go1.10#test Signed-off-by: ale <ale@metaverso.org> * - improve regexp on http/ws api/transport - minor changes Signed-off-by: ale <ale@metaverso.org> * - add generic function parseSubtopic on ws/http adapters Signed-off-by: ale <ale@metaverso.org> * - add generic function fmtSubtopic on coap adapter Signed-off-by: ale <ale@metaverso.org>
2019-03-15 18:38:07 +01:00
var channel = parseTopic(packet.topic);
if (!channel) {
logger.warn('unknown topic');
var err = new Error('unknown topic')
subscribe(err, null); // Bad username or password
return;
}
var channelId = channel[1],
accessReq = {
token: client.password,
MF-475 - Replace increment ID with UUID (#490) * Update increment ID to UUID in things service Update increment ID to UUID for things and channels in things service and proto files. Also, update ID type from uint to string. Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update increment ID to UUID in http adapter Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update increment ID to UUID in ws adapter Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update increment ID to UUID in CoAP adapter Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update increment ID to UUID in normalizer service Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update increment ID to UUID in writer services Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update increment ID to UUID in reader services Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update increment ID to UUID in SDK Update increment ID to UUID in SDK. Update id type to string. Update tests. Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update increment ID to UUID in mqtt adapter Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Remove unnecessary case from influxdb reader Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update tests in order to increase code coverage Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update lora adapter to use string ID instead of unsigned int Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com>
2018-12-05 13:09:25 +01:00
chanID: channelId
},
onAuthorize = function (err, res) {
if (!err) {
subscribe(null, packet);
} else {
logger.warn('unauthorized subscribe: %s', err.message);
subscribe(err, null); // Bad username or password
}
};
things.canAccess(accessReq, onAuthorize);
};
aedes.authenticate = function (client, username, password, acknowledge) {
var pass = (password || '').toString(),
identity = {
value: pass
},
onIdentify = function (err, res) {
if (!err) {
client.thingId = res.value.toString() || '';
client.id = client.id || client.thingId;
client.password = pass;
acknowledge(null, true);
publishConnEvent(client.thingId, 'connect');
} else {
logger.warn('failed to authenticate client with key %s', pass);
err.responseCode = 4;
acknowledge(err, false);
}
};
things.identify(identity, onIdentify);
};
aedes.on('clientDisconnect', function (client) {
logger.info('disconnect client %s', client.id);
client.password = null;
publishConnEvent(client.thingId, 'disconnect');
});
aedes.on('clientError', function (client, err) {
logger.warn('client error: client: %s, error: %s', client.id, err.message);
});
aedes.on('connectionError', function (client, err) {
logger.warn('connection error: client: %s, error: %s', client.id, err.message);
});
aedes.on('error', function (err) {
logger.warn('aedes error: %s', err.message);
});
function publishConnEvent(id, type) {
var onPublish = function (err) {
if (err) {
logger.warn('event publish failed: %s', err);
}
};
esclient.xadd(config.event_stream, '*',
'thing_id', id,
'timestamp', Math.round((new Date()).getTime() / 1000),
'event_type', type,
'instance', config.instance_id,
onPublish);
}