@@ -3,6 +3,7 @@ package devices
33import (
44 "context"
55 "strconv"
6+ "strings"
67 "time"
78
89 amtAlarmClock "github.com/open-amt-cloud-toolkit/go-wsman-messages/v2/pkg/wsman/amt/alarmclock"
@@ -11,6 +12,11 @@ import (
1112 "github.com/open-amt-cloud-toolkit/console/internal/entity/dto/v1"
1213)
1314
15+ const (
16+ minutesPerDay = 24 * 60
17+ minutesPerHour = 60
18+ )
19+
1420func (uc * UseCase ) GetAlarmOccurrences (c context.Context , guid string ) ([]dto.AlarmClockOccurrence , error ) {
1521 item , err := uc .GetByID (c , guid , "" )
1622 if err != nil {
@@ -33,7 +39,7 @@ func (uc *UseCase) GetAlarmOccurrences(c context.Context, guid string) ([]dto.Al
3339
3440 for i := range alarms {
3541 tmpEntity := alarms [i ] // create a new variable to avoid memory aliasing
36- d1 [i ] = * uc .alarmOccurenceEntityToDTO (& tmpEntity )
42+ d1 [i ] = * uc .alarmOccurrenceEntityToDTO (& tmpEntity )
3743 }
3844
3945 return d1 , nil
@@ -83,16 +89,53 @@ func (uc *UseCase) addAlarmOutputEntityToDTO(d *amtAlarmClock.AddAlarmOutput) *d
8389 return d1
8490}
8591
86- func (uc * UseCase ) alarmOccurenceEntityToDTO (d * alarmclock.AlarmClockOccurrence ) * dto.AlarmClockOccurrence {
87- startTime , _ := time . Parse ( time . RFC3339 , d . StartTime )
88- interval , _ := strconv .Atoi (d .Interval )
92+ func (uc * UseCase ) alarmOccurrenceEntityToDTO (d * alarmclock.AlarmClockOccurrence ) * dto.AlarmClockOccurrence {
93+ intervalInMinutes , _ := ParseInterval ( d . Interval . Interval )
94+ interval , _ := strconv .Atoi (d .Interval . Interval )
8995 d1 := & dto.AlarmClockOccurrence {
9096 ElementName : d .ElementName ,
9197 InstanceID : d .InstanceID ,
92- StartTime : startTime ,
98+ StartTime : d . StartTime . Datetime ,
9399 Interval : interval ,
100+ IntervalInMinutes : intervalInMinutes ,
94101 DeleteOnCompletion : d .DeleteOnCompletion ,
95102 }
96103
97104 return d1
98105}
106+
107+ func ParseInterval (duration string ) (int , error ) {
108+ if duration == "" {
109+ return 0 , nil
110+ }
111+
112+ totalMinutes := 0
113+
114+ // parse days
115+ duration = strings .TrimPrefix (duration , "P" )
116+ indexOfD := strings .Index (duration , "D" )
117+
118+ if indexOfD != - 1 {
119+ days , err := strconv .Atoi (duration [:indexOfD ])
120+ if err != nil {
121+ return 0 , err
122+ }
123+
124+ totalMinutes = days * minutesPerDay
125+ }
126+
127+ // parse time
128+ indexOfT := strings .Index (duration , "T" )
129+ if indexOfT != - 1 {
130+ duration = strings .ToLower (duration [indexOfT + 1 :])
131+
132+ timeDuration , err := time .ParseDuration (duration )
133+ if err != nil {
134+ return 0 , err
135+ }
136+
137+ return totalMinutes + int (timeDuration .Minutes ()), nil
138+ }
139+
140+ return totalMinutes , nil
141+ }
0 commit comments