package com.fangshuoit.shipin;
import java.io.IOException;
import com.hsview.client.HsviewClient;
import com.hsview.client.HsviewRequest;
import com.hsview.client.HsviewResponse;
import com.hsview.client.api.civil.device.GetDeviceStream;
import com.hsview.client.api.civil.user.UserLogin;
/**
* @project TestShipin
* @Company 宁夏方硕信息技术有限公司
* @author 周壹
* @QQ群 334594194
* @Blog http://blog.csdn.net/lovelvyan
* @date 2016年1月9日
* @time 下午1:35:43
*/
public class ShiPinMain {
private static HsviewClient m_client;
private static int mErrorCode = 0; // 网络错误码保留
/**
* 发送网络请求,并对请求结果的错误码进行处理
*
* @param req
* @return
* @throws IOException
*/
private static <T extends HsviewResponse> T request(HsviewRequest req)
throws IOException {
T t = m_client.request(req, 5 * 1000);
mErrorCode = 0;// 获取错误码之前设置为0;
filtration(t);
return t;
}
/**
* 发送网络请求,并对请求结果的错误码进行处理
*
* @param req
* @param timeOut
* 访问dms接口是,超时时间设置长一点
* @return
* @throws IOException
*/
private <T extends HsviewResponse> T request(HsviewRequest req, int timeOut)
throws IOException {
T t = m_client.request(req, timeOut);
mErrorCode = 0;// 获取错误码之前设置为0;
filtration(t);
return t;
}
public final class HttpCode {
// OK(API调用成功,但是具体返回结果,由content中的code和desc描述)
public static final int SC_OK = 200;
// Bad Request (API格式错误,无返回内容)
public static final int Bad_Request = 400;
// Unauthorized(用户名密码认证失败,无返回内容)
public static final int Unauthorized = 401;
// Forbidden (认证成功但是无权限,无返回内容)
public static final int Forbidden = 403;
// Not Found (请求的URI错误,无返回内容)
public static final int Not_Found = 404;
// Precondition
// Failed(先决条件失败,无返回内容。通常是由于客户端所带的x-hs-date时间与服务器时间相差过大。)
public static final int Precondition_Failed = 412;
// Internal Server
// Error(服务器内部错误,无返回内容)
public static final int Internal_Server_Error = 500;
// Service Unavailable
// (服务不可用,无返回内容。这种情况一般是因为接口调用超出频率限制。)
public static final int Service_Unavailable = 503;
}
/**
* 检查远程操作是否执行成功,检查网络错误码
*
* @param mHsviewResponse
*/
private static void filtration(HsviewResponse mHsviewResponse) {
if (mHsviewResponse.getCode() == HttpCode.SC_OK) { // 请求成功,则看服务器处理错误。
switch (mHsviewResponse.getApiRetCode()) { // 业务错误码。
case 0: // int默认值。
return;
case 1000:
return;
default:
break;
}
mErrorCode = mHsviewResponse.getApiRetCode();
} else {
mErrorCode = mHsviewResponse.getCode();// http错误码处理。
}
}
// 主函数执行
public void main(String[] args) {
System.out.println("执行到此!");
getLoginData();
}
// 乐橙云账号登录
public void getLoginData() {
m_client = new HsviewClient();
// 设置乐橙服务地址
m_client.setHost("www.lechange.cn");
// m_client.setHost("functiontest.lechange.cn");
// String username = "15857186729";
// String password = "123456";
// 设置乐橙账号
// 以开发者用户名密码设置账号,可以调用添加用户等开发者API,不需要获取令牌。
// 建议使用m_client.setAuthWithMd5(username, passwordMd5)。
String username = "18661931324";
String password = "yds0706ygw";
String md5Pssword = MD5Helper.encodeToLowerCase(password);
// MD5加密
m_client.setAuthWithMd5(username, md5Pssword);
new Thread(new Runnable() {
public void run() {
UserLogin req = new UserLogin();
UserLogin.Response resp = null;
try {
resp = request(req);
System.out.println("执行到此2!");
} catch (IOException e) {
e.printStackTrace();
}
// 网络请求结果处理。
if (resp == null || mErrorCode != 0) {
// 请求失败结果处理。
System.out.println("返回错误结果:" + resp.getBody());
} else {
// 登陆成功后进行数据解析,比如取P2P数据等。
System.out.println("返回正确结果:" + resp.getBody());
getPlayURL();
}
}
}).run();
}
// 获取视频播放地址
public void getPlayURL() {
m_client.setHost("www.lechange.cn");
// m_client.setHost("functiontest.lechange.cn");
GetDeviceStream req = new GetDeviceStream();
req.data.type = "real";// 所需码流类型,“real”表示直播,“playback”表示回放,“talk”表示语音对讲。
req.data.protocol = "HLS";// 码流协议移动端的拉流走的是RTSP,网页用HLS协议,可填“RTSP”或“HLS”。
req.data.streamId = 0;// 码流类型(0:主码流 1:辅码流)
req.data.channelId = "0";// 通道号
req.data.deviceId = "1K00C9DPAU00584";// 设备序列号
try {
GetDeviceStream.Response resp = null;
resp = request(req, 200);
System.out.println("视频播放地址:" + resp.getBody());
} catch (IOException e) {
// 异常信息处理。
}
}
}