Beispiel #1
0
func currentSong(_ *Cmd, conn *mpd.Client) (r *Result, err error) {
	info, err := conn.CurrentSong()

	if err == nil {
		r = NewResult("CurrentSong", info)
	}
	return
}
Beispiel #2
0
func (m MPDInstance) Render() (item modules.Item) {
	mpdItem := MPDItem{Name: m.name, Markup: "pango"}
	mpdFormatData := MPDFormatData{}
	var client *go_mpd.Client

	if c, err := go_mpd.Dial("tcp", m.host_name+":"+strconv.Itoa(m.port)); err != nil {
		log.Error(err.Error())
		return nil
	} else {
		client = c
	}

	defer func(client *go_mpd.Client) {
		if err := client.Close(); err != nil {
			log.Error("Failed to disconnect: " + err.Error())
		}
	}(client)

	if attrs, err := client.Status(); err == nil {
		if state, ok := attrs["state"]; ok {
			mpdFormatData.State = state
		} else {
			log.Error("Failed to read state.")
			return nil
		}
	} else {
		log.Error("Failed to obtain status: " + err.Error())
		return nil
	}

	if attrs, err := client.CurrentSong(); err != nil {
		log.Error("Failed to obtain current song: " + err.Error())
		return nil
	} else {
		obj := reflect.ValueOf(&mpdFormatData).Elem()
		for key, val := range attrs {
			if f := obj.FieldByName(key); f.IsValid() {
				val = strings.TrimSpace(val)
				f.Set(reflect.ValueOf(val))
			}
		}
	}

	buffer := bytes.Buffer{}

	if err := m.template.Execute(&buffer, mpdFormatData); err != nil {
		log.Error("Failed to render mpd template: " + err.Error())
		return nil
	} else {
		mpdItem.Text = buffer.String()
	}

	item = mpdItem
	return
}
Beispiel #3
0
// StatusUpdater is triggerd on "player" events and fetches the current state infos
// needed for the moodbar sync. It then proceeds to generate a MPDEvent that
// MoodbarAdjuster will receive.
func StatusUpdater(client *mpd.Client, cfg *Config, updateCh <-chan bool, eventCh chan<- MPDEvent) {
	lastSongID := ""

	for range updateCh {
		song, err := client.CurrentSong()
		if err != nil {
			log.Printf("Unable to fetch current song: %v", err)
			continue
		}

		status, err := client.Status()
		if err != nil {
			log.Printf("Unable to fetch status: %v", err)
			continue
		}

		// Check if the song changed compared to last time:
		// (always true for the first iteration)
		songChanged := false
		if status["songid"] != lastSongID {
			lastSongID = status["songid"]
			songChanged = true
		}

		// Find out how much progress we did in the current song.
		// These atteributes might be empty for the stopped state.
		elapsedMs, err := strconv.ParseFloat(status["elapsed"], 64)
		if err != nil && status["elapsed"] != "" {
			log.Printf("Failed to parse elpased (%s): %v", status["elapsed"], err)
		}

		elapsedMs *= 1000

		totalMs, err := strconv.Atoi(song["Time"])
		if err != nil && song["Time"] != "" {
			log.Printf("Failed to parse total (%s): %v", song["time"], err)
		}

		totalMs *= 1000

		// Find out if some music is playing...
		isPlaying, isStopped := false, false
		switch status["state"] {
		case "play":
			isPlaying = true
		case "stop":
			isStopped = true
		}

		// Send the appropiate event:
		eventCh <- MPDEvent{
			Path: filepath.Join(
				cfg.MoodDir,
				strings.Replace(song["file"], string(filepath.Separator), "|", -1),
			),
			SongChanged: songChanged,
			ElapsedMs:   elapsedMs,
			TotalMs:     float64(totalMs),
			IsPlaying:   isPlaying,
			IsStopped:   isStopped,
		}
	}
}