inclusion.scm 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. ;;; Guile-ZWave -- Guile talks with ZWave devices.
  2. ;;; Copyright © 2021 Ludovic Courtès <ludo@gnu.org>
  3. ;;;
  4. ;;; This file is part of Guile-ZWave.
  5. ;;;
  6. ;;; Guile-ZWave is free software; you can redistribute it and/or modify it
  7. ;;; under the terms of the GNU General Public License as published by
  8. ;;; the Free Software Foundation; either version 3 of the License, or (at
  9. ;;; your option) any later version.
  10. ;;;
  11. ;;; Guile-ZWave is distributed in the hope that it will be useful, but
  12. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;;; GNU General Public License for more details.
  15. ;;;
  16. ;;; You should have received a copy of the GNU General Public License
  17. ;;; along with Guile-ZWave. If not, see <http://www.gnu.org/licenses/>.
  18. ;; This program puts the ZWave controller in "device inclusion" mode. Once
  19. ;; it's started, you can set devices in inclusion mode as well (usually by
  20. ;; double-clicking a button on the device), and that way they'll find each
  21. ;; other.
  22. (use-modules (zwave)
  23. (srfi srfi-71)
  24. (ice-9 match))
  25. (define (run-device-inclusion port)
  26. (let ((init (initialize-zwave-state port)))
  27. (write-serial-message (make-request (message-class add-node-to-network)
  28. #:high-power? #f
  29. #:network-wide? #t)
  30. port)
  31. (let loop ((state init))
  32. (match (select (list port) '() '())
  33. ((() () ())
  34. (loop state))
  35. (_
  36. (let* ((message (read-serial-message port))
  37. (notification state
  38. (pop-zwave-state-notification
  39. (handle-response message state))))
  40. (match notification
  41. (('inclusion-protocol-done . node)
  42. (format #t "inclusion process complete, total ~a nodes:~%~s~%"
  43. (length (zwave-state-nodes state))
  44. (zwave-state-nodes state)))
  45. (('included-node . node)
  46. (format #t "included node ~s~%" node)
  47. (loop state))
  48. (('included-controller . node)
  49. (format #t "included controller ~s~%" node)
  50. (loop state))
  51. (#f
  52. (loop state))
  53. ('failed
  54. (format #t "inclusion failed~%")
  55. (exit 1))
  56. (event
  57. (format #t "unknown inclusion event: ~s~%" event)
  58. (loop state)))))))))
  59. (run-device-inclusion (open-zwave-serial-port (cadr (command-line))))