fix service url resolving from device map & use an automatic way.

This commit is contained in:
Eamon
2018-05-17 18:10:19 +08:00
parent f4182a60d9
commit dc42c9d688

View File

@@ -188,7 +188,11 @@ func NewDevice(xaddr string) (*Device, error) {
} }
func (dev *Device) addEndpoint(Key, Value string) { func (dev *Device) addEndpoint(Key, Value string) {
dev.endpoints[Key] = Value
//use lowCaseKey
//make key having ability to handle Mixed Case for Different vendor devcie (e.g. Events EVENTS, events)
lowCaseKey := strings.ToLower(Key)
dev.endpoints[lowCaseKey] = Value
} }
//Authenticate function authenticate client in the ONVIF Device. //Authenticate function authenticate client in the ONVIF Device.
@@ -221,27 +225,38 @@ func buildMethodSOAP(msg string) (gosoap.SoapMessage, error) {
return soap, nil return soap, nil
} }
//getEndpoint functions get the target service endpoint in a better way
func (dev Device) getEndpoint(endpointMark string) (string, error) {
// common condition, endpointMark equl targetKey in map we use this.
var endpointURL string
for targetKey := range dev.endpoints {
if strings.Compare(targetKey, endpointMark) == 0 {
endpointURL = dev.endpoints[targetKey]
return endpointURL, nil
}
}
//but ,if we have enpointMark like event、analytic
//and sametime the Targetkey like : events、analytics
//we use this find the best match url
for targetKey := range dev.endpoints {
if strings.Contains(targetKey, endpointMark) {
endpointURL = dev.endpoints[targetKey]
return endpointURL, nil
}
}
return endpointURL, errors.New("target endpoint service not found")
}
//CallMethod functions call an method, defined <method> struct. //CallMethod functions call an method, defined <method> struct.
//You should use Authenticate method to call authorized requests. //You should use Authenticate method to call authorized requests.
func (dev Device) CallMethod(method interface{}) (*http.Response, error) { func (dev Device) CallMethod(method interface{}) (*http.Response, error) {
pkgPath := strings.Split(reflect.TypeOf(method).PkgPath(), "/") pkgPath := strings.Split(reflect.TypeOf(method).PkgPath(), "/")
pkg := strings.ToLower(pkgPath[len(pkgPath)-1]) pkg := strings.ToLower(pkgPath[len(pkgPath)-1])
var endpoint string if endpoint, err := dev.getEndpoint(pkg); err != nil {
switch pkg { return nil, err
case "device": } else {
endpoint = dev.endpoints["Device"]
case "event":
endpoint = dev.endpoints["Event"]
case "imaging":
endpoint = dev.endpoints["Imaging"]
case "media":
endpoint = dev.endpoints["Media"]
case "ptz":
endpoint = dev.endpoints["PTZ"]
}
//TODO: Get endpoint automatically
if dev.login != "" && dev.password != "" { if dev.login != "" && dev.password != "" {
return dev.callAuthorizedMethod(endpoint, method) return dev.callAuthorizedMethod(endpoint, method)
} else { } else {
@@ -249,6 +264,8 @@ func (dev Device) CallMethod(method interface{}) (*http.Response, error) {
} }
} }
}
//CallNonAuthorizedMethod functions call an method, defined <method> struct without authentication data //CallNonAuthorizedMethod functions call an method, defined <method> struct without authentication data
func (dev Device) callNonAuthorizedMethod(endpoint string, method interface{}) (*http.Response, error) { func (dev Device) callNonAuthorizedMethod(endpoint string, method interface{}) (*http.Response, error) {
//TODO: Get endpoint automatically //TODO: Get endpoint automatically