github.com/tommi2day/gomodules@v1.13.2-0.20240423190010-b7d55d252a27/hmlib/roomlist.go (about) 1 package hmlib 2 3 import ( 4 "encoding/xml" 5 "fmt" 6 7 log "github.com/sirupsen/logrus" 8 ) 9 10 // RoomListEndpoint is the endpoint for the device list 11 const RoomListEndpoint = "/addons/xmlapi/roomlist.cgi" 12 13 // RoomListResponse is a list of devices returned by API 14 type RoomListResponse struct { 15 XMLName xml.Name `xml:"roomList"` 16 Rooms []Room `xml:"room"` 17 } 18 19 // Room is a single room in RoomListResponse 20 type Room struct { 21 XMLName xml.Name `xml:"room"` 22 Name string `xml:"name,attr"` 23 IseID string `xml:"ise_id,attr"` 24 Channels []RoomChannel `xml:"channel"` 25 } 26 27 // RoomChannel is a single channel in Room 28 type RoomChannel struct { 29 XMLName xml.Name `xml:"channel"` 30 IseID string `xml:"ise_id,attr"` 31 } 32 33 // RoomMap is a list of rooms by name 34 var RoomMap = make(map[string]Room) 35 36 // GetRoomList returns the list of rooms 37 func GetRoomList() (result RoomListResponse, err error) { 38 log.Debug("getrssi called") 39 err = QueryAPI(RoomListEndpoint, &result, nil) 40 for _, v := range result.Rooms { 41 RoomMap[v.Name] = v 42 } 43 log.Debugf("getRoomList returned %d rooms", len(result.Rooms)) 44 return 45 } 46 47 // String returns a string representation of the device list 48 func (e RoomListResponse) String() string { 49 var s string 50 for _, v := range e.Rooms { 51 s += fmt.Sprintf("%s\n", v) 52 } 53 return s 54 } 55 56 // String returns a string representation of the device list 57 func (e Room) String() string { 58 return fmt.Sprintf("Name:%s Channels:%d ", e.Name, len(e.Channels)) 59 }