snootforge

chee/kasa

a javascript for kasa veho lightbulbs

add a tcp server
6cf5ae6ad6083ed7741aaf19ba593c818a9a6a4d
  1. Fdiff --git a/play/tcp.js b/play/tcp.js
  2. new file mode 100644
  3. index 0000000..961d095
  4. --- /dev/null
  5. +++ b/play/tcp.js
  6. H@@ -0,0 +1,36 @@
  7. +const net = require('net')
  8. +const Blub = require('kasa')
  9. +const blubs = []
  10. +const paired = []
  11. +
  12. +Blub.discover(blub => {
  13. + const {address} = blub
  14. + if (!paired.includes(address)) {
  15. + Blub.pair(blub, dispatch => {
  16. + blubs.push(dispatch)
  17. + paired.push(address)
  18. + })
  19. + }
  20. +})
  21. +
  22. +const port = process.env.PORT || 17000
  23. +
  24. +net.createServer(connection => {
  25. + connection.on('data', data => {
  26. + console.log({data}, 'data here', data.toString())
  27. + const hexmatch = data.toString().match(/^#([\da-z]{2})([\da-z]{2})([\da-z]{2})/)
  28. + const rgbmatch = data.toString().match(/^rgb\(\s*(\d+),\s*(\d+),\s*(\d+)\)/)
  29. + const brightness = 0xaa
  30. + let red, green, blue
  31. + if (hexmatch) {
  32. + [red, green, blue] = hexmatch.slice(1).map(n => parseInt(n, 16))
  33. + }
  34. + if (rgbmatch) {
  35. + [red, green, blue] = rgbmatch.slice(1)
  36. + }
  37. + console.log(`setting blubs to red: ${red}, green: ${green}, blue: ${blue}`)
  38. + blubs.forEach(dispatch => dispatch(Blub.setColor(red, green, blue, brightness)))
  39. + })
  40. +}).listen(port, () => {
  41. + console.log('server bound on %s', port)
  42. +})