博世Vidos 自动化接口调用源码

来源:互联网 发布:mac虚拟机专用xp系统 编辑:程序博客网 时间:2024/05/22 06:52

using System;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.IO;
using log4net;
using jn.utils;
namespace jn.bosch.vidos
{
 /// <summary>
 /// 本类对Vidos的自动化接口命令进行封装,主要是进行编解码器的动态组织。
 /// 本类通过socket端口1758与vidos进行通讯
 /// </summary>
 public class VidosCmds
 {
  private TcpClient m_myclient;
  private NetworkStream networkStream ;
  private StreamReader m_streamReader ;
  private StreamWriter m_streamWriter ;
  private static ILog s_log = LogManager.GetLogger(typeof(VidosCmds));
  private bool m_flag;
        private string m_user;
        private string m_password;
        private string m_hostIp;
        private System.Windows.Forms.Timer m_reLoginTimer = new Timer();
  public VidosCmds(String vidosHostIp)
  {
            m_reLoginTimer.Interval = 1000 * 1 * 5;
            m_reLoginTimer.Tick+=new EventHandler(m_reLoginTimer_Tick);
   m_flag=false;
            m_hostIp = vidosHostIp;
   Conntect(vidosHostIp);
  }
  private void Conntect(String vidosHostIp)
  {         
   try
   {
                m_myclient = new TcpClient();
                ConnectVidos();              
                m_reLoginTimer.Stop();
   }               
   catch
   {
                MessageBox.Show("请检查vidos是否启动!");
                m_flag = false;
                m_reLoginTimer.Start();
    s_log.Info(string.Format("Failed to connect to server at {0}:1758", vidosHostIp));
   }   
  }
        private void m_reLoginTimer_Tick(object sender, EventArgs e)
        {       
            try
            {
                if (!m_myclient.Connected)
                {
                    try
                    {
                        m_myclient.Close();
                    }
                    catch (Exception ex)
                    {
                        s_log.Info(ex.Message);
                    }
                    m_myclient = new TcpClient();
                    ConnectVidos();
                }              
                Login();
            }
            catch(Exception ex)
            {
                s_log.Info(ex.Message);
            }
        }
        private void ConnectVidos()
        {
            int retVal = 1;
        //    TelNet.NDL_TcpClient_Connect(ref retVal, m_hostIp, 1758, 1000, false);
         //   TelNet.NDL_TcpClient_Stop(retVal);
            if (retVal > 0)
            {
                s_log.Info("Conneting to VIDos Server");
                m_myclient.Connect(m_hostIp, 1758);
                s_log.Info("Conneted");
                networkStream = m_myclient.GetStream();
                m_streamReader = new StreamReader(networkStream);
                m_streamWriter = new StreamWriter(networkStream);
                m_flag = true;
            }
            else
            {
                s_log.Info("mytelnet:there is no vidos on" + m_hostIp);
                throw new Exception("there is no vidos on" + m_hostIp);
            }
        }
        private bool Login()
        {
            if (false == m_flag) return false;
            try
            {             
                m_streamWriter.WriteLine("login " + m_user + " " + " " + m_password);
                m_streamWriter.Flush();
                s_log.Info(m_user + "登陆成功!");
                m_reLoginTimer.Stop();
                return m_flag;
            }
            catch
            {              
                s_log.Info(m_user + "登陆失败!");
                m_flag = false;
                m_reLoginTimer.Start();
                return m_flag;
            }
        }
        /// <summary>
        /// 登陆
        /// </summary>
        public bool Login(String userName, String userPws)
        {
            m_user = userName;
            m_password = userPws;
            if (m_flag)
            {
                if (Login())
                {
                    return true;
                }
                else
                {
                    MessageBox.Show("请检查用户名和密码是否正确!");
                    return false;
                }
            }
            else
            {
                return m_flag;
            }
        }
  /// <summary>
  /// 向服务端发送消息,并接收返回消息
  /// </summary>
  /// <param name="msg"></param>
  /// <returns></returns>
  private String SendMessage(String msg)
  {
            if (m_flag == false)
            {
                return "error";
            }
   try
   {                 
    m_streamWriter.WriteLine(msg);   
    s_log.Info("Send Message:"+msg);
    m_streamWriter.Flush();
                String reciveMsg = m_streamReader.ReadLine(); 
    s_log.Info("Reading Message:"+reciveMsg);
    return reciveMsg;   
   }
   catch
   {    
    s_log.Error("发送消息失败!");
                m_flag = false;
                m_reLoginTimer.Start();               
    return "error!";
   }   
  }
  
   
  /// <summary>
  /// 编解码器的连接。
  /// </summary>
  /// <param name="encoderAddress"></param>
  /// <param name="cameraId"></param>
  /// <param name="decoderAddress"></param>
  /// <param name="monitorid"></param>
  /// <returns></returns>
  public int Connect(String encoderAddress,int cameraId,String decoderAddress,int monitorid)
        {
           String msg = string.Format("connect({0}/camera/{1},{2}/monitor/{3});", encoderAddress, cameraId, decoderAddress, monitorid);
           s_log.Info(msg);
           if (m_flag == false) return -1;
   String retStr=SendMessage(msg);
   //成功
      if(null==retStr||retStr.ToLower()!="0") 
   {
    //throw new CmdException(retStr);
                s_log.Info("connect encoder and decoder error:"+retStr);
                return -1;
   }  
   return 0;
   
  }
  /// <summary>
  /// 断开与该监视器连接的视频
  /// </summary>
  /// <param name="decoderAddress"></param>
  /// <param name="monitorid"></param>
  /// <returns></returns>
  public int Disconnect(String decoderAddress,int monitorid)
  {
            if (m_flag == false) return -1;
   String msg=string.Format("disconnect({0}/monitor/{1});",decoderAddress,monitorid);
   String retStr=SendMessage(msg);
   //成功
   if(retStr.ToLower()!="0") 
   {
    throw new CmdException(retStr);
   }   
   return 0;
  }
  /// <summary>
  /// 得到监视器对象的连接的视频信息
  /// </summary>
  /// <param name="decoderAddress"></param>
  /// <param name="monitorid"></param>
  /// <returns></returns>
  public String GetCamera(String decoderAddress,int monitorid)
  {
            if (m_flag == false) return "";
   String msg=string.Format("getCamera({0}/monitor/{1});",decoderAddress,monitorid);
   String retStr=SendMessage(msg);
   //成功
   if(retStr.ToLower()=="noobjecterror")
   {
    throw new CmdException(retStr);
   }
   return retStr;
  }
  /// <summary>
  /// 为某个监视器设置声音类型
  /// mode0,1,2,3
  /// </summary>
  /// <param name="decoderAddress"></param>
  /// <param name="monitorid"></param>
  /// <param name="mode"></param>
  /// <returns></returns>
  public int SetAutio(String decoderAddress,int monitorid,String mode)
  {
          
   String msg=string.Format("setAutio({0}/monitor/{1},{2});",decoderAddress,monitorid,mode);
   String retStr=SendMessage(msg);
   //成功
   if(retStr.ToLower()!="0") 
   {
    throw new CmdException(retStr);
   }   
   return 0;
  }
  /// <summary>
  /// 开始录象
  /// </summary>
  /// <param name="encoderAddress"></param>
  /// <param name="cameraId"></param>
  /// <returns></returns>
  public int StartRecording(String encoderAddress,int cameraId)
  {
   String msg=string.Format("startRecording({0}/camera/{1});",encoderAddress,cameraId);
   String retStr=SendMessage(msg);
   //成功
   if(retStr.ToLower()!="0") 
   {
    throw new CmdException(retStr);
   }   
   return 0;
  }
  /// <summary>
  /// 停止录象
  /// </summary>
  /// <param name="encoderAddress"></param>
  /// <param name="cameraId"></param>
  /// <returns></returns>
  public int StopRecording(String encoderAddress,int cameraId)
  {
   String msg=string.Format("stopRecording({0}/camera/{1});",encoderAddress,cameraId);
   String retStr=SendMessage(msg);
   //成功
   if(retStr.ToLower()!="0") 
   {
    throw new CmdException(retStr);
   }   
   return 0;
  }
  /// <summary>
  /// 得到录象编号
  /// </summary>
  /// <param name="encoderAddress"></param>
  /// <param name="cameraId"></param>
  /// <returns></returns>
  public int GetRecordingId(String encoderAddress,int cameraId)
  {
   String msg=string.Format("getRecordingId({0}/camera/{1});",encoderAddress,cameraId);
   String retStr=SendMessage(msg);
   //成功
   if(retStr.ToLower()=="-1") 
   {
    throw new CmdException(retStr);
   }   
   return Convert.ToInt32(retStr);
  }
  /// <summary>
  ///保存本地快照
  /// </summary>
  /// <param name="decoderAddress"></param>
  /// <param name="monitorid"></param>
  /// <returns></returns>
  public int SaveSnapshot(String decoderAddress,int monitorid)
  {
   return -1;
  // String msg=string.Format("saveSnapshot({0}/monitor/{1});",decoderAddress,monitorid);
            //String msg=string.Format("saveSnapshot(VIP X2 1/sparkCamera/MPEG-4/Encoder 1);");
            //String retStr=SendMessage(msg);
            ////成功
            //if(retStr.ToLower()!="0")
            //{
            //    throw new CmdException(retStr);
            //}
            //return 0;
  }
  /// <summary>
  ///云台控制
  /// </summary>
  /// <param name="decoderAddress"></param>
  /// <param name="monitorid"></param>
  /// <returns></returns>
  public int MovePtz(String decoderAddress,int monitorid,int pan,int titlt,int zoom)
  {
   String msg=string.Format("movePtz({0}/monitor/{1},{2},{3},{4});",decoderAddress,monitorid,pan,titlt,zoom);
   String retStr=SendMessage(msg);
   //成功
   if(retStr.ToLower()!="0")
   {
    throw new CmdException(retStr);
   }
   return 0;
  }
  /// <summary>
  /// 周边设备的管理
  /// </summary>
  /// <param name="decoderAddress"></param>
  /// <param name="cmd"></param>
  /// <param name="cmdValue"></param>
  /// <returns></returns>
  public int ExecPeriperal(String decoderAddress,int monitorid,String cmd,String cmdValue)
  {
   String msg=string.Format("execPeriperal({0}/monitor/{1},{2},{3},{4});",decoderAddress,monitorid,cmd,cmdValue);
   String retStr=SendMessage(msg);
   //成功
   if(retStr.ToLower()!="0")
   {
    throw new CmdException(retStr);
   }
   return 0;
   
  }
  /// <summary>
  /// 设置输出
  /// </summary>
  /// <param name="output"></param>
  /// <param name="outValue"></param>
  /// <returns></returns>
  public int SetOutput(String output,int outValue)
  {
   String msg=string.Format("set({0},{1});",output,outValue);
   String retStr=SendMessage(msg);
   //成功
   if(retStr.ToLower()!="0")
   {
    throw new CmdException(retStr);
   }
   return 0;
  }
  /// <summary>
  /// 得到输入
  /// </summary>
  /// <param name="output"></param>
  /// <param name="outValue"></param>
  /// <returns></returns>
  public int GetInput(String intput)
  {
   String msg=string.Format("get({0});",intput);
   String retStr=SendMessage(msg);
   //成功
   if(retStr.ToLower()!="0")
   {
    throw new CmdException(retStr);
   }
   return Convert.ToInt32(retStr);
  }
  /// <summary>
  /// 设置地图
  /// </summary>
  /// <param name="intput"></param>
  /// <returns></returns>
  public int ChangeSiteMap(String siteMap)
  {
   String msg=string.Format("changeSitemap({0});",siteMap);
   String retStr=SendMessage(msg);
   //成功
   if(retStr.ToLower()!="0")
   {
    throw new CmdException(retStr);
   }
   return 0;
  }
  /// <summary>
  /// 得到活动控制
  /// </summary>
  /// <returns></returns>
  public int GetVisibleControls()
  {
   String msg="getVisibleControls";
   String retStr=SendMessage(msg);
   //成功
   if(retStr.ToLower()!="0")
   {
    throw new CmdException(retStr);
   }
   return 0;
  }

  /// <summary>
  /// 控制界面要素
  /// </summary>
  /// <returns></returns>
  public int ActiveControls(String element,int mode)
  {
   String msg=string.Format("activeControls({0},{1});",element,mode);
   String retStr=SendMessage(msg);
   //成功
   if(retStr.ToLower()!="0")
   {
    throw new CmdException(retStr);
   }
   return 0;
  }

  /// <summary>
  /// 控制界面要素
  /// </summary>
  /// <returns></returns>
  public int ProcessAlarm(String command)
  {
   String msg=string.Format("processAlarm({0});",command);
   String retStr=SendMessage(msg);
   //成功
   if(retStr.ToLower()!="0")
   {
    throw new CmdException(retStr);
   }
   return 0;
  }
  /// <summary>
  /// 控制界面要素
  /// </summary>
  /// <returns></returns>
  public int Replay(String decoderAddress,int monitorid,String command,String cmdValue)
  {
   
   String msg=string.Format("replay({0}/monitor/{1},{2},{3});",decoderAddress,monitorid,command,cmdValue);
   String retStr=SendMessage(msg);  
   //成功
   if(retStr.ToLower()!="0")
   {
    throw new CmdException(retStr);
   }
   return 0;
  }
  /// <summary>
  /// 关闭连接
  /// </summary>
  public void Close()
  {
   try
   {
    m_streamWriter.Close();
    m_streamReader.Close();
    m_myclient.Close();
   }
   catch{}
  }
 }
 public class CmdException:Exception
 {
  public CmdException(String msg):base(msg){}
 }
}