Açıklama Yok
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

vag.js 9.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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. return msg;
  15. }
  16. //获取所有SIP会话
  17. function getSessions(req, res, next) {
  18. let result = { code: 0, err_desc: 'OK' ,data:{}};
  19. let items = [];
  20. let index = 0;
  21. this.sessions.forEach(function (session, id) {
  22. if (session.TAG === 'sip')
  23. 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 };
  24. });
  25. result.data.count=index;
  26. result.data.items=items;
  27. res.json(result);
  28. }
  29. //获取指定设备ID的目录数据
  30. //改造成返回某个设备的信息
  31. function getCatalog(req, res) {
  32. let result = { code: 1, err_desc: 'OK' };
  33. if (this.sessions.has(req.params.device)) {
  34. let session = this.sessions.get(req.params.device);
  35. result.code = 0;
  36. result.data = {};
  37. result.data.count=1;
  38. 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 }];
  39. }
  40. else {
  41. result.err_desc = '设备不在线或没有注册!';
  42. }
  43. res.json(result);
  44. }
  45. //原有版本
  46. /*function getCatalog(req, res) {
  47. let result = { result: false, message: 'OK' };
  48. if (this.sessions.has(req.params.device)) {
  49. let session = this.sessions.get(req.params.device);
  50. result.result = true;
  51. result.data = session.catalog;
  52. }
  53. else {
  54. result.message = 'device not online.';
  55. }
  56. res.json(result);
  57. }*/
  58. //预览请求
  59. async function realplay(req, res) {
  60. let result = { result: true, message: 'OK' };
  61. if (this.sessions.has(req.params.device)) {
  62. let session = this.sessions.get(req.params.device);
  63. //判断当前设备通道里是否存在通道编码
  64. let channelId = req.params.channel;
  65. let channel = session.catalog.devicelist.find(t => t.DeviceID === channelId);
  66. if (channel) {
  67. switch (req.params.action) {
  68. case 'start':
  69. {
  70. result = await session.sendRealPlayMessage(channelId, req.params.host, req.params.port, req.params.mode);
  71. }
  72. break;
  73. case 'stop':
  74. {
  75. result = await session.sendStopRealPlayMessage(channelId, req.params.host, req.params.port);
  76. }
  77. break;
  78. default:
  79. {
  80. result.result = false;
  81. result.message = 'action error.';
  82. }
  83. break;
  84. }
  85. }
  86. else {
  87. result.result = false;
  88. result.message = 'device not found.';
  89. }
  90. }
  91. else {
  92. result.result = false;
  93. result.message = 'device not online.';
  94. }
  95. res.json(ntvMsg(result));
  96. }
  97. //回看请求
  98. async function playback(req, res) {
  99. let result = { result: true, message: 'OK' };
  100. if (this.sessions.has(req.params.device)) {
  101. let session = this.sessions.get(req.params.device);
  102. //判断当前设备通道里是否存在通道编码
  103. let channelId = req.params.channel;
  104. let channel = session.catalog.devicelist.find(t => t.DeviceID === channelId);
  105. if (channel) {
  106. switch (req.params.action) {
  107. case 'start':
  108. {
  109. result = await session.sendPlaybackMessage(req.params.channel, req.params.begin, req.params.end, req.params.host, req.params.port, req.params.mode);
  110. }
  111. break;
  112. case 'stop':
  113. {
  114. result = await session.sendStopPlayBackMessage(req.params.channel, req.params.begin, req.params.end, req.params.host, req.params.port);
  115. }
  116. break;
  117. default:
  118. {
  119. result.result = false;
  120. result.message = 'action error.';
  121. }
  122. break;
  123. }
  124. }
  125. else {
  126. result.result = false;
  127. result.message = 'device not found.';
  128. }
  129. }
  130. else {
  131. result.result = false;
  132. result.message = 'device not online';
  133. }
  134. res.json(ntvMsg(result));
  135. }
  136. //回看播放控制
  137. async function playControl(req, res) {
  138. let result = {};
  139. if (this.sessions.has(req.params.device)) {
  140. let session = this.sessions.get(req.params.device);
  141. //判断当前设备通道里是否存在通道编码
  142. let channelId = req.params.channel;
  143. let channel = session.catalog.devicelist.find(t => t.DeviceID === channelId);
  144. if (channel) {
  145. result = await session.sendPlayControlMessage(req.params.channel, req.params.begin, req.params.end, req.params.cmd, req.params.value);
  146. }
  147. else {
  148. result.result = false;
  149. result.message = 'device not found.';
  150. }
  151. }
  152. else {
  153. result.result = false;
  154. result.message = 'device not online.';
  155. }
  156. res.json(ntvMsg(result));
  157. }
  158. //云台控制
  159. function ptzControl(req, res) {
  160. let result = {};
  161. if (this.sessions.has(req.params.device)) {
  162. let session = this.sessions.get(req.params.device);
  163. //判断当前设备通道里是否存在通道编码
  164. let channelId = req.params.channel;
  165. let channel = session.catalog.devicelist.find(t => t.DeviceID === channelId);
  166. if (channel) {
  167. session.ControlPTZ(req.params.channel, req.params.value);
  168. result.result = true;
  169. result.message = 'OK';
  170. }
  171. else {
  172. result.result = false;
  173. result.message = 'device not found.';
  174. }
  175. }
  176. else {
  177. result.result = false;
  178. result.message = 'device not online.';
  179. }
  180. res.json(ntvMsg(result));
  181. }
  182. //录像文件查询
  183. async function recordQuery(req, res) {
  184. let result = {};
  185. if (this.sessions.has(req.params.device)) {
  186. let session = this.sessions.get(req.params.device);
  187. //判断当前设备通道里是否存在通道编码
  188. let channelId = req.params.channel;
  189. let channel = session.catalog.devicelist.find(t => t.DeviceID === channelId);
  190. if (channel) {
  191. if (req.params.begin < req.params.end) {
  192. //unix时间转换
  193. var beginTime = new Date(req.params.begin * 1000).toJSON();
  194. var endTime = new Date(req.params.end * 1000).toJSON();
  195. result.data = await session.getRecordInfos(req.params.channel, beginTime, endTime);
  196. result.result = true;
  197. result.message = 'OK';
  198. }
  199. else {
  200. result.result = false;
  201. result.message = "beginTime 必须小于 endTime.";
  202. }
  203. }
  204. else {
  205. result.result = false;
  206. result.message = 'device not found.';
  207. }
  208. }
  209. else {
  210. result.result = false;
  211. result.message = 'device not online.';
  212. }
  213. res.json(ntvMsg(result));
  214. }
  215. //关闭流
  216. function closeStream(req, res) {
  217. let body = req.body;
  218. let result = { code: 0, msg: 'success' };
  219. if (body.stream) {
  220. //16位进制转10进制
  221. let ssrc = parseInt(body.stream, 16);
  222. //要补位
  223. ssrc = _prefixInteger(ssrc, 10);
  224. let selectSession = null;
  225. let selectDialog = null;
  226. for (session in this.sessions.values()) {
  227. let dialogs = sessson.dialogs;
  228. for (var key in dialogs) {
  229. let dialog = dialogs[key];
  230. if (dialog.ssrc && dialog.ssrc === ssrc) {
  231. selectSession = session;
  232. selectDialog = dialog;
  233. return;
  234. }
  235. }
  236. }
  237. if (selectDialog != null && selectSession != null) {
  238. if (selectDialog.play) {
  239. switch (selectDialog.play) {
  240. case 'realplay':
  241. {
  242. selectSession.StopRealPlay(selectDialog.channelId, selectDialog.host, selectDialog.port);
  243. }
  244. break;
  245. case 'playback':
  246. {
  247. selectSession.StopPlayBack(selectDialog.channelId, selectDialog.begin, selectDialog.end, selectDialog.host, selectDialog.port)
  248. }
  249. break;
  250. }
  251. }
  252. }
  253. }
  254. res.json(ntvMsg(result));
  255. }
  256. function _prefixInteger(num, m) {
  257. return (Array(m).join(0) + num).slice(-m);
  258. }
  259. module.exports = { getCatalog: getCatalog, realplay: realplay, getSessions: getSessions, playback: playback, ptzControl: ptzControl, playControl: playControl, recordQuery: recordQuery, closeStream: closeStream }