Пример #1
0
 /// <summary>
 /// 重新尝试连接小车
 /// </summary>
 public void ReConnect()
 {
     try
     {
         if (Init())
         {
             if (ReStart())
             {
                 LastConnectTime = DateTime.Now;
             }
             else
             {
                 ChargeStationInfo io = new ChargeStationInfo();
                 io.ID          = this.DeviceID;
                 io.IsCommBreak = true;
                 DelegateState.InvokeChargeChangeEvent(io);
             }
         }
         else
         {
             ChargeStationInfo io = new ChargeStationInfo();
             io.ID          = this.DeviceID;
             io.IsCommBreak = true;
             DelegateState.InvokeChargeChangeEvent(io);
         }
     }
     catch (Exception ex)
     {
         ChargeStationInfo io = new ChargeStationInfo();
         io.ID          = this.DeviceID;
         io.IsCommBreak = true;
         DelegateState.InvokeChargeChangeEvent(io);
     }
 }
Пример #2
0
 /// <summary>
 /// 新增按钮点击事件
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="e"></param>
 private void btnAdd_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
 {
     try
     {
         //移除行离开事件
         gvCharge.BeforeLeaveRow -= gvCharge_BeforeLeaveRow;
         //校检数据
         if (verify())
         {
             //自动赋予新ID值
             int ID = 0;
             if (bsCharge.Count > 0)
             {
                 ID = bsCharge.Count + 1;
             }
             //添加新行
             ChargeStationInfo addRow = new ChargeStationInfo();
             addRow.ID = ID;
             bsCharge.Add(addRow);
             bsCharge.ResetBindings(false);
             bsCharge.MoveLast();
         }
     }
     catch (Exception ex)
     {
         MsgBox.ShowError(ex.Message);
     }
     finally
     {
         gvCharge.BeforeLeaveRow += gvCharge_BeforeLeaveRow;
     }
 }
Пример #3
0
        /// <summary>
        /// 启动
        /// </summary>
        public bool Start()
        {
            try
            {
                IPAddress  ip   = IPAddress.Parse(ComPara.ServerIP);
                IPEndPoint ipep = new IPEndPoint(ip, ComPara.Port);//IP和端口

                Tcpsocket.Connect(new IPEndPoint(ip, ComPara.Port));

                processor = new Thread(Communication);
                processor.IsBackground = true;
                processor.Start();



                //IPAddress ip = IPAddress.Parse(ComPara.ServerIP);
                //IPEndPoint ipep = new IPEndPoint(ip, ComPara.Port);//IP和端口

                ////Tcpsocket.Connect(new IPEndPoint(ip, ComPara.Port));
                //ConnectSocketDelegate connect = ConnectSocket;
                //IAsyncResult asyncResult = connect.BeginInvoke(ipep, Tcpsocket, null, null);

                //bool connectSuccess = asyncResult.AsyncWaitHandle.WaitOne(3 * 1000, false);
                //if (!connectSuccess)
                //{
                //    //MessageBox.Show(string.Format("失败!错误信息:{0}", "连接超时"));
                //    return false;
                //}

                //bool result = connect.EndInvoke(asyncResult);
                //if (!result)
                //{ return false; }
                //LastRecTime = DateTime.Now;

                //processor = new Thread(Communication);
                //processor.IsBackground = true;
                //processor.Start();
                return(true);
            }
            catch (Exception ex)
            {
                ChargeStationInfo io = new ChargeStationInfo();
                io.ID          = this.DeviceID;
                io.IsCommBreak = true;
                DelegateState.InvokeChargeChangeEvent(io);
                return(false);
            }
            finally
            {
                communicationobserve_timer.Enabled = true;
            }
        }
Пример #4
0
 /// <summary>
 /// 校验数据
 /// </summary>
 /// <returns></returns>
 public bool verify()
 {
     try
     {
         gvCharge.CloseEditor();
         ChargeStationInfo Current = bsCharge.Current as ChargeStationInfo;
         if (Current != null)
         {
             if (string.IsNullOrEmpty(Current.IP))
             {
                 MsgBox.ShowError("请先维护IP");
                 return(false);
             }
             if (string.IsNullOrEmpty(Current.Port))
             {
                 MsgBox.ShowError("请先维护端口");
                 return(false);
             }
             if (string.IsNullOrEmpty(Current.ChargeLandCode))
             {
                 MsgBox.ShowError("请先维护充电桩地标");
                 return(false);
             }
             IList <ChargeStationInfo> Data = bsCharge.List as IList <ChargeStationInfo>;
             if (Data.Where(p => p.ID == Current.ID).Count() > 1)
             {
                 MsgBox.ShowError("当前充电桩编号已占用");
                 return(false);
             }
             if (Data.Where(p => p.IP == Current.IP).Count() > 1)
             {
                 MsgBox.ShowError("当前充电桩IP已占用");
                 return(false);
             }
             if (Data.Where(p => p.ChargeLandCode == Current.ChargeLandCode).Count() > 1)
             {
                 MsgBox.ShowError("当前充电桩对照地标已占用");
                 return(false);
             }
             return(true);
         }
         else
         {
             return(true);
         }
     }
     catch (Exception ex)
     {
         MsgBox.ShowError(ex.Message);
         return(false);
     }
 }
Пример #5
0
 /// <summary>
 /// 触发充电桩改变时事件
 /// </summary>
 /// <param name="e"></param>
 public async static void InvokeChargeChangeEvent(ChargeStationInfo e)
 {
     try
     {
         await Task.Factory.StartNew(() =>
         {
             ChargeEvent?.Invoke(e);
         });
     }
     catch (Exception ex)
     {
         LogHelper.WriteErrorLog(ex);
     }
 }
Пример #6
0
 /// <summary>
 /// 通讯观察线程
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="e"></param>
 public void CommunicationObser(object sender, System.Timers.ElapsedEventArgs e)
 {
     try
     {
         communicationobserve_timer.Enabled = false;
         if (LastRecLong > 2)
         {
             //如果接受消息时间已经大于2秒,则认为车子掉线了。
             //DelegateState.InvokeDispatchStateEvent(this.DeviceID.ToString() + "充电桩,已经掉线,将重新尝试连接...");
             //通知调度程序  充电桩已经掉线
             ChargeStationInfo ChargeInfo = new ChargeStationInfo();
             ChargeInfo.ID          = this.DeviceID;
             ChargeInfo.IsCommBreak = true;
             DelegateState.InvokeChargeChangeEvent(ChargeInfo);
             if (LastConnectLong > 3)
             {
                 //如果车子掉线且连接时间超过3秒则需要重连
                 LogHelper.WriteLog("重连充电桩" + DeviceID.ToString());
                 ReConnect();
             }
         }
         //if (LastConnectLong > 4 && LastRecLong > 3)
         //{
         //    ReConnect();//重新尝试连接
         //}
         //if (LastRecLong > 3)
         //{
         //    //通知调度程序  充电桩已经掉线
         //    ChargeStationInfo ChargeInfo = new ChargeStationInfo();
         //    ChargeInfo.ID = this.DeviceID;
         //    ChargeInfo.IsCommBreak = true;
         //    DelegateState.InvokeChargeChangeEvent(ChargeInfo);
         //}
     }
     catch (Exception ex)
     {
         LogHelper.WriteErrorLog(ex);
         //DelegateState.InvokeDispatchStateEvent(this.DeviceID.ToString() + "车,观察线程异常");
     }
     finally
     {
         communicationobserve_timer.Enabled = true;
     }
 }
Пример #7
0
 /// <summary>
 /// 初始化
 /// </summary>
 public bool Init()
 {
     try
     {
         Clear();
         Tcpsocket = new Socket(AddressFamily.InterNetwork,
                                SocketType.Stream, ProtocolType.Tcp);
         Tcpsocket.ReceiveTimeout = 200;
         return(true);
     }
     catch (Exception ex)
     {
         ChargeStationInfo io = new ChargeStationInfo();
         io.ID          = this.DeviceID;
         io.IsCommBreak = true;
         DelegateState.InvokeChargeChangeEvent(io);
         return(false);
     }
 }
Пример #8
0
        /// <summary>
        /// 命令反馈
        /// </summary>
        public bool GetCallBack()
        {
            try
            {
                int    offlinecount     = 0;
                int    allheadleftlengh = 3;
                int    receivedlengh    = 0;
                byte[] bufferhead       = new byte[3];
                while (allheadleftlengh - receivedlengh > 0)
                {
                    byte[] buffertemp = new byte[allheadleftlengh - receivedlengh];
                    if (Tcpsocket.Available <= 0)
                    {
                        continue;
                    }
                    int lengh = Tcpsocket.Receive(buffertemp);
                    if (lengh <= 0)
                    {
                        if (offlinecount == 3)
                        {
                            LogHelper.WriteReciveChargeMessLog("接受的充电桩" + DeviceID.ToString() + "反馈命令超时");
                            return(false);
                        }
                        offlinecount += 1;
                        Thread.Sleep(50);
                    }
                    Buffer.BlockCopy(buffertemp, 0, bufferhead, receivedlengh, lengh);
                    receivedlengh += lengh;
                }
                if (bufferhead[0] == 0x01 && bufferhead[1] == 0x01)
                {
                    offlinecount  = 0;
                    receivedlengh = 0;
                    int    allcontentleftlengh = Convert.ToInt32(bufferhead[2]) + 2;
                    byte[] buffercontent       = new byte[allcontentleftlengh];
                    while (allcontentleftlengh - receivedlengh > 0)
                    {
                        byte[] buffertemp = new byte[allcontentleftlengh - receivedlengh];
                        if (Tcpsocket.Available <= 0)
                        {
                            continue;
                        }
                        int lengh = Tcpsocket.Receive(buffertemp);
                        if (lengh <= 0)
                        {
                            if (offlinecount == 3)
                            {
                                LogHelper.WriteReciveChargeMessLog("接受的充电桩" + DeviceID.ToString() + "反馈命令超时");
                                return(false);
                            }
                            offlinecount += 1;
                            Thread.Sleep(50);
                        }
                        Buffer.BlockCopy(buffertemp, 0, buffercontent, receivedlengh, lengh);
                        receivedlengh += lengh;
                    }
                    List <byte> msg     = new List <byte>();
                    string      SenDLog = "";
                    msg.AddRange(bufferhead);
                    msg.AddRange(buffercontent);
                    foreach (byte item in msg)
                    {
                        SenDLog += ((int)item).ToString("X") + " ";
                    }
                    LogHelper.WriteReciveChargeMessLog("接受的充电桩" + this.DeviceID.ToString() + "反馈命令:" + SenDLog);


                    //List<byte> dd = new List<byte>();
                    //dd.AddRange(new byte[] { 0x01, 0x01, 0x03, 0x06, 0x01, 0x01, 0x1C, 0x1F });
                    //var rr = BitConverter.GetBytes(CRC.CRC16(dd.Take(dd.Count - 2).ToArray(), 0, dd.Count - 3));


                    var r = BitConverter.GetBytes(CRC.CRC16(msg.Take(msg.Count - 2).ToArray(), 0, msg.Count - 3));
                    if (msg[msg.Count - 2] != r[1] || msg[msg.Count - 1] != r[0])
                    {
                        LogHelper.WriteReciveChargeMessLog("接受的充电桩" + this.DeviceID.ToString() + "校验位错误!");
                        return(false);
                    }


                    //分析充电桩状态 0待机 1故障 2进行 3完成
                    ChargeStationInfo chargestation = new ChargeStationInfo();
                    chargestation.ID = this.DeviceID;
                    string StateStr = Convert.ToString(msg[3], 2).PadLeft(8, '0');
                    if (StateStr.Substring(7, 1) == "1")
                    {
                        chargestation.ChargeState = 0;
                    }
                    else if (StateStr.Substring(6, 1) == "1")
                    {
                        chargestation.ChargeState = 1;
                    }
                    else if (StateStr.Substring(5, 1) == "1")
                    {
                        chargestation.ChargeState = 2;
                    }
                    else if (StateStr.Substring(4, 1) == "1")
                    {
                        chargestation.ChargeState = 3;
                    }
                    else
                    {
                        chargestation.ChargeState = -1;
                    }
                    chargestation.IsCommBreak = false;
                    DelegateState.InvokeChargeChangeEvent(chargestation);
                    LastRecTime = DateTime.Now;
                    return(true);
                }
                else if (bufferhead[0] == 0x01 && bufferhead[1] == 0x05)
                {
                    return(true);
                }
                return(false);
            }
            catch (Exception ex)
            { LogHelper.WriteLog("充电桩解析编解码错误!" + ex.Message); }
            return(false);
        }