ntv-wangjian преди 3 години
родител
ревизия
8f9a03f297
променени са 2 файла, в които са добавени 54 реда и са изтрити 19 реда
  1. 46
    13
      api/controllers/vag.js
  2. 8
    6
      api/server.js

+ 46
- 13
api/controllers/vag.js Целия файл

@@ -1,18 +1,51 @@
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 17
 //获取所有SIP会话
3 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 22
     this.sessions.forEach(function (session, id) {
7 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 31
 //获取指定设备ID的目录数据
32
+//改造成返回某个设备的信息
15 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 49
     let result = { result: false, message: 'OK' };
17 50
     if (this.sessions.has(req.params.device)) {
18 51
         let session = this.sessions.get(req.params.device);
@@ -24,7 +57,7 @@ function getCatalog(req, res) {
24 57
         result.message = 'device not online.';
25 58
     }
26 59
     res.json(result);
27
-}
60
+}*/
28 61
 
29 62
 //预览请求
30 63
 async function realplay(req, res) {
@@ -69,7 +102,7 @@ async function realplay(req, res) {
69 102
         result.result = false;
70 103
         result.message = 'device not online.';
71 104
     }
72
-    res.json(result);
105
+    res.json(ntvMsg(result));
73 106
 }
74 107
 
75 108
 //回看请求
@@ -113,7 +146,7 @@ async function playback(req, res) {
113 146
         result.result = false;
114 147
         result.message = 'device not online';
115 148
     }
116
-    res.json(result);
149
+    res.json(ntvMsg(result));
117 150
 }
118 151
 
119 152
 //回看播放控制
@@ -140,7 +173,7 @@ async function playControl(req, res) {
140 173
         result.result = false;
141 174
         result.message = 'device not online.';
142 175
     }
143
-    res.json(result);
176
+    res.json(ntvMsg(result));
144 177
 }
145 178
 
146 179
 //云台控制
@@ -169,7 +202,7 @@ function ptzControl(req, res) {
169 202
         result.result = false;
170 203
         result.message = 'device not online.';
171 204
     }
172
-    res.json(result);
205
+    res.json(ntvMsg(result));
173 206
 }
174 207
 
175 208
 //录像文件查询
@@ -209,7 +242,7 @@ async function recordQuery(req, res) {
209 242
         result.result = false;
210 243
         result.message = 'device not online.';
211 244
     }
212
-    res.json(result);
245
+    res.json(ntvMsg(result));
213 246
 }
214 247
 
215 248
 //关闭流
@@ -258,7 +291,7 @@ function closeStream(req, res) {
258 291
         }
259 292
     }
260 293
 
261
-    res.json(result);
294
+    res.json(ntvMsg(result));
262 295
 }
263 296
 
264 297
 function _prefixInteger(num, m) {

+ 8
- 6
api/server.js Целия файл

@@ -30,12 +30,14 @@ class NodeHttpServer {
30 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 37
     app.get('/', (req, res, next) => {
36 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 41
       res.end();
40 42
     });
41 43
 
@@ -44,15 +46,15 @@ class NodeHttpServer {
44 46
 
45 47
   run() {
46 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 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 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…
Отказ
Запис