Try to continue after encountering an exception

This commit is contained in:
niten 2023-06-07 13:27:47 -07:00
parent f0d8f59cc1
commit 4e3fa0914a
1 changed files with 61 additions and 43 deletions

View File

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