noolite-cli.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. package main
  2. import (
  3. "errors"
  4. "flag"
  5. "fmt"
  6. "github.com/dedkovd/noolite"
  7. "net/http"
  8. "strings"
  9. "strconv"
  10. )
  11. func sendCommand(n *noolite.NooliteAdapter, command string, channel, value, r, g, b int) error {
  12. if channel == -1 {
  13. return errors.New("Channel was not set")
  14. }
  15. if command == "" {
  16. return errors.New("Command was not set")
  17. }
  18. if command == "set" {
  19. if value != 0 {
  20. return n.SetBrightnesValue(channel, value)
  21. } else if r != 0 || g != 0 || b != 0 {
  22. return n.SetBrightnesValues(channel, r, g, b)
  23. } else {
  24. return errors.New("Need some value")
  25. }
  26. } else {
  27. commands := map[string]func(int) error{
  28. "on": n.On,
  29. "off": n.Off,
  30. "switch": n.Switch,
  31. "decraseBrightnes": n.DecraseBrightnes,
  32. "incraseBrightnes": n.IncraseBrightnes,
  33. "invertBrightnes": n.InvertBrightnes,
  34. }
  35. cmd, ok := commands[command]
  36. if !ok {
  37. return errors.New("Command not found")
  38. }
  39. return cmd(channel)
  40. }
  41. }
  42. func parseParams(path string) (string, int, int, int, int, int) {
  43. params := strings.Split(path, "/")[1:]
  44. command := ""
  45. channel := -1
  46. value := 0
  47. r := 0
  48. g := 0
  49. b := 0
  50. command = params[0]
  51. if len(params) > 1 {
  52. channel, _ = strconv.Atoi(params[1])
  53. }
  54. if len(params) > 2 {
  55. value, _ = strconv.Atoi(params[2])
  56. }
  57. if len(params) == 5 {
  58. value = 0
  59. r, _ = strconv.Atoi(params[2])
  60. g, _ = strconv.Atoi(params[3])
  61. b, _ = strconv.Atoi(params[4])
  62. }
  63. return command, channel, value, r, g, b
  64. }
  65. func main() {
  66. channel := flag.Int("channel", -1, "Noolite adapter channel")
  67. command := flag.String("command", "", "Command")
  68. value := flag.Int("val", 0, "Set value")
  69. red := flag.Int("r", 0, "Red channel")
  70. green := flag.Int("g", 0, "Green channel")
  71. blue := flag.Int("b", 0, "Blue channel")
  72. http_port := flag.Int("p", -1, "Http port")
  73. flag.Parse()
  74. n, err := noolite.DefaultNooliteAdapter()
  75. if err != nil {
  76. panic(err)
  77. }
  78. defer n.Close()
  79. if *http_port < 0 {
  80. err := sendCommand(n, *command, *channel, *value, *red, *green, *blue)
  81. if err != nil {
  82. panic(err)
  83. }
  84. } else {
  85. http.HandleFunc("/noolite/", func(w http.ResponseWriter, r *http.Request) {
  86. command, channel, value, red, green, blue := parseParams(r.URL.Path[1:])
  87. err := sendCommand(n, command, channel, value, red, green, blue)
  88. if err != nil {
  89. fmt.Fprintf(w, "{\"error\": %q}", err)
  90. } else {
  91. fmt.Fprintf(w, "{\"command\": %q, \"channel\": \"%d\"}", command, channel)
  92. }
  93. })
  94. fs := http.FileServer(http.Dir("/var/www/static"))
  95. http.Handle("/static/", http.StripPrefix("/static/", fs))
  96. panic(http.ListenAndServe(fmt.Sprintf(":%d", *http_port), nil))
  97. }
  98. }