From 3d202a04b99f0e61b6ccf8b7a5610e1a15ca58e7 Mon Sep 17 00:00:00 2001 From: Herbert Eiselt Date: Mon, 11 Feb 2019 14:54:12 +0100 Subject: Add sdnr wt odlux Add complete sdnr wireless transport app odlux core and apps Change-Id: I5dcbfb8f3b790e3bda7c8df67bd69d81958f65e5 Issue-ID: SDNC-576 Signed-off-by: Herbert Eiselt --- .../mediatorApp/src/services/mediatorService.ts | 149 +++++++++++++++++++++ 1 file changed, 149 insertions(+) create mode 100644 sdnr/wt/odlux/apps/mediatorApp/src/services/mediatorService.ts (limited to 'sdnr/wt/odlux/apps/mediatorApp/src/services') diff --git a/sdnr/wt/odlux/apps/mediatorApp/src/services/mediatorService.ts b/sdnr/wt/odlux/apps/mediatorApp/src/services/mediatorService.ts new file mode 100644 index 000000000..50fd869b1 --- /dev/null +++ b/sdnr/wt/odlux/apps/mediatorApp/src/services/mediatorService.ts @@ -0,0 +1,149 @@ +import * as $ from 'jquery'; + +import { requestRest } from '../../../../framework/src/services/restService'; +import { MediatorServer, MediatorServerVersionInfo, MediatorConfig, MediatorServerDevice, MediatorConfigResponse } from '../models/mediatorServer'; +import { HitEntry } from '../../../../framework/src/models'; + +export const mediatorServerResourcePath = "mwtn/mediator-server"; + +type MediatorServerResponse = { code: number, data: TData }; +type IndexableMediatorServer = MediatorServer & { [key: string]: any; } ; + +/** + * Represents a web api accessor service for all mediator server actions. + */ +class MediatorService { + /** + * Inserts data into the mediator servers table. + */ + public async insertMediatorServer(server: IndexableMediatorServer): Promise { + const path = `database/${mediatorServerResourcePath}`; + const data = Object.keys(server).reduce((acc, cur) => { + if (cur !== "_id") acc[cur] = server[cur]; + return acc; + }, {} as IndexableMediatorServer); + const result = await requestRest(path, { method: "POST", body: JSON.stringify(data) }); + return result || null; + } + + /** + * Updates data into the mediator servers table. + */ + public async updateMediatorServer(server: IndexableMediatorServer): Promise { + const path = `database/${mediatorServerResourcePath}/${server._id}`; + const data = Object.keys(server).reduce((acc, cur) => { + if (cur !== "_id") { acc[cur] = server[cur] } else { acc["id"] = 0 }; + return acc; + }, {} as IndexableMediatorServer); + const result = await requestRest(path, { method: "PUT", body: JSON.stringify(data) }); + return result || null; + } + + /** + * Deletes data from the mediator servers table. + */ + public async deleteMediatorServer(server: MediatorServer): Promise { + const path = `database/${mediatorServerResourcePath}/${server._id}`; + const result = await requestRest(path, { method: "DELETE" }); + return result || null; + } + + public async getMediatorServerById(serverId: string): Promise { + const path = `database/${mediatorServerResourcePath}/${serverId}`; + const result = await requestRest & { found: boolean }>(path, { method: "GET" }); + return result && result.found && result._source && { + _id: result._id, + name: result._source.name, + url: result._source.url, + } || null; + } + + // https://cloud-highstreet-technologies.com/wiki/doku.php?id=att:ms:api + + private accassMediatorServer(mediatorServerUrl: string, task: string, data?: {}): Promise | null> { + const url = `${mediatorServerUrl}/api/?task=${task}`; + // return (await requestRest<{ code: number, data: TData}>(path, { method: "POST" })) || null ; + return new Promise<{ code: number, data: TData }>((resolve, reject) => { + $.post({ + url, + data: data, + //contentType: 'application/json' + }).then((result: any) => { + if (typeof result === "string") { + resolve(JSON.parse(result)); + } else { + resolve(result); + }; + }); + }); + } + + public async getMediatorServerVersion(mediatorServerUrl: string): Promise { + const result = await this.accassMediatorServer(mediatorServerUrl, 'version'); + if (result && result.code === 1) return result.data; + return null; + } + + public async getMediatorServerAllConfigs(mediatorServerUrl: string): Promise { + const result = await this.accassMediatorServer(mediatorServerUrl, 'getconfig'); + if (result && result.code === 1) return result.data; + return null; + } + + public async getMediatorServerConfigByName(mediatorServerUrl: string, name: string): Promise { + const result = await this.accassMediatorServer(mediatorServerUrl, 'getconfig', { name } ); + if (result && result.code === 1 && result.data && result.data.length === 1) return result.data[0]; + return null; + } + + public async getMediatorServerSupportedDevices(mediatorServerUrl: string): Promise { + const result = await this.accassMediatorServer(mediatorServerUrl, 'getdevices' ); + if (result && result.code === 1) return result.data; + return null; + } + + public async startMediatorByName(mediatorServerUrl: string, name: string): Promise { + const result = await this.accassMediatorServer(mediatorServerUrl, 'start', { name } ); + if (result && result.code === 1) return result.data; + return null; + } + + public async stopMediatorByName(mediatorServerUrl: string, name: string): Promise { + const result = await this.accassMediatorServer(mediatorServerUrl, 'stop', { name } ); + if (result && result.code === 1) return result.data; + return null; + } + + public async createMediatorConfig(mediatorServerUrl: string, config: MediatorConfig): Promise { + const result = await this.accassMediatorServer(mediatorServerUrl, 'create', { config: JSON.stringify(config) } ); + if (result && result.code === 1) return result.data; + return null; + } + + public async updateMediatorConfigByName(mediatorServerUrl: string, config: MediatorConfig): Promise { + const result = await this.accassMediatorServer(mediatorServerUrl, 'update', { config: JSON.stringify(config) } ); + if (result && result.code === 1) return result.data; + return null; + } + + public async deleteMediatorConfigByName(mediatorServerUrl: string, name: string): Promise { + const result = await this.accassMediatorServer(mediatorServerUrl, 'delete', { name } ); + if (result && result.code === 1) return result.data; + return null; + } + + public async getMediatorServerFreeNcPorts(mediatorServerUrl: string, limit?: number): Promise { + const result = await this.accassMediatorServer(mediatorServerUrl, 'getncports', { limit } ); + if (result && result.code === 1) return result.data; + return null; + } + + public async getMediatorServerFreeSnmpPorts(mediatorServerUrl: string, limit?: number): Promise { + const result = await this.accassMediatorServer(mediatorServerUrl, 'getsnmpports', { limit } ); + if (result && result.code === 1) return result.data; + return null; + } +} + +export const mediatorService = new MediatorService; +export default mediatorService; \ No newline at end of file -- cgit 1.2.3-korg