Пример #1
0
        public DataPacket GetShelterPacket(string deviceKey, Dictionary <string, object> data, bool realTime = false)
        {
            if (data.Count == 0)
            {
                return(null);
            }
            DataPacket dp = new DataPacket(deviceKey, realTime);

            dp.Cn       = string.Format("{0}", (int)SentCommand.DoorState);
            dp.Settings = Settings.Instance;
            dp.St       = Value.SysSend;
            string sno     = Settings.Instance.Sno;
            string eno     = Settings.Instance.GetEquipNumber(deviceKey);
            string timeStr = string.Empty;

            if (data.ContainsKey("time"))
            {
                timeStr = (string)data["time"];
            }
            string dataTime = DeviceTime.Convert(DateTime.Parse(timeStr));

            dp.SetContent(sno, eno, dataTime, data);
            dp.Build();
            return(dp);
        }
Пример #2
0
        private void OnSetTime(string msg)
        {
            string qn = Value.Parse(msg, "QN");

            this.SendReplyPacket(qn);

            string   time = Value.Parse(msg, "SystemTime");
            DateTime dt   = DeviceTime.Parse(time);

            this.agent.OnTimeChanged(dt);

            if (this.CanHandleSetTime)
            {
                SystemTime st = new SystemTime();

                st.wYear         = (ushort)dt.Year;
                st.wMonth        = (ushort)dt.Month;
                st.wDay          = (ushort)dt.Day;
                st.whour         = (ushort)dt.Hour;
                st.wMinute       = (ushort)dt.Minute;
                st.wSecond       = (ushort)dt.Second;
                st.wMilliseconds = 0;

                SetLocalTime(st);
            }
            this.SendResultPacket(qn);
        }
Пример #3
0
        public DataPacket GetFlowDataPacket(string deviceKey, Dictionary <string, object> data, bool realTime = false)
        {
            if (data.Count == 0)
            {
                return(null);
            }
            DataPacket dp = new DataPacket(deviceKey, realTime, true);

            // Adjust 'CN' !!
            if (realTime)
            {
                dp.Cn = string.Format("{0}", (int)SentCommand.FlowData);
            }
            else
            {
                dp.Cn = string.Format("{0}", (int)ReceivedCommand.GetFlowData);
            }

            dp.Settings = Settings.Instance;
            dp.St       = Value.SysSend;
            string sno      = Settings.Instance.Sno;
            string eno      = Settings.Instance.GetEquipNumber(deviceKey);
            string timeStr  = (string)data["time"];
            string dataTime = DeviceTime.Convert(DateTime.Parse(timeStr));

            dp.SetContent(sno, eno, dataTime, data);
            dp.Build();
            return(dp);
        }
Пример #4
0
        internal DataPacket GetTimePacket(string qn)
        {
            DataPacket dp = new DataPacket(SentCommand.GetTime);

            dp.Settings = Settings.Instance;
            dp.QN       = qn;
            dp.St       = Value.SysSend;
            dp.BuildGetTime(DeviceTime.Convert(DateTime.Now));
            return(dp);
        }
Пример #5
0
        public DataPacket GetRunStatusPacket(string qn, string[] devices, string[] status)
        {
            DataPacket dp = new DataPacket(SentCommand.RunStatus);

            dp.QN       = qn;
            dp.Settings = Settings.Instance;
            dp.St       = Value.SysSend;
            string sno      = Settings.Instance.Sno;
            string dateTime = DeviceTime.Convert(DateTime.Now);

            dp.SetRunStatusContent(sno, dateTime, devices, status);
            dp.Build();
            return(dp);
        }
Пример #6
0
        internal DataPacket GetDoorStatePacket(string state)
        {
            DataPacket dp = new DataPacket(SentCommand.DoorState);

            dp.Settings = Settings.Instance;

            string sno      = Settings.Instance.Sno;
            string dateTime = DeviceTime.Convert(DateTime.Now);

            dp.St = Value.SysSend;
            dp.SetDoorStateContent(sno, dateTime, state);
            dp.Build();
            return(dp);
        }
Пример #7
0
        internal DataPacket GetThresholdPacket(string polId, string eno, string v1, string v2)
        {
            DataPacket dp = new DataPacket(SentCommand.GetAlertThreshold);

            dp.Settings = Settings.Instance;

            string sno      = Settings.Instance.Sno;
            string dateTime = DeviceTime.Convert(DateTime.Now);

            dp.St = Value.SysSend;
            dp.SetThresholdContent(sno, eno, polId, v1, v2);
            dp.Build();
            return(dp);
        }
Пример #8
0
        internal DataPacket GetExceptionNotifyPacket(string deviceKey, bool p)
        {
            DataPacket dp = new DataPacket(p ? SentCommand.WentException : SentCommand.AfterException);

            dp.Settings = Settings.Instance;

            string sno      = Settings.Instance.Sno;
            string dateTime = DeviceTime.Convert(DateTime.Now);

            dp.St   = Value.SysSend;
            dp.Flag = "1";
            dp.SetExceptionNotifyContent(sno, dateTime, deviceKey, (p ? "0" : "1"));
            dp.Build();
            return(dp);
        }
Пример #9
0
        private void UploadHistoryData(string qn, string eno, string beginTime, string endTime, string sid, string polId)
        {
            DateTime f = DeviceTime.Parse(beginTime);
            DateTime t = DeviceTime.Parse(endTime);

            if (f >= t)
            {
                return;
            }
            string deviceKey      = Settings.Instance.GetDeviceKeyByEno(eno);
            string deviceKeyLower = deviceKey.ToLower();

            if (deviceKey.Equals("Scada.NaIDevice", StringComparison.OrdinalIgnoreCase))
            {
                // NaIDevice ... Gose here.
                // 分包
                DateTime dt = f;
                while (dt <= t)
                {
                    string content = DBDataSource.Instance.GetNaIDeviceData(dt);
                    if (!string.IsNullOrEmpty(content))
                    {
                        List <DataPacket> pks = null;
                        try
                        {
                            pks = builder.GetDataPackets(deviceKey, dt, content, true);
                        }
                        catch (Exception e)
                        {
                            this.agent.OnHandleHistoryData(deviceKey, e.Message, true);
                        }

                        if (pks != null)
                        {
                            foreach (var p in pks)
                            {
                                this.agent.SendHistoryDataPacket(p);
                            }
                        }
                    }


                    dt = dt.AddSeconds(60 * 5);
                }
            }
            else
            {
                List <Dictionary <string, object> > data = new List <Dictionary <string, object> >();
                ReadResult r            = ReadResult.NoDataFound;
                string     errorMessage = string.Empty;
                using (var conn = DBDataSource.Instance.CreateMySqlConnection())
                {
                    try
                    {
                        conn.Open();
                        var cmd = conn.CreateCommand();
                        r = DBDataSource.GetData(cmd, deviceKey, f, t, sid, polId, data, out errorMessage);
                    }
                    catch (Exception e)
                    {
                        string line = string.Format("HD Error: {0} [{1}: {2} ~ {3}]", e.Message, deviceKey, f, t);
                        this.agent.OnHandleHistoryData(deviceKey, line, true);
                        return;
                    }
                }

                if (r == ReadResult.ReadOK)
                {
                    if (data.Count == 0)
                    {
                        return;
                    }
                    foreach (var item in data)
                    {
                        DataPacket p = null;
                        // By different device.

                        if (deviceKey.Equals("Scada.mds", StringComparison.OrdinalIgnoreCase) ||
                            deviceKey.Equals("Scada.ais", StringComparison.OrdinalIgnoreCase))
                        {
                            p = builder.GetFlowDataPacket(deviceKey, item, false);
                        }
                        else if (deviceKey.Equals("Scada.Shelter", StringComparison.OrdinalIgnoreCase))
                        {
                            p = builder.GetShelterPacket(deviceKey, item);
                        }
                        else
                        {
                            p = builder.GetDataPacket(deviceKey, item);
                        }

                        this.agent.SendHistoryDataPacket(p);
                    }
                }
                else
                {
                    string line = string.Format("HD Error: {0} - {1} [{2}: {3} ~ {4}]", r.ToString(), errorMessage, deviceKey, f, t);
                    this.agent.OnHandleHistoryData(deviceKey, line, true);
                }
            }
        }
Пример #10
0
        private void PrepareUploadHistoryData()
        {
            while (true)
            {
                if (this.fQuit)
                {
                    break;
                }
                if (this.historyDataBundleQueue.Count > 0)
                {
                    string taskId = this.historyDataBundleQueue.Dequeue();
                    if (!this.taskDict.ContainsKey(taskId))
                    {
                        continue;
                    }
                    HistoryDataBundle hdb = this.taskDict[taskId];

                    if (!string.IsNullOrEmpty(hdb.QN))
                    {
                        string msg = string.Format("Uploading history data [{0} - {1}]", DeviceTime.Parse(hdb.BeginTime), DeviceTime.Parse(hdb.EndTime));
                        this.agent.OnHandleHistoryData("", msg, true);
                        if (string.IsNullOrEmpty(hdb.ENO))
                        {
                            string[] enos = new string[] { "001001", "002000", "003001", "004000", "005000", "010002", "999000" };
                            foreach (string e in enos)
                            {
                                // for this Command, polId should be Null (means All polId);
                                this.UploadHistoryData(hdb.QN, e, hdb.BeginTime, hdb.EndTime, hdb.Sid, null);
                            }
                        }
                        else
                        {
                            this.UploadHistoryData(hdb.QN, hdb.ENO, hdb.BeginTime, hdb.EndTime, hdb.Sid, hdb.PolId);
                        }
                    }

                    this.SendResultPacket(hdb.QN);
                    //
                    this.taskDict.Remove(taskId);
                }
                else
                {
                    Thread.Sleep(150);
                }
            }
        }
Пример #11
0
        internal List <DataPacket> GetDataPackets(string deviceKey, DateTime dateTime, string content, bool history = false, string qn = null)
        {
            List <DataPacket> rets = new List <DataPacket>();
            int       from         = 0;
            const int MaxContent   = 920;
            int       count        = content.Length / MaxContent + 2;
            int       index        = 1;

            string sno = Settings.Instance.Sno;
            string eno = Settings.Instance.GetEquipNumber(deviceKey);

            string dataTime = DeviceTime.Convert(dateTime);

            // Header packet
            DataPacket dp = null;

            if (!history)
            {
                dp = new DataPacket(SentCommand.Data);
            }
            else
            {
                dp = new DataPacket(SentCommand.HistoryData);
            }
            // Set settings
            dp.Settings    = Settings.Instance;
            dp.DeviceKey   = deviceKey;
            dp.Splitted    = true;
            dp.PacketCount = count;
            dp.PacketIndex = index;
            dp.St          = Value.SysSend;
            Dictionary <string, object> data  = new Dictionary <string, object>();
            List <Settings.DeviceCode>  codes = Settings.Instance.GetCodes(deviceKey);
            string contentCode = codes[0].Code;

            data.Add(contentCode, string.Empty);
            data.Add("time", dateTime.ToString());
            dp.SetContent(sno, eno, dataTime, data);
            dp.Build();
            rets.Add(dp);

            string pqn = dp.QN;

            while (true)
            {
                index += 1;
                dp     = null;

                if (!history)
                {
                    dp = new DataPacket(SentCommand.Data);
                }
                else
                {
                    dp = new DataPacket(SentCommand.HistoryData);
                }
                dp.Settings    = Settings.Instance;
                dp.Splitted    = true;
                dp.PacketCount = count;
                dp.PacketIndex = index;
                dp.St          = Value.SysSend;

                string c = content.Substring(from, Math.Min(MaxContent, content.Length - from));
                dp.QN = pqn;
                dp.SetContent(sno, eno, dataTime, c);
                dp.Build();

                rets.Add(dp);

                from += c.Length;
                if (from >= content.Length)
                {
                    break;
                }
            }
            return(rets);
        }