|
17 | 17 |
|
18 | 18 | package otaapi
|
19 | 19 |
|
20 |
| -import "fmt" |
| 20 | +import ( |
| 21 | + "fmt" |
| 22 | + "time" |
| 23 | +) |
21 | 24 |
|
22 | 25 | type (
|
23 | 26 | OtaStatusResponse struct {
|
@@ -49,28 +52,39 @@ type (
|
49 | 52 | OtaStatusPrint struct {
|
50 | 53 | Ota Ota `json:"ota"`
|
51 | 54 | Details []State `json:"details,omitempty"`
|
52 |
| - } |
| 55 | + } |
53 | 56 | )
|
54 | 57 |
|
55 | 58 | func (o Ota) ToLine() string {
|
56 | 59 | if o.ErrorReason != "" {
|
57 |
| - return fmt.Sprintf("ota_id: %s, started_at: %s, ended_at: %s, status: %s, error_reason: %s", o.ID, o.StartedAt, o.EndedAt, o.Status, o.ErrorReason) |
| 60 | + return fmt.Sprintf("ota_id: %s, started_at: %s, ended_at: %s, status: %s, error_reason: %s", o.ID, formatHumanReadableTs(o.StartedAt), formatHumanReadableTs(o.EndedAt), o.Status, o.ErrorReason) |
58 | 61 | }
|
59 |
| - return fmt.Sprintf("ota_id: %s, started_at: %s, ended_at: %s, status: %s", o.ID, o.StartedAt, o.EndedAt, o.Status) |
| 62 | + return fmt.Sprintf("ota_id: %s, started_at: %s, ended_at: %s, status: %s", o.ID, formatHumanReadableTs(o.StartedAt), formatHumanReadableTs(o.EndedAt), o.Status) |
60 | 63 | }
|
61 | 64 |
|
62 | 65 | func (o Ota) ToText() string {
|
63 |
| - out := fmt.Sprintf("ota_id: %s\nstatus: %s\nstarted_at: %s\nended_at: %s\ndevice_id: %s", o.ID, o.Status, o.StartedAt, o.EndedAt, o.DeviceID) |
| 66 | + out := fmt.Sprintf("ota_id: %s\nstatus: %s\nstarted_at: %s\nended_at: %s\ndevice_id: %s", o.ID, o.Status, formatHumanReadableTs(o.StartedAt), formatHumanReadableTs(o.EndedAt), o.DeviceID) |
64 | 67 | if o.ErrorReason != "" {
|
65 | 68 | out += fmt.Sprintf("\nerror_reason: %s", o.ErrorReason)
|
66 | 69 | }
|
67 | 70 | return out
|
68 | 71 | }
|
69 | 72 |
|
70 | 73 | func (s State) ToText() string {
|
71 |
| - rep := fmt.Sprintf("time: %s, status: %s", s.Timestamp, s.State) |
| 74 | + rep := fmt.Sprintf("time: %s, status: %s", formatHumanReadableTs(s.Timestamp), s.State) |
72 | 75 | if s.StateData != "" {
|
73 | 76 | rep += fmt.Sprintf(", detail: %s", s.StateData)
|
74 | 77 | }
|
75 | 78 | return rep
|
76 | 79 | }
|
| 80 | + |
| 81 | +func formatHumanReadableTs(ts string) string { |
| 82 | + if ts == "" { |
| 83 | + return "" |
| 84 | + } |
| 85 | + parsed, err := time.Parse(time.RFC3339Nano, ts) |
| 86 | + if err != nil { |
| 87 | + return ts |
| 88 | + } |
| 89 | + return parsed.Format(time.RFC3339) |
| 90 | +} |
0 commit comments