public void Init(string host, int args = 0) { this.Host = host; this.Args = args; transportType = this.Host.StartsWith("COM") ? TransportTypeEnum.Rs232 : TransportTypeEnum.Tcp; }
void IMessageNotifier.RaiseMessageProcessed(TransportTypeEnum transportType, TransportMessage message) { if (MessageProcessed != null) { MessageProcessed(this, new MessageTransportArgs { TransportType = transportType, Message = message }); } }
public static ITransport GetTransport(TransportTypeEnum type, string host, int e, int timeout) { string key = "{0}://{1}:{2}".F(type.ToString(), host, e); ITransport trans = null; lock (TransportsCache) { if (TransportsCache.ContainsKey(key)) { trans = TransportsCache.GetItem(key, null); if (trans == null) { TransportsCache.Remove(key); } else { if (type == TransportTypeEnum.Rs232) { var sp = trans as SerialTransport; if (sp == null || sp.Disposed) { TransportsCache.Remove(key); trans = CreateRS232Transport(host, e, timeout); TransportsCache.Add(key, trans); } } else if (type == TransportTypeEnum.Tcp || type == TransportTypeEnum.Udp) { var session = trans as SessionBase; if (session == null || session.Disposed || !session.Active) { TransportsCache.Remove(key); trans = CreateNetTransport(type, host, e, timeout); TransportsCache.Add(key, trans); } } } } else { if (type == TransportTypeEnum.Rs232) { trans = CreateRS232Transport(host, e, timeout); TransportsCache.Add(key, trans); } else if (type == TransportTypeEnum.Tcp || type == TransportTypeEnum.Udp) { trans = CreateNetTransport(type, host, e, timeout); TransportsCache.Add(key, trans); } } } return(trans); }
/// <summary> /// 创建Transport /// </summary> ///// <param name="args"></param> ///// <param name="type"></param> ///// <returns></returns> //public static ITransport CreateTransport(dynamic args, TransportTypeEnum type = TransportTypeEnum.Tcp) //{ // string args1 = args.Args1 + ""; // int args2 = Convert.ToInt32(args.Args2 + ""); // //读超时时间 // int timeout = Convert.ToInt32(args.Args3 + ""); // //try // //{ // // object outArgs3; // // if (args.TryGetValue("Args3", out outArgs3)) // // { // // timeout = Convert.ToInt32(outArgs3 + ""); // // } // //} // //catch (Exception) // //{ // //} // var key = string.Format("{0}://{1}:{2}", type.ToString(), args1, args2); // var transport = TransportsCache.GetItem(key, type, args1, args2, timeout, // (k, t, a1, a2, time) => // { // if (t == TransportTypeEnum.Rs232) // { // var sp = new SerialTransport { PortName = a1, BaudRate = a2 }; // try // { // sp.Open(); // sp.Serial.ReadTimeout = time; // sp.Disconnected += (s, e) => // { // if (TransportsCache.ContainsKey(key)) // { // lock (TransportsCache) // { // if (TransportsCache.ContainsKey(key)) // { // TransportsCache.Remove(key); // } // } // } // }; // } // catch (Exception ex) // { // XTrace.WriteException(ex); // } // return sp; // } // else if (t == TransportTypeEnum.Tcp || t == TransportTypeEnum.Udp) // { // var session = NetService.CreateSession( // new NetUri(string.Format("{0}://{1}:{2}", t.ToString(), NetHelper.ParseAddress(a1), a2)), // timeout); // session.Log = XTrace.Log; // if (session.GetType() == typeof(TcpSession)) // { // var tcpSession = session as TcpSession; // tcpSession.Open(); // tcpSession.Client.ReceiveTimeout = time; // } // session.Error += Session_Error; // return session as ITransport; // } // return null; // }); // if (transport == null) // { // TransportsCache.Remove(key); // } // // todo transport 释放空间时如何处理? // return transport; //} public static ITransport CreateTransport(TransportTypeEnum type, string host, int e, int timeout) { if (type == TransportTypeEnum.Rs232) { return(CreateRS232Transport(host, e, timeout)); } else if (type == TransportTypeEnum.Tcp || type == TransportTypeEnum.Udp) { return(CreateNetTransport(type, host, e, timeout)); } return(null); }
public void Init(string host, int args = 0) { this.Host = host; this.Args = args; if (this.Host.StartsWith("COM")) { transportType = TransportTypeEnum.Rs232; } else { transportType = TransportTypeEnum.Tcp; } }
private static ITransport CreateNetTransport(TransportTypeEnum type, string host, int e, int timeout) { IPAddress ip; lock (_hostCache) { if (_hostCache.ContainsKey(host)) { ip = _hostCache[host]; } else { try { ip = NetHelper.ParseAddress(host); _hostCache.TryAdd(host, ip); } catch (Exception ex) { XTrace.WriteException(ex); return(null); } } } var session = new NetUri(string.Format("{0}://{1}:{2}", type.ToString(), ip, e)).CreateRemote(); session.Log = XTrace.Log; session.Error += Session_Error; //if (session.GetType() == typeof(TcpSession)) //{ // var se = session as SessionBase; // se.Timeout = timeout; //} return(session as ITransport); }
private void Display() { CommunicateDevice.FindAllWithCache().ForEach(com => { ///判断一下是否为TTS设备。。。 TransportTypeEnum type; if (!TransportTypeEnum.TryParse(com.CommunicateDeviceTypeName, true, out type)) { type = TransportTypeEnum.Unknow; } var host = com.Args1; var e = Convert.ToInt32(com.Args2); var timeout = Convert.ToInt32(com.Args3); //获取Transport var transport = TransportFactory.GetTransport(type, host, e, timeout); var kdxf = new KdxfDevice(transport); com.ShowDevices.ForEach(showDevice => { var devices = showDevice.ShowDatas.ToList().Select(sd => sd.SensorDeviceUnit); devices.ForEach(dev => { var message = "{0} {1} {2} {3}".F("", dev.Name, dev.ShowValue, dev.Sensor.Unit); var result = kdxf.Display(message); if (result) { ServiceLogger.Current.WriteDebugLog("播报成功"); } else { ServiceLogger.Current.WriteError("语音播报失败:{0}", message); } }); }); }); }
void IMessageNotifier.RaiseMessageProcessed(TransportTypeEnum transportType, TransportMessage message) { if (MessageProcessed != null) MessageProcessed(this, new MessageTransportArgs { TransportType = transportType, Message = message }); }