|
| 1 | +package com.qiniu.caster; |
| 2 | + |
| 3 | +import com.google.gson.Gson; |
| 4 | +import com.google.gson.GsonBuilder; |
| 5 | +import com.google.gson.JsonElement; |
| 6 | +import com.qiniu.caster.model.CasterParams; |
| 7 | +import com.qiniu.caster.model.Overlay; |
| 8 | +import com.qiniu.caster.model.Text; |
| 9 | +import com.qiniu.common.Constants; |
| 10 | +import com.qiniu.common.QiniuException; |
| 11 | +import com.qiniu.http.Client; |
| 12 | +import com.qiniu.http.Response; |
| 13 | +import com.qiniu.util.Auth; |
| 14 | +import com.qiniu.util.Json; |
| 15 | +import com.qiniu.util.StringMap; |
| 16 | + |
| 17 | +import java.util.HashMap; |
| 18 | + |
| 19 | +public class CasterManager { |
| 20 | + private final Auth auth; |
| 21 | + private final String server; |
| 22 | + private final Client client; |
| 23 | + |
| 24 | + public CasterManager(Auth auth) { |
| 25 | + this(auth, "http://pili-caster.qiniuapi.com"); |
| 26 | + } |
| 27 | + |
| 28 | + private CasterManager(Auth auth, String server) { |
| 29 | + this.auth = auth; |
| 30 | + this.server = server; |
| 31 | + this.client = new Client(); |
| 32 | + } |
| 33 | + |
| 34 | + public CasterManager(Auth auth, String server, Client client) { |
| 35 | + this.auth = auth; |
| 36 | + this.server = server; |
| 37 | + this.client = client; |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * |
| 42 | + * @param name 导播台name |
| 43 | + * @param casterParams 导播台参数 |
| 44 | + * @return |
| 45 | + */ |
| 46 | + public Response createCaster(String name, CasterParams casterParams) { |
| 47 | + String url = server + "/v1/casters"; |
| 48 | + Gson gson = new GsonBuilder().setPrettyPrinting().create(); |
| 49 | + JsonElement jsonElement = gson.toJsonTree(casterParams); |
| 50 | + jsonElement.getAsJsonObject().addProperty("name",name); |
| 51 | + byte[] body = gson.toJson(jsonElement).getBytes(Constants.UTF_8); |
| 52 | + System.out.println(body.toString()); |
| 53 | + StringMap headers = auth.authorizationV2(url, "POST", body, Client.JsonMime); |
| 54 | + Response response = null; |
| 55 | + try { |
| 56 | + response = client.post(url, body, headers, Client.JsonMime); |
| 57 | + } catch (QiniuException e) { |
| 58 | + e.printStackTrace(); |
| 59 | + } |
| 60 | + return response; |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * |
| 65 | + * @param name 导播台name |
| 66 | + * @param marker |
| 67 | + * @param limit |
| 68 | + * @return |
| 69 | + */ |
| 70 | + public Response findCasters(String name, String marker, int limit) { |
| 71 | + StringMap map = new StringMap().putNotEmpty("marker", marker) |
| 72 | + .putNotEmpty("name", name).putWhen("limit", limit, limit > 0); |
| 73 | + String queryString = map.formString(); |
| 74 | + if (map.size() > 0) { |
| 75 | + queryString = "?" + queryString; |
| 76 | + } |
| 77 | + String url = String.format("%s/v1/casters/%s", server, queryString); |
| 78 | + StringMap headers = auth.authorizationV2(url); |
| 79 | + Response response = null; |
| 80 | + try { |
| 81 | + response = client.get(url,headers); |
| 82 | + } catch (QiniuException e) { |
| 83 | + e.printStackTrace(); |
| 84 | + } |
| 85 | + return response; |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * |
| 90 | + * @param casterId 导播台id |
| 91 | + * @return |
| 92 | + */ |
| 93 | + public Response getCasterInfo(String casterId){ |
| 94 | + String url = String.format("%s/v1/casters/%s", server, casterId); |
| 95 | + StringMap headers = auth.authorizationV2(url); |
| 96 | + Response response = null; |
| 97 | + try { |
| 98 | + response = client.get(url, headers); |
| 99 | + } catch (QiniuException e) { |
| 100 | + e.printStackTrace(); |
| 101 | + } |
| 102 | + return response; |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * |
| 107 | + * @param casterId 导播台id |
| 108 | + * @param start 开始时间戳 |
| 109 | + * @param hour 开启时长,设置为0,为1小时;导播台已开启时,表示延长开启时长 |
| 110 | + * @return |
| 111 | + */ |
| 112 | + public Response startCaster(String casterId, int start, int hour) { |
| 113 | + String url = String.format("%s/v1/casters/%s/start", server, casterId); |
| 114 | + StringMap map = new StringMap().putWhen("start", start, start>0) |
| 115 | + .putWhen("hour", hour, hour>0); |
| 116 | + byte[] body = Json.encode(map).getBytes(Constants.UTF_8); |
| 117 | + StringMap headers = auth.authorizationV2(url, "POST", body, Client.JsonMime); |
| 118 | + Response response = null; |
| 119 | + try { |
| 120 | + response = client.post(url, body, headers, Client.JsonMime); |
| 121 | + } catch (QiniuException e) { |
| 122 | + e.printStackTrace(); |
| 123 | + } |
| 124 | + return response; |
| 125 | + } |
| 126 | + |
| 127 | + /** |
| 128 | + * |
| 129 | + * @param casterId 导播台id |
| 130 | + * @return |
| 131 | + */ |
| 132 | + public Response stopCaster(String casterId) { |
| 133 | + String url = String.format("%s/v1/casters/%s/stop", server, casterId); |
| 134 | + StringMap map = new StringMap().putNotNull("CasterId", casterId); |
| 135 | + byte[] body = Json.encode(map).getBytes(Constants.UTF_8); |
| 136 | + StringMap headers = auth.authorizationV2(url, "POST", body, Client.JsonMime); |
| 137 | + Response response = null; |
| 138 | + try { |
| 139 | + response = client.post(url, body, headers, Client.JsonMime); |
| 140 | + } catch (QiniuException e) { |
| 141 | + e.printStackTrace(); |
| 142 | + } |
| 143 | + return response; |
| 144 | + } |
| 145 | + |
| 146 | + public Response deleteCaster(String casterId){ |
| 147 | + String url = String.format("%s/v1/casters/%s", server, casterId); |
| 148 | + StringMap map = new StringMap().putNotNull("CasterId", casterId); |
| 149 | + byte[] body = Json.encode(map).getBytes(Constants.UTF_8); |
| 150 | + StringMap headers = auth.authorizationV2(url, "DELETE", body, Client.JsonMime); |
| 151 | + Response response = null; |
| 152 | + try { |
| 153 | + response = client.delete(url, body, headers, Client.JsonMime); |
| 154 | + } catch (QiniuException e) { |
| 155 | + e.printStackTrace(); |
| 156 | + } |
| 157 | + return response; |
| 158 | + } |
| 159 | + |
| 160 | + public Response update(String casterId, CasterParams casterParams){ |
| 161 | + String url = String.format("%s/v1/casters/%s", server, casterId); |
| 162 | + byte[] body = Json.encode(casterParams).getBytes(Constants.UTF_8); |
| 163 | + StringMap headers = auth.authorizationV2(url, "POST", body, Client.JsonMime); |
| 164 | + Response response = null; |
| 165 | + try { |
| 166 | + response = client.post(url, body, headers, Client.JsonMime); |
| 167 | + } catch (QiniuException e) { |
| 168 | + e.printStackTrace(); |
| 169 | + } |
| 170 | + return response; |
| 171 | + } |
| 172 | + |
| 173 | + public Response updatePvw(String casterId, int channel, String staticKey){ |
| 174 | + String url = String.format("%s/v1/static/casters/%s/pvw", server, casterId); |
| 175 | + StringMap map = new StringMap().putNotNull("CasterID", casterId) |
| 176 | + .putWhen("channel", channel, channel>=0); |
| 177 | + byte[] body = Json.encode(map).getBytes(Constants.UTF_8); |
| 178 | + |
| 179 | + StringMap headers = new StringMap().put("Authorization", staticKey); |
| 180 | + Response response = null; |
| 181 | + try { |
| 182 | + response = client.post(url, body, headers, Client.JsonMime); |
| 183 | + } catch (QiniuException e) { |
| 184 | + e.printStackTrace(); |
| 185 | + } |
| 186 | + return response; |
| 187 | + } |
| 188 | + |
| 189 | + public Response updatePgm(String casterId, int channel){ |
| 190 | + String url = String.format("%s/v1/static/casters/%s/pgm", server, casterId); |
| 191 | + StringMap map = new StringMap().putNotNull("CasterID", casterId) |
| 192 | + .putWhen("channel", channel, channel>=0); |
| 193 | + byte[] body = Json.encode(map).getBytes(Constants.UTF_8); |
| 194 | + StringMap headers = auth.authorizationV2(url, "POST", body, Client.JsonMime); |
| 195 | + Response response = null; |
| 196 | + try { |
| 197 | + response = client.post(url, body, headers, Client.JsonMime); |
| 198 | + } catch (QiniuException e) { |
| 199 | + e.printStackTrace(); |
| 200 | + } |
| 201 | + return response; |
| 202 | + } |
| 203 | + |
| 204 | + public Response pvwSwitchToPgm(String casterId, boolean switchVol){ |
| 205 | + String url = String.format("%s/v1/static/casters/%s/switch", server, casterId); |
| 206 | + StringMap map = new StringMap().putNotNull("CasterID", casterId) |
| 207 | + .putNotNull("switchVol", switchVol); |
| 208 | + byte[] body = Json.encode(map).getBytes(Constants.UTF_8); |
| 209 | + StringMap headers = auth.authorizationV2(url, "POST", body, Client.JsonMime); |
| 210 | + Response response = null; |
| 211 | + try { |
| 212 | + response = client.post(url, body, headers, Client.JsonMime); |
| 213 | + } catch (QiniuException e) { |
| 214 | + e.printStackTrace(); |
| 215 | + } |
| 216 | + return response; |
| 217 | + } |
| 218 | + public Response updateMonitors(String casterId, int channel, String newUrl, int vol, boolean muted){ |
| 219 | + String url = String.format("%s/v1/static/casters/%s/monitors", server, casterId); |
| 220 | + StringMap map = new StringMap().putNotNull("CasterID", casterId) |
| 221 | + .putWhen("channel", channel, channel>=0) |
| 222 | + .putNotNull("url",newUrl).putWhen("vol", vol, vol>=0).putNotNull("muted",muted); |
| 223 | + byte[] body = Json.encode(map).getBytes(Constants.UTF_8); |
| 224 | + StringMap headers = auth.authorizationV2(url, "POST", body, Client.JsonMime); |
| 225 | + Response response = null; |
| 226 | + try { |
| 227 | + response = client.post(url, body, headers, Client.JsonMime); |
| 228 | + } catch (QiniuException e) { |
| 229 | + e.printStackTrace(); |
| 230 | + } |
| 231 | + return response; |
| 232 | + } |
| 233 | + public Response updatePublish(String casterId, String newUrl, int ab, int vb, boolean closed, boolean emergencyMode, int emergencyChannel, int delay){ |
| 234 | + String url = String.format("%s/v1/static/casters/%s/publish", server, casterId); |
| 235 | + StringMap map = new StringMap().putNotNull("CasterID", casterId) |
| 236 | + .putNotNull("url",newUrl).putWhen("vb", vb, vb>=300) |
| 237 | + .putWhen("ab",ab, ab>=64) |
| 238 | + .putNotNull("closed",closed).putNotNull("emergencyMode",emergencyMode) |
| 239 | + .putWhen("emergencyChannel", emergencyChannel, emergencyChannel>=0) |
| 240 | + .putWhen("delay", delay, delay>=0); |
| 241 | + |
| 242 | + byte[] body = Json.encode(map).getBytes(Constants.UTF_8); |
| 243 | + StringMap headers = auth.authorizationV2(url, "POST", body, Client.JsonMime); |
| 244 | + Response response = null; |
| 245 | + try { |
| 246 | + response = client.post(url, body, headers, Client.JsonMime); |
| 247 | + } catch (QiniuException e) { |
| 248 | + e.printStackTrace(); |
| 249 | + } |
| 250 | + return response; |
| 251 | + } |
| 252 | + public Response changeLayout(String casterId, int layout, String staticKey){ |
| 253 | + String url = String.format("%s/v1/static/casters/%s/pvw/layouts", server, casterId); |
| 254 | + StringMap map = new StringMap().putNotNull("CasterID", casterId) |
| 255 | + .putWhen("layout", layout, layout>=0); |
| 256 | + byte[] body = Json.encode(map).getBytes(Constants.UTF_8); |
| 257 | + StringMap headers = new StringMap().put("Authorization", staticKey); |
| 258 | + Response response = null; |
| 259 | + try { |
| 260 | + response = client.post(url, body, headers, Client.JsonMime); |
| 261 | + } catch (QiniuException e) { |
| 262 | + e.printStackTrace(); |
| 263 | + } |
| 264 | + return response; |
| 265 | + } |
| 266 | + public Response updateLayout(String casterId, int layout, String title, Overlay overlay, Text text, String staticKey){ |
| 267 | + String url = String.format("%s/v1/static/casters/%s/layouts", server, casterId); |
| 268 | + StringMap map = new StringMap().putNotNull("CasterID", casterId) |
| 269 | + .putWhen("layout", layout, layout>=0) |
| 270 | + .putNotNull("title",title).putNotNull("overlay", overlay).putNotNull("text", text); |
| 271 | + byte[] body = Json.encode(map).getBytes(Constants.UTF_8); |
| 272 | + StringMap headers = new StringMap().put("Authorization", staticKey); |
| 273 | + Response response = null; |
| 274 | + try { |
| 275 | + response = client.post(url, body, headers, Client.JsonMime); |
| 276 | + } catch (QiniuException e) { |
| 277 | + e.printStackTrace(); |
| 278 | + } |
| 279 | + return response; |
| 280 | + } |
| 281 | + |
| 282 | + |
| 283 | + |
| 284 | + |
| 285 | + |
| 286 | + |
| 287 | + |
| 288 | + |
| 289 | + |
| 290 | + |
| 291 | +} |
0 commit comments