aboutsummaryrefslogtreecommitdiffstats
path: root/sdnr/wt/odlux/apps/mediatorApp/src/services/mediatorService.ts
blob: 50fd869b1b7dfa9ff53427f8a8ebbe4fc3e86f0d (plain)
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
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<TData> = { 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<MediatorServer | null> {
    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<MediatorServer>(path, { method: "POST", body: JSON.stringify(data) });
    return result || null;
  }

  /**
    * Updates data into the mediator servers table.
    */
  public async updateMediatorServer(server: IndexableMediatorServer): Promise<MediatorServer | null> {
    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<MediatorServer>(path, { method: "PUT", body: JSON.stringify(data) });
    return result || null;
  }

  /**
    * Deletes data from the mediator servers table.
    */
  public async deleteMediatorServer(server: MediatorServer): Promise<MediatorServer | null> {
    const path = `database/${mediatorServerResourcePath}/${server._id}`;
    const result = await requestRest<MediatorServer>(path, { method: "DELETE" });
    return result || null;
  }

  public async getMediatorServerById(serverId: string): Promise<MediatorServer | null> {
    const path = `database/${mediatorServerResourcePath}/${serverId}`;
    const result = await requestRest<HitEntry<MediatorServer> & { 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<TData ={}>(mediatorServerUrl: string, task: string, data?: {}): Promise<MediatorServerResponse<TData> | 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<MediatorServerVersionInfo | null> {
    const result = await this.accassMediatorServer<MediatorServerVersionInfo>(mediatorServerUrl, 'version');
    if (result && result.code === 1) return result.data;
    return null;
  }

  public async getMediatorServerAllConfigs(mediatorServerUrl: string): Promise<MediatorConfigResponse[] | null> {
    const result = await this.accassMediatorServer<MediatorConfigResponse[]>(mediatorServerUrl, 'getconfig');
    if (result && result.code === 1) return result.data;
    return null;
  }

  public async getMediatorServerConfigByName(mediatorServerUrl: string, name: string): Promise<MediatorConfigResponse | null> {
    const result = await this.accassMediatorServer<MediatorConfigResponse[]>(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<MediatorServerDevice[] | null> {
    const result = await this.accassMediatorServer<MediatorServerDevice[]>(mediatorServerUrl, 'getdevices' );
    if (result && result.code === 1) return result.data;
    return null;
  }

  public async startMediatorByName(mediatorServerUrl: string, name: string): Promise<string | null> {
    const result = await this.accassMediatorServer<string>(mediatorServerUrl, 'start', { name } );
    if (result && result.code === 1) return result.data;
    return null;
  }

  public async stopMediatorByName(mediatorServerUrl: string, name: string): Promise<string | null> {
    const result = await this.accassMediatorServer<string>(mediatorServerUrl, 'stop', { name } );
    if (result && result.code === 1) return result.data;
    return null;
  }

  public async createMediatorConfig(mediatorServerUrl: string, config: MediatorConfig): Promise<string | null> {
    const result = await this.accassMediatorServer<string>(mediatorServerUrl, 'create', { config: JSON.stringify(config) }  );
    if (result && result.code === 1) return result.data;
    return null;
  }

  public async updateMediatorConfigByName(mediatorServerUrl: string, config: MediatorConfig): Promise<string | null> {
    const result = await this.accassMediatorServer<string>(mediatorServerUrl, 'update', { config: JSON.stringify(config) } );
    if (result && result.code === 1) return result.data;
    return null;
  }

  public async deleteMediatorConfigByName(mediatorServerUrl: string, name: string): Promise<string | null> {
    const result = await this.accassMediatorServer<string>(mediatorServerUrl, 'delete', { name } );
    if (result && result.code === 1) return result.data;
    return null;
  }

  public async getMediatorServerFreeNcPorts(mediatorServerUrl: string, limit?: number): Promise<number[] | null> {
    const result = await this.accassMediatorServer<number[]>(mediatorServerUrl, 'getncports', { limit } );
    if (result && result.code === 1) return result.data;
    return null;
  }
  
  public async getMediatorServerFreeSnmpPorts(mediatorServerUrl: string, limit?: number): Promise<number[] | null> {
    const result = await this.accassMediatorServer<number[]>(mediatorServerUrl, 'getsnmpports', { limit } );
    if (result && result.code === 1) return result.data;
    return null;
  }
}

export const mediatorService = new MediatorService;
export default mediatorService;