aboutsummaryrefslogtreecommitdiff
path: root/schedule.go
blob: 50755efceeb706a1c6214c9165c2fefe01820ef3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/*
* schedule.go
* GoHue library for Philips Hue
* Copyright (C) 2016 Collin Guarino (Collinux) collin.guarino@gmail.com
* License: GPL version 2 or higher http://www.gnu.org/licenses/gpl.html
 */
// http://www.developers.meethue.com/documentation/schedules-api-0

package hue

import (
	"encoding/json"
	"fmt"
)

// Schedule struct defines attributes of Alarms and Timers
type Schedule struct {
	Name        string `json:"name"`
	Description string `json:"description"`
	Command     struct {
		Address string `json:"address"`
		Body    struct {
			Scene string `json:"scene"`
		} `json:"body"`
		Method string `json:"method"`
	} `json:"command"`
	Localtime  string `json:"localtime"`
	Time       string `json:"time"`
	Created    string `json:"created"`
	Status     string `json:"status"`
	Autodelete bool   `json:"autodelete"`
	ID         string
}

// Bridge.GetAllSchedules gets Alarms and Timers in a Schedule struct.
func (bridge *Bridge) GetAllSchedules() ([]Schedule, error) {
	uri := fmt.Sprintf("/api/%s/schedules", bridge.Username)
	body, _, err := bridge.Get(uri)
	if err != nil {
		return []Schedule{}, err
	}

	// Each index key is the topmost element of the json array.
	// Unmarshal the array, loop through each index key, and add it to the list
	schedules := map[string]Schedule{}
	err = json.Unmarshal(body, &schedules)
	if err != nil {
		return []Schedule{}, err
	}
	scheduleList := []Schedule{}
	for key, value := range schedules {
		schedule := Schedule{}
		schedule = value
		schedule.ID = key
		scheduleList = append(scheduleList, schedule)
	}
	return scheduleList, nil
}

// Bridge.GetSchedule gets the attributes for an individual schedule.
// This is used to optimize time when updating the state of a schedule item.
// Note: The ID is not an index, it's a unique key generated for each schedule.
func (bridge *Bridge) GetSchedule(id string) (Schedule, error) {
	uri := fmt.Sprintf("/api/%s/schedules/%s", bridge.Username, id)
	body, _, err := bridge.Get(uri)
	if err != nil {
		return Schedule{}, err
	}

	schedule := Schedule{}
	err = json.Unmarshal(body, &schedule)
	if err != nil {
		return Schedule{}, err
	}
	return schedule, nil
}

// TODO: NOT TESTED, NOT FULLY IMPLEMENTED
func (bridge *Bridge) CreateSchedule(schedule Schedule) error {
	uri := fmt.Sprintf("/api/%s/schedules", bridge.Username)
	body, _, err := bridge.Post(uri, schedule)
	if err != nil {
		return err
	}

	fmt.Println("CREATE SCHEDULE BODY: ", string(body))
	return nil
}

// func (schedule *Schedule) Disable() {
//
// }
//
// func (schedule *Schedule) Enable() {
//
// }
//
//
// func (bridge *Bridge) GetSchedule(index int) (interface{}, error) {
//     return []interface{}, nil
// }
//
// func (bridge *Bridge) SetSchedule(index int, schedule interface{}) error {
//     return nil
// }
//
// func (bridge *Bridge) DeleteSchedule(index int) error {
//     return nil
// }