|
|
package models
import ( "strconv" "strings" )
type ReferenceKind string
var ( RKDeviceID ReferenceKind = "DeviceID" RKBridgeID ReferenceKind = "BridgeID" RKTag ReferenceKind = "Tag" RKAll ReferenceKind = "All" RKName ReferenceKind = "Name" )
func (rk ReferenceKind) Matches(device *Device, value string) bool { switch rk { case RKName: if strings.HasPrefix(value, "*") { return strings.HasSuffix(device.Name, value[1:]) } else if strings.HasSuffix(value, "*") { return strings.HasPrefix(device.Name, value[:len(value)-1]) } else { return device.Name == value } case RKDeviceID: idStr := strconv.Itoa(device.ID) for _, idStr2 := range strings.Split(value, ",") { if idStr == idStr2 { return true } } case RKBridgeID: idStr := strconv.Itoa(device.BridgeID) for _, idStr2 := range strings.Split(value, ",") { if idStr == idStr2 { return true } } case RKTag: hadAny := false for _, tag := range strings.Split(value, ",") { if strings.HasPrefix(tag, "-") { if device.HasTag(tag[1:]) { return false } } else if strings.HasPrefix(tag, "+") { if !device.HasTag(tag[1:]) { return false } } else { if device.HasTag(tag) { hadAny = true } } }
return hadAny case RKAll: return true }
return false }
func ParseFetchString(fetchStr string) (ReferenceKind, string) { if strings.HasPrefix(fetchStr, "tag:") { return RKTag, fetchStr[4:] } else if strings.HasPrefix(fetchStr, "bridge:") { return RKBridgeID, fetchStr[7:] } else if strings.HasPrefix(fetchStr, "id:") { return RKDeviceID, fetchStr[7:] } else if strings.HasPrefix(fetchStr, "name:") { return RKName, fetchStr[7:] } else if fetchStr == "all" { return RKAll, "" } else { _, err := strconv.Atoi(fetchStr) if err != nil { return RKName, fetchStr }
return RKDeviceID, fetchStr } }
|