|
| 1 | +package com.qiniu.qvs; |
| 2 | + |
| 3 | +import com.qiniu.common.QiniuException; |
| 4 | +import com.qiniu.qvs.model.Device; |
| 5 | +import com.qiniu.qvs.model.PatchOperation; |
| 6 | +import com.qiniu.http.Client; |
| 7 | +import com.qiniu.http.Response; |
| 8 | +import com.qiniu.util.Auth; |
| 9 | +import com.qiniu.util.StringMap; |
| 10 | +import com.qiniu.util.UrlUtils; |
| 11 | + |
| 12 | +public class DeviceManager { |
| 13 | + |
| 14 | + private final String apiServer; |
| 15 | + private final Client client; |
| 16 | + private final Auth auth; |
| 17 | + |
| 18 | + public DeviceManager(Auth auth) { |
| 19 | + this(auth, "http://qvs.qiniuapi.com"); |
| 20 | + } |
| 21 | + |
| 22 | + public DeviceManager(Auth auth, String apiServer) { |
| 23 | + this(auth, apiServer, new Client()); |
| 24 | + } |
| 25 | + |
| 26 | + public DeviceManager(Auth auth, String apiServer, Client client) { |
| 27 | + this.auth = auth; |
| 28 | + this.apiServer = apiServer; |
| 29 | + this.client = client; |
| 30 | + } |
| 31 | + |
| 32 | + |
| 33 | + /* |
| 34 | + * 创建设备 |
| 35 | + */ |
| 36 | + public Response createDevice(String namespaceId, Device device) throws QiniuException { |
| 37 | + StringMap params = new StringMap(); |
| 38 | + params.put("name", device.getName()); |
| 39 | + params.put("gbId", device.getGbId()); |
| 40 | + params.putNotNull("username", device.getUsername()); |
| 41 | + params.putNotNull("password", device.getPassword()); |
| 42 | + params.put("pullIfRegister", device.isPullIfRegister()); |
| 43 | + params.put("desc", device.getDesc()); |
| 44 | + |
| 45 | + String url = String.format("%s/v1/namespaces/%s/devices", apiServer, namespaceId); |
| 46 | + return QvsResponse.post(url, params, client, auth); |
| 47 | + } |
| 48 | + |
| 49 | + /* |
| 50 | + * 删除设备 |
| 51 | + */ |
| 52 | + public Response deleteDevice(String namespaceId, String gbId) throws QiniuException { |
| 53 | + String url = String.format("%s/v1/namespaces/%s/devices/%s", apiServer, namespaceId, gbId); |
| 54 | + return QvsResponse.delete(url, client, auth); |
| 55 | + } |
| 56 | + |
| 57 | + /* |
| 58 | + * 更新设备 |
| 59 | + */ |
| 60 | + public Response updateDevice(String namespaceId, String gbId, PatchOperation[] patchOperation) |
| 61 | + throws QiniuException { |
| 62 | + StringMap params = new StringMap().put("operations", patchOperation); |
| 63 | + String url = String.format("%s/v1/namespaces/%s/devices/%s", apiServer, namespaceId, gbId); |
| 64 | + return QvsResponse.patch(url, params, client, auth); |
| 65 | + } |
| 66 | + |
| 67 | + /* |
| 68 | + * 查询设备 |
| 69 | + */ |
| 70 | + public Response queryDevice(String namespaceId, String gbId) throws QiniuException { |
| 71 | + String url = String.format("%s/v1/namespaces/%s/devices/%s", apiServer, namespaceId, gbId); |
| 72 | + return QvsResponse.get(url, client, auth); |
| 73 | + } |
| 74 | + |
| 75 | + /* |
| 76 | + * 获取设备列表 |
| 77 | + */ |
| 78 | + public Response listDevice(String namespaceId, int offset, int line, String prefix, String state, int qtype) |
| 79 | + throws QiniuException { |
| 80 | + String requestUrl = String.format("%s/v1/namespaces/%s/devices", apiServer, namespaceId); |
| 81 | + StringMap map = new StringMap().put("offset", offset).put("line", line).put("qtype", qtype) |
| 82 | + .put("prefix", prefix).put("state", state); |
| 83 | + requestUrl = UrlUtils.composeUrlWithQueries(requestUrl, map); |
| 84 | + return QvsResponse.get(requestUrl, client, auth); |
| 85 | + } |
| 86 | + |
| 87 | + /* |
| 88 | + * 获取通道列表 |
| 89 | + */ |
| 90 | + public Response listChannels(String namespaceId, String gbId, String prefix) throws QiniuException { |
| 91 | + String requestUrl = String.format("%s/v1/namespaces/%s/devices/%s/channels", apiServer, namespaceId, gbId); |
| 92 | + StringMap map = new StringMap().put("prefix", prefix); |
| 93 | + requestUrl = UrlUtils.composeUrlWithQueries(requestUrl, map); |
| 94 | + return QvsResponse.get(requestUrl, client, auth); |
| 95 | + } |
| 96 | + |
| 97 | + /* |
| 98 | + * 启动设备拉流 |
| 99 | + */ |
| 100 | + public Response startDevice(String namespaceId, String gbId, String[] channels) throws QiniuException { |
| 101 | + String url = String.format("%s/v1/namespaces/%s/devices/%s/start", apiServer, namespaceId, gbId); |
| 102 | + StringMap params = new StringMap().put("channels", channels); |
| 103 | + return QvsResponse.post(url, params, client, auth); |
| 104 | + } |
| 105 | + |
| 106 | + /* |
| 107 | + * 停止设备拉流 |
| 108 | + */ |
| 109 | + public Response stopDevice(String namespaceId, String gbId, String[] channels) throws QiniuException { |
| 110 | + String url = String.format("%s/v1/namespaces/%s/devices/%s/stop", apiServer, namespaceId, gbId); |
| 111 | + StringMap params = new StringMap().put("channels", channels); |
| 112 | + return QvsResponse.post(url, params, client, auth); |
| 113 | + } |
| 114 | + |
| 115 | +} |
0 commit comments