Browse Source

修改API规则中

ntv-wangjian 3 years ago
parent
commit
8f9a03f297
2 changed files with 54 additions and 19 deletions
  1. 46
    13
      api/controllers/vag.js
  2. 8
    6
      api/server.js

+ 46
- 13
api/controllers/vag.js View File

1
-
1
+/**
2
+ * 将数据打包成ntv接口返回格式
3
+ * @param {json} msg 
4
+ */
5
+function ntvMsg(msg){
6
+    if("result" in msg){
7
+        msg.code     = msg.result?0:1;
8
+        delete msg.result;
9
+    }
10
+    if("message" in msg){
11
+        msg.err_desc = msg.message;
12
+        delete msg.message;
13
+    }
14
+    
15
+    return msg;
16
+}
2
 //获取所有SIP会话
17
 //获取所有SIP会话
3
 function getSessions(req, res, next) {
18
 function getSessions(req, res, next) {
4
-    let sessions = {};
5
-
19
+    let result = { code: 0, err_desc: 'OK' ,data:{}};
20
+    let items  = [];
21
+    let index  = 0;
6
     this.sessions.forEach(function (session, id) {
22
     this.sessions.forEach(function (session, id) {
7
         if (session.TAG === 'sip')
23
         if (session.TAG === 'sip')
8
-            sessions[session.id] = { host: session.via.host, port: session.via.port, info: session.deviceinfo, status: session.devicestatus, catalog: session.catalog };
24
+            items[index++] = { id:session.id,host: session.via.host, port: session.via.port, info: session.deviceinfo, status: session.devicestatus, catalog: session.catalog, playmode:session.playmode };
9
     });
25
     });
10
-
11
-    res.json(sessions);
26
+    result.data.count=index;
27
+    result.data.items=items;
28
+    res.json(result);
12
 }
29
 }
13
 
30
 
14
 //获取指定设备ID的目录数据
31
 //获取指定设备ID的目录数据
32
+//改造成返回某个设备的信息
15
 function getCatalog(req, res) {
33
 function getCatalog(req, res) {
34
+    let result = { code: 1, err_desc: 'OK' };
35
+    if (this.sessions.has(req.params.device)) {
36
+        let session = this.sessions.get(req.params.device);
37
+        result.code = 0;
38
+        result.data = {};
39
+        result.data.count=1;
40
+        result.data.items = [{ id:session.id, host: session.via.host, port: session.via.port, info: session.deviceinfo, status: session.devicestatus, catalog: session.catalog, playmode:session.playmode }];
41
+    }
42
+    else {
43
+        result.err_desc = '设备不在线或没有注册!';
44
+    }
45
+    res.json(result);
46
+}
47
+//原有版本
48
+/*function getCatalog(req, res) {
16
     let result = { result: false, message: 'OK' };
49
     let result = { result: false, message: 'OK' };
17
     if (this.sessions.has(req.params.device)) {
50
     if (this.sessions.has(req.params.device)) {
18
         let session = this.sessions.get(req.params.device);
51
         let session = this.sessions.get(req.params.device);
24
         result.message = 'device not online.';
57
         result.message = 'device not online.';
25
     }
58
     }
26
     res.json(result);
59
     res.json(result);
27
-}
60
+}*/
28
 
61
 
29
 //预览请求
62
 //预览请求
30
 async function realplay(req, res) {
63
 async function realplay(req, res) {
69
         result.result = false;
102
         result.result = false;
70
         result.message = 'device not online.';
103
         result.message = 'device not online.';
71
     }
104
     }
72
-    res.json(result);
105
+    res.json(ntvMsg(result));
73
 }
106
 }
74
 
107
 
75
 //回看请求
108
 //回看请求
113
         result.result = false;
146
         result.result = false;
114
         result.message = 'device not online';
147
         result.message = 'device not online';
115
     }
148
     }
116
-    res.json(result);
149
+    res.json(ntvMsg(result));
117
 }
150
 }
118
 
151
 
119
 //回看播放控制
152
 //回看播放控制
140
         result.result = false;
173
         result.result = false;
141
         result.message = 'device not online.';
174
         result.message = 'device not online.';
142
     }
175
     }
143
-    res.json(result);
176
+    res.json(ntvMsg(result));
144
 }
177
 }
145
 
178
 
146
 //云台控制
179
 //云台控制
169
         result.result = false;
202
         result.result = false;
170
         result.message = 'device not online.';
203
         result.message = 'device not online.';
171
     }
204
     }
172
-    res.json(result);
205
+    res.json(ntvMsg(result));
173
 }
206
 }
174
 
207
 
175
 //录像文件查询
208
 //录像文件查询
209
         result.result = false;
242
         result.result = false;
210
         result.message = 'device not online.';
243
         result.message = 'device not online.';
211
     }
244
     }
212
-    res.json(result);
245
+    res.json(ntvMsg(result));
213
 }
246
 }
214
 
247
 
215
 //关闭流
248
 //关闭流
258
         }
291
         }
259
     }
292
     }
260
 
293
 
261
-    res.json(result);
294
+    res.json(ntvMsg(result));
262
 }
295
 }
263
 
296
 
264
 function _prefixInteger(num, m) {
297
 function _prefixInteger(num, m) {

+ 8
- 6
api/server.js View File

30
       app.use('/api/*', basicAuth(this.config.VAG.auth.api_user, this.config.VAG.auth.api_pass));
30
       app.use('/api/*', basicAuth(this.config.VAG.auth.api_user, this.config.VAG.auth.api_pass));
31
     }
31
     }
32
 
32
 
33
-    app.use('/api/v1/vag', vagRoute(context));
33
+    // ntv 简化
34
+    //app.use('/api/v1/vag', vagRoute(context));
35
+    app.use('/api', vagRoute(context));
34
 
36
 
35
     app.get('/', (req, res, next) => {
37
     app.get('/', (req, res, next) => {
36
       res.setHeader('Content-type', 'application/json');
38
       res.setHeader('Content-type', 'application/json');
37
-      res.setHeader('Server', 'VAG');
38
-      res.send('{"success":true,"name":"YTMS-VAG 网关","version":"1.0.0.0"}');
39
+      res.setHeader('Server', 'ntv-api-server on www.ruiboyun.com');
40
+      res.send('{"success":true,"name":"api server","version":"1.1"}');
39
       res.end();
41
       res.end();
40
     });
42
     });
41
 
43
 
44
 
46
 
45
   run() {
47
   run() {
46
     this.httpServer.listen(this.port, () => {
48
     this.httpServer.listen(this.port, () => {
47
-      Logger.log(`Node Media Http Server started on port: ${this.port}`);
49
+      Logger.log(`Http API Server started on port: ${this.port}`);
48
     });
50
     });
49
 
51
 
50
     this.httpServer.on('error', (e) => {
52
     this.httpServer.on('error', (e) => {
51
-      Logger.error(`Node Media Http Server ${e}`);
53
+      Logger.error(`Http API Server ${e}`);
52
     });
54
     });
53
 
55
 
54
     this.httpServer.on('close', () => {
56
     this.httpServer.on('close', () => {
55
-      Logger.log('Node Media Http Server Close.');
57
+      Logger.log('Http API Server Close.');
56
     });
58
     });
57
   }
59
   }
58
 
60
 

Loading…
Cancel
Save