Advertisement
Guest User

DemolitionMan

a guest
May 6th, 2018
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.86 KB | None | 0 0
  1. (defun make-keyword (name)
  2. (values (intern name "KEYWORD")))
  3.  
  4. (defun sensors-simulator (sensors-list &rest parameters &key (debug nil))
  5. (declare (ignorable parameters debug))
  6. (check-type sensors-list list)
  7. (loop
  8. for x in sensors-list
  9. do
  10. (check-type x sensor-descriptor))
  11. (let ((random-state (make-random-state t)))
  12. (loop
  13. named thread-main-loop
  14. do
  15. (when *exit-thread*
  16. (return-from thread-main-loop))
  17. (loop
  18. for x in sensors-list
  19. do
  20. (bt:with-lock-held ((lock-object x))
  21. (setf (value x) (antik:make-pq (/ (coerce (random 100 random-state) 'double-float)
  22. 10d0)
  23. 'antik:ampere)
  24. (timestamp x) (get-universal-time)))
  25. (when debug
  26. (format *standard-output*
  27. "Setting sensor ~a data ~a.~%"
  28. (name x)
  29. (value x)))
  30. (multiple-value-bind (v u status)
  31. (antik:pqval (value x))
  32. (when (and status
  33. *database*)
  34. (let* ((point (list (get-universal-time)
  35. v
  36. (timestamp x)))
  37. (database-data (cl-influxdb:make-influxdb-data :key (string-downcase (name x))
  38. :tags (list (cons :host *hostname*)
  39. (cons :region "one"))
  40. :columns '(:timestamp :value)
  41. :points (list point))))
  42. (multiple-value-bind (response reason)
  43. (cl-influxdb:write-points *database*
  44. database-data
  45. :time-precision "s")
  46. (if response
  47. (format *standard-output*
  48. "Write point ~a to database, "
  49. point)
  50. (format *standard-output*
  51. "Could not write point ~a to database, "
  52. point))
  53. (format *standard-output*
  54. "reason: ~a~%"
  55. reason)))))
  56. (bt:thread-yield)
  57. (sleep 1)))))
  58.  
  59. (defun start (&rest parameters &key (debug nil))
  60. (declare (ignorable parameters debug))
  61. (let ((database-creation nil))
  62. (unless *database*
  63. (setq *database* (make-instance 'cl-influxdb:influxdb
  64. :database *database-name*
  65. :user (getf *database-login* :name)
  66. :password (getf *database-login* :password))))
  67. (when *database*
  68. (handler-case
  69. (setq database-creation (cl-influxdb:create-database *database*
  70. *database-name*))
  71. (cl-influxdb:command-fail (e)
  72. e))
  73. (unless *thread-object*
  74. (setq *thread-object* (bt:make-thread #'(lambda ()
  75. (sensors-simulator *sensors-list*
  76. :debug debug))
  77. :name (symbol-name (gensym "sensor-simulator-thread-"))))))))
  78.  
  79. (defun stop ()
  80. (when (bt:threadp *thread-object*)
  81. (setq *exit-thread* t)
  82. (loop
  83. while (bt:thread-alive-p *thread-object*)
  84. do
  85. (bt:destroy-thread *thread-object*)
  86. (sleep 1))
  87. (setq *thread-object* nil))
  88. (when *database*
  89. (cl-influxdb:query *database*
  90. (concatenate 'string
  91. "drop database "
  92. *database-name*))))
  93.  
  94. (start :debug t)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement