Try to continue after encountering an exception
This commit is contained in:
parent
f0d8f59cc1
commit
4e3fa0914a
@ -6,7 +6,8 @@
|
||||
[objectifier-client.core :as obj]
|
||||
[clojure.core.async :as async :refer [<! >! go-loop <!! timeout]]
|
||||
[clojure.string :as str])
|
||||
(:import java.time.Instant))
|
||||
(:import java.time.Instant
|
||||
java.lang.Exception))
|
||||
|
||||
;; Let's see:
|
||||
;;
|
||||
@ -19,6 +20,10 @@
|
||||
;;
|
||||
;; - If anything is detected, send a notification to the callback.
|
||||
|
||||
|
||||
(defn- exception? [obj]
|
||||
(instance? Exception obj))
|
||||
|
||||
(defprotocol ISuanNiServer
|
||||
(object-channel [_]))
|
||||
|
||||
@ -39,13 +44,17 @@
|
||||
[false e]))))
|
||||
max-wait (* 5 60 1000)] ;; wait at most 5 minutes
|
||||
(loop [[success? result] (wrap-attempt)
|
||||
wait-ms 1000]
|
||||
wait-ms 1000
|
||||
n 0]
|
||||
(if success?
|
||||
result
|
||||
(do (when verbose
|
||||
(println (format "attempt failed, sleeping %s ms" wait-ms)))
|
||||
(if (< n 10)
|
||||
(do
|
||||
(<!! (timeout wait-ms))
|
||||
(recur (wrap-attempt) (min (* wait-ms 1.25) max-wait)))))))
|
||||
(recur (wrap-attempt) (min (* wait-ms 1.25) max-wait) (+ n 1)))
|
||||
(throw result)))))))
|
||||
|
||||
(defn start!
|
||||
[& {:keys [listen-host
|
||||
@ -72,6 +81,7 @@
|
||||
(async/close! image-chan))
|
||||
(when (-> event :type (= :motion-detected))
|
||||
(let [cam (syno/get-camera-by-location! syno-client (:location event))]
|
||||
(try
|
||||
(>! image-chan
|
||||
{
|
||||
:location (syno/location cam)
|
||||
@ -79,7 +89,9 @@
|
||||
:snapshot (syno/take-snapshot! cam)
|
||||
:time (Instant/now)
|
||||
:camera cam
|
||||
}))
|
||||
})
|
||||
(catch Exception e
|
||||
(println (.toString e)))))
|
||||
(recur (<! event-chan)))))
|
||||
(go-loop [image-data (<! image-chan)]
|
||||
(if (nil? image-data)
|
||||
@ -89,6 +101,9 @@
|
||||
(let [{:keys [location camera-id snapshot time]} image-data
|
||||
summary (retry-attempt verbose
|
||||
#(obj/get-summary! obj-client snapshot))]
|
||||
(if (exception? summary)
|
||||
(println (.toString summary))
|
||||
(do
|
||||
(when verbose
|
||||
(println (str "detected "
|
||||
(count (:objects summary))
|
||||
@ -108,13 +123,14 @@
|
||||
:snapshot snapshot
|
||||
:objects (:objects summary)
|
||||
:detection-url (:output summary)
|
||||
}))
|
||||
}))))
|
||||
(recur (<! image-chan)))))
|
||||
(go-loop [detection-event (<! obj-chan)]
|
||||
(if (nil? detection-event)
|
||||
(when verbose
|
||||
(println "stopping object listener")
|
||||
(async/close! mqtt-chan))
|
||||
(try
|
||||
(do (>! mqtt-chan
|
||||
{:type :detection-event
|
||||
:time (Instant/now)
|
||||
@ -125,5 +141,7 @@
|
||||
:detect-time
|
||||
:objects
|
||||
:detection-url])})
|
||||
(recur (<! obj-chan)))))
|
||||
(recur (<! obj-chan)))
|
||||
(catch Exception e
|
||||
(println (.toString e))))))
|
||||
(->SuanNiServer event-chan image-chan obj-chan listener)))
|
||||
|
Loading…
x
Reference in New Issue
Block a user