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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
package com.genersoft.iot.vmp.vmanager.server;
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.genersoft.iot.vmp.VManageBootstrap;
import com.genersoft.iot.vmp.common.VersionPo;
import com.genersoft.iot.vmp.conf.SipConfig;
import com.genersoft.iot.vmp.conf.UserSetting;
import com.genersoft.iot.vmp.conf.VersionInfo;
import com.genersoft.iot.vmp.media.zlm.dto.MediaServerItem;
import com.genersoft.iot.vmp.service.IMediaServerService;
import com.genersoft.iot.vmp.utils.SpringBeanFactory;
import com.genersoft.iot.vmp.vmanager.bean.WVPResult;
import gov.nist.javax.sip.SipStackImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
 
import javax.sip.ListeningPoint;
import javax.sip.ObjectInUseException;
import javax.sip.SipProvider;
import java.util.Iterator;
import java.util.List;
 
@SuppressWarnings("rawtypes")
@Api(tags = "服务控制")
@CrossOrigin
@RestController
@RequestMapping("/api/server")
public class ServerController {
 
    @Autowired
    private ConfigurableApplicationContext context;
 
    @Autowired
    private IMediaServerService mediaServerService;
 
    @Autowired
    VersionInfo versionInfo;
 
    @Autowired
    SipConfig sipConfig;
 
    @Autowired
    UserSetting userSetting;
 
    @Value("${server.port}")
    private int serverPort;
 
 
    @ApiOperation("流媒体服务列表")
    @GetMapping(value = "/media_server/list")
    @ResponseBody
    public WVPResult<List<MediaServerItem>> getMediaServerList(boolean detail){
        WVPResult<List<MediaServerItem>> result = new WVPResult<>();
        result.setCode(0);
        result.setMsg("success");
        result.setData(mediaServerService.getAll());
        return result;
    }
 
    @ApiOperation("在线流媒体服务列表")
    @GetMapping(value = "/media_server/online/list")
    @ResponseBody
    public WVPResult<List<MediaServerItem>> getOnlineMediaServerList(){
        WVPResult<List<MediaServerItem>> result = new WVPResult<>();
        result.setCode(0);
        result.setMsg("success");
        result.setData(mediaServerService.getAllOnline());
        return result;
    }
 
    @ApiOperation("获取流媒体服务")
    @GetMapping(value = "/media_server/one/{id}")
    @ResponseBody
    public WVPResult<MediaServerItem> getMediaServer(@PathVariable String id){
        WVPResult<MediaServerItem> result = new WVPResult<>();
        result.setCode(0);
        result.setMsg("success");
        result.setData(mediaServerService.getOne(id));
        return result;
    }
 
    @ApiOperation("测试流媒体服务")
    @ApiImplicitParams({
            @ApiImplicitParam(name="ip", value = "流媒体服务IP", dataTypeClass = String.class),
            @ApiImplicitParam(name="port", value = "流媒体服务HTT端口", dataTypeClass = Integer.class),
            @ApiImplicitParam(name="secret", value = "流媒体服务secret", dataTypeClass = String.class),
    })
    @GetMapping(value = "/media_server/check")
    @ResponseBody
    public WVPResult<MediaServerItem> checkMediaServer(@RequestParam String ip, @RequestParam int port, @RequestParam String secret){
        return mediaServerService.checkMediaServer(ip, port, secret);
    }
 
    @ApiOperation("测试流媒体录像管理服务")
    @ApiImplicitParams({
            @ApiImplicitParam(name="ip", value = "流媒体服务IP", dataTypeClass = String.class),
            @ApiImplicitParam(name="port", value = "流媒体服务HTT端口", dataTypeClass = Integer.class),
            @ApiImplicitParam(name="secret", value = "流媒体服务secret", dataTypeClass = String.class),
    })
    @GetMapping(value = "/media_server/record/check")
    @ResponseBody
    public WVPResult<String> checkMediaRecordServer(@RequestParam String ip, @RequestParam int port){
        boolean checkResult = mediaServerService.checkMediaRecordServer(ip, port);
        WVPResult<String> result = new WVPResult<>();
        if (checkResult) {
            result.setCode(0);
            result.setMsg("success");
 
        }else {
            result.setCode(-1);
            result.setMsg("连接失败");
        }
        return result;
    }
 
    @ApiOperation("保存流媒体服务")
    @ApiImplicitParams({
            @ApiImplicitParam(name="mediaServerItem", value = "流媒体信息", dataTypeClass = MediaServerItem.class)
    })
    @PostMapping(value = "/media_server/save")
    @ResponseBody
    public WVPResult<String> saveMediaServer(@RequestBody  MediaServerItem mediaServerItem){
        MediaServerItem mediaServerItemInDatabase = mediaServerService.getOne(mediaServerItem.getId());
 
        if (mediaServerItemInDatabase != null) {
            if (StringUtils.isEmpty(mediaServerItemInDatabase.getSendRtpPortRange())
                    && StringUtils.isEmpty(mediaServerItem.getSendRtpPortRange())){
                mediaServerItem.setSendRtpPortRange("30000,30500");
            }
           mediaServerService.update(mediaServerItem);
        }else {
            if (StringUtils.isEmpty(mediaServerItem.getSendRtpPortRange())){
                mediaServerItem.setSendRtpPortRange("30000,30500");
            }
            return mediaServerService.add(mediaServerItem);
        }
 
        WVPResult<String> result = new WVPResult<>();
        result.setCode(0);
        result.setMsg("success");
        return result;
    }
 
    @ApiOperation("移除流媒体服务")
    @ApiImplicitParams({
            @ApiImplicitParam(name="id", value = "流媒体ID", dataTypeClass = String.class)
    })
    @DeleteMapping(value = "/media_server/delete")
    @ResponseBody
    public WVPResult<String> deleteMediaServer(@RequestParam  String id){
        if (mediaServerService.getOne(id) != null) {
            mediaServerService.delete(id);
            mediaServerService.deleteDb(id);
        }else {
            WVPResult<String> result = new WVPResult<>();
            result.setCode(-1);
            result.setMsg("未找到此节点");
            return result;
        }
        WVPResult<String> result = new WVPResult<>();
        result.setCode(0);
        result.setMsg("success");
        return result;
    }
 
 
 
    @ApiOperation("重启服务")
    @GetMapping(value = "/restart")
    @ResponseBody
    public Object restart(){
        Thread restartThread = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(3000);
                    SipProvider up = (SipProvider) SpringBeanFactory.getBean("udpSipProvider");
                    SipStackImpl stack = (SipStackImpl)up.getSipStack();
                    stack.stop();
                    Iterator listener = stack.getListeningPoints();
                    while (listener.hasNext()) {
                        stack.deleteListeningPoint((ListeningPoint) listener.next());
                    }
                    Iterator providers = stack.getSipProviders();
                    while (providers.hasNext()) {
                        stack.deleteSipProvider((SipProvider) providers.next());
                    }
                    VManageBootstrap.restart();
                } catch (InterruptedException ignored) {
                } catch (ObjectInUseException e) {
                    e.printStackTrace();
                }
            }
        });
 
        restartThread.setDaemon(false);
        restartThread.start();
        return "success";
    }
 
    @ApiOperation("版本信息")
    @GetMapping(value = "/version")
    @ResponseBody
    public WVPResult<VersionPo> getVersion(){
        WVPResult<VersionPo> result = new WVPResult<>();
        result.setCode(0);
        result.setMsg("success");
        result.setData(versionInfo.getVersion());
        return result;
    }
 
    @ApiOperation("配置信息")
    @GetMapping(value = "/config")
    @ApiImplicitParams({
            @ApiImplicitParam(name="type", value = "配置类型(sip, base)", dataTypeClass = String.class),
    })
    @ResponseBody
    public WVPResult<JSONObject> getVersion(String type){
        WVPResult<JSONObject> result = new WVPResult<>();
        result.setCode(0);
        result.setMsg("success");
 
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("server.port", serverPort);
        if (StringUtils.isEmpty(type)) {
            jsonObject.put("sip", JSON.toJSON(sipConfig));
            jsonObject.put("base", JSON.toJSON(userSetting));
        }else {
            switch (type){
                case "sip":
                    jsonObject.put("sip", sipConfig);
                    break;
                case "base":
                    jsonObject.put("base", userSetting);
                    break;
                default:
                    break;
            }
        }
        result.setData(jsonObject);
        return result;
    }
}