Compare commits
2 Commits
0.1.0
...
f63c90db2b
Author | SHA1 | Date | |
---|---|---|---|
f63c90db2b
|
|||
1a99e03f88
|
3
.gitignore
vendored
3
.gitignore
vendored
@ -16,6 +16,3 @@
|
|||||||
# vendor/
|
# vendor/
|
||||||
|
|
||||||
.lumerc
|
.lumerc
|
||||||
.vscode/configurationCache.log
|
|
||||||
.vscode/dryrun.log
|
|
||||||
.vscode/targets.log
|
|
||||||
|
312
LICENSE
312
LICENSE
@ -1,312 +0,0 @@
|
|||||||
Mozilla Public License Version 2.0
|
|
||||||
|
|
||||||
1. Definitions
|
|
||||||
|
|
||||||
1.1. "Contributor" means each individual or legal entity that creates, contributes
|
|
||||||
to the creation of, or owns Covered Software.
|
|
||||||
|
|
||||||
1.2. "Contributor Version" means the combination of the Contributions of others
|
|
||||||
(if any) used by a Contributor and that particular Contributor's Contribution.
|
|
||||||
|
|
||||||
1.3. "Contribution" means Covered Software of a particular Contributor.
|
|
||||||
|
|
||||||
1.4. "Covered Software" means Source Code Form to which the initial Contributor
|
|
||||||
has attached the notice in Exhibit A, the Executable Form of such Source Code
|
|
||||||
Form, and Modifications of such Source Code Form, in each case including portions
|
|
||||||
thereof.
|
|
||||||
|
|
||||||
1.5. "Incompatible With Secondary Licenses" means
|
|
||||||
|
|
||||||
(a) that the initial Contributor has attached the notice described in Exhibit
|
|
||||||
B to the Covered Software; or
|
|
||||||
|
|
||||||
(b) that the Covered Software was made available under the terms of version
|
|
||||||
1.1 or earlier of the License, but not also under the terms of a Secondary
|
|
||||||
License.
|
|
||||||
|
|
||||||
1.6. "Executable Form" means any form of the work other than Source Code Form.
|
|
||||||
|
|
||||||
1.7. "Larger Work" means a work that combines Covered Software with other
|
|
||||||
material, in a separate file or files, that is not Covered Software.
|
|
||||||
|
|
||||||
1.8. "License" means this document.
|
|
||||||
|
|
||||||
1.9. "Licensable" means having the right to grant, to the maximum extent possible,
|
|
||||||
whether at the time of the initial grant or subsequently, any and all of the
|
|
||||||
rights conveyed by this License.
|
|
||||||
|
|
||||||
1.10. "Modifications" means any of the following:
|
|
||||||
|
|
||||||
(a) any file in Source Code Form that results from an addition to, deletion
|
|
||||||
from, or modification of the contents of Covered Software; or
|
|
||||||
|
|
||||||
(b) any new file in Source Code Form that contains any Covered Software.
|
|
||||||
|
|
||||||
1.11. "Patent Claims" of a Contributor means any patent claim(s), including
|
|
||||||
without limitation, method, process, and apparatus claims, in any patent Licensable
|
|
||||||
by such Contributor that would be infringed, but for the grant of the License,
|
|
||||||
by the making, using, selling, offering for sale, having made, import, or
|
|
||||||
transfer of either its Contributions or its Contributor Version.
|
|
||||||
|
|
||||||
1.12. "Secondary License" means either the GNU General Public License, Version
|
|
||||||
2.0, the GNU Lesser General Public License, Version 2.1, the GNU Affero General
|
|
||||||
Public License, Version 3.0, or any later versions of those licenses.
|
|
||||||
|
|
||||||
1.13. "Source Code Form" means the form of the work preferred for making modifications.
|
|
||||||
|
|
||||||
1.14. "You" (or "Your") means an individual or a legal entity exercising rights
|
|
||||||
under this License. For legal entities, "You" includes any entity that controls,
|
|
||||||
is controlled by, or is under common control with You. For purposes of this
|
|
||||||
definition, "control" means (a) the power, direct or indirect, to cause the
|
|
||||||
direction or management of such entity, whether by contract or otherwise,
|
|
||||||
or (b) ownership of more than fifty percent (50%) of the outstanding shares
|
|
||||||
or beneficial ownership of such entity.
|
|
||||||
|
|
||||||
2. License Grants and Conditions
|
|
||||||
|
|
||||||
2.1. Grants
|
|
||||||
|
|
||||||
Each Contributor hereby grants You a world-wide, royalty-free, non-exclusive
|
|
||||||
license:
|
|
||||||
|
|
||||||
(a) under intellectual property rights (other than patent or trademark) Licensable
|
|
||||||
by such Contributor to use, reproduce, make available, modify, display, perform,
|
|
||||||
distribute, and otherwise exploit its Contributions, either on an unmodified
|
|
||||||
basis, with Modifications, or as part of a Larger Work; and
|
|
||||||
|
|
||||||
(b) under Patent Claims of such Contributor to make, use, sell, offer for
|
|
||||||
sale, have made, import, and otherwise transfer either its Contributions or
|
|
||||||
its Contributor Version.
|
|
||||||
|
|
||||||
2.2. Effective Date
|
|
||||||
|
|
||||||
The licenses granted in Section 2.1 with respect to any Contribution become
|
|
||||||
effective for each Contribution on the date the Contributor first distributes
|
|
||||||
such Contribution.
|
|
||||||
|
|
||||||
2.3. Limitations on Grant Scope
|
|
||||||
|
|
||||||
The licenses granted in this Section 2 are the only rights granted under this
|
|
||||||
License. No additional rights or licenses will be implied from the distribution
|
|
||||||
or licensing of Covered Software under this License. Notwithstanding Section
|
|
||||||
2.1(b) above, no patent license is granted by a Contributor:
|
|
||||||
|
|
||||||
(a) for any code that a Contributor has removed from Covered Software; or
|
|
||||||
|
|
||||||
(b) for infringements caused by: (i) Your and any other third party's modifications
|
|
||||||
of Covered Software, or (ii) the combination of its Contributions with other
|
|
||||||
software (except as part of its Contributor Version); or
|
|
||||||
|
|
||||||
(c) under Patent Claims infringed by Covered Software in the absence of its
|
|
||||||
Contributions.
|
|
||||||
|
|
||||||
This License does not grant any rights in the trademarks, service marks, or
|
|
||||||
logos of any Contributor (except as may be necessary to comply with the notice
|
|
||||||
requirements in Section 3.4).
|
|
||||||
|
|
||||||
2.4. Subsequent Licenses
|
|
||||||
|
|
||||||
No Contributor makes additional grants as a result of Your choice to distribute
|
|
||||||
the Covered Software under a subsequent version of this License (see Section
|
|
||||||
10.2) or under the terms of a Secondary License (if permitted under the terms
|
|
||||||
of Section 3.3).
|
|
||||||
|
|
||||||
2.5. Representation
|
|
||||||
|
|
||||||
Each Contributor represents that the Contributor believes its Contributions
|
|
||||||
are its original creation(s) or it has sufficient rights to grant the rights
|
|
||||||
to its Contributions conveyed by this License.
|
|
||||||
|
|
||||||
2.6. Fair Use
|
|
||||||
|
|
||||||
This License is not intended to limit any rights You have under applicable
|
|
||||||
copyright doctrines of fair use, fair dealing, or other equivalents.
|
|
||||||
|
|
||||||
2.7. Conditions
|
|
||||||
|
|
||||||
Sections 3.1, 3.2, 3.3, and 3.4 are conditions of the licenses granted in
|
|
||||||
Section 2.1.
|
|
||||||
|
|
||||||
3. Responsibilities
|
|
||||||
|
|
||||||
3.1. Distribution of Source Form
|
|
||||||
|
|
||||||
All distribution of Covered Software in Source Code Form, including any Modifications
|
|
||||||
that You create or to which You contribute, must be under the terms of this
|
|
||||||
License. You must inform recipients that the Source Code Form of the Covered
|
|
||||||
Software is governed by the terms of this License, and how they can obtain
|
|
||||||
a copy of this License. You may not attempt to alter or restrict the recipients'
|
|
||||||
rights in the Source Code Form.
|
|
||||||
|
|
||||||
3.2. Distribution of Executable Form
|
|
||||||
|
|
||||||
If You distribute Covered Software in Executable Form then:
|
|
||||||
|
|
||||||
(a) such Covered Software must also be made available in Source Code Form,
|
|
||||||
as described in Section 3.1, and You must inform recipients of the Executable
|
|
||||||
Form how they can obtain a copy of such Source Code Form by reasonable means
|
|
||||||
in a timely manner, at a charge no more than the cost of distribution to the
|
|
||||||
recipient; and
|
|
||||||
|
|
||||||
(b) You may distribute such Executable Form under the terms of this License,
|
|
||||||
or sublicense it under different terms, provided that the license for the
|
|
||||||
Executable Form does not attempt to limit or alter the recipients' rights
|
|
||||||
in the Source Code Form under this License.
|
|
||||||
|
|
||||||
3.3. Distribution of a Larger Work
|
|
||||||
|
|
||||||
You may create and distribute a Larger Work under terms of Your choice, provided
|
|
||||||
that You also comply with the requirements of this License for the Covered
|
|
||||||
Software. If the Larger Work is a combination of Covered Software with a work
|
|
||||||
governed by one or more Secondary Licenses, and the Covered Software is not
|
|
||||||
Incompatible With Secondary Licenses, this License permits You to additionally
|
|
||||||
distribute such Covered Software under the terms of such Secondary License(s),
|
|
||||||
so that the recipient of the Larger Work may, at their option, further distribute
|
|
||||||
the Covered Software under the terms of either this License or such Secondary
|
|
||||||
License(s).
|
|
||||||
|
|
||||||
3.4. Notices
|
|
||||||
|
|
||||||
You may not remove or alter the substance of any license notices (including
|
|
||||||
copyright notices, patent notices, disclaimers of warranty, or limitations
|
|
||||||
of liability) contained within the Source Code Form of the Covered Software,
|
|
||||||
except that You may alter any license notices to the extent required to remedy
|
|
||||||
known factual inaccuracies.
|
|
||||||
|
|
||||||
3.5. Application of Additional Terms
|
|
||||||
|
|
||||||
You may choose to offer, and to charge a fee for, warranty, support, indemnity
|
|
||||||
or liability obligations to one or more recipients of Covered Software. However,
|
|
||||||
You may do so only on Your own behalf, and not on behalf of any Contributor.
|
|
||||||
You must make it absolutely clear that any such warranty, support, indemnity,
|
|
||||||
or liability obligation is offered by You alone, and You hereby agree to indemnify
|
|
||||||
every Contributor for any liability incurred by such Contributor as a result
|
|
||||||
of warranty, support, indemnity or liability terms You offer. You may include
|
|
||||||
additional disclaimers of warranty and limitations of liability specific to
|
|
||||||
any jurisdiction.
|
|
||||||
|
|
||||||
4. Inability to Comply Due to Statute or Regulation
|
|
||||||
|
|
||||||
If it is impossible for You to comply with any of the terms of this License
|
|
||||||
with respect to some or all of the Covered Software due to statute, judicial
|
|
||||||
order, or regulation then You must: (a) comply with the terms of this License
|
|
||||||
to the maximum extent possible; and (b) describe the limitations and the code
|
|
||||||
they affect. Such description must be placed in a text file included with
|
|
||||||
all distributions of the Covered Software under this License. Except to the
|
|
||||||
extent prohibited by statute or regulation, such description must be sufficiently
|
|
||||||
detailed for a recipient of ordinary skill to be able to understand it.
|
|
||||||
|
|
||||||
5. Termination
|
|
||||||
|
|
||||||
5.1. The rights granted under this License will terminate automatically if
|
|
||||||
You fail to comply with any of its terms. However, if You become compliant,
|
|
||||||
then the rights granted under this License from a particular Contributor are
|
|
||||||
reinstated (a) provisionally, unless and until such Contributor explicitly
|
|
||||||
and finally terminates Your grants, and (b) on an ongoing basis, if such Contributor
|
|
||||||
fails to notify You of the non-compliance by some reasonable means prior to
|
|
||||||
60 days after You have come back into compliance. Moreover, Your grants from
|
|
||||||
a particular Contributor are reinstated on an ongoing basis if such Contributor
|
|
||||||
notifies You of the non-compliance by some reasonable means, this is the first
|
|
||||||
time You have received notice of non-compliance with this License from such
|
|
||||||
Contributor, and You become compliant prior to 30 days after Your receipt
|
|
||||||
of the notice.
|
|
||||||
|
|
||||||
5.2. If You initiate litigation against any entity by asserting a patent infringement
|
|
||||||
claim (excluding declaratory judgment actions, counter-claims, and cross-claims)
|
|
||||||
alleging that a Contributor Version directly or indirectly infringes any patent,
|
|
||||||
then the rights granted to You by any and all Contributors for the Covered
|
|
||||||
Software under Section 2.1 of this License shall terminate.
|
|
||||||
|
|
||||||
5.3. In the event of termination under Sections 5.1 or 5.2 above, all end
|
|
||||||
user license agreements (excluding distributors and resellers) which have
|
|
||||||
been validly granted by You or Your distributors under this License prior
|
|
||||||
to termination shall survive termination.
|
|
||||||
|
|
||||||
6. Disclaimer of Warranty
|
|
||||||
|
|
||||||
Covered Software is provided under this License on an "as is" basis, without
|
|
||||||
warranty of any kind, either expressed, implied, or statutory, including,
|
|
||||||
without limitation, warranties that the Covered Software is free of defects,
|
|
||||||
merchantable, fit for a particular purpose or non-infringing. The entire risk
|
|
||||||
as to the quality and performance of the Covered Software is with You. Should
|
|
||||||
any Covered Software prove defective in any respect, You (not any Contributor)
|
|
||||||
assume the cost of any necessary servicing, repair, or correction. This disclaimer
|
|
||||||
of warranty constitutes an essential part of this License. No use of any Covered
|
|
||||||
Software is authorized under this License except under this disclaimer.
|
|
||||||
|
|
||||||
7. Limitation of Liability
|
|
||||||
|
|
||||||
Under no circumstances and under no legal theory, whether tort (including
|
|
||||||
negligence), contract, or otherwise, shall any Contributor, or anyone who
|
|
||||||
distributes Covered Software as permitted above, be liable to You for any
|
|
||||||
direct, indirect, special, incidental, or consequential damages of any character
|
|
||||||
including, without limitation, damages for lost profits, loss of goodwill,
|
|
||||||
work stoppage, computer failure or malfunction, or any and all other commercial
|
|
||||||
damages or losses, even if such party shall have been informed of the possibility
|
|
||||||
of such damages. This limitation of liability shall not apply to liability
|
|
||||||
for death or personal injury resulting from such party's negligence to the
|
|
||||||
extent applicable law prohibits such limitation. Some jurisdictions do not
|
|
||||||
allow the exclusion or limitation of incidental or consequential damages,
|
|
||||||
so this exclusion and limitation may not apply to You.
|
|
||||||
|
|
||||||
8. Litigation
|
|
||||||
|
|
||||||
Any litigation relating to this License may be brought only in the courts
|
|
||||||
of a jurisdiction where the defendant maintains its principal place of business
|
|
||||||
and such litigation shall be governed by laws of that jurisdiction, without
|
|
||||||
reference to its conflict-of-law provisions. Nothing in this Section shall
|
|
||||||
prevent a party's ability to bring cross-claims or counter-claims.
|
|
||||||
|
|
||||||
9. Miscellaneous
|
|
||||||
|
|
||||||
This License represents the complete agreement concerning the subject matter
|
|
||||||
hereof. If any provision of this License is held to be unenforceable, such
|
|
||||||
provision shall be reformed only to the extent necessary to make it enforceable.
|
|
||||||
Any law or regulation which provides that the language of a contract shall
|
|
||||||
be construed against the drafter shall not be used to construe this License
|
|
||||||
against a Contributor.
|
|
||||||
|
|
||||||
10. Versions of the License
|
|
||||||
|
|
||||||
10.1. New Versions
|
|
||||||
|
|
||||||
Mozilla Foundation is the license steward. Except as provided in Section 10.3,
|
|
||||||
no one other than the license steward has the right to modify or publish new
|
|
||||||
versions of this License. Each version will be given a distinguishing version
|
|
||||||
number.
|
|
||||||
|
|
||||||
10.2. Effect of New Versions
|
|
||||||
|
|
||||||
You may distribute the Covered Software under the terms of the version of
|
|
||||||
the License under which You originally received the Covered Software, or under
|
|
||||||
the terms of any subsequent version published by the license steward.
|
|
||||||
|
|
||||||
10.3. Modified Versions
|
|
||||||
|
|
||||||
If you create software not governed by this License, and you want to create
|
|
||||||
a new license for such software, you may create and use a modified version
|
|
||||||
of this License if you rename the license and remove any references to the
|
|
||||||
name of the license steward (except to note that such modified license differs
|
|
||||||
from this License).
|
|
||||||
|
|
||||||
10.4. Distributing Source Code Form that is Incompatible With Secondary Licenses
|
|
||||||
|
|
||||||
If You choose to distribute Source Code Form that is Incompatible With Secondary
|
|
||||||
Licenses under the terms of this version of the License, the notice described
|
|
||||||
in Exhibit B of this License must be attached. Exhibit A - Source Code Form
|
|
||||||
License Notice
|
|
||||||
|
|
||||||
This Source Code Form is subject to the terms of the Mozilla Public License,
|
|
||||||
v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain
|
|
||||||
one at http://mozilla.org/MPL/2.0/.
|
|
||||||
|
|
||||||
If it is not possible or desirable to put the notice in a particular file,
|
|
||||||
then You may include the notice in a location (such as a LICENSE file in a
|
|
||||||
relevant directory) where a recipient would be likely to look for such a notice.
|
|
||||||
|
|
||||||
You may add additional accurate notices of copyright ownership.
|
|
||||||
|
|
||||||
Exhibit B - "Incompatible With Secondary Licenses" Notice
|
|
||||||
|
|
||||||
This Source Code Form is "Incompatible With Secondary Licenses", as defined
|
|
||||||
by the Mozilla Public License, v. 2.0.
|
|
24
Makefile
24
Makefile
@ -1,27 +1,7 @@
|
|||||||
V ?= 0
|
|
||||||
Q = $(if $(filter 1, $V),, @)
|
|
||||||
|
|
||||||
ifeq ($(OS), Windows_NT)
|
|
||||||
EXE=lume.exe
|
|
||||||
RM=del /f /q
|
|
||||||
BUILD_DATE=$(shell powershell Get-Date -Format "yyyy-MM-ddThh:mm:sszzz")
|
|
||||||
else
|
|
||||||
EXE=lume
|
|
||||||
EXE=rm -f
|
|
||||||
BUILD_DATE=$(shell date --iso-8601=seconds)
|
|
||||||
endif
|
|
||||||
|
|
||||||
LUME_VERSION ?= $(shell git describe --tags --always)
|
|
||||||
GIT_COMMIT := $(shell git rev-parse --short HEAD)
|
|
||||||
LDFLAGS := $(LDFLAGS) \
|
|
||||||
-X git.kill0.net/chill9/lume/cmd.Version=$(LUME_VERSION) \
|
|
||||||
-X git.kill0.net/chill9/lume/cmd.BuildDate=$(BUILD_DATE) \
|
|
||||||
-X git.kill0.net/chill9/lume/cmd.GitCommit=$(GIT_COMMIT)
|
|
||||||
|
|
||||||
.PHONY: build
|
.PHONY: build
|
||||||
build:
|
build:
|
||||||
$(Q) go build -o $(EXE) -ldflags="$(LDFLAGS)" ./cmd/lume
|
go build -o lume ./cmd/lume
|
||||||
|
|
||||||
.PHONY: clean
|
.PHONY: clean
|
||||||
clean:
|
clean:
|
||||||
$(Q) $(RM) $(EXE)
|
rm -f ./lifx
|
||||||
|
309
client.go
Normal file
309
client.go
Normal file
@ -0,0 +1,309 @@
|
|||||||
|
package lifx
|
||||||
|
|
||||||
|
import (
|
||||||
|
//"crypto/tls"
|
||||||
|
"bytes"
|
||||||
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"net/http"
|
||||||
|
"net/url"
|
||||||
|
"strconv"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
const UserAgent = "lume"
|
||||||
|
|
||||||
|
type (
|
||||||
|
Client struct {
|
||||||
|
accessToken string
|
||||||
|
Client *http.Client
|
||||||
|
}
|
||||||
|
|
||||||
|
Result struct {
|
||||||
|
Id string `json:"id"`
|
||||||
|
Label string `json:"label"`
|
||||||
|
Status Status `json:"status"`
|
||||||
|
}
|
||||||
|
|
||||||
|
Error struct {
|
||||||
|
Field string `json:"field"`
|
||||||
|
Message []string `json:"message"`
|
||||||
|
}
|
||||||
|
|
||||||
|
Warning struct {
|
||||||
|
Warning string `json:"warning"`
|
||||||
|
}
|
||||||
|
|
||||||
|
RateLimit struct {
|
||||||
|
Limit int
|
||||||
|
Remaining int
|
||||||
|
Reset time.Time
|
||||||
|
}
|
||||||
|
|
||||||
|
Response struct {
|
||||||
|
StatusCode int
|
||||||
|
Header http.Header
|
||||||
|
Body io.ReadCloser
|
||||||
|
RateLimit RateLimit
|
||||||
|
}
|
||||||
|
|
||||||
|
LifxResponse struct {
|
||||||
|
Error string `json:"error"`
|
||||||
|
Errors []Error `json:"errors"`
|
||||||
|
Warnings []Warning `json:"warnings"`
|
||||||
|
Results []Result `json:"results"`
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
var errorMap = map[int]error{
|
||||||
|
http.StatusNotFound: errors.New("Selector did not match any lights"),
|
||||||
|
http.StatusUnauthorized: errors.New("Bad access token"),
|
||||||
|
http.StatusForbidden: errors.New("Bad OAuth scope"),
|
||||||
|
http.StatusUnprocessableEntity: errors.New("Missing or malformed parameters"),
|
||||||
|
http.StatusUpgradeRequired: errors.New("HTTP was used to make the request instead of HTTPS. Repeat the request using HTTPS instead"),
|
||||||
|
http.StatusTooManyRequests: errors.New("The request exceeded a rate limit"),
|
||||||
|
http.StatusInternalServerError: errors.New("Something went wrong on LIFX's end"),
|
||||||
|
http.StatusBadGateway: errors.New("Something went wrong on LIFX's end"),
|
||||||
|
http.StatusServiceUnavailable: errors.New("Something went wrong on LIFX's end"),
|
||||||
|
523: errors.New("Something went wrong on LIFX's end"),
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewClient(accessToken string) *Client {
|
||||||
|
tr := &http.Transport{
|
||||||
|
//TLSNextProto: make(map[string]func(authority string, c *tls.Conn) http.RoundTripper),
|
||||||
|
}
|
||||||
|
return &Client{
|
||||||
|
accessToken: accessToken,
|
||||||
|
Client: &http.Client{Transport: tr},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewResponse(r *http.Response) (*Response, error) {
|
||||||
|
resp := Response{
|
||||||
|
StatusCode: r.StatusCode,
|
||||||
|
Header: r.Header,
|
||||||
|
Body: r.Body,
|
||||||
|
}
|
||||||
|
|
||||||
|
if t := r.Header.Get("X-RateLimit-Limit"); t != "" {
|
||||||
|
if n, err := strconv.ParseInt(t, 10, 32); err == nil {
|
||||||
|
resp.RateLimit.Limit = int(n)
|
||||||
|
} else {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if t := r.Header.Get("X-RateLimit-Remaining"); t != "" {
|
||||||
|
if n, err := strconv.ParseInt(t, 10, 32); err == nil {
|
||||||
|
resp.RateLimit.Remaining = int(n)
|
||||||
|
} else {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if t := r.Header.Get("X-RateLimit-Reset"); t != "" {
|
||||||
|
if n, err := strconv.ParseInt(t, 10, 32); err == nil {
|
||||||
|
resp.RateLimit.Reset = time.Unix(n, 0)
|
||||||
|
} else {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return &resp, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Response) IsError() bool {
|
||||||
|
return r.StatusCode > 299
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Response) GetLifxError() (err error) {
|
||||||
|
var (
|
||||||
|
s *LifxResponse
|
||||||
|
)
|
||||||
|
if err = json.NewDecoder(r.Body).Decode(&s); err != nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return errors.New(s.Error)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Client) NewRequest(method, url string, body io.Reader) (req *http.Request, err error) {
|
||||||
|
req, err = http.NewRequest(method, url, body)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", c.accessToken))
|
||||||
|
req.Header.Add("Content-Type", "application/json")
|
||||||
|
req.Header.Add("User-Agent", UserAgent)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Client) setState(selector string, state State) (*Response, error) {
|
||||||
|
var (
|
||||||
|
err error
|
||||||
|
j []byte
|
||||||
|
req *http.Request
|
||||||
|
r *http.Response
|
||||||
|
resp *Response
|
||||||
|
)
|
||||||
|
|
||||||
|
if j, err = json.Marshal(state); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if req, err = c.NewRequest("PUT", EndpointState(selector), bytes.NewBuffer(j)); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if r, err = c.Client.Do(req); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
resp, err = NewResponse(r)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return resp, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Client) setStates(selector string, states States) (*Response, error) {
|
||||||
|
var (
|
||||||
|
err error
|
||||||
|
j []byte
|
||||||
|
req *http.Request
|
||||||
|
r *http.Response
|
||||||
|
resp *Response
|
||||||
|
)
|
||||||
|
|
||||||
|
if j, err = json.Marshal(states); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if req, err = c.NewRequest("PUT", EndpointStates(), bytes.NewBuffer(j)); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if r, err = c.Client.Do(req); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
resp, err = NewResponse(r)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return resp, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Client) toggle(selector string, duration float64) (*Response, error) {
|
||||||
|
var (
|
||||||
|
err error
|
||||||
|
j []byte
|
||||||
|
req *http.Request
|
||||||
|
r *http.Response
|
||||||
|
resp *Response
|
||||||
|
)
|
||||||
|
|
||||||
|
if j, err = json.Marshal(&Toggle{Duration: duration}); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if req, err = c.NewRequest("POST", EndpointToggle(selector), bytes.NewBuffer(j)); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if r, err = c.Client.Do(req); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
resp, err = NewResponse(r)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return resp, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Client) validateColor(color Color) (*Response, error) {
|
||||||
|
var (
|
||||||
|
err error
|
||||||
|
req *http.Request
|
||||||
|
r *http.Response
|
||||||
|
resp *Response
|
||||||
|
q url.Values
|
||||||
|
)
|
||||||
|
|
||||||
|
if req, err = c.NewRequest("GET", EndpointColor(), nil); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
q = req.URL.Query()
|
||||||
|
q.Set("string", color.ColorString())
|
||||||
|
req.URL.RawQuery = q.Encode()
|
||||||
|
|
||||||
|
if r, err = c.Client.Do(req); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
resp, err = NewResponse(r)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return resp, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Client) listLights(selector string) (*Response, error) {
|
||||||
|
var (
|
||||||
|
err error
|
||||||
|
req *http.Request
|
||||||
|
r *http.Response
|
||||||
|
resp *Response
|
||||||
|
)
|
||||||
|
|
||||||
|
if req, err = c.NewRequest("GET", EndpointListLights(selector), nil); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if r, err = c.Client.Do(req); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
resp, err = NewResponse(r)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return resp, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Client) stateDelta(selector string, delta StateDelta) (*Response, error) {
|
||||||
|
var (
|
||||||
|
err error
|
||||||
|
j []byte
|
||||||
|
req *http.Request
|
||||||
|
r *http.Response
|
||||||
|
resp *Response
|
||||||
|
)
|
||||||
|
|
||||||
|
if j, err = json.Marshal(delta); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if req, err = c.NewRequest("POST", EndpointStateDelta(selector), bytes.NewBuffer(j)); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if r, err = c.Client.Do(req); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
resp, err = NewResponse(r)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return resp, nil
|
||||||
|
}
|
@ -6,7 +6,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
"git.kill0.net/chill9/lifx-go"
|
lifx "git.kill0.net/chill9/lume"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@ -16,16 +16,13 @@ const (
|
|||||||
|
|
||||||
type Config struct {
|
type Config struct {
|
||||||
AccessToken string `toml:"access_token"`
|
AccessToken string `toml:"access_token"`
|
||||||
OutputFormat string `toml:"output_format"`
|
|
||||||
Colors map[string][]float32 `toml:"colors"`
|
Colors map[string][]float32 `toml:"colors"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type CmdArgs struct {
|
type CmdArgs struct {
|
||||||
Flags Flags
|
Flags Flags
|
||||||
Args []string
|
|
||||||
Client *lifx.Client
|
Client *lifx.Client
|
||||||
Config Config
|
Config Config
|
||||||
Name string
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type Flags struct {
|
type Flags struct {
|
||||||
@ -57,7 +54,6 @@ var (
|
|||||||
defaultSaturation string = ""
|
defaultSaturation string = ""
|
||||||
defaultRGB string = ""
|
defaultRGB string = ""
|
||||||
defaultName string = ""
|
defaultName string = ""
|
||||||
defaultOutputFormat string = ""
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func (f Flags) String(name string) string {
|
func (f Flags) String(name string) string {
|
||||||
@ -84,11 +80,12 @@ func (f Flags) Bool(name string) bool {
|
|||||||
return val
|
return val
|
||||||
}
|
}
|
||||||
|
|
||||||
func RegisterCommand(cmd Command) error {
|
func RegisterCommand(name string, cmd Command) error {
|
||||||
if _, ok := commandRegistry[cmd.Name]; ok {
|
if _, ok := commandRegistry[name]; ok {
|
||||||
return fmt.Errorf("%s command is already registered")
|
return fmt.Errorf("%s command is already registered")
|
||||||
}
|
}
|
||||||
commandRegistry[cmd.Name] = cmd
|
cmd.Name = name
|
||||||
|
commandRegistry[name] = cmd
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
23
cmd/help.go
23
cmd/help.go
@ -5,20 +5,13 @@ import (
|
|||||||
"sort"
|
"sort"
|
||||||
)
|
)
|
||||||
|
|
||||||
func NewCmdHelp() Command {
|
|
||||||
return Command{
|
|
||||||
Name: "help",
|
|
||||||
Func: HelpCmd,
|
|
||||||
Use: "<command>",
|
|
||||||
Short: "Show help for a command",
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func HelpCmd(args CmdArgs) (int, error) {
|
func HelpCmd(args CmdArgs) (int, error) {
|
||||||
if len(args.Args) == 0 {
|
argv := args.Flags.Args()
|
||||||
|
|
||||||
|
if len(argv) == 0 {
|
||||||
printHelp(commandRegistry)
|
printHelp(commandRegistry)
|
||||||
} else if len(args.Args) >= 1 {
|
} else if len(argv) >= 1 {
|
||||||
printCmdHelp(args.Args[0])
|
printCmdHelp(argv[0])
|
||||||
}
|
}
|
||||||
|
|
||||||
return ExitSuccess, nil
|
return ExitSuccess, nil
|
||||||
@ -55,15 +48,11 @@ func printCmdHelp(name string) error {
|
|||||||
|
|
||||||
if subCmd.Use != "" {
|
if subCmd.Use != "" {
|
||||||
fmt.Printf("usage:\n lume %s %s\n", subCmd.Name, subCmd.Use)
|
fmt.Printf("usage:\n lume %s %s\n", subCmd.Name, subCmd.Use)
|
||||||
} else {
|
fmt.Println()
|
||||||
fmt.Printf("usage:\n lume %s\n", subCmd.Name)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if subCmd.Flags != nil {
|
|
||||||
fmt.Println()
|
|
||||||
fmt.Print("flags:\n")
|
fmt.Print("flags:\n")
|
||||||
subCmd.Flags.PrintDefaults()
|
subCmd.Flags.PrintDefaults()
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
36
cmd/ls.go
36
cmd/ls.go
@ -1,48 +1,12 @@
|
|||||||
package lumecmd
|
package lumecmd
|
||||||
|
|
||||||
import (
|
|
||||||
"flag"
|
|
||||||
)
|
|
||||||
|
|
||||||
func NewCmdLs() Command {
|
|
||||||
return Command{
|
|
||||||
Name: "ls",
|
|
||||||
Func: LsCmd,
|
|
||||||
Flags: func() *flag.FlagSet {
|
|
||||||
fs := flag.NewFlagSet("ls", flag.ExitOnError)
|
|
||||||
|
|
||||||
selector := fs.String("selector", defaultSelector, "Set the selector")
|
|
||||||
fs.StringVar(selector, "s", defaultSelector, "Set the selector")
|
|
||||||
|
|
||||||
fs.String("format", defaultOutputFormat, "Set the output format")
|
|
||||||
|
|
||||||
return fs
|
|
||||||
}(),
|
|
||||||
Use: "[--selector=<selector>]",
|
|
||||||
Short: "List the lights",
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func LsCmd(args CmdArgs) (int, error) {
|
func LsCmd(args CmdArgs) (int, error) {
|
||||||
c := args.Client
|
c := args.Client
|
||||||
selector := args.Flags.String("selector")
|
selector := args.Flags.String("selector")
|
||||||
format := args.Flags.String("format")
|
|
||||||
|
|
||||||
if format == "" && args.Config.OutputFormat != "" {
|
|
||||||
format = args.Config.OutputFormat
|
|
||||||
}
|
|
||||||
|
|
||||||
lights, err := c.ListLights(selector)
|
lights, err := c.ListLights(selector)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return ExitFailure, err
|
return ExitFailure, err
|
||||||
}
|
}
|
||||||
|
|
||||||
switch format {
|
|
||||||
case "table":
|
|
||||||
PrintLightsTable(lights)
|
|
||||||
default:
|
|
||||||
PrintLights(lights)
|
PrintLights(lights)
|
||||||
}
|
|
||||||
|
|
||||||
return ExitSuccess, nil
|
return ExitSuccess, nil
|
||||||
}
|
}
|
||||||
|
213
cmd/main.go
213
cmd/main.go
@ -6,33 +6,187 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
"strings"
|
|
||||||
|
|
||||||
"git.kill0.net/chill9/lifx-go"
|
lifx "git.kill0.net/chill9/lume"
|
||||||
"github.com/BurntSushi/toml"
|
"github.com/BurntSushi/toml"
|
||||||
)
|
)
|
||||||
|
|
||||||
var userAgent string
|
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
userAgent = initUserAgent()
|
RegisterCommand("help", Command{
|
||||||
|
Func: HelpCmd,
|
||||||
|
Flags: func() *flag.FlagSet {
|
||||||
|
fs := flag.NewFlagSet("help", flag.ExitOnError)
|
||||||
|
|
||||||
RegisterCommand(NewCmdHelp())
|
return fs
|
||||||
RegisterCommand(NewCmdLs())
|
}(),
|
||||||
RegisterCommand(NewCmdPoweroff())
|
Use: "<command>",
|
||||||
RegisterCommand(NewCmdPoweron())
|
Short: "Show help for a command",
|
||||||
RegisterCommand(NewCmdSetColor())
|
})
|
||||||
RegisterCommand(NewCmdSetState())
|
RegisterCommand("ls", Command{
|
||||||
RegisterCommand(NewCmdSetWhite())
|
Func: LsCmd,
|
||||||
RegisterCommand(NewCmdShow())
|
Flags: func() *flag.FlagSet {
|
||||||
RegisterCommand(NewCmdToggle())
|
fs := flag.NewFlagSet("ls", flag.ExitOnError)
|
||||||
RegisterCommand(NewCmdVersion())
|
|
||||||
|
selector := fs.String("selector", defaultSelector, "Set the selector")
|
||||||
|
fs.StringVar(selector, "s", defaultSelector, "Set the selector")
|
||||||
|
|
||||||
|
return fs
|
||||||
|
}(),
|
||||||
|
Use: "[--selector=<selector>]",
|
||||||
|
Short: "List the lights",
|
||||||
|
})
|
||||||
|
RegisterCommand("poweroff", Command{
|
||||||
|
Func: PoweroffCmd,
|
||||||
|
Flags: func() *flag.FlagSet {
|
||||||
|
fs := flag.NewFlagSet("poweroff", flag.ExitOnError)
|
||||||
|
|
||||||
|
duration := fs.Float64("duration", defaultDuration, "Set the duration")
|
||||||
|
fs.Float64Var(duration, "d", defaultDuration, "Set the duration")
|
||||||
|
|
||||||
|
selector := fs.String("selector", defaultSelector, "Set the selector")
|
||||||
|
fs.StringVar(selector, "s", defaultSelector, "Set the selector")
|
||||||
|
|
||||||
|
return fs
|
||||||
|
}(),
|
||||||
|
Use: "[--selector <selector>] [--duration <sec>]",
|
||||||
|
Short: "Power on",
|
||||||
|
})
|
||||||
|
RegisterCommand("poweron", Command{
|
||||||
|
Func: PoweronCmd,
|
||||||
|
Flags: func() *flag.FlagSet {
|
||||||
|
fs := flag.NewFlagSet("poweron", flag.ExitOnError)
|
||||||
|
|
||||||
|
duration := fs.Float64("duration", defaultDuration, "Set the duration")
|
||||||
|
fs.Float64Var(duration, "d", defaultDuration, "Set the duration")
|
||||||
|
|
||||||
|
selector := fs.String("selector", defaultSelector, "Set the selector")
|
||||||
|
fs.StringVar(selector, "s", defaultSelector, "Set the selector")
|
||||||
|
|
||||||
|
return fs
|
||||||
|
}(),
|
||||||
|
Use: "[--selector <selector>] [--duration <sec>]",
|
||||||
|
Short: "Power on",
|
||||||
|
})
|
||||||
|
RegisterCommand("set-color", Command{
|
||||||
|
Func: SetColorCmd,
|
||||||
|
Flags: func() *flag.FlagSet {
|
||||||
|
fs := flag.NewFlagSet("set-color", flag.ExitOnError)
|
||||||
|
|
||||||
|
selector := fs.String("selector", "all", "the selector")
|
||||||
|
fs.StringVar(selector, "s", "all", "the selector")
|
||||||
|
|
||||||
|
power := fs.String("power", defaultPower, "power state")
|
||||||
|
fs.StringVar(power, "p", defaultPower, "power state")
|
||||||
|
|
||||||
|
hue := fs.String("hue", defaultHue, "hue level")
|
||||||
|
fs.StringVar(hue, "H", defaultHue, "hue level")
|
||||||
|
|
||||||
|
saturation := fs.String("saturation", defaultSaturation, "saturation level")
|
||||||
|
fs.StringVar(saturation, "S", defaultSaturation, "saturation level")
|
||||||
|
|
||||||
|
rgb := fs.String("rgb", defaultRGB, "RGB value")
|
||||||
|
fs.StringVar(rgb, "r", defaultRGB, "RGB value")
|
||||||
|
|
||||||
|
name := fs.String("name", defaultName, "named color")
|
||||||
|
fs.StringVar(name, "n", defaultName, "named color")
|
||||||
|
|
||||||
|
brightness := fs.String("brightness", defaultBrightness, "brightness state")
|
||||||
|
fs.StringVar(brightness, "b", defaultBrightness, "brightness state")
|
||||||
|
|
||||||
|
duration := fs.Float64("duration", defaultDuration, "duration state")
|
||||||
|
fs.Float64Var(duration, "d", defaultDuration, "duration state")
|
||||||
|
|
||||||
|
fast := fs.Bool("fast", defaultFast, "fast state")
|
||||||
|
fs.BoolVar(fast, "f", defaultFast, "fast state")
|
||||||
|
|
||||||
|
return fs
|
||||||
|
}(),
|
||||||
|
Use: "[--selector <selector>] [--power (on|off)] [--hue <hue>] [--saturation <saturation>] [--rgb <rbg>] [--name <color>] [--brightness <brightness>] [--duration <sec>] [--fast]",
|
||||||
|
Short: "Set the color",
|
||||||
|
})
|
||||||
|
RegisterCommand("set-state", Command{
|
||||||
|
Func: SetStateCmd,
|
||||||
|
Flags: func() *flag.FlagSet {
|
||||||
|
fs := flag.NewFlagSet("set-state", flag.ExitOnError)
|
||||||
|
|
||||||
|
selector := fs.String("selector", defaultSelector, "Set the selector")
|
||||||
|
fs.StringVar(selector, "s", defaultSelector, "Set the selector")
|
||||||
|
|
||||||
|
power := fs.String("power", defaultPower, "power state")
|
||||||
|
fs.StringVar(power, "p", defaultPower, "power state")
|
||||||
|
|
||||||
|
color := fs.String("color", defaultColor, "color state")
|
||||||
|
fs.StringVar(color, "c", defaultColor, "color state")
|
||||||
|
|
||||||
|
brightness := fs.String("brightness", defaultBrightness, "brightness state")
|
||||||
|
fs.StringVar(brightness, "b", defaultBrightness, "brightness state")
|
||||||
|
|
||||||
|
duration := fs.Float64("duration", defaultDuration, "duration state")
|
||||||
|
fs.Float64Var(duration, "d", defaultDuration, "duration state")
|
||||||
|
|
||||||
|
infrared := fs.String("infrared", defaultInfrared, "infrared state")
|
||||||
|
fs.StringVar(infrared, "i", defaultInfrared, "infrared state")
|
||||||
|
|
||||||
|
fast := fs.Bool("fast", defaultFast, "fast state")
|
||||||
|
fs.BoolVar(fast, "f", defaultFast, "fast state")
|
||||||
|
|
||||||
|
return fs
|
||||||
|
}(),
|
||||||
|
Use: "[--selector <selector>] [--power (on|off)] [--color <color>] [--brightness <brightness>] [--duration <sec>] [--infrared <infrared>] [--fast]",
|
||||||
|
Short: "Set various state attributes",
|
||||||
|
})
|
||||||
|
RegisterCommand("set-white", Command{
|
||||||
|
Func: SetWhiteCmd,
|
||||||
|
Flags: func() *flag.FlagSet {
|
||||||
|
fs := flag.NewFlagSet("set-white", flag.ExitOnError)
|
||||||
|
|
||||||
|
selector := fs.String("selector", "all", "the selector")
|
||||||
|
fs.StringVar(selector, "s", "all", "the selector")
|
||||||
|
|
||||||
|
power := fs.String("power", defaultPower, "power state")
|
||||||
|
fs.StringVar(power, "p", defaultPower, "power state")
|
||||||
|
|
||||||
|
kelvin := fs.String("kelvin", defaultWhiteKelvin, "kelvin level")
|
||||||
|
fs.StringVar(kelvin, "k", defaultWhiteKelvin, "kelvin level")
|
||||||
|
|
||||||
|
name := fs.String("name", defaultWhiteName, "named white level")
|
||||||
|
fs.StringVar(name, "n", defaultWhiteName, "named white level")
|
||||||
|
|
||||||
|
brightness := fs.String("brightness", defaultBrightness, "brightness state")
|
||||||
|
fs.StringVar(brightness, "b", defaultBrightness, "brightness state")
|
||||||
|
|
||||||
|
duration := fs.Float64("duration", defaultDuration, "duration state")
|
||||||
|
fs.Float64Var(duration, "d", defaultDuration, "duration state")
|
||||||
|
|
||||||
|
infrared := fs.String("infrared", defaultInfrared, "infrared state")
|
||||||
|
fs.StringVar(infrared, "i", defaultInfrared, "infrared state")
|
||||||
|
|
||||||
|
fast := fs.Bool("fast", defaultFast, "fast state")
|
||||||
|
fs.BoolVar(fast, "f", defaultFast, "fast state")
|
||||||
|
|
||||||
|
return fs
|
||||||
|
}(),
|
||||||
|
Use: "[--selector <selector>] [--power (on|off)] [--kelvin <kelvin>] [--name <color>] [--brightness <brightness>] [--duration <sec>] [--infrared] [--fast]",
|
||||||
|
Short: "Set the white level",
|
||||||
|
})
|
||||||
|
RegisterCommand("toggle", Command{
|
||||||
|
Func: ToggleCmd,
|
||||||
|
Flags: func() *flag.FlagSet {
|
||||||
|
fs := flag.NewFlagSet("toggle", flag.ExitOnError)
|
||||||
|
|
||||||
|
duration := fs.Float64("duration", defaultDuration, "Set the duration")
|
||||||
|
fs.Float64Var(duration, "d", defaultDuration, "Set the duration")
|
||||||
|
|
||||||
|
selector := fs.String("selector", defaultSelector, "Set the selector")
|
||||||
|
fs.StringVar(selector, "s", defaultSelector, "Set the selector")
|
||||||
|
|
||||||
|
return fs
|
||||||
|
}(),
|
||||||
|
Use: "[--selector <selector>] [--duration <sec>]",
|
||||||
|
Short: "Toggle the power on/off",
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
var Version string
|
|
||||||
var BuildDate string
|
|
||||||
var GitCommit string
|
|
||||||
|
|
||||||
const lumercFile string = ".lumerc"
|
const lumercFile string = ".lumerc"
|
||||||
|
|
||||||
func Main(args []string) (int, error) {
|
func Main(args []string) (int, error) {
|
||||||
@ -67,15 +221,11 @@ func Main(args []string) (int, error) {
|
|||||||
|
|
||||||
command := args[1]
|
command := args[1]
|
||||||
|
|
||||||
c := lifx.NewClient(
|
c := lifx.NewClient(config.AccessToken)
|
||||||
config.AccessToken,
|
|
||||||
lifx.WithUserAgent(userAgent),
|
|
||||||
)
|
|
||||||
|
|
||||||
cmdArgs := CmdArgs{
|
cmdArgs := CmdArgs{
|
||||||
Client: c,
|
Client: c,
|
||||||
Config: config,
|
Config: config,
|
||||||
Args: args[2:],
|
|
||||||
}
|
}
|
||||||
|
|
||||||
cmd, ok := GetCommand(command)
|
cmd, ok := GetCommand(command)
|
||||||
@ -83,14 +233,10 @@ func Main(args []string) (int, error) {
|
|||||||
err = fmt.Errorf("lume: '%s' is not lume command. See 'lume help'", command)
|
err = fmt.Errorf("lume: '%s' is not lume command. See 'lume help'", command)
|
||||||
return ExitFailure, err
|
return ExitFailure, err
|
||||||
}
|
}
|
||||||
|
|
||||||
fs := cmd.Flags
|
fs := cmd.Flags
|
||||||
if fs != nil {
|
|
||||||
fs.Parse(args[2:])
|
fs.Parse(args[2:])
|
||||||
cmdArgs.Flags = Flags{FlagSet: fs}
|
|
||||||
}
|
|
||||||
cmdArgs.Name = command
|
|
||||||
|
|
||||||
|
cmdArgs.Flags = Flags{FlagSet: fs}
|
||||||
exitCode, err := cmd.Func(cmdArgs)
|
exitCode, err := cmd.Func(cmdArgs)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
err = fmt.Errorf("fatal: %s", err)
|
err = fmt.Errorf("fatal: %s", err)
|
||||||
@ -122,12 +268,3 @@ func getConfigPath() string {
|
|||||||
|
|
||||||
return configPath
|
return configPath
|
||||||
}
|
}
|
||||||
|
|
||||||
func initUserAgent() string {
|
|
||||||
var b strings.Builder
|
|
||||||
|
|
||||||
b.WriteString("lume")
|
|
||||||
b.WriteRune('/')
|
|
||||||
b.WriteString(Version)
|
|
||||||
return b.String()
|
|
||||||
}
|
|
||||||
|
@ -1,55 +1,19 @@
|
|||||||
package lumecmd
|
package lumecmd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"flag"
|
lifx "git.kill0.net/chill9/lume"
|
||||||
|
|
||||||
"git.kill0.net/chill9/lifx-go"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func NewCmdPoweroff() Command {
|
|
||||||
return Command{
|
|
||||||
Name: "poweroff",
|
|
||||||
Func: PoweroffCmd,
|
|
||||||
Flags: func() *flag.FlagSet {
|
|
||||||
fs := flag.NewFlagSet("poweroff", flag.ExitOnError)
|
|
||||||
|
|
||||||
duration := fs.Float64("duration", defaultDuration, "Set the duration")
|
|
||||||
fs.Float64Var(duration, "d", defaultDuration, "Set the duration")
|
|
||||||
|
|
||||||
selector := fs.String("selector", defaultSelector, "Set the selector")
|
|
||||||
fs.StringVar(selector, "s", defaultSelector, "Set the selector")
|
|
||||||
|
|
||||||
fs.String("format", defaultOutputFormat, "Set the output format")
|
|
||||||
|
|
||||||
return fs
|
|
||||||
}(),
|
|
||||||
Use: "[--selector <selector>] [--duration <sec>]",
|
|
||||||
Short: "Power on",
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func PoweroffCmd(args CmdArgs) (int, error) {
|
func PoweroffCmd(args CmdArgs) (int, error) {
|
||||||
c := args.Client
|
c := args.Client
|
||||||
duration := args.Flags.Float64("duration")
|
duration := args.Flags.Float64("duration")
|
||||||
selector := args.Flags.String("selector")
|
selector := args.Flags.String("selector")
|
||||||
format := args.Flags.String("format")
|
|
||||||
state := lifx.State{Power: "off", Duration: duration}
|
state := lifx.State{Power: "off", Duration: duration}
|
||||||
|
|
||||||
if format == "" && args.Config.OutputFormat != "" {
|
|
||||||
format = args.Config.OutputFormat
|
|
||||||
}
|
|
||||||
|
|
||||||
r, err := c.SetState(selector, state)
|
r, err := c.SetState(selector, state)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return ExitFailure, err
|
return ExitFailure, err
|
||||||
}
|
}
|
||||||
|
|
||||||
switch format {
|
|
||||||
case "table":
|
|
||||||
PrintResultsTable(r.Results)
|
|
||||||
default:
|
|
||||||
PrintResults(r.Results)
|
PrintResults(r.Results)
|
||||||
}
|
|
||||||
|
|
||||||
return ExitSuccess, nil
|
return ExitSuccess, nil
|
||||||
}
|
}
|
||||||
|
@ -1,55 +1,19 @@
|
|||||||
package lumecmd
|
package lumecmd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"flag"
|
lifx "git.kill0.net/chill9/lume"
|
||||||
|
|
||||||
"git.kill0.net/chill9/lifx-go"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func NewCmdPoweron() Command {
|
|
||||||
return Command{
|
|
||||||
Name: "poweron",
|
|
||||||
Func: PoweronCmd,
|
|
||||||
Flags: func() *flag.FlagSet {
|
|
||||||
fs := flag.NewFlagSet("poweron", flag.ExitOnError)
|
|
||||||
|
|
||||||
duration := fs.Float64("duration", defaultDuration, "Set the duration")
|
|
||||||
fs.Float64Var(duration, "d", defaultDuration, "Set the duration")
|
|
||||||
|
|
||||||
selector := fs.String("selector", defaultSelector, "Set the selector")
|
|
||||||
fs.StringVar(selector, "s", defaultSelector, "Set the selector")
|
|
||||||
|
|
||||||
fs.String("format", defaultOutputFormat, "Set the output format")
|
|
||||||
|
|
||||||
return fs
|
|
||||||
}(),
|
|
||||||
Use: "[--selector <selector>] [--duration <sec>]",
|
|
||||||
Short: "Power on",
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func PoweronCmd(args CmdArgs) (int, error) {
|
func PoweronCmd(args CmdArgs) (int, error) {
|
||||||
c := args.Client
|
c := args.Client
|
||||||
duration := args.Flags.Float64("duration")
|
duration := args.Flags.Float64("duration")
|
||||||
selector := args.Flags.String("selector")
|
selector := args.Flags.String("selector")
|
||||||
format := args.Flags.String("format")
|
|
||||||
state := lifx.State{Power: "on", Duration: duration}
|
state := lifx.State{Power: "on", Duration: duration}
|
||||||
|
|
||||||
if format == "" && args.Config.OutputFormat != "" {
|
|
||||||
format = args.Config.OutputFormat
|
|
||||||
}
|
|
||||||
|
|
||||||
r, err := c.SetState(selector, state)
|
r, err := c.SetState(selector, state)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return ExitFailure, err
|
return ExitFailure, err
|
||||||
}
|
}
|
||||||
|
|
||||||
switch format {
|
|
||||||
case "table":
|
|
||||||
PrintResultsTable(r.Results)
|
|
||||||
default:
|
|
||||||
PrintResults(r.Results)
|
PrintResults(r.Results)
|
||||||
}
|
|
||||||
|
|
||||||
return ExitSuccess, nil
|
return ExitSuccess, nil
|
||||||
}
|
}
|
||||||
|
145
cmd/print.go
145
cmd/print.go
@ -1,145 +0,0 @@
|
|||||||
package lumecmd
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"os"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"git.kill0.net/chill9/lifx-go"
|
|
||||||
"github.com/fatih/color"
|
|
||||||
"github.com/olekukonko/tablewriter"
|
|
||||||
)
|
|
||||||
|
|
||||||
func ColorizePower(s string) string {
|
|
||||||
c := color.New(color.FgRed)
|
|
||||||
if s == "on" {
|
|
||||||
c = color.New(color.FgGreen)
|
|
||||||
}
|
|
||||||
|
|
||||||
return c.Sprint(s)
|
|
||||||
}
|
|
||||||
|
|
||||||
func ColorizeStatus(s lifx.Status) string {
|
|
||||||
c := color.New(color.FgRed)
|
|
||||||
if s == "ok" {
|
|
||||||
c = color.New(color.FgGreen)
|
|
||||||
}
|
|
||||||
|
|
||||||
return c.Sprint(s)
|
|
||||||
}
|
|
||||||
|
|
||||||
func PrintWithIndent(indent int, s string) {
|
|
||||||
fmt.Printf("%*s%s", indent, "", s)
|
|
||||||
}
|
|
||||||
|
|
||||||
func PrintfWithIndent(indent int, format string, a ...interface{}) (n int, err error) {
|
|
||||||
format = fmt.Sprintf("%*s%s", indent, "", format)
|
|
||||||
return fmt.Printf(format, a...)
|
|
||||||
}
|
|
||||||
|
|
||||||
func makeLightsTable(lights []lifx.Light) (hdr []string, rows [][]string) {
|
|
||||||
hdr = []string{"ID", "Location", "Group", "Label", "Last Seen", "Power"}
|
|
||||||
|
|
||||||
for _, l := range lights {
|
|
||||||
rows = append(rows, []string{
|
|
||||||
fmt.Sprint(l.Id),
|
|
||||||
fmt.Sprint(l.Location.Name),
|
|
||||||
fmt.Sprint(l.Group.Name),
|
|
||||||
fmt.Sprint(l.Label),
|
|
||||||
fmt.Sprint(l.LastSeen.Local().Format(time.RFC3339)),
|
|
||||||
fmt.Sprint(ColorizePower(l.Power)),
|
|
||||||
})
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
func makeResultsTable(results []lifx.Result) (hdr []string, rows [][]string) {
|
|
||||||
hdr = []string{"ID", "Label", "Status"}
|
|
||||||
|
|
||||||
for _, r := range results {
|
|
||||||
rows = append(rows, []string{
|
|
||||||
fmt.Sprint(r.Id),
|
|
||||||
fmt.Sprint(r.Label),
|
|
||||||
fmt.Sprint(ColorizeStatus(r.Status)),
|
|
||||||
})
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
func PrintLights(lights []lifx.Light) {
|
|
||||||
sortLights(lights)
|
|
||||||
|
|
||||||
table := tablewriter.NewWriter(os.Stdout)
|
|
||||||
_, rows := makeLightsTable(lights)
|
|
||||||
|
|
||||||
for _, v := range rows {
|
|
||||||
table.Append(v)
|
|
||||||
}
|
|
||||||
|
|
||||||
fmt.Printf("total %d\n", len(lights))
|
|
||||||
table.SetAlignment(tablewriter.ALIGN_LEFT)
|
|
||||||
table.SetAutoWrapText(false)
|
|
||||||
table.SetBorder(false)
|
|
||||||
table.SetCenterSeparator("")
|
|
||||||
table.SetColumnSeparator("")
|
|
||||||
table.SetHeaderLine(false)
|
|
||||||
table.SetNoWhiteSpace(true)
|
|
||||||
table.SetRowSeparator("")
|
|
||||||
table.SetTablePadding(" ")
|
|
||||||
table.Render()
|
|
||||||
}
|
|
||||||
|
|
||||||
func PrintLightsTable(lights []lifx.Light) {
|
|
||||||
sortLights(lights)
|
|
||||||
|
|
||||||
table := tablewriter.NewWriter(os.Stdout)
|
|
||||||
hdr, rows := makeLightsTable(lights)
|
|
||||||
|
|
||||||
for _, v := range rows {
|
|
||||||
table.Append(v)
|
|
||||||
}
|
|
||||||
|
|
||||||
table.SetHeader(hdr)
|
|
||||||
table.Render()
|
|
||||||
}
|
|
||||||
|
|
||||||
func PrintResults(results []lifx.Result) {
|
|
||||||
sortResults(results)
|
|
||||||
|
|
||||||
table := tablewriter.NewWriter(os.Stdout)
|
|
||||||
_, rows := makeResultsTable(results)
|
|
||||||
|
|
||||||
for _, v := range rows {
|
|
||||||
table.Append(v)
|
|
||||||
}
|
|
||||||
|
|
||||||
fmt.Printf("total %d\n", len(results))
|
|
||||||
table.SetAlignment(tablewriter.ALIGN_LEFT)
|
|
||||||
table.SetAutoWrapText(false)
|
|
||||||
table.SetBorder(false)
|
|
||||||
table.SetCenterSeparator("")
|
|
||||||
table.SetColumnSeparator("")
|
|
||||||
table.SetHeaderLine(false)
|
|
||||||
table.SetNoWhiteSpace(true)
|
|
||||||
table.SetRowSeparator("")
|
|
||||||
table.SetTablePadding(" ")
|
|
||||||
table.Render()
|
|
||||||
}
|
|
||||||
|
|
||||||
func PrintResultsTable(results []lifx.Result) {
|
|
||||||
sortResults(results)
|
|
||||||
|
|
||||||
table := tablewriter.NewWriter(os.Stdout)
|
|
||||||
hdr, rows := makeResultsTable(results)
|
|
||||||
|
|
||||||
for _, v := range rows {
|
|
||||||
table.Append(v)
|
|
||||||
}
|
|
||||||
|
|
||||||
table.SetHeader(hdr)
|
|
||||||
table.Render()
|
|
||||||
}
|
|
@ -1,64 +1,16 @@
|
|||||||
package lumecmd
|
package lumecmd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"flag"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"os"
|
||||||
|
|
||||||
"git.kill0.net/chill9/lifx-go"
|
lifx "git.kill0.net/chill9/lume"
|
||||||
)
|
)
|
||||||
|
|
||||||
func NewCmdSetColor() Command {
|
|
||||||
return Command{
|
|
||||||
Name: "set-color",
|
|
||||||
Func: SetColorCmd,
|
|
||||||
Flags: func() *flag.FlagSet {
|
|
||||||
fs := flag.NewFlagSet("set-color", flag.ExitOnError)
|
|
||||||
|
|
||||||
selector := fs.String("selector", "all", "the selector")
|
|
||||||
fs.StringVar(selector, "s", "all", "the selector")
|
|
||||||
|
|
||||||
power := fs.String("power", defaultPower, "power state")
|
|
||||||
fs.StringVar(power, "p", defaultPower, "power state")
|
|
||||||
|
|
||||||
hue := fs.String("hue", defaultHue, "hue level")
|
|
||||||
fs.StringVar(hue, "H", defaultHue, "hue level")
|
|
||||||
|
|
||||||
saturation := fs.String("saturation", defaultSaturation, "saturation level")
|
|
||||||
fs.StringVar(saturation, "S", defaultSaturation, "saturation level")
|
|
||||||
|
|
||||||
rgb := fs.String("rgb", defaultRGB, "RGB value")
|
|
||||||
fs.StringVar(rgb, "r", defaultRGB, "RGB value")
|
|
||||||
|
|
||||||
name := fs.String("name", defaultName, "named color")
|
|
||||||
fs.StringVar(name, "n", defaultName, "named color")
|
|
||||||
|
|
||||||
brightness := fs.String("brightness", defaultBrightness, "brightness state")
|
|
||||||
fs.StringVar(brightness, "b", defaultBrightness, "brightness state")
|
|
||||||
|
|
||||||
duration := fs.Float64("duration", defaultDuration, "duration state")
|
|
||||||
fs.Float64Var(duration, "d", defaultDuration, "duration state")
|
|
||||||
|
|
||||||
fast := fs.Bool("fast", defaultFast, "fast state")
|
|
||||||
fs.BoolVar(fast, "f", defaultFast, "fast state")
|
|
||||||
|
|
||||||
fs.String("format", defaultOutputFormat, "Set the output format")
|
|
||||||
|
|
||||||
return fs
|
|
||||||
}(),
|
|
||||||
Use: "[--selector <selector>] [--power (on|off)] [--hue <hue>] [--saturation <saturation>] [--rgb <rbg>] [--name <color>] [--brightness <brightness>] [--duration <sec>] [--fast]",
|
|
||||||
Short: "Set the color",
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func SetColorCmd(args CmdArgs) (int, error) {
|
func SetColorCmd(args CmdArgs) (int, error) {
|
||||||
c := args.Client
|
c := args.Client
|
||||||
state := lifx.State{}
|
state := lifx.State{}
|
||||||
selector := args.Flags.String("selector")
|
selector := args.Flags.String("selector")
|
||||||
format := args.Flags.String("format")
|
|
||||||
|
|
||||||
if format == "" && args.Config.OutputFormat != "" {
|
|
||||||
format = args.Config.OutputFormat
|
|
||||||
}
|
|
||||||
|
|
||||||
power := args.Flags.String("power")
|
power := args.Flags.String("power")
|
||||||
if power != "" {
|
if power != "" {
|
||||||
@ -71,7 +23,7 @@ func SetColorCmd(args CmdArgs) (int, error) {
|
|||||||
name := args.Flags.String("name")
|
name := args.Flags.String("name")
|
||||||
|
|
||||||
if (hueFlag == "" || saturationFlag == "") && rgbFlag == "" && name == "" {
|
if (hueFlag == "" || saturationFlag == "") && rgbFlag == "" && name == "" {
|
||||||
printCmdHelp(args.Name)
|
printCmdHelp(os.Args[1])
|
||||||
return ExitFailure, nil
|
return ExitFailure, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -126,13 +78,8 @@ func SetColorCmd(args CmdArgs) (int, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if !fast {
|
if !fast {
|
||||||
switch format {
|
|
||||||
case "table":
|
|
||||||
PrintResultsTable(r.Results)
|
|
||||||
default:
|
|
||||||
PrintResults(r.Results)
|
PrintResults(r.Results)
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
return ExitSuccess, nil
|
return ExitSuccess, nil
|
||||||
}
|
}
|
||||||
|
@ -1,57 +1,13 @@
|
|||||||
package lumecmd
|
package lumecmd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"flag"
|
lifx "git.kill0.net/chill9/lume"
|
||||||
|
|
||||||
"git.kill0.net/chill9/lifx-go"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func NewCmdSetState() Command {
|
|
||||||
return Command{
|
|
||||||
Name: "set-state",
|
|
||||||
Func: SetStateCmd,
|
|
||||||
Flags: func() *flag.FlagSet {
|
|
||||||
fs := flag.NewFlagSet("set-state", flag.ExitOnError)
|
|
||||||
|
|
||||||
selector := fs.String("selector", defaultSelector, "Set the selector")
|
|
||||||
fs.StringVar(selector, "s", defaultSelector, "Set the selector")
|
|
||||||
|
|
||||||
power := fs.String("power", defaultPower, "power state")
|
|
||||||
fs.StringVar(power, "p", defaultPower, "power state")
|
|
||||||
|
|
||||||
color := fs.String("color", defaultColor, "color state")
|
|
||||||
fs.StringVar(color, "c", defaultColor, "color state")
|
|
||||||
|
|
||||||
brightness := fs.String("brightness", defaultBrightness, "brightness state")
|
|
||||||
fs.StringVar(brightness, "b", defaultBrightness, "brightness state")
|
|
||||||
|
|
||||||
duration := fs.Float64("duration", defaultDuration, "duration state")
|
|
||||||
fs.Float64Var(duration, "d", defaultDuration, "duration state")
|
|
||||||
|
|
||||||
infrared := fs.String("infrared", defaultInfrared, "infrared state")
|
|
||||||
fs.StringVar(infrared, "i", defaultInfrared, "infrared state")
|
|
||||||
|
|
||||||
fast := fs.Bool("fast", defaultFast, "fast state")
|
|
||||||
fs.BoolVar(fast, "f", defaultFast, "fast state")
|
|
||||||
|
|
||||||
fs.String("format", defaultOutputFormat, "Set the output format")
|
|
||||||
|
|
||||||
return fs
|
|
||||||
}(),
|
|
||||||
Use: "[--selector <selector>] [--power (on|off)] [--color <color>] [--brightness <brightness>] [--duration <sec>] [--infrared <infrared>] [--fast]",
|
|
||||||
Short: "Set various state attributes",
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func SetStateCmd(args CmdArgs) (int, error) {
|
func SetStateCmd(args CmdArgs) (int, error) {
|
||||||
c := args.Client
|
c := args.Client
|
||||||
state := lifx.State{}
|
state := lifx.State{}
|
||||||
selector := args.Flags.String("selector")
|
selector := args.Flags.String("selector")
|
||||||
format := args.Flags.String("format")
|
|
||||||
|
|
||||||
if format == "" && args.Config.OutputFormat != "" {
|
|
||||||
format = args.Config.OutputFormat
|
|
||||||
}
|
|
||||||
|
|
||||||
power := args.Flags.String("power")
|
power := args.Flags.String("power")
|
||||||
if power != "" {
|
if power != "" {
|
||||||
@ -81,24 +37,14 @@ func SetStateCmd(args CmdArgs) (int, error) {
|
|||||||
fast := args.Flags.Bool("fast")
|
fast := args.Flags.Bool("fast")
|
||||||
state.Fast = fast
|
state.Fast = fast
|
||||||
|
|
||||||
if power == "" && color == "" && brightnessFlag == "" && infraredFlag == "" {
|
|
||||||
printCmdHelp(args.Name)
|
|
||||||
return ExitFailure, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
r, err := c.SetState(selector, state)
|
r, err := c.SetState(selector, state)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return ExitFailure, err
|
return ExitFailure, err
|
||||||
}
|
}
|
||||||
|
|
||||||
if !fast {
|
if !fast {
|
||||||
switch format {
|
|
||||||
case "table":
|
|
||||||
PrintResultsTable(r.Results)
|
|
||||||
default:
|
|
||||||
PrintResults(r.Results)
|
PrintResults(r.Results)
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
return ExitSuccess, nil
|
return ExitSuccess, nil
|
||||||
}
|
}
|
||||||
|
@ -1,60 +1,13 @@
|
|||||||
package lumecmd
|
package lumecmd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"flag"
|
lifx "git.kill0.net/chill9/lume"
|
||||||
|
|
||||||
"git.kill0.net/chill9/lifx-go"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func NewCmdSetWhite() Command {
|
|
||||||
return Command{
|
|
||||||
Name: "set-white",
|
|
||||||
Func: SetWhiteCmd,
|
|
||||||
Flags: func() *flag.FlagSet {
|
|
||||||
fs := flag.NewFlagSet("set-white", flag.ExitOnError)
|
|
||||||
|
|
||||||
selector := fs.String("selector", "all", "the selector")
|
|
||||||
fs.StringVar(selector, "s", "all", "the selector")
|
|
||||||
|
|
||||||
power := fs.String("power", defaultPower, "power state")
|
|
||||||
fs.StringVar(power, "p", defaultPower, "power state")
|
|
||||||
|
|
||||||
kelvin := fs.String("kelvin", defaultWhiteKelvin, "kelvin level")
|
|
||||||
fs.StringVar(kelvin, "k", defaultWhiteKelvin, "kelvin level")
|
|
||||||
|
|
||||||
name := fs.String("name", defaultWhiteName, "named white level")
|
|
||||||
fs.StringVar(name, "n", defaultWhiteName, "named white level")
|
|
||||||
|
|
||||||
brightness := fs.String("brightness", defaultBrightness, "brightness state")
|
|
||||||
fs.StringVar(brightness, "b", defaultBrightness, "brightness state")
|
|
||||||
|
|
||||||
duration := fs.Float64("duration", defaultDuration, "duration state")
|
|
||||||
fs.Float64Var(duration, "d", defaultDuration, "duration state")
|
|
||||||
|
|
||||||
infrared := fs.String("infrared", defaultInfrared, "infrared state")
|
|
||||||
fs.StringVar(infrared, "i", defaultInfrared, "infrared state")
|
|
||||||
|
|
||||||
fast := fs.Bool("fast", defaultFast, "fast state")
|
|
||||||
fs.BoolVar(fast, "f", defaultFast, "fast state")
|
|
||||||
|
|
||||||
fs.String("format", defaultOutputFormat, "Set the output format")
|
|
||||||
|
|
||||||
return fs
|
|
||||||
}(),
|
|
||||||
Use: "[--selector <selector>] [--power (on|off)] [--kelvin <kelvin>] [--name <color>] [--brightness <brightness>] [--duration <sec>] [--infrared] [--fast]",
|
|
||||||
Short: "Set the white level",
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func SetWhiteCmd(args CmdArgs) (int, error) {
|
func SetWhiteCmd(args CmdArgs) (int, error) {
|
||||||
c := args.Client
|
c := args.Client
|
||||||
state := lifx.State{}
|
state := lifx.State{}
|
||||||
selector := args.Flags.String("selector")
|
selector := args.Flags.String("selector")
|
||||||
format := args.Flags.String("format")
|
|
||||||
|
|
||||||
if format == "" && args.Config.OutputFormat != "" {
|
|
||||||
format = args.Config.OutputFormat
|
|
||||||
}
|
|
||||||
|
|
||||||
power := args.Flags.String("power")
|
power := args.Flags.String("power")
|
||||||
if power != "" {
|
if power != "" {
|
||||||
@ -99,24 +52,14 @@ func SetWhiteCmd(args CmdArgs) (int, error) {
|
|||||||
fast := args.Flags.Bool("fast")
|
fast := args.Flags.Bool("fast")
|
||||||
state.Fast = fast
|
state.Fast = fast
|
||||||
|
|
||||||
if power == "" && kelvinFlag == "" && name == "" && brightnessFlag == "" && infraredFlag == "" {
|
|
||||||
printCmdHelp(args.Name)
|
|
||||||
return ExitFailure, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
r, err := c.SetState(selector, state)
|
r, err := c.SetState(selector, state)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return ExitFailure, err
|
return ExitFailure, err
|
||||||
}
|
}
|
||||||
|
|
||||||
if !fast {
|
if !fast {
|
||||||
switch format {
|
|
||||||
case "table":
|
|
||||||
PrintResultsTable(r.Results)
|
|
||||||
default:
|
|
||||||
PrintResults(r.Results)
|
PrintResults(r.Results)
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
return ExitSuccess, nil
|
return ExitSuccess, nil
|
||||||
}
|
}
|
||||||
|
92
cmd/show.go
92
cmd/show.go
@ -1,92 +0,0 @@
|
|||||||
package lumecmd
|
|
||||||
|
|
||||||
import (
|
|
||||||
"flag"
|
|
||||||
"fmt"
|
|
||||||
)
|
|
||||||
|
|
||||||
const Tabstop int = 2
|
|
||||||
|
|
||||||
func NewCmdShow() Command {
|
|
||||||
return Command{
|
|
||||||
Name: "show",
|
|
||||||
Func: ShowCmd,
|
|
||||||
Flags: func() *flag.FlagSet {
|
|
||||||
fs := flag.NewFlagSet("show", flag.ExitOnError)
|
|
||||||
|
|
||||||
selector := fs.String("selector", defaultSelector, "Set the selector")
|
|
||||||
fs.StringVar(selector, "s", defaultSelector, "Set the selector")
|
|
||||||
|
|
||||||
return fs
|
|
||||||
}(),
|
|
||||||
Use: "[--selector=<selector>]",
|
|
||||||
Short: "Show details about the lights",
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func ShowCmd(args CmdArgs) (int, error) {
|
|
||||||
var indent int
|
|
||||||
c := args.Client
|
|
||||||
selector := args.Flags.String("selector")
|
|
||||||
lights, err := c.ListLights(selector)
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
return ExitFailure, err
|
|
||||||
}
|
|
||||||
|
|
||||||
sortLights(lights)
|
|
||||||
|
|
||||||
for i, l := range lights {
|
|
||||||
indent = 0
|
|
||||||
fmt.Printf(
|
|
||||||
"Light ID: %s, %s, Power: %s\n",
|
|
||||||
l.Id,
|
|
||||||
connected(l.Connected),
|
|
||||||
ColorizePower(l.Power),
|
|
||||||
)
|
|
||||||
indent += Tabstop
|
|
||||||
PrintfWithIndent(indent, "Label: %s, ID: %s\n", l.Label, l.Id)
|
|
||||||
PrintfWithIndent(indent, "UUID: %s\n", l.UUID)
|
|
||||||
PrintfWithIndent(indent, "Location: %s, ID: %s\n", l.Location.Name, l.Location.Id)
|
|
||||||
PrintfWithIndent(indent, "Group: %s, ID: %s\n", l.Group.Name, l.Group.Id)
|
|
||||||
PrintfWithIndent(indent, "Color: Hue: %.1f, Saturation: %.1f%%, Kelvin: %d\n",
|
|
||||||
*l.Color.H, *l.Color.S, *l.Color.K)
|
|
||||||
PrintfWithIndent(indent, "Brightness: %.1f%%\n", l.Brightness*100)
|
|
||||||
if l.Effect != "" {
|
|
||||||
PrintfWithIndent(indent, "Effect: %s\n", l.Effect)
|
|
||||||
}
|
|
||||||
PrintfWithIndent(indent, "Product: %s\n", l.Product.Name)
|
|
||||||
PrintfWithIndent(indent, "Capabilities: ")
|
|
||||||
fmt.Printf("Color: %s, ", YesNo(l.Product.Capabilities.HasColor))
|
|
||||||
fmt.Printf("Variable Color Temp: %s, ", YesNo(l.Product.Capabilities.HasVariableColorTemp))
|
|
||||||
fmt.Printf("IR: %s, ", YesNo(l.Product.Capabilities.HasIR))
|
|
||||||
fmt.Printf("Chain: %s, ", YesNo(l.Product.Capabilities.HasChain))
|
|
||||||
fmt.Printf("Multizone: %s, ", YesNo(l.Product.Capabilities.HasMultizone))
|
|
||||||
fmt.Printf("Min Kelvin: %.1f, ", l.Product.Capabilities.MinKelvin)
|
|
||||||
fmt.Printf("Max Kelvin: %.1f ", l.Product.Capabilities.MaxKelvin)
|
|
||||||
fmt.Println()
|
|
||||||
// List applicable selectors (most to least specific)
|
|
||||||
PrintfWithIndent(indent, "Selectors:\n")
|
|
||||||
indent += Tabstop
|
|
||||||
PrintfWithIndent(indent, "id:%s\n", l.Id)
|
|
||||||
PrintfWithIndent(indent, "label:%s\n", l.Label)
|
|
||||||
PrintfWithIndent(indent, "group_id:%s\n", l.Group.Id)
|
|
||||||
PrintfWithIndent(indent, "group:%s\n", l.Group.Name)
|
|
||||||
PrintfWithIndent(indent, "location_id:%s\n", l.Location.Id)
|
|
||||||
PrintfWithIndent(indent, "location:%s\n", l.Location.Name)
|
|
||||||
indent -= Tabstop
|
|
||||||
PrintfWithIndent(indent, "Last Seen: %s (%.1fs ago)\n", l.LastSeen, l.SecondsLastSeen)
|
|
||||||
|
|
||||||
if i < len(lights)-1 {
|
|
||||||
fmt.Println()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return ExitSuccess, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func connected(c bool) string {
|
|
||||||
if c {
|
|
||||||
return "Connected"
|
|
||||||
}
|
|
||||||
return "Disconnected"
|
|
||||||
}
|
|
@ -1,52 +1,13 @@
|
|||||||
package lumecmd
|
package lumecmd
|
||||||
|
|
||||||
import (
|
|
||||||
"flag"
|
|
||||||
)
|
|
||||||
|
|
||||||
func NewCmdToggle() Command {
|
|
||||||
return Command{
|
|
||||||
Name: "toggle",
|
|
||||||
Func: ToggleCmd,
|
|
||||||
Flags: func() *flag.FlagSet {
|
|
||||||
fs := flag.NewFlagSet("toggle", flag.ExitOnError)
|
|
||||||
|
|
||||||
duration := fs.Float64("duration", defaultDuration, "Set the duration")
|
|
||||||
fs.Float64Var(duration, "d", defaultDuration, "Set the duration")
|
|
||||||
|
|
||||||
selector := fs.String("selector", defaultSelector, "Set the selector")
|
|
||||||
fs.StringVar(selector, "s", defaultSelector, "Set the selector")
|
|
||||||
|
|
||||||
fs.String("format", defaultOutputFormat, "Set the output format")
|
|
||||||
|
|
||||||
return fs
|
|
||||||
}(),
|
|
||||||
Use: "[--selector <selector>] [--duration <sec>]",
|
|
||||||
Short: "Toggle the power on/off",
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func ToggleCmd(args CmdArgs) (int, error) {
|
func ToggleCmd(args CmdArgs) (int, error) {
|
||||||
c := args.Client
|
c := args.Client
|
||||||
duration := args.Flags.Float64("duration")
|
duration := args.Flags.Float64("duration")
|
||||||
selector := args.Flags.String("selector")
|
selector := args.Flags.String("selector")
|
||||||
format := args.Flags.String("format")
|
|
||||||
|
|
||||||
if format == "" && args.Config.OutputFormat != "" {
|
|
||||||
format = args.Config.OutputFormat
|
|
||||||
}
|
|
||||||
|
|
||||||
r, err := c.Toggle(selector, duration)
|
r, err := c.Toggle(selector, duration)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return ExitFailure, err
|
return ExitFailure, err
|
||||||
}
|
}
|
||||||
|
|
||||||
switch format {
|
|
||||||
case "table":
|
|
||||||
PrintResultsTable(r.Results)
|
|
||||||
default:
|
|
||||||
PrintResults(r.Results)
|
PrintResults(r.Results)
|
||||||
}
|
|
||||||
|
|
||||||
return ExitSuccess, nil
|
return ExitSuccess, nil
|
||||||
}
|
}
|
||||||
|
115
cmd/util.go
115
cmd/util.go
@ -6,10 +6,116 @@ import (
|
|||||||
"sort"
|
"sort"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
"git.kill0.net/chill9/lifx-go"
|
lifx "git.kill0.net/chill9/lume"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func powerColor(s string) string {
|
||||||
|
fs := "\033[1;31m%s\033[0m"
|
||||||
|
if s == "on" {
|
||||||
|
fs = "\033[1;32m%s\033[0m"
|
||||||
|
}
|
||||||
|
|
||||||
|
return fmt.Sprintf(fs, s)
|
||||||
|
}
|
||||||
|
|
||||||
|
func statusColor(s lifx.Status) string {
|
||||||
|
fs := "\033[1;31m%s\033[0m"
|
||||||
|
if s == "ok" {
|
||||||
|
fs = "\033[1;32m%s\033[0m"
|
||||||
|
}
|
||||||
|
|
||||||
|
return fmt.Sprintf(fs, s)
|
||||||
|
}
|
||||||
|
|
||||||
|
func PrintResults(res []lifx.Result) {
|
||||||
|
var length int
|
||||||
|
var widths map[string]int
|
||||||
|
|
||||||
|
widths = make(map[string]int)
|
||||||
|
|
||||||
|
for _, r := range res {
|
||||||
|
length = len(r.Id)
|
||||||
|
if widths["id"] < length {
|
||||||
|
widths["id"] = length
|
||||||
|
}
|
||||||
|
|
||||||
|
length = len(r.Label)
|
||||||
|
if widths["label"] < length {
|
||||||
|
widths["label"] = length
|
||||||
|
}
|
||||||
|
|
||||||
|
length = len(r.Status)
|
||||||
|
if widths["status"] < length {
|
||||||
|
widths["status"] = length
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
sortResults(res)
|
||||||
|
|
||||||
|
for _, r := range res {
|
||||||
|
fmt.Printf("%*s %*s %*s\n",
|
||||||
|
widths["id"], r.Id,
|
||||||
|
widths["label"], r.Label,
|
||||||
|
widths["status"], statusColor(r.Status))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func PrintLights(lights []lifx.Light) {
|
||||||
|
var length int
|
||||||
|
var widths map[string]int
|
||||||
|
|
||||||
|
widths = make(map[string]int)
|
||||||
|
|
||||||
|
for _, l := range lights {
|
||||||
|
length = len(l.Id)
|
||||||
|
if widths["id"] < length {
|
||||||
|
widths["id"] = length
|
||||||
|
}
|
||||||
|
|
||||||
|
length = len(l.Location.Name)
|
||||||
|
if widths["location"] < length {
|
||||||
|
widths["location"] = length
|
||||||
|
}
|
||||||
|
|
||||||
|
length = len(l.Group.Name)
|
||||||
|
if widths["group"] < length {
|
||||||
|
widths["group"] = length
|
||||||
|
}
|
||||||
|
|
||||||
|
length = len(l.Label)
|
||||||
|
if widths["label"] < length {
|
||||||
|
widths["label"] = length
|
||||||
|
}
|
||||||
|
|
||||||
|
length = len(l.LastSeen.Local().Format(time.RFC3339))
|
||||||
|
if widths["last_seen"] < length {
|
||||||
|
widths["last_seen"] = length
|
||||||
|
}
|
||||||
|
|
||||||
|
length = len(l.Power)
|
||||||
|
if widths["power"] < length {
|
||||||
|
widths["power"] = length
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
sortLights(lights)
|
||||||
|
|
||||||
|
fmt.Printf("total %d\n", len(lights))
|
||||||
|
for _, l := range lights {
|
||||||
|
fmt.Printf(
|
||||||
|
"%*s %*s %*s %*s %*s %-*s\n",
|
||||||
|
widths["id"], l.Id,
|
||||||
|
widths["loction"], l.Location.Name,
|
||||||
|
widths["group"], l.Group.Name,
|
||||||
|
widths["label"], l.Label,
|
||||||
|
widths["last_seen"], l.LastSeen.Local().Format(time.RFC3339),
|
||||||
|
widths["power"], powerColor(l.Power),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func parseRGB(s string) (lifx.RGBColor, error) {
|
func parseRGB(s string) (lifx.RGBColor, error) {
|
||||||
var c lifx.RGBColor
|
var c lifx.RGBColor
|
||||||
rgb := strings.SplitN(s, ",", 3)
|
rgb := strings.SplitN(s, ",", 3)
|
||||||
@ -53,10 +159,3 @@ func ExitWithCode(code int, err error) {
|
|||||||
}
|
}
|
||||||
os.Exit(code)
|
os.Exit(code)
|
||||||
}
|
}
|
||||||
|
|
||||||
func YesNo(v bool) string {
|
|
||||||
if v {
|
|
||||||
return "yes"
|
|
||||||
}
|
|
||||||
return "no"
|
|
||||||
}
|
|
||||||
|
@ -1,33 +0,0 @@
|
|||||||
package lumecmd
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"strings"
|
|
||||||
)
|
|
||||||
|
|
||||||
func NewCmdVersion() Command {
|
|
||||||
return Command{
|
|
||||||
Name: "version",
|
|
||||||
Func: VersionCmd,
|
|
||||||
Flags: nil,
|
|
||||||
Use: "",
|
|
||||||
Short: "Show version",
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func VersionCmd(args CmdArgs) (int, error) {
|
|
||||||
var b strings.Builder
|
|
||||||
|
|
||||||
fmt.Fprintf(&b, "lume %s", Version)
|
|
||||||
b.WriteString(" ")
|
|
||||||
if GitCommit != "" {
|
|
||||||
fmt.Fprintf(&b, "(git: %s)", GitCommit)
|
|
||||||
b.WriteString(" ")
|
|
||||||
}
|
|
||||||
if BuildDate != "" {
|
|
||||||
fmt.Fprintf(&b, "build_date: %s", BuildDate)
|
|
||||||
}
|
|
||||||
|
|
||||||
fmt.Println(b.String())
|
|
||||||
return ExitSuccess, nil
|
|
||||||
}
|
|
224
color.go
Normal file
224
color.go
Normal file
@ -0,0 +1,224 @@
|
|||||||
|
package lifx
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
type (
|
||||||
|
Color interface {
|
||||||
|
ColorString() string
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
type (
|
||||||
|
RGBColor struct {
|
||||||
|
R, G, B uint8
|
||||||
|
}
|
||||||
|
|
||||||
|
HSBKColor struct {
|
||||||
|
H *float32 `json:"hue"`
|
||||||
|
S *float32 `json:"saturation"`
|
||||||
|
B *float32 `json:"brightness"`
|
||||||
|
K *int16 `json:"kelvin"`
|
||||||
|
}
|
||||||
|
|
||||||
|
NamedColor string
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
KelvinCandlelight = 1500
|
||||||
|
KelvinSunset = 2000
|
||||||
|
KelvinUltraWarm = 2500
|
||||||
|
KelvinIncandescent = 2700
|
||||||
|
KelvinWarm = 3000
|
||||||
|
KelvinCool = 4000
|
||||||
|
KelvinCoolDaylight = 4500
|
||||||
|
KelvinSoftDaylight = 5000
|
||||||
|
KelvinDaylight = 5600
|
||||||
|
KelvinNoonDaylight = 6000
|
||||||
|
KelvinBrightDaylight = 6500
|
||||||
|
KelvinCloudDaylight = 7000
|
||||||
|
KelvinBlueDaylight = 7500
|
||||||
|
KelvinBlueOvercast = 8000
|
||||||
|
KelvinBlueIce = 9000
|
||||||
|
|
||||||
|
HueWhite = 0
|
||||||
|
HueRed = 0
|
||||||
|
HueOrange = 36
|
||||||
|
HueYellow = 60
|
||||||
|
HueGreen = 120
|
||||||
|
HueCyan = 180
|
||||||
|
HueBlue = 250
|
||||||
|
HuePurple = 280
|
||||||
|
HuePink = 325
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
DefaultWhites = map[string]int{
|
||||||
|
"candlelight": KelvinCandlelight,
|
||||||
|
"sunset": KelvinSunset,
|
||||||
|
"ultrawarm": KelvinUltraWarm,
|
||||||
|
"incandescent": KelvinIncandescent,
|
||||||
|
"warm": KelvinWarm,
|
||||||
|
"cool": KelvinCool,
|
||||||
|
"cooldaylight": KelvinCoolDaylight,
|
||||||
|
"softdaylight": KelvinSoftDaylight,
|
||||||
|
"daylight": KelvinDaylight,
|
||||||
|
"noondaylight": KelvinNoonDaylight,
|
||||||
|
"brightdaylight": KelvinBrightDaylight,
|
||||||
|
"clouddaylight": KelvinCloudDaylight,
|
||||||
|
"bluedaylight": KelvinBlueDaylight,
|
||||||
|
"blueovercast": KelvinBlueOvercast,
|
||||||
|
"blueice": KelvinBlueIce,
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
func NewRGBColor(r, g, b uint8) (RGBColor, error) {
|
||||||
|
var c RGBColor
|
||||||
|
if (r < 0 || r > 255) && (g < 0 || r > 255) && (b < 0 || b > 255) {
|
||||||
|
return c, errors.New("values must be between 0-255")
|
||||||
|
}
|
||||||
|
|
||||||
|
return RGBColor{R: r, G: g, B: b}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewHSColor(h, s float32) (HSBKColor, error) {
|
||||||
|
var c HSBKColor
|
||||||
|
|
||||||
|
if h < 0 || h > 360 {
|
||||||
|
return c, errors.New("hue must be between 0.0-360.0")
|
||||||
|
}
|
||||||
|
if s < 0 || s > 1 {
|
||||||
|
return c, errors.New("saturation must be between 0.0-1.0")
|
||||||
|
}
|
||||||
|
|
||||||
|
c = HSBKColor{
|
||||||
|
H: Float32Ptr(h),
|
||||||
|
S: Float32Ptr(s),
|
||||||
|
}
|
||||||
|
|
||||||
|
return c, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewHSBColor(h, s, b float32) (HSBKColor, error) {
|
||||||
|
var c HSBKColor
|
||||||
|
|
||||||
|
if h < 0 || h > 360 {
|
||||||
|
return c, errors.New("hue must be between 0.0-360.0")
|
||||||
|
}
|
||||||
|
if s < 0 || s > 1 {
|
||||||
|
return c, errors.New("saturation must be between 0.0-1.0")
|
||||||
|
}
|
||||||
|
if b < 0 || b > 1 {
|
||||||
|
return c, errors.New("brightness must be between 0.0-1.0")
|
||||||
|
}
|
||||||
|
|
||||||
|
c = HSBKColor{
|
||||||
|
H: Float32Ptr(h),
|
||||||
|
S: Float32Ptr(s),
|
||||||
|
B: Float32Ptr(b),
|
||||||
|
}
|
||||||
|
|
||||||
|
return c, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewRed() (HSBKColor, error) { return NewHSColor(HueRed, 1) }
|
||||||
|
func NewOrange() (HSBKColor, error) { return NewHSColor(HueOrange, 1) }
|
||||||
|
func NewYellow() (HSBKColor, error) { return NewHSColor(HueYellow, 1) }
|
||||||
|
func NewGreen() (HSBKColor, error) { return NewHSColor(HueGreen, 1) }
|
||||||
|
func NewCyan() (HSBKColor, error) { return NewHSColor(HueCyan, 1) }
|
||||||
|
func NewPurple() (HSBKColor, error) { return NewHSColor(HuePurple, 1) }
|
||||||
|
func NewPink() (HSBKColor, error) { return NewHSColor(HuePink, 1) }
|
||||||
|
|
||||||
|
func NewWhite(k int16) (HSBKColor, error) {
|
||||||
|
var c HSBKColor
|
||||||
|
|
||||||
|
if k < 1500 || k > 9000 {
|
||||||
|
return c, errors.New("kelvin must be between 1500-9000")
|
||||||
|
}
|
||||||
|
|
||||||
|
c = HSBKColor{
|
||||||
|
H: Float32Ptr(HueWhite),
|
||||||
|
S: Float32Ptr(0.0),
|
||||||
|
K: Int16Ptr(k),
|
||||||
|
}
|
||||||
|
|
||||||
|
return c, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewWhiteString(s string) (HSBKColor, error) {
|
||||||
|
k, ok := DefaultWhites[s]
|
||||||
|
|
||||||
|
if !ok {
|
||||||
|
return HSBKColor{}, fmt.Errorf("'%s' is not a valid default white", s)
|
||||||
|
}
|
||||||
|
|
||||||
|
return NewWhite(int16(k))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c RGBColor) ColorString() string {
|
||||||
|
return fmt.Sprintf("rgb:%d,%d,%d", c.R, c.G, c.B)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c RGBColor) Hex() string {
|
||||||
|
return fmt.Sprintf("#%x%x%x", c.R, c.G, c.B)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c HSBKColor) ColorString() string {
|
||||||
|
var s []string
|
||||||
|
if c.H != nil {
|
||||||
|
s = append(s, fmt.Sprintf("hue:%g", *c.H))
|
||||||
|
}
|
||||||
|
if c.S != nil {
|
||||||
|
s = append(s, fmt.Sprintf("saturation:%g", *c.S))
|
||||||
|
}
|
||||||
|
if c.B != nil {
|
||||||
|
s = append(s, fmt.Sprintf("brightness:%g", *c.B))
|
||||||
|
}
|
||||||
|
if c.K != nil {
|
||||||
|
s = append(s, fmt.Sprintf("kelvin:%d", *c.K))
|
||||||
|
}
|
||||||
|
return strings.Join(s, " ")
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c HSBKColor) MarshalText() ([]byte, error) {
|
||||||
|
return []byte(c.ColorString()), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c RGBColor) MarshalText() ([]byte, error) {
|
||||||
|
return []byte(c.ColorString()), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c NamedColor) ColorString() string {
|
||||||
|
return string(c)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Client) ValidateColor(color Color) (Color, error) {
|
||||||
|
var (
|
||||||
|
err error
|
||||||
|
s *HSBKColor
|
||||||
|
r *http.Response
|
||||||
|
resp *Response
|
||||||
|
)
|
||||||
|
|
||||||
|
if resp, err = c.validateColor(color); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
resp, err = NewResponse(r)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
defer resp.Body.Close()
|
||||||
|
|
||||||
|
if err = json.NewDecoder(resp.Body).Decode(&s); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return s, nil
|
||||||
|
}
|
35
endpoints.go
Normal file
35
endpoints.go
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
package lifx
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"net/url"
|
||||||
|
"path"
|
||||||
|
)
|
||||||
|
|
||||||
|
func BuildURL(rawurl, rawpath string) string {
|
||||||
|
u, _ := url.Parse(rawurl)
|
||||||
|
u.Path = path.Join(u.Path, rawpath)
|
||||||
|
return u.String()
|
||||||
|
}
|
||||||
|
|
||||||
|
var (
|
||||||
|
Endpoint = "https://api.lifx.com/v1"
|
||||||
|
EndpointState = func(selector string) string {
|
||||||
|
return BuildURL(Endpoint, fmt.Sprintf("/lights/%s/state", selector))
|
||||||
|
}
|
||||||
|
EndpointStateDelta = func(selector string) string {
|
||||||
|
return BuildURL(Endpoint, fmt.Sprintf("/lights/%s/state/delta", selector))
|
||||||
|
}
|
||||||
|
EndpointListLights = func(selector string) string {
|
||||||
|
return BuildURL(Endpoint, fmt.Sprintf("/lights/%s", selector))
|
||||||
|
}
|
||||||
|
EndpointStates = func() string {
|
||||||
|
return BuildURL(Endpoint, "/lights/states")
|
||||||
|
}
|
||||||
|
EndpointColor = func() string {
|
||||||
|
return BuildURL(Endpoint, "/color")
|
||||||
|
}
|
||||||
|
EndpointToggle = func(selector string) string {
|
||||||
|
return BuildURL(Endpoint, fmt.Sprintf("/lights/%s/toggle", selector))
|
||||||
|
}
|
||||||
|
)
|
3
go.mod
3
go.mod
@ -3,9 +3,6 @@ module git.kill0.net/chill9/lume
|
|||||||
go 1.15
|
go 1.15
|
||||||
|
|
||||||
require (
|
require (
|
||||||
git.kill0.net/chill9/lifx-go v0.0.0-20210215004437-f86c28b0a5ef
|
|
||||||
github.com/BurntSushi/toml v0.3.1
|
github.com/BurntSushi/toml v0.3.1
|
||||||
github.com/fatih/color v1.10.0
|
|
||||||
github.com/olekukonko/tablewriter v0.0.5
|
|
||||||
golang.org/x/sys v0.0.0-20210110051926-789bb1bd4061
|
golang.org/x/sys v0.0.0-20210110051926-789bb1bd4061
|
||||||
)
|
)
|
||||||
|
14
go.sum
14
go.sum
@ -1,18 +1,4 @@
|
|||||||
git.kill0.net/chill9/lifx-go v0.0.0-20210215004437-f86c28b0a5ef h1:8yyXAk+qiRvrowTeHHAOBZqGXXCr8SxWFydez61ZGp8=
|
|
||||||
git.kill0.net/chill9/lifx-go v0.0.0-20210215004437-f86c28b0a5ef/go.mod h1:ZFKIcwdJ4Nqlrkn/eUHbeLt0NVhFsfxBREkVoA+jzUc=
|
|
||||||
github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=
|
github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=
|
||||||
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
|
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
|
||||||
github.com/fatih/color v1.10.0 h1:s36xzo75JdqLaaWoiEHk767eHiwo0598uUxyfiPkDsg=
|
|
||||||
github.com/fatih/color v1.10.0/go.mod h1:ELkj/draVOlAH/xkhN6mQ50Qd0MPOk5AAr3maGEBuJM=
|
|
||||||
github.com/mattn/go-colorable v0.1.8 h1:c1ghPdyEDarC70ftn0y+A/Ee++9zz8ljHG1b13eJ0s8=
|
|
||||||
github.com/mattn/go-colorable v0.1.8/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc=
|
|
||||||
github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHXY=
|
|
||||||
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
|
|
||||||
github.com/mattn/go-runewidth v0.0.9 h1:Lm995f3rfxdpd6TSmuVCHVb/QhupuXlYr8sCI/QdE+0=
|
|
||||||
github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
|
|
||||||
github.com/olekukonko/tablewriter v0.0.5 h1:P2Ga83D34wi1o9J6Wh1mRuqd4mF/x/lgBS7N7AbDhec=
|
|
||||||
github.com/olekukonko/tablewriter v0.0.5/go.mod h1:hPp6KlRPjbx+hW8ykQs1w3UBbZlj6HuIJcUGPhkA7kY=
|
|
||||||
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
|
||||||
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
|
||||||
golang.org/x/sys v0.0.0-20210110051926-789bb1bd4061 h1:DQmQoKxQWtyybCtX/3dIuDBcAhFszqq8YiNeS6sNu1c=
|
golang.org/x/sys v0.0.0-20210110051926-789bb1bd4061 h1:DQmQoKxQWtyybCtX/3dIuDBcAhFszqq8YiNeS6sNu1c=
|
||||||
golang.org/x/sys v0.0.0-20210110051926-789bb1bd4061/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
golang.org/x/sys v0.0.0-20210110051926-789bb1bd4061/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
|
221
lights.go
Normal file
221
lights.go
Normal file
@ -0,0 +1,221 @@
|
|||||||
|
package lifx
|
||||||
|
|
||||||
|
import (
|
||||||
|
//"crypto/tls"
|
||||||
|
"encoding/json"
|
||||||
|
"net/http"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
OK Status = "ok"
|
||||||
|
TimedOut Status = "timed_out"
|
||||||
|
Offline Status = "offline"
|
||||||
|
)
|
||||||
|
|
||||||
|
type (
|
||||||
|
Status string
|
||||||
|
|
||||||
|
Selector struct {
|
||||||
|
Id string `json:"id"`
|
||||||
|
Name string `json:"name"`
|
||||||
|
}
|
||||||
|
|
||||||
|
Product struct {
|
||||||
|
Name string `json:"name"`
|
||||||
|
Identifier string `json:"identifier"`
|
||||||
|
Company string `json:"company"`
|
||||||
|
Capabilities Capabilities `json:"capabilities"`
|
||||||
|
}
|
||||||
|
|
||||||
|
Capabilities struct {
|
||||||
|
HasColor bool `json:"has_color"`
|
||||||
|
HasVariableColorTemp bool `json:"has_variable_color_temp"`
|
||||||
|
HasIR bool `json:"has_ir"`
|
||||||
|
HasChain bool `json:"has_chain"`
|
||||||
|
HasMultizone bool `json:"has_multizone"`
|
||||||
|
MinKelvin float64 `json:"min_kelvin"`
|
||||||
|
MaxKelvin float64 `json:"max_kelvin"`
|
||||||
|
}
|
||||||
|
|
||||||
|
Light struct {
|
||||||
|
Id string `json:"id"`
|
||||||
|
UUID string `json:"uuid"`
|
||||||
|
Label string `json:"label"`
|
||||||
|
Connected bool `json:"connected"`
|
||||||
|
Power string `json:"power"`
|
||||||
|
Color HSBKColor `json:"color"`
|
||||||
|
Brightness float64 `json:"brightness"`
|
||||||
|
Effect string `json:"effect"`
|
||||||
|
Group Selector `json:"group"`
|
||||||
|
Location Selector `json:"location"`
|
||||||
|
Product Product `json:"product"`
|
||||||
|
LastSeen time.Time `json:"last_seen"`
|
||||||
|
SecondsLastSeen float64 `json:"seconds_last_seen"`
|
||||||
|
}
|
||||||
|
|
||||||
|
State struct {
|
||||||
|
Power string `json:"power,omitempty"`
|
||||||
|
Color Color `json:"color,omitempty"`
|
||||||
|
Brightness float64 `json:"brightness,omitempty"`
|
||||||
|
Duration float64 `json:"duration,omitempty"`
|
||||||
|
Infrared float64 `json:"infrared,omitempty"`
|
||||||
|
Fast bool `json:"fast,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
StateDelta struct {
|
||||||
|
Power *string `json:"power,omitempty"`
|
||||||
|
Duration *float64 `json:"duration,omitempty"`
|
||||||
|
Infrared *float64 `json:"infrared,omitempty"`
|
||||||
|
Hue *float64 `json:"hue,omitempty"`
|
||||||
|
Saturation *float64 `json:"saturation,omitempty"`
|
||||||
|
Brightness *float64 `json:"brightness,omitempty"`
|
||||||
|
Kelvin *int `json:"kelvin,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
StateWithSelector struct {
|
||||||
|
State
|
||||||
|
Selector string `json:"selector"`
|
||||||
|
}
|
||||||
|
|
||||||
|
States struct {
|
||||||
|
States []StateWithSelector `json:"states,omitempty"`
|
||||||
|
Defaults State `json:"defaults,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
Toggle struct {
|
||||||
|
Duration float64 `json:"duration,omitempty"`
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
func (c *Client) SetState(selector string, state State) (*LifxResponse, error) {
|
||||||
|
var (
|
||||||
|
err error
|
||||||
|
s *LifxResponse
|
||||||
|
resp *Response
|
||||||
|
)
|
||||||
|
|
||||||
|
if resp, err = c.setState(selector, state); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer resp.Body.Close()
|
||||||
|
|
||||||
|
if resp.IsError() {
|
||||||
|
return nil, resp.GetLifxError()
|
||||||
|
}
|
||||||
|
|
||||||
|
if state.Fast && resp.StatusCode == http.StatusAccepted {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
if err = json.NewDecoder(resp.Body).Decode(&s); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return s, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Client) FastSetState(selector string, state State) (*LifxResponse, error) {
|
||||||
|
state.Fast = true
|
||||||
|
return c.SetState(selector, state)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Client) SetStates(selector string, states States) (*LifxResponse, error) {
|
||||||
|
var (
|
||||||
|
err error
|
||||||
|
s *LifxResponse
|
||||||
|
resp *Response
|
||||||
|
)
|
||||||
|
|
||||||
|
if resp, err = c.setStates(selector, states); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer resp.Body.Close()
|
||||||
|
|
||||||
|
if err = json.NewDecoder(resp.Body).Decode(&s); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return s, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Client) StateDelta(selector string, delta StateDelta) (*LifxResponse, error) {
|
||||||
|
var (
|
||||||
|
err error
|
||||||
|
s *LifxResponse
|
||||||
|
resp *Response
|
||||||
|
)
|
||||||
|
|
||||||
|
if resp, err = c.stateDelta(selector, delta); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer resp.Body.Close()
|
||||||
|
|
||||||
|
if err = json.NewDecoder(resp.Body).Decode(&s); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return s, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Client) Toggle(selector string, duration float64) (*LifxResponse, error) {
|
||||||
|
var (
|
||||||
|
err error
|
||||||
|
s *LifxResponse
|
||||||
|
resp *Response
|
||||||
|
)
|
||||||
|
|
||||||
|
if resp, err = c.toggle(selector, duration); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer resp.Body.Close()
|
||||||
|
|
||||||
|
if resp.IsError() {
|
||||||
|
return nil, resp.GetLifxError()
|
||||||
|
}
|
||||||
|
|
||||||
|
if err = json.NewDecoder(resp.Body).Decode(&s); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return s, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Client) ListLights(selector string) ([]Light, error) {
|
||||||
|
var (
|
||||||
|
err error
|
||||||
|
s []Light
|
||||||
|
resp *Response
|
||||||
|
)
|
||||||
|
|
||||||
|
if resp, err = c.listLights(selector); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer resp.Body.Close()
|
||||||
|
|
||||||
|
if resp.StatusCode > 299 {
|
||||||
|
return nil, resp.GetLifxError()
|
||||||
|
}
|
||||||
|
|
||||||
|
if err = json.NewDecoder(resp.Body).Decode(&s); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return s, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Client) PowerOff(selector string) (*LifxResponse, error) {
|
||||||
|
return c.SetState(selector, State{Power: "off"})
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Client) FastPowerOff(selector string) {
|
||||||
|
c.SetState(selector, State{Power: "off", Fast: true})
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Client) PowerOn(selector string) (*LifxResponse, error) {
|
||||||
|
return c.SetState(selector, State{Power: "on"})
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Client) FastPowerOn(selector string) {
|
||||||
|
c.SetState(selector, State{Power: "on", Fast: true})
|
||||||
|
}
|
Reference in New Issue
Block a user