Пример #1
0
 // worker idle && connection isAvaliable
 private static bool IsWorkerAvaliable(DACWorker worker)
 {
     Log.DebugFormat("{0}", worker == null ? "worker is null" : worker.ToString());
     return(worker != null &&
            worker.GetContext() != null &&
            worker.IsIdle() &&
            worker.GetContext().IsAvaliable());
 }
Пример #2
0
            public void Execute(IJobExecutionContext context)
            {
                JobInfo ji = GetJobInfo(context.JobDetail);

                if (ji.Type == TaskType.INSTANT)
                {
                    Console.WriteLine("remove job {1} {0}", DateTime.Now, ji.Dtu.DtuId);
                    TryRemoveJob(ji.Dtu.DtuId);
                }
                log.InfoFormat("Execute Job {0} , type={1}", context.JobDetail.Key, ji.Type);
                try
                {
                    DACWorker worker = ji.Finder.FindWorker(ji.Dtu.DtuId);
                    DACTask   task   = new DACTask(ji.TID, ji.Dtu.DtuId, ji.Sensors, ji.Type, ji.Consumer, ji.ID);
                    if (ji.Type != TaskType.INSTANT)
                    {
                        task.TID = GetTaskGuid(ji.Dtu.DtuCode);
                    }
                    if (!IsWorkerAvaliable(worker))
                    {
                        log.InfoFormat("Worker not avaliable: job=[{0}], worker=[{1}]", ji, GetWorkerInfo(worker));
                        if (ji.Type == TaskType.INSTANT)
                        {
                            int errorcode;
                            if (worker != null && (worker.GetContext() == null || (worker.GetContext().DtuConnection == null) || (worker.GetContext().DtuConnection != null && !worker.GetContext().DtuConnection.IsOnline)))
                            {
                                errorcode = (int)Errors.ERR_NOT_CONNECTED;
                            }
                            else if (worker != null && !worker.IsIdle())
                            {
                                errorcode = (int)Errors.ERR_DTU_BUSY;
                            }
                            else
                            {
                                errorcode = (int)Errors.ERR_UNKNOW;
                            }
                            OnInstantTaskFinished(new FailedDACTaskResult(errorcode, task)
                            {
                                Finished = DateTime.Now
                            });
                        }
                        return;
                    }
                    log.InfoFormat("Worker start working. job={0}, worker=[{1}]", ji, GetWorkerInfo(worker));

                    if (task.Type == TaskType.INSTANT)
                    {
                        task.Consumer += OnInstantTaskFinished;
                    }
                    worker.NewTask(task);
                }
                catch (Exception e)
                {
                    log.Error("DTUDacJob Execute ERR", e);
                }
            }
Пример #3
0
 public DACWorker FindWorker(uint dtuId)
 {
     if (_workers.ContainsKey(dtuId) && _workers[dtuId] != null)
     {
         return(_workers[dtuId]);
     }
     else
     {
         DACWorker w = new DACWorker(_adapterManager, this.OnSensorCollectMsgHandler);
         w.AssignContext(this.CreateContext(dtuId));
         _workers[dtuId] = w;
         w.StartWork();
         return(w);
     }
 }
Пример #4
0
        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);
        }
Пример #5
0
        // 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");
                }
                this.CheckDtuInfo(c);
                DACWorker w = this.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
                    });
                }
            }
        }