dev.azure.com/aidainnovazione0090/DeviceManager/_git/go-mod-core-contracts@v1.0.2/models/devicestatus.go (about) 1 package models 2 3 import ( 4 "encoding/json" 5 "errors" 6 ) 7 8 // import ( 9 // "errors" 10 // ) 11 12 // StatusKind represents the kind of status a device can have. 13 type StatusKind string 14 15 const ( 16 KindUnknown StatusKind = "Unknown" 17 KindOnline StatusKind = "Online" 18 KindOffline StatusKind = "Offline" 19 KindIdle StatusKind = "Idle" 20 21 /*not used now*/ 22 KindStandby StatusKind = "Standby" 23 KindInactive StatusKind = "Inactive" 24 KindMaintenance StatusKind = "Maintenance" 25 KindBusy StatusKind = "Busy" 26 KindUpdating StatusKind = "Updating" 27 KindConfiguring StatusKind = "Configuring" 28 ) 29 30 // SilenceKind represents the kind of silence a device can have. 31 type SilenceKind string 32 33 const ( 34 KindNoSilence SilenceKind = "None" // The device is not silenced. 35 KindScheduled SilenceKind = "Scheduled" // The device is silenced for a scheduled maintenance. 36 KindOnDemand SilenceKind = "OnDemand" // The device is silenced on demand. 37 ) 38 39 // // DeviceStatus represents the status information of a device. 40 type DeviceStatus struct { 41 DeviceName string // The name or identifier of the device. 42 Status StatusKind // The current status of the device. 43 Origin int64 // The origin or source of the status update. 44 LastUpdate int64 // The timestamp of the last status update. 45 46 AlarmCount int64 // The number of alarms associated with the device. 47 MaxSeverity AlarmSeverity `default:"None"` // The maximum severity level of the alarms for the device. 48 Silence SilenceKind `default:"None"` // The silent status of the device. 49 50 } 51 52 func (d *DeviceStatus) UnmarshalJSON(data []byte) error { 53 type Alias DeviceStatus 54 aux := &struct { 55 *Alias 56 }{ 57 Alias: (*Alias)(d), 58 } 59 aux.MaxSeverity = AlarmNone 60 aux.Silence = KindNoSilence 61 if err := json.Unmarshal(data, &aux); err != nil { 62 return err 63 } 64 return nil 65 } 66 67 func (s *StatusKind) UnmarshalText(text []byte) error { 68 switch string(text) { 69 case "": 70 case "Unknown": 71 *s = KindUnknown 72 case "Online": 73 *s = KindOnline 74 case "Offline": 75 *s = KindOffline 76 case "Idle": 77 *s = KindIdle 78 case "Standby": 79 *s = KindStandby 80 case "Inactive": 81 *s = KindInactive 82 case "Maintenance": 83 *s = KindMaintenance 84 case "Busy": 85 *s = KindBusy 86 case "Updating": 87 *s = KindUpdating 88 case "Configuring": 89 *s = KindConfiguring 90 default: 91 return errors.New("unknown StatusKind") 92 } 93 return nil 94 } 95 96 func (s *SilenceKind) UnmarshalText(text []byte) error { 97 txtString := string(text) 98 switch txtString { 99 case "": 100 *s = KindNoSilence 101 case "None": 102 *s = KindNoSilence 103 case "Scheduled": 104 *s = KindScheduled 105 case "OnDemand": 106 *s = KindOnDemand 107 default: 108 return errors.New(`unknown SilenceKind: expected "Scheduled" or "OnDemand" or "None", got ` + txtString) 109 } 110 111 return nil 112 }