<commit_before>package main

import (
	"errors"
	"fmt"
	"github.com/gorilla/mux"
	"github.com/ti-mo/comfo/libcomfo"
	"net/http"
	"sync"
	"time"
)

var (
	fanLock = sync.Mutex{}
)

// TempHandlerGet displays the temperature cache object.
func TempHandlerGet(w http.ResponseWriter, r *http.Request) {

	jsonWrite(w, &tempCache)
}

// FanHandlerGet displays the fan speed cache object.
func FanHandlerGet(w http.ResponseWriter, r *http.Request) {

	jsonWrite(w, &fanCache)
}

// FanHandlerSet updates the fan speed on the unit and updates
// the fan speed and fan profile cache objects.
func FanHandlerSet(w http.ResponseWriter, r *http.Request) {

	start := time.Now()
	vars := mux.Vars(r)

	var err error
	var modified bool

	// Initialize speed values
	origSpeed := fanProfilesCache.CurrentLevel

	// Input validation
	actionStr, ok := vars["speed"]
	if !ok {
		jsonError(w, errors.New("missing request variable 'speed'"))
		return
	}

	// Apply action string to original speed
	tgtSpeed, err := modifySpeed(int(origSpeed), actionStr)
	if err != nil {
		jsonError(w, err)
		return
	}

	// Only send actions to the unit if speed needs to be modified
	if uint8(tgtSpeed) != origSpeed {

		// Lock the fan speed mutex
		fanLock.Lock()
		defer fanLock.Unlock()

		// Send target speed to the unit
		err = libcomfo.SetSpeed(uint8(tgtSpeed), comfo)
		if err != nil {
			jsonError(w, err)
			return
		}

		// Poll the unit for the target speed
		err = fanCache.UpdatePoll(10, uint8(tgtSpeed))
		if err != nil {
			jsonError(w, err)
			return
		}

		// Flush the fanProfilesCache when values have converged
		fanProfilesCache.Update(true)

		// Set modified flag
		modified = true
	}

	// Compose response
	resp := map[string]interface{}{
		"success":        true,
		"modified":       modified,
		"original_speed": origSpeed,
		"target_speed":   tgtSpeed,
		"fans":           &fanCache,
		"reqtime":        fmt.Sprint(time.Since(start)),
	}

	jsonWrite(w, &resp)
}

// FanProfilesHandlerGet displays the fan speed profiles cache object.
func FanProfilesHandlerGet(w http.ResponseWriter, r *http.Request) {

	jsonWrite(w, &fanProfilesCache)
}

// FlushCacheHandler synchronously updates all data caches of the unit.<commit_msg>comfo - return `profiles` key from fanspeed set operation<commit_after>package main

import (
	"errors"
	"fmt"
	"github.com/gorilla/mux"
	"github.com/ti-mo/comfo/libcomfo"
	"net/http"
	"sync"
	"time"
)

var (
	fanLock = sync.Mutex{}
)

// TempHandlerGet displays the temperature cache object.
func TempHandlerGet(w http.ResponseWriter, r *http.Request) {

	jsonWrite(w, &tempCache)
}

// FanHandlerGet displays the fan speed cache object.
func FanHandlerGet(w http.ResponseWriter, r *http.Request) {

	jsonWrite(w, &fanCache)
}

// FanHandlerSet updates the fan speed on the unit and updates
// the fan speed and fan profile cache objects.
func FanHandlerSet(w http.ResponseWriter, r *http.Request) {

	start := time.Now()
	vars := mux.Vars(r)

	var err error
	var modified bool

	// Initialize speed values
	origSpeed := fanProfilesCache.CurrentLevel

	// Input validation
	actionStr, ok := vars["speed"]
	if !ok {
		jsonError(w, errors.New("missing request variable 'speed'"))
		return
	}

	// Apply action string to original speed
	tgtSpeed, err := modifySpeed(int(origSpeed), actionStr)
	if err != nil {
		jsonError(w, err)
		return
	}

	// Only send actions to the unit if speed needs to be modified
	if uint8(tgtSpeed) != origSpeed {

		// Lock the fan speed mutex
		fanLock.Lock()
		defer fanLock.Unlock()

		// Send target speed to the unit
		err = libcomfo.SetSpeed(uint8(tgtSpeed), comfo)
		if err != nil {
			jsonError(w, err)
			return
		}

		// Poll the unit for the target speed
		err = fanCache.UpdatePoll(10, uint8(tgtSpeed))
		if err != nil {
			jsonError(w, err)
			return
		}

		// Flush the fanProfilesCache when values have converged
		fanProfilesCache.Update(true)

		// Set modified flag
		modified = true
	}

	// Compose response
	resp := map[string]interface{}{
		"success":        true,
		"modified":       modified,
		"original_speed": origSpeed,
		"target_speed":   tgtSpeed,
		"fans":           &fanCache,
		"profiles":       &fanProfilesCache,
		"reqtime":        fmt.Sprint(time.Since(start)),
	}

	jsonWrite(w, &resp)
}

// FanProfilesHandlerGet displays the fan speed profiles cache object.
func FanProfilesHandlerGet(w http.ResponseWriter, r *http.Request) {

	jsonWrite(w, &fanProfilesCache)
}

// FlushCacheHandler synchronously updates all data caches of the unit.
{'fraction_non_alphanumeric': 0.11241425094047355, 'fraction_numerical': 0.002212878955521133, 'mean_word_length': 3.584178498985801, 'pattern_counts': {'":': 13, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4456812', 'n_tokens_mistral': 1599, 'n_tokens_neox': 1490, 'n_words': 440}