예제 #1
0
 /// <summary>
 /// 数据接收
 /// </summary>
 /// <param name="buffer"></param>
 public void OnDataReceived(DtuMsg buffer)
 {
     if (this._handler != null)
     {
         this._handler.OnDataReceived(this, buffer);
     }
 }
예제 #2
0
        /// <summary>
        /// 接收数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void server_ReceiveData(object sender, ZYBEventArgs e)
        {
            string dtuid = e.DTU.ID;
            var    c     = _connectPool[dtuid];

            c.RefreshTime = e.DTU.RefreshTime;
            DtuMsg m = EventToMsg(e);

            m.IsWorking = true;
            c.OnDataReceived(m); // passthrough
        }
예제 #3
0
        public void OnDataReceived(IDtuConnection c, DtuMsg msg)
        {
            if (msg == null)
            {
                Log.ErrorFormat("Received a null msg from {0}.", c);
                return;
            }

            if (!msg.IsOK())
            {
                Log.DebugFormat("Invalid Msg, dropped.");
                return;
            }
            TranMsg tm = new TranMsg(msg.Databuffer);
            // Send ack.
            TranMsg ack = new HeartBeatTranMsg(Convert.ToInt32(msg.DtuId), tm.LoadSize); // ACK

            ack.ID = tm.ID;                                                              // SAME.

            c.Asend(ack.Marshall());
            if (TranMsgType.HeartBeat == tm.Type)
            {
                // HeartBeat.
                Log.DebugFormat("heartBeat, send ack and continue");
                return;
            }
            Log.DebugFormat("OnDataReceived: id={4},result={0}, len={1}, pkg={2}/{3}", msg.ErrorCode,
                            msg.Databuffer != null ? msg.Databuffer.Length : 0, tm.PackageIndex, tm.PackageCount, tm.ID);
            if (!tm.IsLastPackage())
            {
                // 入列.
                this.EnqueueMsg(tm);
                return;
            }
            // 最后一包已到达, 或仅一包.
            TranMsg outMsg = null;

            if (tm.PackageCount > 1)
            {
                Log.DebugFormat("Last package, combine them.");
                outMsg = this.CombineMsg(tm.Type); //组包
            }
            else
            {
                outMsg = tm; //单包.
            }
            // 委托调用.
            if (this.OnTranMsgReceived != null)
            {
                this.OnTranMsgReceived(tm.Type, outMsg);
            }
        }
예제 #4
0
        public bool SSend(TranMsg req, int timeout, out TranAckMsg resp)
        {
            DtuMsg msg = this._dtuConnection.Ssend(req.Marshall(), timeout);

            resp = new TranAckMsg();
            if (msg.IsOK())
            {
                resp.Unmarshall(msg.Databuffer);
                return(true);
            }
            else
            {
                resp.ErrorMsg  = msg.ErrorMsg;
                resp.ErrorCode = msg.ErrorCode;
                return(false);
            }
        }
예제 #5
0
            public void OnDataReceived(object sender, SerialDataReceivedEventArgs e)
            {
                int byteToRead = this._comconn.ReceiveFrame(this.port, this.timeout);

                byte[] recByteBuffer = null;
                if (byteToRead > 0)
                {
                    recByteBuffer = new byte[byteToRead];
                    this.port.Read(recByteBuffer, 0, recByteBuffer.Length);
                    this.port.DiscardOutBuffer();
                    var name = ((SerialPort)sender).PortName;
                    this.Log.DebugFormat("[{0}] received: {1}", this.port.PortName, ValueHelper.BytesToHexStr(recByteBuffer));
                    this.receivedMsg = new DtuMsg
                    {
                        Databuffer  = recByteBuffer,
                        Refreshtime = System.DateTime.Now,
                        DtuId       = this.dtuId,
                        ErrorCode   = 0
                    };
                    this.port.DataReceived -= this.OnDataReceived;
                    this.dataReceived       = true;
                }
            }
예제 #6
0
        public ATCommandResult Execute(GprsDtuConnection conn, ATCommand cmd, ushort timeout = 2)
        {
            var r = new ATCommandResult();

            if (!conn.IsOnline)
            {
                r.IsOK = false;
                r.GetJsonResult(cmd.ToATString());
                return(r);
            }
            try
            {
                logger.InfoFormat("======> AtCommand:{0}-{1} ,timeout={2}", conn.DtuID, cmd.ToATString(), timeout);
                DtuMsg result = conn.Ssend(Encoding.ASCII.GetBytes(cmd.ToATString()), timeout);

                r.ResultBuffer = result.Databuffer;
                string bufstr = string.Empty;
                if (r.ResultBuffer != null)
                {
                    bufstr = Encoding.ASCII.GetString(result.Databuffer);
                }
                r.IsOK = ((bufstr.Contains("OK")) || ("Remote Config Ready" == bufstr)) ||
                         cmd.ToATString().Contains("RESET");
                // r.GetJsonResult(cmd.ToATString());
                r.Elapsed = result.Elapsed;
                // System.Console.WriteLine("> {0}\r\n< {1} : {2}", cmd.ToATString(), bufstr, r.IsOK? "successed":"failed");
                logger.InfoFormat("======> AtCommand:{0}-{1} ,result: {2}, {3} ", conn.DtuID, cmd.ToATString(), bufstr,
                                  r.IsOK ? "successed" : "failed");
            }
            catch (Exception ex)
            {
                logger.ErrorFormat("CommandExecutor Error: {0}", ex);
            }
            r.GetJsonResult(cmd.ToATString());
            return(r);
        }
예제 #7
0
        private void OnDataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            DtuMsg msg        = null;
            int    byteToRead = this.ReceiveFrame(this._port, 3); //3秒超时.

            byte[] recByteBuffer = null;
            if (byteToRead > 0)
            {
                recByteBuffer = new byte[byteToRead];
                this._port.Read(recByteBuffer, 0, recByteBuffer.Length);
                // _port.DiscardOutBuffer();
                msg = new DtuMsg
                {
                    Databuffer  = recByteBuffer,
                    Refreshtime = System.DateTime.Now,
                    DtuId       = this.DtuID,
                    ErrorCode   = 0
                };
            }
            if (this._handler != null)
            {
                this._handler.OnDataReceived(this, msg);
            }
        }
예제 #8
0
        private SensorAcqResult RequestGprsSensor(Sensor si, IDtuConnection conn, int timeout)
        {
            var r = new SensorAcqResult
            {
                DtuCode   = conn.DtuID,
                Sensor    = si,
                Request   = null,
                Response  = null,
                Data      = null,
                ErrorCode = (int)Errors.ERR_DEFAULT
            };

            this.SendSensorCollectMsg(CollectState.Request, r);
            var senadapter = this._adapterManager.GetAdapter(si.ProtocolType);

            //var senadapter = _adapterManager.GetSensorAdapter(si.ProtocolType);
            if (senadapter == null)
            {
                return(this.CreateAcqResult(conn.DtuID, si, (int)Errors.ERR_UNKNOW_PROTOCOL, "Sensor has no ProtocolCode"));
            }

            try
            {
                //senadapter.Request(ref r);
                var      mp = new object[] { r };
                object[] cp = null;
                CrossDomainCompiler.Call(senadapter.ScriptPath, typeof(ISensorAdapter), senadapter.ClassName, "Request", ref cp, ref mp);
                r = mp[0] as SensorAcqResult;
            }
            catch (Exception ex)
            {
                log.ErrorFormat("dtu{0} sensor:{1} create cmd error {2}", conn.DtuID, si.SensorID, ex.Message);
                return(this.CreateAcqResult(conn.DtuID, si, (int)Errors.ERR_COMPILED, "internal error: SensorAdapter ERROR"));
            }
            if (r == null)
            {
                return(this.CreateAcqResult(conn.DtuID, si, (int)Errors.ERR_CREATE_CMD, "create cmd error: SensorAdapter ERROR"));
            }
            r.RequestTime = DateTime.Now;
            if (r.ErrorCode != (int)Errors.SUCCESS)
            {
                return(CreateAcqResult(conn.DtuID, si, r.ErrorCode, "Sensor has no SensorAdapter"));
            }
            // send
            if (r.ErrorCode == (int)Errors.SUCCESS && r.Request != null)
            {
                DtuMsg msg = conn.Ssend(r.Request, timeout);
                if (msg == null)
                {
                    return(this.CreateAcqResult(conn.DtuID, si, (int)Errors.ERR_NULL_RECEIVED_DATA, "Receive buff is null !"));
                }
                r.ResponseTime = msg.Refreshtime; // 若结果错误, 该时间无意义。
                // Parse
                if (msg.IsOK())
                {
                    try
                    {
                        r.Response = msg.Databuffer;
                        if (r.Response != null)
                        {
                            //senadapter.ParseResult(ref r);
                            var      mp = new object[] { r };
                            object[] cp = null;
                            CrossDomainCompiler.Call(senadapter.ScriptPath, typeof(ISensorAdapter), senadapter.ClassName, "ParseResult", ref cp, ref mp);
                            r = mp[0] as SensorAcqResult;
                        }
                        else
                        {
                            log.ErrorFormat("sensor:{0}, error Received buff is null", r.Sensor.SensorID);
                        }
                    }
                    catch (Exception ex)
                    {
                        log.ErrorFormat("dtu:{0},s[sid:{2}-m:{3}-c:{4}] , ERR_COMPILED {1}", conn.DtuID, ex.Message, si.SensorID, si.ModuleNo, si.ChannelNo);
                        return(this.CreateAcqResult(conn.DtuID, si, (int)Errors.ERR_COMPILED, "internal error: COMPILED ERROR"));
                    }
                    if (r == null)
                    {
                        return(this.CreateAcqResult(conn.DtuID, si, (int)Errors.ERR_DATA_PARSEFAILED, "internal error: SensorAdapter ERROR"));
                    }

                    r.ErrorMsg = r.ErrorCode == (int)Errors.SUCCESS ? "OK!" : ValueHelper.CreateJsonResultStr(si.SensorID, EnumHelper.GetDescription((Errors)r.ErrorCode));
                }
                else
                {
                    r.ErrorCode = msg.ErrorCode;
                    r.ErrorMsg  = msg.ErrorMsg;
                }
                r.Elapsed = msg.Elapsed;
            }
            else
            {
                if (r.ErrorCode == (int)Errors.ERR_DEFAULT)
                {
                    r.ErrorCode = (int)Errors.ERR_UNKNOW;
                }
                r.ErrorMsg = "create cmd error";
            }
            if (r.ErrorCode != (int)Errors.SUCCESS)
            {
                r.Data = new SensorErrorData(r.Sensor.SensorID, r.ErrorCode);
            }
            this.SendSensorCollectMsg(CollectState.Response, r);
            return(r);
        }
예제 #9
0
 public void OnDataReceived(IDtuConnection c, DtuMsg msg)
 {
     this._log.DebugFormat("Data received: from {0} buff: {1}", c.DtuID, msg != null && msg.Databuffer != null ? ValueHelper.BytesToHexStr(msg.Databuffer) : " empty.");
     this._dataReceived = true;
     this._receivedMsg  = msg;
 }