// Unsubscribe unsubscribes from data change
Unsubscribe unsubscribes from data change
// Unsubscribe unsubscribes from data change
func (provider *DataProvider) Unsubscribe(id uint64, authInfo *AuthInfo) (err error) {
	provider.Lock()
	defer provider.Unlock()
	log.WithField("subscribeID", id).Debug("Unsubscribe")
	subscribeInfo, ok := provider.subscribeInfoMap[id]
	if !ok {
		return fmt.Errorf("subscribe id %v not found", id)
	}
	close(subscribeInfo.channel)
	delete(provider.subscribeInfoMap, id)
	unsubscribeMap := make(map[DataAdapter][]string)
	for path, sensor := range provider.sensors {
		if sensor.subscribeIds.Len() == 0 {
			continue
		}
		var nextElement *list.Element
		for idElement := sensor.subscribeIds.Front(); idElement != nil; idElement = nextElement {
			nextElement = idElement.Next()
			if idElement.Value.(uint64) == id {
				sensor.subscribeIds.Remove(idElement)
			}
		}
		if sensor.subscribeIds.Len() == 0 {
			if unsubscribeMap[sensor.adapter] == nil {
				unsubscribeMap[sensor.adapter] = make([]string, 0, 10)
			}
			unsubscribeMap[sensor.adapter] = append(unsubscribeMap[sensor.adapter], path)
		}
	}
	for adapter, pathList := range unsubscribeMap {
		for _, path := range pathList {
			log.WithFields(log.Fields{"adapter": adapter.GetName(), "path": path}).Debug("Unsubscribe from adapter data")
		}
		if err = adapter.Unsubscribe(pathList); err != nil {
			return err
		}
	}
	return nil
}
func ( provider * DataProvider ) Unsubscribe ( id uint64 , authInfo * AuthInfo ) ( err error ) { provider . Lock ( ) defer provider . Unlock ( ) log . WithField ( " " , id ) . Debug ( " " ) subscribeInfo , ok := provider . subscribeInfoMap [ id ] if ! ok { return fmt . Errorf ( " " , id ) } close ( subscribeInfo . channel ) delete ( provider . subscribeInfoMap , id ) unsubscribeMap := make ( map [ DataAdapter ] [ ] string ) for path , sensor := range provider . sensors { if sensor . subscribeIds . Len ( ) == 0 { continue } var nextElement * list . Element for idElement := sensor . subscribeIds . Front ( ) ; idElement != nil ; idElement = nextElement { nextElement = idElement . Next ( ) if idElement . Value . ( uint64 ) == id { sensor . subscribeIds . Remove ( idElement ) } } if sensor . subscribeIds . Len ( ) == 0 { if unsubscribeMap [ sensor . adapter ] == nil { unsubscribeMap [ sensor . adapter ] = make ( [ ] string , 0 , 10 ) } unsubscribeMap [ sensor . adapter ] = append ( unsubscribeMap [ sensor . adapter ] , path ) } } for adapter , pathList := range unsubscribeMap { for _ , path := range pathList { log . WithFields ( log . Fields { " " : adapter . GetName ( ) , " " : path } ) . Debug ( " " ) } if err = adapter . Unsubscribe ( pathList ) ; err != nil { return err } } return nil }
Unsubscribe unsubscribes from data change
Unsubscribe unsubscribes from data change
// Create map of pathes grouped by adapter // Go through all sensors and remove id // Add path to unsubscribeMap // Unsubscribe from adapter data changes