Ejemplo n.º 1
0
        void Timer1_Tick(object sender, EventArgs e)
        {
            foreach (Item item in Items)
            {
                string itemName = item.ItemName;
                int    itemId   = item.ItemId;

                TunnelLog currentLog = null;
                currentLog = Tunnel.GetValue(x => x.VarName == itemName);

                if (currentLog != null)
                {
                    TunnelLog lastLog = LastTunnelLogs.FirstOrDefault(x => x.VarName == itemName);

                    if (lastLog == null)
                    {
                        Log log = new Log();
                        log.ItemId = itemId;
                        if (itemId == 2)
                        {
                            log.ItemValue = currentLog.VarValue / 10;
                        }
                        else
                        {
                            log.ItemValue = currentLog.VarValue;
                        }

                        log.Date = DateTime.Now;

                        Entities.Logs.Add(log);
                        Entities.SaveChanges();

                        var beforeHash = new StringBuilder();
                        beforeHash.Append(log.LogId);
                        beforeHash.Append(log.ItemId);
                        beforeHash.Append(log.ItemValue);
                        beforeHash.Append(log.Date);

                        var afterHash = ComputeHash(beforeHash.ToString());
                        log.HashValue = afterHash;

                        Entities.SaveChanges();

                        LastTunnelLogs.Add(currentLog);

                        if (item.ItemId == 1)
                        {
                            if (item1Live.Count > 61)
                            {
                                item1Live.RemoveAt(0);
                            }

                            item1Live.Add(log);
                        }
                        else if (item.ItemId == 2)
                        {
                            if (item2Live.Count > 61)
                            {
                                item2Live.RemoveAt(0);
                            }

                            item2Live.Add(log);
                        }
                        else if (item.ItemId == 3)
                        {
                            if (item3Live.Count > 61)
                            {
                                item3Live.RemoveAt(0);
                            }

                            item3Live.Add(log);
                        }
                    }
                    else
                    {
                        if (lastLog.Time_ms != currentLog.Time_ms)
                        {
                            LastTunnelLogs.Remove(lastLog);

                            Log log = new Log();
                            log.ItemId = itemId;
                            if (itemId == 2)
                            {
                                log.ItemValue = currentLog.VarValue / 10;
                            }
                            else
                            {
                                log.ItemValue = currentLog.VarValue;
                            }
                            //log.ItemValue = currentLog.VarValue;
                            log.Date = DateTime.Now;

                            Entities.Logs.Add(log);
                            Entities.SaveChanges();

                            var beforeHash = new StringBuilder();
                            beforeHash.Append(log.LogId);
                            beforeHash.Append(log.ItemId);
                            beforeHash.Append(log.ItemValue);
                            beforeHash.Append(log.Date);

                            var afterHash = ComputeHash(beforeHash.ToString());
                            log.HashValue = afterHash;

                            Entities.SaveChanges();

                            LastTunnelLogs.Add(currentLog);

                            if (item.ItemId == 1)
                            {
                                if (item1Live.Count > 61)
                                {
                                    item1Live.RemoveAt(0);
                                }

                                item1Live.Add(log);
                            }
                            else if (item.ItemId == 2)
                            {
                                if (item2Live.Count > 61)
                                {
                                    item2Live.RemoveAt(0);
                                }

                                item2Live.Add(log);
                            }
                            else if (item.ItemId == 3)
                            {
                                if (item3Live.Count > 61)
                                {
                                    item3Live.RemoveAt(0);
                                }

                                item3Live.Add(log);
                            }
                        }
                    }
                }
            }
        }