noolite-web.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*
  2. Copyright 2016 Denis V. Dedkov (denis.v.dedkov@gmail.com)
  3. This file is part of Noolite Go bindings.
  4. Noolite Go bindings is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. Noolite Go bindings is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with Noolite Go bindings. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. // Example WEB-server for control noolite adapter
  16. //
  17. // Default run on :8080 with static dir /var/www/static
  18. package main
  19. import (
  20. "errors"
  21. "flag"
  22. "fmt"
  23. "github.com/dedkovd/noolite"
  24. "net/http"
  25. "strconv"
  26. "strings"
  27. )
  28. func sendCommand(n *noolite.NooliteAdapter, command string, channel, value, r, g, b int) error {
  29. if channel == -1 {
  30. return errors.New("Channel was not set")
  31. }
  32. if command == "" {
  33. return errors.New("Command was not set")
  34. }
  35. if command == "set" {
  36. if value != 0 {
  37. return n.SetBrightnesValue(channel, value)
  38. } else if r != 0 || g != 0 || b != 0 {
  39. return n.SetBrightnesValues(channel, r, g, b)
  40. } else {
  41. return errors.New("Need some value")
  42. }
  43. } else {
  44. cmd, ok := n.FindCommand(command)
  45. if !ok {
  46. return errors.New("Command not found")
  47. }
  48. return cmd(channel)
  49. }
  50. }
  51. func parseParams(path string) (string, int, int, int, int, int) {
  52. params := strings.Split(path, "/")[1:]
  53. command := ""
  54. channel := -1
  55. value := 0
  56. r := 0
  57. g := 0
  58. b := 0
  59. command = params[0]
  60. if len(params) > 1 {
  61. channel, _ = strconv.Atoi(params[1])
  62. }
  63. if len(params) > 2 {
  64. value, _ = strconv.Atoi(params[2])
  65. }
  66. if len(params) == 5 {
  67. value = 0
  68. r, _ = strconv.Atoi(params[2])
  69. g, _ = strconv.Atoi(params[3])
  70. b, _ = strconv.Atoi(params[4])
  71. }
  72. return command, channel, value, r, g, b
  73. }
  74. func main() {
  75. binding := *flag.String("bind", ":8080", "Address binding")
  76. static_dir := *flag.String("static", "/var/www/static", "Static directory")
  77. flag.Parse()
  78. n, err := noolite.DefaultNooliteAdapter()
  79. if err != nil {
  80. panic(err)
  81. }
  82. defer n.Close()
  83. http.HandleFunc("/noolite/", func(w http.ResponseWriter, r *http.Request) {
  84. command, channel, value, red, green, blue := parseParams(r.URL.Path[1:])
  85. err := sendCommand(n, command, channel, value, red, green, blue)
  86. if err != nil {
  87. fmt.Fprintf(w, "{\"error\": %q}", err)
  88. } else {
  89. fmt.Fprintf(w, "{\"command\": %q, \"channel\": \"%d\"}", command, channel)
  90. }
  91. })
  92. fs := http.FileServer(http.Dir(static_dir))
  93. http.Handle("/static/", http.StripPrefix("/static/", fs))
  94. panic(http.ListenAndServe(binding, nil))
  95. }