probe/ssdp/ssdp.go
2018-08-24 14:41:07 +09:00

73 lines
1.8 KiB
Go

package ssdp
import (
"encoding/xml"
"net/http"
"github.com/koron/go-ssdp" // MIT license
)
type SpecVersion struct {
Major int `xml:"major"`
Minor int `xml:"minor"`
}
type Icon struct {
MIMEType string `xml:"mimetype"`
Width int `xml:"width"`
Height int `xml:"height"`
Depth int `xml:"depth"`
URL string `xml:"url"`
}
type Device struct {
SpecVersion SpecVersion `xml:"specVersion"`
URLBase string `xml:"URLBase"`
DeviceType string `xml:"device>deviceType"`
FriendlyName string `xml:"device>friendlyName"`
Manufacturer string `xml:"device>manufacturer"`
ManufacturerURL string `xml:"device>manufacturerURL"`
ModelDescription string `xml:"device>modelDescription"`
ModelName string `xml:"device>modelName"`
ModelNumber string `xml:"device>modelNumber"`
ModelURL string `xml:"device>modelURL"`
SerialNumber string `xml:"device>serialNumber"`
UDN string `xml:"device>UDN"`
UPC string `xml:"device>UPC"`
PresentationURL string `xml:"device>presentationURL"`
Icons []Icon `xml:"device>iconList>icon"`
}
func SearchRootDevices() ([]Device, error) {
list, err := ssdp.Search(ssdp.RootDevice, 1, "")
if err != nil {
return nil, err
}
devices := make([]Device, 0, len(list))
for _, srv := range list {
device, err := parseResponse(srv.Location)
if err != nil {
continue
}
devices = append(devices, *device)
}
return devices, nil
}
func parseResponse(url string) (*Device, error) {
response, err := http.Get(url)
if err != nil {
return nil, err
}
defer response.Body.Close()
decoder := xml.NewDecoder(response.Body)
device := &Device{}
if err := decoder.Decode(device); err != nil {
return nil, err
}
return device, nil
}