mirror of
https://github.com/absmach/magistrala.git
synced 2026-06-23 04:10:28 +00:00
61d0427898
Signed-off-by: dusan <borovcanindusan1@gmail.com>
71 lines
1.4 KiB
Go
71 lines
1.4 KiB
Go
// Copyright (c) Abstract Machines
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
package alarms
|
|
|
|
import (
|
|
"encoding/json"
|
|
"strings"
|
|
|
|
svcerr "github.com/absmach/magistrala/pkg/errors/service"
|
|
)
|
|
|
|
type Status uint8
|
|
|
|
const (
|
|
ActiveStatus Status = iota
|
|
ClearedStatus
|
|
|
|
// AllStatus is used for querying purposes to list alarms irrespective
|
|
// of their status. It is never stored in the database as the actual
|
|
// Alarm status and should always be the largest value in this enumeration.
|
|
AllStatus
|
|
)
|
|
|
|
const (
|
|
Active = "active"
|
|
Cleared = "cleared"
|
|
Unknown = "unknown"
|
|
All = "all"
|
|
)
|
|
|
|
// String converts alarm status to string literal.
|
|
func (s Status) String() string {
|
|
switch s {
|
|
case ActiveStatus:
|
|
return Active
|
|
case ClearedStatus:
|
|
return Cleared
|
|
default:
|
|
return Unknown
|
|
}
|
|
}
|
|
|
|
// ToStatus converts string value to a valid Alarm status.
|
|
func ToStatus(status string) (Status, error) {
|
|
switch strings.ToLower(status) {
|
|
case Active:
|
|
return ActiveStatus, nil
|
|
case Cleared:
|
|
return ClearedStatus, nil
|
|
case All:
|
|
return AllStatus, nil
|
|
default:
|
|
return Status(0), svcerr.ErrInvalidStatus
|
|
}
|
|
}
|
|
|
|
// Custom Marshaller for Alarm.
|
|
func (s Status) MarshalJSON() ([]byte, error) {
|
|
return json.Marshal(s.String())
|
|
}
|
|
|
|
// Custom Unmarshaler for Alarm.
|
|
func (s *Status) UnmarshalJSON(data []byte) error {
|
|
str := strings.Trim(string(data), "\"")
|
|
val, err := ToStatus(str)
|
|
*s = val
|
|
|
|
return err
|
|
}
|