noolite-web.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. "log"
  25. "net/http"
  26. "strconv"
  27. "strings"
  28. )
  29. func sendCommand(n *noolite.NooliteAdapter, command string, channel, value, r, g, b int) error {
  30. if channel == -1 {
  31. return errors.New("Channel was not set")
  32. }
  33. if command == "" {
  34. return errors.New("Command was not set")
  35. }
  36. if command == "set" {
  37. if value != 0 {
  38. return n.SetBrightnesValue(channel, value)
  39. } else if r != 0 || g != 0 || b != 0 {
  40. return n.SetBrightnesValues(channel, r, g, b)
  41. } else {
  42. return errors.New("Need some value")
  43. }
  44. } else {
  45. cmd, ok := n.FindCommand(command)
  46. if !ok {
  47. return errors.New("Command not found")
  48. }
  49. return cmd(channel)
  50. }
  51. }
  52. func parseParams(path string) (string, int, int, int, int, int) {
  53. params := strings.Split(path, "/")[1:]
  54. command := ""
  55. channel := -1
  56. value := 0
  57. r := 0
  58. g := 0
  59. b := 0
  60. command = params[0]
  61. if len(params) > 1 {
  62. channel, _ = strconv.Atoi(params[1])
  63. }
  64. if len(params) > 2 {
  65. value, _ = strconv.Atoi(params[2])
  66. }
  67. if len(params) == 5 {
  68. value = 0
  69. r, _ = strconv.Atoi(params[2])
  70. g, _ = strconv.Atoi(params[3])
  71. b, _ = strconv.Atoi(params[4])
  72. }
  73. return command, channel, value, r, g, b
  74. }
  75. func main() {
  76. binding := flag.String("bind", ":8080", "Address binding")
  77. static_dir := flag.String("static", "/var/www/static", "Static directory")
  78. flag.Parse()
  79. n, err := noolite.DefaultNooliteAdapter()
  80. if err != nil {
  81. panic(err)
  82. }
  83. defer n.Close()
  84. http.HandleFunc("/noolite/", func(w http.ResponseWriter, r *http.Request) {
  85. command, channel, value, red, green, blue := parseParams(r.URL.Path[1:])
  86. err := sendCommand(n, command, channel, value, red, green, blue)
  87. if err != nil {
  88. fmt.Fprintf(w, "{\"error\": %q}", err)
  89. } else {
  90. fmt.Fprintf(w, "{\"command\": %q, \"channel\": \"%d\"}", command, channel)
  91. }
  92. })
  93. fs := http.FileServer(http.Dir(*static_dir))
  94. http.Handle("/static/", http.StripPrefix("/static/", fs))
  95. log.Println("Start listening at", *binding)
  96. panic(http.ListenAndServe(*binding, nil))
  97. }