public static String GetMessageId(this AlarmMessageType alarmMessageType)
 {
     return(alarmMessageType.GetStringValue());
 }
Beispiel #2
0
        public static S7AlarmMessage TranslateFromMemory(Memory <byte> data, AlarmMessageType subfunction)
        {
            var span    = data.Span;
            var current = new S7AlarmMessage();
            var offset  = 0;

            current.Timestamp          = GetDt(span.Slice(offset)); offset += 8;
            current.FunctionIdentifier = span[offset++];
            current.NumberOfMessages   = span[offset++];

            var alarms = new List <S7PlcAlarmItemDatagram>();

            for (var i = 0; i < current.NumberOfMessages; i++)
            {
                var alarm = new S7PlcAlarmItemDatagram
                {
                    AlarmType = subfunction
                };

                //var varspec = span[offset];
                offset++;
                alarm.Length = span[offset++];

                if (alarm.Length > 0)
                {
                    var syntaxId = span[offset++];

                    switch (syntaxId)
                    {
                    case (byte)SyntaxIds.AlarmInd:
                    case (byte)SyntaxIds.AlarmAck:
                    {
                        //var numberOfAssociatedValues = span[offset];
                        offset++;
                        alarm.MsgNumber = BinaryPrimitives.ReadUInt32BigEndian(span.Slice(offset, 4));
                        offset         += 2; // 2 is correct, we use the offset twice
                        alarm.Id        = BinaryPrimitives.ReadUInt16BigEndian(span.Slice(offset, 2));
                        offset         += 2;


                        switch (subfunction)
                        {
                        case AlarmMessageType.AlarmSQ:             // ALARM_SQ
                        case AlarmMessageType.AlarmS:              // ALARM_S
                        {
                            alarm.EventState     = span[offset++]; // 0x00 == going   0x01  == coming
                            alarm.State          = span[offset++]; // isAck
                            alarm.AckStateGoing  = span[offset++];
                            alarm.AckStateComing = span[offset++]; // 0x00 == no ack  0x01  == ack

                            var details = new S7PlcAlarmDetails {
                                Timestamp = current.Timestamp
                            };
                            S7PlcAlarmDetails.ExtractAssotiatedValue(ref span, ref offset, details);

                            if (alarm.EventState == 0x00)
                            {
                                alarm.Going = details;
                            }
                            else if (alarm.EventState == 0x01)
                            {
                                alarm.Coming = details;
                            }
                        }
                        break;

                        case AlarmMessageType.AlarmAck:             // ALARM ack
                        {
                            alarm.AckStateGoing  = span[offset++];
                            alarm.AckStateComing = span[offset++];                 // 0x00 == no ack  0x01  == ack
                        }
                        break;

                        default:
                        {
                            ThrowHelper.ThrowUnknownAlarmSubfunction(subfunction);
                            break;
                        }
                        }
                        break;
                    }

                    default:
                    {
                        ThrowHelper.ThrowUnknownAlarmSyntax(syntaxId);
                        break;
                    }
                    }
                }

                alarms.Add(alarm);
            }
            current.Alarms = alarms;
            return(current);
        }
Beispiel #3
0
        public static void AddProviderAlarm(AlarmType alarmType, EntityType objectType, AlarmStatusType alarmStatus, AlarmMessageType messageType, Guid objectId, Guid containerId, String providerId)
        {
            if (String.IsNullOrEmpty(providerId))
            {
                throw new ArgumentNullException("providerId");
            }

            providerId = providerId.ToLower();

            using (var context = new DataService())
            {
                var storageAlarm = context.StorageAlarms.GetAll()
                                   .OrderByDescending(sa => sa.AlarmTimeStamp)
                                   .FirstOrDefault(sAlarm => sAlarm.AlarmType == alarmType &&
                                                   sAlarm.ObjectType == objectType &&
                                                   sAlarm.ObjectId == objectId &&
                                                   sAlarm.ProviderId != null &&
                                                   sAlarm.ProviderId.ToLower() == providerId);

                if (storageAlarm != null && storageAlarm.Status == alarmStatus)
                {
                    return;
                }

                context.StorageAlarms.Add(new StorageAlarm
                {
                    Id             = Guid.NewGuid(),
                    AlarmType      = alarmType,
                    ObjectType     = objectType,
                    Status         = alarmStatus,
                    AlarmTimeStamp = DateTime.UtcNow,
                    MessageType    = messageType,
                    ContainerId    = containerId,
                    ObjectId       = objectId,
                    ProviderId     = providerId
                });

                context.Save();
            }
        }
Beispiel #4
0
 public static void ThrowUnknownAlarmSubfunction(AlarmMessageType subfunction) => throw new Exception($"Unknown alarm subfunction {subfunction}");
Beispiel #5
0
        public static void AddAlarm(AlarmType alarmType, EntityType objectType, AlarmStatusType alarmStatus, AlarmMessageType messageType, Guid objectId, Guid containerId)
        {
            using (var context = new DataService())
            {
                var storageAlarm = context.StorageAlarms.GetAll()
                                   .OrderByDescending(sa => sa.AlarmTimeStamp)
                                   .FirstOrDefault(
                    sAlarm =>
                    sAlarm.AlarmType == alarmType && sAlarm.ObjectType == objectType &&
                    sAlarm.ObjectId == objectId);

                if (storageAlarm != null && storageAlarm.Status == alarmStatus)
                {
                    return;
                }

                context.StorageAlarms.Add(new StorageAlarm
                {
                    Id             = Guid.NewGuid(),
                    AlarmType      = alarmType,
                    ObjectType     = objectType,
                    Status         = alarmStatus,
                    AlarmTimeStamp = DateTime.UtcNow,
                    MessageType    = messageType,
                    ContainerId    = containerId,
                    ObjectId       = objectId
                });

                context.Save();
            }
        }