-
Notifications
You must be signed in to change notification settings - Fork 0
/
signs.go
96 lines (76 loc) · 2.14 KB
/
signs.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package main
import (
"encoding/xml"
"strings"
"time"
)
const xmlTimeFormat = "2006-01-02T15:04:05"
type updatedTime time.Time
func (ut *updatedTime) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
var ts string
err := d.DecodeElement(&ts, &start)
if err != nil {
return err
}
t, err := time.Parse(xmlTimeFormat, ts)
if err != nil {
return err
}
*ut = updatedTime(t)
return nil
}
// When we're actually displaying, just give the kitchen time
// since we're looking at it for use in the next few minutes
func (ut updatedTime) MarshalText() ([]byte, error) {
t := time.Time(ut)
return []byte(t.Format(time.Kitchen)), nil
}
type messageSigns struct {
Signs []sign `xml:"messageSign"`
}
func (ms messageSigns) FindByName(names ...string) []sign {
var signs []sign
for _, name := range names {
for _, mSign := range ms.Signs {
if mSign.Name == name {
signs = append(signs, mSign)
break
}
}
}
return signs
}
type sign struct {
Location string `xml:"location"`
DmsID string `xml:"dmsid"`
Name string `xml:"name"`
Message string `xml:"message"`
Updated updatedTime `xml:"updated"`
Beacon bool `xml:"beacon"`
Latitude string `xml:"latitude"`
Longitude string `xml:"longitude"`
}
type displaySign struct {
Location string
MessageLines []string
Updated string
}
func toDisplaySigns(signs []sign) []displaySign {
var displaySigns []displaySign
for _, s := range signs {
d := displaySign{Location: s.Location}
noBreaks := strings.Replace(strings.ToUpper(s.Message), "<BR>", "|", -1)
noBreaks = strings.Replace(noBreaks, "<BR/>", "|", -1)
noBreaks = strings.Replace(noBreaks, "<BR />", "|", -1)
noBreaks = strings.Replace(noBreaks, "<P>", "|", -1)
noBreaks = strings.Replace(noBreaks, "<P/>", "|", -1)
noBreaks = strings.Replace(noBreaks, "<P />", "|", -1)
d.MessageLines = strings.Split(noBreaks, "|")
// we can safely ignore the error here because
// we know the implementation can't return one
updated, _ := s.Updated.MarshalText()
d.Updated = string(updated)
displaySigns = append(displaySigns, d)
}
return displaySigns
}