日期:2014-05-18 浏览次数:20700 次
public String controlDoor(final String siteId,String order,final String shutdown,final String loginId,final String ip) throws NumberFormatException, UnknownHostException, IOException{
if(ip.equals(null) || order.equals(null)){
log.info("ip地址、设备id、命令不能为空");
return null;
}
Socket socket = null;
try {
socket = new Socket(ip,Integer.valueOf(Constants.DBMAP.get("WS_DOORLOCK_PORT")));
} catch (Exception e) {
log.info("***********连接失败*************"+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
return "fail";
}
log.info("***********连接已经成功*************"+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
DataInputStream dis = new DataInputStream(socket.getInputStream());
DataOutputStream dos = new DataOutputStream(socket.getOutputStream());
// 执行开门动作
String result = openDoor(siteId, order, loginId, ip, socket, dis, dos);
// 开门成功
if("true".equals(result))
{
Timer timer = new Timer();
// 当开门成功6s后 自动关门
timer.schedule(new TimerTask() {
@Override
public void run() {
String flagString = "false";
Socket socket = null;
DataInputStream dis = null;
DataOutputStream dos = null;
try
{
socket = new Socket(ip,Integer.valueOf(Constants.DBMAP.get("WS_DOORLOCK_PORT")));
dis = new DataInputStream(socket.getInputStream());
dos = new DataOutputStream(socket.getOutputStream());
flagString =closeDoor(siteId, shutdown,loginId, ip, dis, dos, socket);
}
catch (UnknownHostException e)
{
log.error("第1次关门失败!!");
e.printStackTrace();
}
catch (IOException e)
{
log.error("第1次关门失败!!");
e.printStackTrace();
}
finally
{
// 如果关门失败 则启动保护线程 继续执行关门动作
if ("false".equals(flagString)||flagString.endsWith("false"))
{
TimeoutThread time = new TimeoutThread(2000,dis);
for(int i=1;i<5;i++)
{
try
{
Thread.sleep(400);
} catch (InterruptedException e)
{
e.printStackTrace();
}
if ("false".equals(flagString)||flagString.endsWith("false"))
{
try
{
socket = new Socket(ip,Integer.valueOf(Constants.DBMAP.get("WS_DOORLOCK_PORT")));
dis = new DataInputStream(socket.getInputStream());
dos =