utils

package
v0.5.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 7, 2021 License: BSD-3-Clause Imports: 10 Imported by: 0

Documentation

Overview

Package utils contains common routines for the hue web application.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FutureTime

func FutureTime(now time.Time, hour, minute int) time.Time

FutureTime returns hour:minute as a future time from now. The returned time is the closest hour:minute from now that is just after now. The returned time is in the same timezone as now. hour is 0-23; minute is 0-59.

func NewTemplate

func NewTemplate(name, templateStr string) *template.Template

NewTemplate returns a new template instance. name is the name of the template; templateStr is the template string.

Types

type AtTimeTaskStore

type AtTimeTaskStore interface {
	// Returns all stored tasks
	All() []*ops.AtTimeTask

	// Removes a particular task by schedule Id
	Remove(scheduleId string)

	// Adds a new task
	Add(task *ops.AtTimeTask)
}

Interface AtTimeTaskStore keeps persistent storage of all scheduled tasks in a MultiTimer.

type BackgroundRunner

type BackgroundRunner struct {
	// contains filtered or unexported fields
}

BackgroundRunner runs a single task in the background. BackgroundRunner is safe to use with multiple goroutines.

func NewBackgroundRunner

func NewBackgroundRunner(task tasks.Task) *BackgroundRunner

func (*BackgroundRunner) Disable

func (br *BackgroundRunner) Disable()

Disable stops the task.

func (*BackgroundRunner) Enable

func (br *BackgroundRunner) Enable()

Enable runs the task.

func (*BackgroundRunner) IsEnabled

func (br *BackgroundRunner) IsEnabled() bool

IsEnabled returns true if the task is running.

type FutureHueTask

type FutureHueTask interface {

	// Refresh returns the HueTask
	Refresh() *ops.HueTask

	// GetDescription returns the description.
	GetDescription() string
}

FutureHueTask represents a future hue task.

type HueTaskBeginner

type HueTaskBeginner interface {
	Begin(t *ops.HueTask, ls lights.Set)
}

Interface HueTaskBeginner can begin a hue task. MultiExecutor implements this interface.

type HueTaskWrapper

type HueTaskWrapper struct {
	// The hue task
	H *ops.HueTask

	// Empty set means all lights
	Ls lights.Set
	// contains filtered or unexported fields
}

HueTaskWrapper represents a hue task bound to a context and a light set. Implements Task.

func (*HueTaskWrapper) ConflictsWith

func (t *HueTaskWrapper) ConflictsWith(other Task) bool

func (*HueTaskWrapper) Do

func (t *HueTaskWrapper) Do(e *tasks.Execution)

Do performs the task

func (*HueTaskWrapper) String

func (t *HueTaskWrapper) String() string

func (*HueTaskWrapper) TaskId

func (t *HueTaskWrapper) TaskId() string

TaskId is a combination of the hue task Id and the light set.

type LightReaderWriter

type LightReaderWriter interface {
	ops.Context
	ops.LightReader
}

Interface LightReaderWriter can both read and update the state of lights

type MultiExecutor

type MultiExecutor struct {
	// contains filtered or unexported fields
}

MultiExecutor executes hue tasks while ensuring that no more than one task is controlling any given light. MultiExecutor is safe to use with multiple goroutines.

func NewMultiExecutor

func NewMultiExecutor(c ops.Context, hlog *log.Logger) *MultiExecutor

NewMultiExecutor creates a new MultiExecutor instance. c is the connection to the hue bridge. c should implement all the methods beyond the Context interface that HueTask instances passed to Start and MaybeStart need. If a HueTask needs a method that c does not implement then it does nothing. hlog captures the start of each HueTask along with its ending or interruption.

func NewNamedMultiExecutor

func NewNamedMultiExecutor(
	name string, c ops.Context, hlog *log.Logger) *MultiExecutor

NewNamedMultiExecutor works like NewMultiExecutor except that it creates a named MultiExecutor instance. The name appears in the execution logs.

func (*MultiExecutor) Begin

func (m *MultiExecutor) Begin(
	h *ops.HueTask, lightSet lights.Set)

Begin is a synonym for Start. Needed to implement HueTaskBeginner.

func (*MultiExecutor) Close

func (m *MultiExecutor) Close() error

Close closes resources associated with this instance and interrupts all running tasks in this instance.

func (*MultiExecutor) MaybeStart

func (m *MultiExecutor) MaybeStart(
	h *ops.HueTask, lightSet lights.Set) *tasks.Execution

MaybeStart is like Start but avoids interrupting running tasks by either not running h or by running h on a subset of the lights in lightSet.

func (*MultiExecutor) Pause

func (m *MultiExecutor) Pause()

Pause pauses this executor waiting for all tasks to actually stop. Pause() and Resume() must be called from the same goroutine. Calling Pause() and Resume() concurrently from different goroutines causes undefined behavior and may cause Pause() to block indefinitely.

func (*MultiExecutor) Resume

func (m *MultiExecutor) Resume()

Resume resumes this executor. Pause() and Resume() must be called from the same goroutine. Calling Pause() and Resume() concurrently from different goroutines causes undefined behavior and may cause Pause() to block indefinitely.

func (*MultiExecutor) Start

func (m *MultiExecutor) Start(
	h *ops.HueTask, lightSet lights.Set) *tasks.Execution

Start starts a task for a suggested set of lights. Start interrupts any running task using the lights that h needs before starting h. Start returns the execution of h.

func (*MultiExecutor) Stop

func (m *MultiExecutor) Stop(taskId string)

Stop stops a particular task. taskId is the ID of the task as returned by HueTaskWrapper.TaskId().

func (*MultiExecutor) Tasks

func (m *MultiExecutor) Tasks() []*HueTaskWrapper

Tasks returns the current HueTasks being run

type MultiTimer

type MultiTimer struct {
	// contains filtered or unexported fields
}

MultiTimer schedules hue tasks to run at certain times. MultiTimer is safe to use wit multiple goroutines.

func NewMultiTimer

func NewMultiTimer(executor HueTaskBeginner) *MultiTimer

NewMultiTimer creates a new MultiTimer. executor is the MultiExecutor to which this instance will send hue tasks.

func NewMultiTimerWithStore

func NewMultiTimerWithStore(
	executor HueTaskBeginner, store AtTimeTaskStore) *MultiTimer

NewMultiTimerWithStore creates a new MultiTimer. executor is the MultiExecutor to which this instance will send hue tasks. store handles the persistent storage of tasks.

func NewMultiTimerWithStoreAndClock

func NewMultiTimerWithStoreAndClock(
	executor HueTaskBeginner,
	store AtTimeTaskStore,
	clock tasks.Clock) *MultiTimer

NewMultiTimerWithStoreAndClock provides a caller supplied clock for testing

func (*MultiTimer) Cancel

func (m *MultiTimer) Cancel(taskId string)

Cancel cancels a scheduled task. scheduleId comes from TimerTaskWrapper.TaskId() and identifies the scheduling of a task. This ID is different from the ID of a running task

func (*MultiTimer) FindByScheduleId

func (m *MultiTimer) FindByScheduleId(scheduleId string) *tasks.Execution

FindByScheduleId returns the execution that controls the scheduling of a task. scheduleId identifies the scheduling of the task and comes from TimerTaskWrapper.TaskId() which is different from the ID of a running task.

func (*MultiTimer) Schedule

func (m *MultiTimer) Schedule(
	h *ops.HueTask, lightSet lights.Set, startTime time.Time)

Schedule schedules a hue task to be run. h is the hue task; lightSet is suggested set of lights for which the task should run; startTime is the time that the hue task should run.

func (*MultiTimer) Scheduled

func (m *MultiTimer) Scheduled() []*TimerTaskWrapper

Scheduled returns the tasks scheduled to be run.

type Recurring

type Recurring struct {
	Id int
	recurring.R
	Description string
}

Recurring represents recurring time with an ID and description. These instances must be treated as immutable.

type ScheduledTask

type ScheduledTask struct {
	// ID of the scheduled task
	Id int
	// Description of the scheduled task
	Description string
	// Requested lights for scheduled task
	Lights lights.Set
	// When to run. nil means running always.
	Times *Recurring
	// If false this scheduled task won't interrupt already running tasks.
	HighPriority bool
	*BackgroundRunner
}

ScheduledTask represents a scheduled task that runs periodically to operate hue lights. ScheduledTask instances should not be copied via assignment operator. Multiple goroutines can enable or disable this scheduled task via its BackgroundRunner. Other than that, ScheduledTask instances are to be treated as immutable.

func HueTaskToScheduledTask

func HueTaskToScheduledTask(
	id int,
	h FutureHueTask,
	lightSet lights.Set,
	r *Recurring,
	hiPriority bool,
	te *MultiExecutor) *ScheduledTask

HueTaskToScheduledTask creates a ScheduledTask from a FutureHueTask. id is the id of the new ScheduledTask. h is the FutureHueTask. lightSet is the lights h is to run on. r is when h should run. hiPriority is true if h should preempt other tasks when run. te is what runs h.

func TaskToScheduledTask

func TaskToScheduledTask(
	id int,
	description string,
	r *Recurring,
	task tasks.Task) *ScheduledTask

TaskToScheduledTask creates a ScheduledTask from an ordinary task. id is the id of the new HueTaskToScheduledTask. description is a description for task. r is when task should run. If nil, task runs all the time. task is the original task.

type ScheduledTaskList

type ScheduledTaskList []*ScheduledTask

ScheduledTaskList represents an immutable list of scheduled tasks.

func (ScheduledTaskList) ToMap

func (l ScheduledTaskList) ToMap() map[int]*ScheduledTask

ToMap returns this ScheduledTaskList as a map keyed by Id

type Stack

type Stack struct {
	Base  *MultiExecutor
	Extra *MultiExecutor
	// All the lights that this instance controls
	AllLights lights.Set
	// contains filtered or unexported fields
}

Stack consists of two MultiExecutors: the main one, Base, and an extra one Extra. Calling Push pauses Base, saves the state of the lights and resumes Extra. Then Extra can be used to run programs without messing up what was running in Base. Finally call Pop to pause Extra, restore the lights and resume Base as if no programs were ever run on Extra. Stack can be safely used with multiple goroutines.

func NewStack

func NewStack(
	base, extra *MultiExecutor,
	context LightReaderWriter,
	allLights lights.Set,
	slog *log.Logger) *Stack

NewStack creates a new Stack instance.

func (*Stack) Pop

func (s *Stack) Pop()

func (*Stack) Push

func (s *Stack) Push()

type Task

type Task interface {
	tasks.Task

	// Returns true if this instance conflicts with other.
	ConflictsWith(other Task) bool

	// Returns the task ID of this instance.
	TaskId() string
}

Task represents a Task that works with TaskCollection

type TaskCollection

type TaskCollection struct {
	// contains filtered or unexported fields
}

TaskCollection represents running tasks and implements tasks.TaskCollection. It adds the Tasks method to get all running tasks and the FindByTaskId method to find the execution of a particular task.

func (*TaskCollection) Add

func (c *TaskCollection) Add(t tasks.Task, e *tasks.Execution)

func (*TaskCollection) Conflicts

func (c *TaskCollection) Conflicts(t tasks.Task) []*tasks.Execution

func (*TaskCollection) FindByTaskId

func (c *TaskCollection) FindByTaskId(taskId string) *tasks.Execution

FindByTaskId returns the execution of a particular task or nil if that task is not found.

func (*TaskCollection) Remove

func (c *TaskCollection) Remove(t tasks.Task)

func (*TaskCollection) Tasks

func (c *TaskCollection) Tasks(aSlicePtr interface{})

Gets all running tasks. aSlicePtr points to the slice to hold the running tasks.

type TimerTaskWrapper

type TimerTaskWrapper struct {

	// The hue task
	H *ops.HueTask

	// Empty set means all lights
	Ls lights.Set

	// The time to start
	StartTime time.Time
	// contains filtered or unexported fields
}

TimerTaskWrapper represents a hue task bound to a light set to start at a particular time. Implements Task.

func (*TimerTaskWrapper) ConflictsWith

func (t *TimerTaskWrapper) ConflictsWith(other Task) bool

func (*TimerTaskWrapper) Do

func (t *TimerTaskWrapper) Do(e *tasks.Execution)

func (*TimerTaskWrapper) TaskId

func (t *TimerTaskWrapper) TaskId() string

TaskId is combination of hue task Id, light set, and start time

func (*TimerTaskWrapper) TimeLeft

func (t *TimerTaskWrapper) TimeLeft(now time.Time) time.Duration

TimeLeft returns the time left before the hue task starts

func (*TimerTaskWrapper) TimeLeftStr

func (t *TimerTaskWrapper) TimeLeftStr(now time.Time) string

TimeLeftStr returns the time left before the hue task starts as m:ss

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL