Exemple #1
0
        public void UpdateData(ushort[] hregs, int start, int count, bool remoted = false)
        {
            if (start == 0 && count == 0)
            {
                _hregs = new ushort[]
                {
                    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
                };
                _oldregs = new ushort[]
                {
                    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
                };
            }
            else if ((start + count) <= 61)
            {
                Array.Copy(_hregs, _oldregs, 61);
                Array.Copy(hregs, 0, _hregs, start, count);
            }
            else
            {
                return;
            }
            Ready         = false;
            HasAlarmLevel = false;
            HasNoGround   = null;
            HasHandMode   = false;
            WorkMode      = false;
            SmallValve    = false;
            BigValve      = false;
            StopCause     = 0;
            if (_hregs[0] != 0xFFFF && _hregs[1] != 0xFFFF && _hregs[2] != 0xFFFF && _hregs[5] != 0xFFFF)
            {
                var level     = _hregs[0];
                var calcLevel = level > 32767 ? level - 65536 : level;
                CurrentLevel = calcLevel;
                if (Data.UseSmartLevel)
                {
                    //_queue.Add(_lastLevel - calcLevel);
                    //if (_queue.Count > Data.SmartLevelQueueSize) _queue.RemoveAt(0);
                    //FreshedValue = Math.Abs(_queue.Average()) > Data.SmartLevelDifferent;
                    FreshedValue = _hregs[0x24] >= 5000;
                    if (FilledKind == null) // идет налив
                    {
                        ShowedLevel = FreshedValue ? calcLevel : 0;
                        _lastLevel  = calcLevel;
                    }
                    else
                    {
                        ShowedLevel = calcLevel;
                    }
                }
                else
                {
                    ShowedLevel = calcLevel;
                }
                HasNoGround = (_hregs[1] & 0x0400) > 0;
                HasHandMode = (_hregs[1] & 0x0800) > 0;
                StopCause   = _hregs[2];
                SetStopCount(_hregs[1] & 0xFF, remoted);
                if (!remoted)
                {
                    var riserName = string.Format("{0}_{1}_{2}_{3}_{4}",
                                                  Channel, Overpass, Way, Product, Riser);
                    var filledKind = (StopCause & 0x2000) > 0 ? "1" : "0";
                    Data.UpdateRiserProperty(riserName, "FilledKind", filledKind);
                    Data.UpdateRiserProperty(riserName, "FilledLevel", FilledLevel.ToString("0"));
                    Data.UpdateRiserProperty(riserName, "HasNoGround", (bool)HasNoGround ? "1" : "0");
                    Data.UpdateRiserProperty(riserName, "HasHandMode", HasHandMode ? "1" : "0");
                    Data.UpdateRiserProperty(riserName, "StopCause", StopCause.ToString("0"));
                    Data.UpdateRiserProperty(riserName, "FreshedValue", FreshedValue ? "1" : "0");
                    Data.UpdateRiserProperty(riserName, "ShowedLevel", ShowedLevel.ToString("0"));
                }
            }
            if (_hregs[1] != 0xFFFF && _hregs[3] != 0xFFFF && _hregs[4] != 0xFFFF)
            {
                Ready         = (_hregs[3] & 0x08) > 0;
                BigValve      = (_hregs[1] & 0x1000) > 0 || (_hregs[3] & 0x02) > 0;
                SmallValve    = (_hregs[1] & 0x2000) > 0 || (_hregs[3] & 0x04) > 0;
                WorkMode      = (_hregs[1] & 0x4000) > 0 || (_hregs[3] & 0x10) > 0;
                HasAlarmLevel = (_hregs[1] & 0x0100) > 0 ||
                                (_hregs[3] & 0xC000) > 0 || (_hregs[4] & 0x0001) > 0;
                if (!remoted)
                {
                    var riserName = string.Format("{0}_{1}_{2}_{3}_{4}",
                                                  Channel, Overpass, Way, Product, Riser);
                    Data.UpdateRiserProperty(riserName, "Ready", Ready ? "1" : "0");
                    Data.UpdateRiserProperty(riserName, "BigValve", BigValve ? "1" : "0");
                    Data.UpdateRiserProperty(riserName, "SmallValve", SmallValve ? "1" : "0");
                    Data.UpdateRiserProperty(riserName, "WorkMode", WorkMode ? "1" : "0");
                    Data.UpdateRiserProperty(riserName, "HasAlarmLevel", HasAlarmLevel ? "1" : "0");
                }
            }
            if (_hregs[5] != 0xFFFF)
            {
                var state = _hregs[5];
                switch (state)
                {
                case 0:
                    RiserMode = RiserState.None;
                    break;

                case 1:
                    RiserMode = RiserState.Waiting;
                    break;

                case 2:
                    RiserMode = RiserState.HandWaiting;
                    break;

                case 3:
                    RiserMode = RiserState.HandSmallFilling;
                    break;

                case 4:
                    RiserMode = RiserState.HandBigFilling;
                    break;

                case 5:
                    RiserMode = RiserState.SmallFilling;
                    break;

                case 6:
                    RiserMode = RiserState.BigFilling;
                    break;

                case 7:
                    RiserMode = RiserState.FillingByAuto;
                    break;

                case 8:
                    RiserMode = RiserState.FillingByOper;
                    break;

                case 9:
                    RiserMode = RiserState.Filled;
                    break;
                }
                if (!remoted)
                {
                    var riserName = string.Format("{0}_{1}_{2}_{3}_{4}",
                                                  Channel, Overpass, Way, Product, Riser);
                    Data.UpdateRiserProperty(riserName, "RiserMode", ((int)RiserMode).ToString("0"));
                }
            }
            if (_hregs[0x35] != 0xFFFF)
            {
                DeepLevel = _hregs[0x35];
            }
            if (_hregs[0x36] != 0xFFFF)
            {
                WorkLevel = _hregs[0x36];
            }

            // заполнение журнала переключений
            if (_oldregs.Length != _hregs.Length || remoted)
            {
                return;
            }
            for (var i = 0; i < _hregs.Length; i++)
            {
                if (i == 1 && (_oldregs[i] & 0xFF00) == (_hregs[i] & 0xFF00))
                {
                    continue;
                }
                if (_oldregs[i] == _hregs[i] && !((DateTime.Now - _lastFething).TotalSeconds > 60))
                {
                    continue;
                }
                var oldflags = Convert.ToString(_oldregs[i], 2);
                if (oldflags.Length < 16)
                {
                    oldflags = string.Concat(new string('0', 16 - oldflags.Length), oldflags);
                }
                oldflags = Reverse(oldflags);
                var newflags = Convert.ToString(_hregs[i], 2);
                if (newflags.Length < 16)
                {
                    newflags = string.Concat(new string('0', 16 - newflags.Length), newflags);
                }
                newflags = Reverse(newflags);
                var regname = Convert.ToString(i, 16);
                if (regname.Length < 2)
                {
                    regname = string.Concat(new string('0', 2 - regname.Length), regname);
                }
                regname = string.Concat("HR", regname);

                List <int> exceptbits;
                switch (i)
                {
                case 0x01:
                    for (var j = 8; j < 16; j++)
                    {
                        if (oldflags[j] == newflags[j])
                        {
                            continue;
                        }
                        Data.SendToSwitchLog(Name, Address, string.Concat(regname, ".", j),
                                             new string(oldflags[j], 1),
                                             new string(newflags[j], 1), GetFlagDescriptor(i, j));
                    }
                    break;

                case 0x02:
                    for (var j = 0; j < 16; j++)
                    {
                        if (oldflags[j] == newflags[j])
                        {
                            continue;
                        }
                        Data.SendToSwitchLog(Name, Address, string.Concat(regname, ".", j),
                                             new string(oldflags[j], 1),
                                             new string(newflags[j], 1), GetFlagDescriptor(i, j));
                    }
                    break;

                case 0x03:
                    exceptbits = new List <int> {
                        8, 9, 12, 13
                    };
                    for (var j = 0; j < 16; j++)
                    {
                        if (exceptbits.IndexOf(j) >= 0)
                        {
                            continue;
                        }
                        if (oldflags[j] == newflags[j])
                        {
                            continue;
                        }
                        Data.SendToSwitchLog(Name, Address, string.Concat(regname, ".", j),
                                             new string(oldflags[j], 1),
                                             new string(newflags[j], 1), GetFlagDescriptor(i, j));
                    }
                    break;

                case 0x04:
                    exceptbits = new List <int> {
                        10, 11
                    };
                    for (var j = 0; j < 16; j++)
                    {
                        if (exceptbits.IndexOf(j) >= 0)
                        {
                            continue;
                        }
                        if (oldflags[j] == newflags[j])
                        {
                            continue;
                        }
                        Data.SendToSwitchLog(Name, Address, string.Concat(regname, ".", j),
                                             new string(oldflags[j], 1),
                                             new string(newflags[j], 1), GetFlagDescriptor(i, j));
                    }
                    break;
                }
                var level              = _hregs[0x00];
                var maxheight          = _hregs[0x07];
                var levlow             = _hregs[0x07] - _hregs[0x35];
                var levhigh            = levlow + _hregs[0x36];
                var levbigoff          = _hregs[0x08] - _hregs[0x1A];
                var setpoint           = _hregs[0x08];
                var analoglevelcurrent = _hregs[0x24];
                var alarmlevelcurrent  = _hregs[0x25];
                // ReSharper disable InconsistentNaming
                var hr3_3  = (_hregs[0x03] & 0x0008) > 0;
                var hr3_4  = (_hregs[0x03] & 0x0010) > 0;
                var hr1_14 = (_hregs[0x01] & 0x4000) > 0;
                var hr4_9  = (_hregs[0x04] & 0x0200) > 0;
                var hr3_2  = (_hregs[0x03] & 0x0004) > 0;
                var hr1_13 = (_hregs[0x01] & 0x2000) > 0;
                var hr4_13 = (_hregs[0x04] & 0x2000) > 0;
                var hr4_8  = (_hregs[0x04] & 0x0100) > 0;
                var hr3_1  = (_hregs[0x03] & 0x0002) > 0;
                var hr1_12 = (_hregs[0x01] & 0x1000) > 0;
                var hr4_12 = (_hregs[0x04] & 0x1000) > 0;
                var hr1_8  = (_hregs[0x01] & 0x0100) > 0;
                var hr3_14 = (_hregs[0x03] & 0x4000) > 0;
                var hr3_15 = (_hregs[0x03] & 0x8000) > 0;
                var hr3_6  = (_hregs[0x03] & 0x0040) > 0;
                var hr3_10 = (_hregs[0x03] & 0x0400) > 0;
                var hr3_11 = (_hregs[0x03] & 0x0800) > 0;
                // ReSharper restore InconsistentNaming
                Data.SendToTrend(string.Concat(Name, "_"), Address,
                                 level, 0, maxheight, levlow, levhigh, levbigoff, setpoint,
                                 analoglevelcurrent, alarmlevelcurrent,
                                 hr3_3, hr3_4, hr1_14, hr4_9, hr3_2, hr1_13, hr4_13, hr4_8,
                                 hr3_1, hr1_12, hr4_12, hr1_8, hr3_14, hr3_15, hr3_6, hr3_10, hr3_11);
                _lastFething = DateTime.Now;
            }
        }