Skip to content
Snippets Groups Projects
Commit b275e7fe authored by Erkan Karabulut's avatar Erkan Karabulut
Browse files

create tsmatch connector

parent f6c8a3c5
No related branches found
No related tags found
No related merge requests found
.idea
.eslintignore
.eslintrc.json
.gitignore
.prettierrc.js
build/
node_modules/
package-lock.json
package.json
tsconfig.json
tslint.json
{
"extends": "./node_modules/gts/"
}
node_modules
build
.eslintignore
.gitignore
package-lock.json
src/agent.info.ts
{
"name": "tsmatch-connector",
"version": "1.0.0",
"description": "A package that allows to interact with a TSMatch (Things to Service Matching) instance",
"main": "index.js",
"scripts": {
"build": "tsc ./src/**.ts --outDir ./build",
"test": "node ./build/Test.js",
"lint": "gts lint",
"clean": "gts clean",
"fix": "gts fix"
},
"keywords": [
"tsmatch",
"iiot",
"efpf"
],
"author": "Erkan Karabulut",
"license": "ISC",
"devDependencies": {
"tslint": "^6.1.3",
"typescript": "^4.5.4",
"gts": "^3.1.0",
"@types/node": "^14.11.2"
},
"dependencies": {
"mqtt": "^4.3.1"
}
}
import * as mqtt from 'mqtt';
import * as Constants from './constant/Constants';
import ServiceRequestInterface from "./interface/ServiceRequestInterface";
import ThingService from "./service/ThingService";
import RequestService from "./service/RequestService";
import RequestDeletionInterface from "./interface/RequestDeletionInterface";
export default class TSMatchConnector {
private mqttClient: mqtt.MqttClient;
private isConnected: boolean;
private sensorAddedCallback: CallableFunction | undefined;
private sensorRemovedCallback: CallableFunction | undefined;
private getAllSensorCallback: CallableFunction | undefined;
private serviceResponseCallback: CallableFunction | undefined;
private observationReceivedCallback: CallableFunction | undefined;
private thingService: ThingService | null;
private requestService: RequestService | null;
constructor(connectionParameters: any, onTSMatchConnect: CallableFunction) {
this.mqttClient = mqtt.connect(connectionParameters.url, connectionParameters.options);
this.mqttClient.on("connect", () => this.onMQTTConnect(onTSMatchConnect));
this.mqttClient.on("message", (topic, payload) => this.onMQTTMessage(topic, payload));
this.mqttClient.on("error", (error => this.onMQTTError(error, onTSMatchConnect)));
this.mqttClient.on("disconnect", (() => this.onMQTTDisconnect()));
this.thingService = null;
this.requestService = null;
this.isConnected = false;
}
onMQTTConnect(onTSMatchConnect: CallableFunction) {
console.log("Connected to the TSMatch broker.");
this.mqttClient.subscribe(Constants.TOPIC_DISCOVERY);
this.mqttClient.subscribe(Constants.TOPIC_DEADVERTISEMENT);
this.mqttClient.subscribe(Constants.TOPIC_RESPONSE_ALL_SENSORS);
this.mqttClient.subscribe(Constants.TOPIC_SERVICE_RESPONSE);
this.mqttClient.subscribe(Constants.TOPIC_OBSERVATION);
this.thingService = new ThingService(this.mqttClient);
this.requestService = new RequestService(this.mqttClient);
this.isConnected = true;
onTSMatchConnect(true);
}
onMQTTMessage(topic: String, payload: Buffer) {
switch (topic) {
case Constants.TOPIC_SERVICE_RESPONSE:
if (this.serviceResponseCallback != undefined) {
this.serviceResponseCallback(payload);
}
break;
case Constants.TOPIC_RESPONSE_ALL_SENSORS:
if (this.getAllSensorCallback != undefined) {
this.getAllSensorCallback(payload);
}
break;
case Constants.TOPIC_DISCOVERY:
if (this.sensorAddedCallback != undefined) {
this.sensorAddedCallback(payload);
}
break;
case Constants.TOPIC_DEADVERTISEMENT:
if (this.sensorRemovedCallback != undefined) {
this.sensorRemovedCallback(payload);
}
break;
case Constants.TOPIC_OBSERVATION:
if (this.observationReceivedCallback != undefined) {
this.observationReceivedCallback(payload);
}
break;
}
}
onMQTTError(error: any, onTSMatchConnectionError: CallableFunction) {
console.error(JSON.stringify(error));
this.isConnected = false;
onTSMatchConnectionError(false);
}
onMQTTDisconnect() {
console.log("Disconnected from the TSMatch");
this.isConnected = false;
}
subscribeSensorDiscovery(callback: CallableFunction) {
if (this.checkConnection()) {
this.sensorAddedCallback = callback;
console.log("Subscribed to sensor discovery");
}
}
subscribeSensorRemoval(callback: CallableFunction) {
if (this.checkConnection()) {
this.sensorRemovedCallback = callback;
console.log("Subscribed to sensor removal");
}
}
unSubscribeSensorDiscovery() {
if (this.checkConnection()) {
this.sensorAddedCallback = undefined;
console.log("Unsubscribed to sensor discovery");
}
}
unSubscribeSensorRemoval() {
if (this.checkConnection()) {
this.sensorRemovedCallback = undefined;
console.log("Unsubscribed to sensor removal");
}
}
requestAllSensors(callback: CallableFunction) {
if (this.checkConnection()) {
console.log("Getting all available sensors...");
if (this.thingService != null) {
this.getAllSensorCallback = callback;
this.thingService.requestAllSensors();
}
}
}
serviceRequest(requestDescription: ServiceRequestInterface, responseCallback: CallableFunction, observationCallback: CallableFunction) {
if (this.checkConnection()) {
if (this.requestService != null) {
this.serviceResponseCallback = responseCallback;
this.observationReceivedCallback = observationCallback;
this.requestService.request(requestDescription);
}
}
}
deleteRequest(deleteRequest: RequestDeletionInterface) {
if (this.checkConnection() && this.requestService != null) {
this.requestService.deleteRequest(deleteRequest);
console.log("Delete request is sent.");
}
}
checkConnection(): boolean {
if (this.isConnected) {
return true;
}
console.error("Client is not connected to the TSMatch!");
return false;
}
}
export const TOPIC_DISCOVERY = "INNOVINT/DISCOVERY/NDATA/TSMATCH_INNOVINT_1"
export const TOPIC_DEADVERTISEMENT = "INNOVINT/DEADVERTIZE/NDATA/TSMATCH_INNOVINT_1"
export const TOPIC_SERVICE_REQUEST = "INNOVINT/HAMBURG_FACTORY1/REQUEST"
export const TOPIC_SERVICE_RESPONSE = "INNOVINT/RESPONSE/NDATA/TSMATCH_INNOVINT_1"
export const TOPIC_REQUEST_ALL_SENSORS = "INNOVINT/HAMBURG_FACTORY1/REQUEST_DISCOVERY_ALL"
export const TOPIC_RESPONSE_ALL_SENSORS = "INNOVINT/DISCOVERY_ALL/NDATA/TSMATCH_INNOVINT_1"
export const TOPIC_REQUEST_DELETION = "INNOVINT/HAMBURG_FACTORY1/DELETE_REQUEST"
export const TOPIC_OBSERVATION = "INNOVINT/OBSERVATION/NDATA/TSMATCH_INNOVINT_1"
export default interface RequestDeletionInterface {
requestId: String;
}
export default interface ServiceRequestInterface {
featureOfInterest: String;
unitOfMeasurement: String;
creatorId: String;
location: String;
uuid: String;
domain: String;
domainDetail: String;
monitoringAspect: String;
monitoringAspectDetail: String;
}
import * as mqtt from "mqtt";
import ServiceRequestInterface from "../interface/ServiceRequestInterface";
import * as Constants from '../constant/Constants';
import RequestDeletionInterface from "../interface/RequestDeletionInterface";
export default class RequestService {
private mqttClient: mqtt.MqttClient;
constructor(mqttClient: mqtt.MqttClient) {
this.mqttClient = mqttClient;
}
request(serviceDescription: ServiceRequestInterface) {
this.mqttClient.publish(Constants.TOPIC_SERVICE_REQUEST, JSON.stringify(serviceDescription));
}
deleteRequest(deleteRequest: RequestDeletionInterface) {
this.mqttClient.publish(Constants.TOPIC_REQUEST_DELETION, JSON.stringify(deleteRequest));
}
}
import * as mqtt from "mqtt";
import * as Constants from "../constant/Constants";
export default class ThingService {
private mqttClient: mqtt.MqttClient;
constructor(mqttClient: mqtt.MqttClient) {
this.mqttClient = mqttClient;
}
requestAllSensors() {
this.mqttClient.publish(Constants.TOPIC_REQUEST_ALL_SENSORS, "");
}
}
{
"extends": "./node_modules/gts/tsconfig-google.json",
"compilerOptions": {
"rootDir": ".",
"outDir": "build"
},
"include": [
"src/**/*.ts",
"test/**/*.ts"
]
}
{
"defaultSeverity": "error",
"extends": [
"tslint:recommended"
],
"jsRules": {},
"rules": {},
"rulesDirectory": []
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment