public int ArrangeInstantTask(string tid, uint dtuId, JObject[] cmdobjs, ATTaskResultConsumer consumer)
        {
            ATTask task = new ATTask(tid, dtuId, cmdobjs, TaskType.INSTANT, consumer);

            ATTaskPersistent.Init();
            ATTaskPersistent.SaveTaskImmedate(task);
            task.Consumer += OnAtInstantTaskFinished;
            DacTaskContext context = null;

            if (_workers.ContainsKey(dtuId))
            {
                if (!IsWorkerAvaliable(_workers[dtuId]))
                {
                    _workers[dtuId].Stop();
                }
                context = _workers[dtuId].GetContext();
            }
            else
            {
                context = CreateContext(dtuId);
            }
            var atworker = new AtCommandWorker(task, context);

            atworker.StartWork();
            return(task.ID);
        }
 public AtCommandWorker(ATTask task, DacTaskContext context)
 {
     this.task     = task;
     this._source  = new CancellationTokenSource();
     this._token   = this._source.Token;
     this._context = context;
 }
Exemple #3
0
        public DACTaskResult Run(DACTask task, DacTaskContext context)
        {
            DACTaskResult rslt;
            DtuNode       dtu = context.Node;

            switch (dtu.NetworkType)
            {
            case NetworkType.gprs:
                rslt = this.ExcuteGprsDacTask(task, context);
                break;

            case NetworkType.hclocal:
                rslt = this.ExcuteLocalDacTask(task, context);
                break;

            default:
                log.ErrorFormat("Dtu:{0}-Network Type:{1} : Error, Executor Is Not Implemented", dtu.DtuCode, dtu.NetworkType);
                rslt             = new DACTaskResult();
                rslt.Task        = task;
                rslt.Task.Status = DACTaskStatus.DONE;
                rslt.ErrorCode   = (int)Errors.ERR_INVALID_DTU;
                rslt.ErrorMsg    = string.Format("Dtu:{0},Network Type Error", dtu.DtuCode);
                rslt.Finished    = System.DateTime.Now;
                break;
            }

            return(rslt);
        }
Exemple #4
0
        private DACTaskResult ExcuteGprsDacTask(DACTask task, DacTaskContext context)
        {
            DtuNode dtu = context.Node;
            var     r   = new DACTaskResult {
                Task = task
            };

            r.Task.Status = DACTaskStatus.RUNNING;
            r.ErrorMsg    = "OK";
            r.DtuCode     = dtu.DtuCode;
            // 循环发送数据
            long totalElapsed = 0;

            context.DtuConnection.Connect(); // Connect to Resource.
            r.Started = DateTime.Now;
            foreach (Sensor si in dtu.Sensors)
            {
                SensorAcqResult resp = null;
                if (!this.IsSensorRequired(context, task.Sensors, si))
                {
                    continue;
                }

                try
                {
                    resp = this.RequestGprsSensor(si, context.DtuConnection, (int)dtu.DacTimeout);
                }
                catch (Exception ex)
                {
                    log.ErrorFormat("[DACTaskExecutor] : unknown exceptions {0}", ex.Message);
                    if (resp == null)
                    {
                        resp = this.CreateAcqResult(context.DtuConnection.DtuID, si, (int)Errors.ERR_UNKNOW, "unknown exceptions");
                    }
                }
                if (resp.Data == null)
                {
                    resp.Data = new SensorErrorData(si.SensorID, resp.ErrorCode);
                }
                r.AddSensorResult(resp);
                totalElapsed += resp.Elapsed;
                if (!r.IsOK)
                {
                    r.ErrorCode = resp.ErrorCode;
                    r.ErrorMsg  = resp.ErrorMsg;
                    break; // ERROR.
                }

                Thread.Sleep(DELAY_BEFORE_NEW_COMMAND); //延迟.
            }
            r.Finished    = DateTime.Now;
            r.Elapsed     = totalElapsed;
            r.Task.Status = DACTaskStatus.DONE;
            context.DtuConnection.Disconnect();

            return(r);
        }
        private DacTaskContext CreateContext(uint dtuId)
        {
            DacTaskContext _ctx = new DacTaskContext {
                Node = _dtus[dtuId], SensorMatcher = SensorMatcher
            };

            if (_ctx.Node.NetworkType == NetworkType.hclocal)
            {
                _ctx.DtuConnection = _fileServer.GetConnection(_ctx.Node);
            }
            else
            {
                _ctx.DtuConnection = _dtuServer.GetConnection(_ctx.Node);
            }
            return(_ctx);
        }
Exemple #6
0
 public bool IsSensorRequired(DacTaskContext ctx, IList <uint> list, Sensor s)
 {
     if (ctx.SensorMatcher != null)
     {
         if (!ctx.SensorMatcher(s))
         {
             return(false);
         }
     }
     if (list == null || list.Count <= 0)
     {
         return(!s.UnEnable && s.SensorType != SensorType.Virtual);
     }
     //是否使能 false-是 true-否 //&&(!s.UnEnable)
     return(list.Contains(s.SensorID) && s.SensorType != SensorType.Virtual);
 }
        private static string GetWorkerInfo(DACWorker worker)
        {
            string info = null;

            if (worker == null)
            {
                info = "null";
            }
            else
            {
                DacTaskContext ctx = worker.GetContext();
                if (ctx != null)
                {
                    IDtuConnection c = ctx.DtuConnection;
                    DtuNode        d = ctx.Node;
                    info = string.Format("Node={0}, conn={1}", d.DtuCode, c != null ? (c.IsOnline ? "online" : "offline") : "null");
                }
                else
                {
                    info = "Context null";
                }
            }
            return(info);
        }
Exemple #8
0
 public void AssignContext(DacTaskContext context)
 {
     this._context = context;
 }
        // Node 状态变更。
        public void OnConnectionStatusChanged(IDtuConnection c, WorkingStatus oldStat, WorkingStatus newStat)
        {
            string dtuCode = c.DtuID;

            if (newStat == WorkingStatus.IDLE)
            {
                // Online;
                if (OnDTUConnectionStatusChanged != null)
                {
                    OnDTUConnectionStatusChanged.Invoke(new DTUConnectionStatusChangedMsg
                    {
                        DTUID             = dtuCode,
                        IsOnline          = true,
                        TimeStatusChanged = DateTime.Now //c.LoginTime
                    });
                }
                if (c is GprsDtuConnection)
                {
                    var cg = c as GprsDtuConnection;
                    Log.InfoFormat("Gprs Node Online: {0}, ip={1}, phone={2}.", cg.DtuID, cg.IP, cg.PhoneNumber);
                }
                else if (c is FileDtuConnection)
                {
                    var cf = c as FileDtuConnection;
                    Log.InfoFormat("File Node Online: {0}, path={1}.", cf.DtuID, cf.FilePath);
                }
                else
                {
                    Log.WarnFormat("Node Unkown Type Online");
                }
                CheckDtuInfo(c);
                DACWorker w = FindWorker(dtuCode);

                if (w != null)
                {
                    DacTaskContext ctx = w.GetContext();
                    ctx.UpdateConnection(c);
                    w.AssignContext(ctx);
                }
            }
            else if (newStat == WorkingStatus.NA)
            {
                if (c is GprsDtuConnection)
                {
                    var cg = c as GprsDtuConnection;
                    Log.InfoFormat("Gprs Node Offline: {0}, ip={1}, phone={2}.", cg.DtuID, cg.IP, cg.PhoneNumber);
                }
                else if (c is FileDtuConnection)
                {
                    var cf = c as FileDtuConnection;
                    Log.InfoFormat("File Node Offline: {0}, path={1}.", cf.DtuID, cf.FilePath);
                }
                else
                {
                    Log.WarnFormat("Node Unkown Type Offline");
                }
                // offline;
                if (OnDTUConnectionStatusChanged != null)
                {
                    OnDTUConnectionStatusChanged.Invoke(new DTUConnectionStatusChangedMsg
                    {
                        DTUID             = dtuCode,
                        IsOnline          = false,
                        TimeStatusChanged = DateTime.Now
                    });
                }
            }
        }
Exemple #10
0
        private DACTaskResult ExcuteLocalDacTask(DACTask task, DacTaskContext context)
        {
            DtuNode       dtu  = context.Node;
            DACTaskResult rslt = new DACTaskResult();

            rslt.Task             = task;
            rslt.Task.Status      = DACTaskStatus.RUNNING;
            rslt.ErrorMsg         = "OK";
            rslt.StoragedTimeType = SensorAcqResultTimeType.SensorResponseTime;

            Stopwatch sw = new Stopwatch();

            sw.Start();
            var dtuConn = context.DtuConnection as FileDtuConnection; // 强转成FileDtuConnection

            if (dtuConn != null && dtuConn.Connect())
            {
                try
                {
                    foreach (Sensor si in dtu.Sensors)
                    {
                        if (!this.IsSensorRequired(context, task.Sensors, si))
                        {
                            continue;
                        }

                        var result = this.RequestFileSensor(si, dtuConn);// 请求文件传感器
                        foreach (SensorAcqResult r in result)
                        {
                            rslt.AddSensorResult(r);
                        }
                    }
                    CheckFileAndBackup(dtuConn.FilePath);
                }
                catch (System.Exception e)
                {
                    rslt.ErrorCode = (int)Errors.ERR_NOT_CONNECTED;
                    rslt.ErrorMsg  = string.Format(
                        "DTU:{0},FilePath:{1},Failed To Read the File,Msg:{2}",
                        dtu.DtuCode,
                        dtuConn.FilePath,
                        e.Message);
                    log.ErrorFormat(
                        "dtu:{0} network={1},file={2},Failed To Read the File,Msg:{3}",
                        dtu.DtuCode,
                        dtu.NetworkType,
                        dtuConn.FilePath,
                        e.Message);
                }
            }
            else
            {
                rslt.ErrorCode = (int)Errors.ERR_NOT_CONNECTED;
                rslt.ErrorMsg  = string.Format(
                    "DTU:{0},FilePath:{1},File Not Exists",
                    dtu.DtuCode,
                    dtuConn != null ? dtuConn.FilePath : null);
                log.ErrorFormat("dtu:{0} network={1},file={2},File Not Exists", dtu.DtuCode, dtu.NetworkType, dtuConn != null ? dtuConn.FilePath : null);
            }
            sw.Stop();
            rslt.Finished    = System.DateTime.Now;
            rslt.Elapsed     = sw.ElapsedMilliseconds;
            rslt.Task.Status = DACTaskStatus.DONE;
            context.DtuConnection.Disconnect();

            return(rslt);
        }