SHARE
TWEET
DemolitionMan
a guest
May 6th, 2018
84
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
- (defun make-keyword (name)
- (values (intern name "KEYWORD")))
- (defun sensors-simulator (sensors-list &rest parameters &key (debug nil))
- (declare (ignorable parameters debug))
- (check-type sensors-list list)
- (loop
- for x in sensors-list
- do
- (check-type x sensor-descriptor))
- (let ((random-state (make-random-state t)))
- (loop
- named thread-main-loop
- do
- (when *exit-thread*
- (return-from thread-main-loop))
- (loop
- for x in sensors-list
- do
- (bt:with-lock-held ((lock-object x))
- (setf (value x) (antik:make-pq (/ (coerce (random 100 random-state) 'double-float)
- 10d0)
- 'antik:ampere)
- (timestamp x) (get-universal-time)))
- (when debug
- (format *standard-output*
- "Setting sensor ~a data ~a.~%"
- (name x)
- (value x)))
- (multiple-value-bind (v u status)
- (antik:pqval (value x))
- (when (and status
- *database*)
- (let* ((point (list (get-universal-time)
- v
- (timestamp x)))
- (database-data (cl-influxdb:make-influxdb-data :key (string-downcase (name x))
- :tags (list (cons :host *hostname*)
- (cons :region "one"))
- :columns '(:timestamp :value)
- :points (list point))))
- (multiple-value-bind (response reason)
- (cl-influxdb:write-points *database*
- database-data
- :time-precision "s")
- (if response
- (format *standard-output*
- "Write point ~a to database, "
- point)
- (format *standard-output*
- "Could not write point ~a to database, "
- point))
- (format *standard-output*
- "reason: ~a~%"
- reason)))))
- (bt:thread-yield)
- (sleep 1)))))
- (defun start (&rest parameters &key (debug nil))
- (declare (ignorable parameters debug))
- (let ((database-creation nil))
- (unless *database*
- (setq *database* (make-instance 'cl-influxdb:influxdb
- :database *database-name*
- :user (getf *database-login* :name)
- :password (getf *database-login* :password))))
- (when *database*
- (handler-case
- (setq database-creation (cl-influxdb:create-database *database*
- *database-name*))
- (cl-influxdb:command-fail (e)
- e))
- (unless *thread-object*
- (setq *thread-object* (bt:make-thread #'(lambda ()
- (sensors-simulator *sensors-list*
- :debug debug))
- :name (symbol-name (gensym "sensor-simulator-thread-"))))))))
- (defun stop ()
- (when (bt:threadp *thread-object*)
- (setq *exit-thread* t)
- (loop
- while (bt:thread-alive-p *thread-object*)
- do
- (bt:destroy-thread *thread-object*)
- (sleep 1))
- (setq *thread-object* nil))
- (when *database*
- (cl-influxdb:query *database*
- (concatenate 'string
- "drop database "
- *database-name*))))
- (start :debug t)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy.
