class SensorsDBHandler(web.RequestHandler): def __init__(self, app, request, **kwargs): super(SensorsDBHandler, self).__init__(app, request, **kwargs) self.executor = ThreadPoolExecutor(4) self._dbi = DBIngest(index="sensors", office=office, host=dbhost) def check_origin(self, origin): return True @run_on_executor def _update(self, sensor, source): try: print("Ingesting", sensor, flush=True) r = self._dbi.ingest(source, refresh="wait_for") return r except Exception as e: print(str(e), flush=True) return str(e) @gen.coroutine def put(self): options = json.loads(self.request.body.decode('utf-8')) r = yield self._update(sensor=options["sensor"], source=options["source"]) if isinstance(r, str): self.set_status(400, encode(r)) return self.write(r) self.set_status(200, 'OK') self.finish()
def register_office(): global db,r db=DBIngest(index="offices",office="",host=dbhost) while True: try: r=db.ingest({ "office": { "lat": office[0], "lon": office[1], }, "uri": host, },"$".join(map(str,office))) return except Exception as e: print("Exception: "+str(e), flush=True) time.sleep(10)
def quit_service(signum, sigframe): global stop stop=True signal(SIGTERM, quit_service) dba=DBIngest(host=dbhost, index="algorithms", office=office) dbs=DBQuery(host=dbhost, index="sensors", office=office) # register algorithm (while waiting for db to startup) while True: try: algorithm=dba.ingest({ "name": "crowd-counting", "office": { "lat": office[0], "lon": office[1], }, "status": "processing", "skip": every_nth_frame, }) break except Exception as e: print("Exception: "+str(e), flush=True) time.sleep(10) # compete for a sensor connection while not stop: try: print("Searching...", flush=True) for sensor in dbs.search("sensor:'camera' and status:'idle' and algorithm='crowd-counting' and office:["+str(office[0])+","+str(office[1])+"]"): try:
'status': 'idle', }) if not dbhost: continue try: if not r: # new camera print("Searching for template", flush=True) template = list( dbp.search(" or ".join( ['{}="{}"'.format(id1[0], id1[1]) for id1 in camids]), size=1)) if not template: template = list( dbp.search("ip={} and port={}".format(ip, port), size=1)) if template: print("Ingesting", flush=True) record = template[0]["_source"] record.update(sinfo) dbi.ingest(record, refresh="wait_for") else: print("Template not found", flush=True) else: # camera re-connect dbs.update(r[0]["_id"], sinfo) except Exception as e: print(traceback.format_exc(), flush=True) if not dbhost: break time.sleep(service_interval)
def _rec2db(self, office, sensor, timestamp, path): disk_usage = psutil.disk_usage(self._storage)[3] if disk_usage < halt_rec_th: dt = datetime.datetime.fromtimestamp(timestamp / 1000) officestr = (str(office[0]) + "c" + str(office[1])).replace( "-", "n").replace(".", "d") mp4path = self._storage + "/" + officestr + "/" + sensor + "/" + str( dt.year) + "/" + str(dt.month) + "/" + str(dt.day) os.makedirs(mp4path, exist_ok=True) mp4file = mp4path + "/" + str(timestamp) + ".mp4" # perform a straight copy to fix negative timestamp for chrome list( run([ "/usr/local/bin/ffmpeg", "-f", "mp4", "-i", path, "-c", "copy", mp4file ])) sinfo = probe(mp4file) sinfo.update({ "sensor": sensor, "office": { "lat": office[0], "lon": office[1], }, "time": timestamp, "path": mp4file[len(self._storage) + 1:], }) else: print("Disk full: recording halted", flush=True) sinfo = None if local_office: if sinfo["bandwidth"]: db_cam = DBQuery(host=dbhost, index="sensors", office=office) db_cam.update(sensor, {"bandwidth": sinfo["bandwidth"]}) # check disk usage and send alert disk_usage = psutil.disk_usage(self._storage).percent if disk_usage >= warn_disk_th: level = "fatal" if disk_usage >= fatal_disk_th else "warning" db_alt = DBIngest(host=dbhost, index="alerts", office=office) message = text["halt recording"].format( disk_usage ) if disk_usage >= halt_rec_th else text["disk usage"].format( disk_usage) db_alt.ingest({ "time": int(time.time() * 1000), "office": { "lat": office[0], "lon": office[1], }, "location": { "lat": office[0], "lon": office[1], }, level: [{ "message": message, "args": { "disk": disk_usage, } }] }) # ingest recording local if sinfo: print("Ingest recording: {}".format(sinfo), flush=True) office1 = office if local_office else "" # denormalize sensor address to recordings dbs = DBQuery(host=dbhost, index="sensors", office=office1) r = list(dbs.search("_id='" + sinfo["sensor"] + "'", size=1)) if r: sinfo["address"] = r[0]["_source"]["address"] db_rec = DBIngest(host=dbhost, index="recordings", office=office1) db_rec.ingest(sinfo)
class Feeder(): def __init__(self): logger.debug("Initializing Feeder") self.office = list(map(float, os.environ["OFFICE"].split(","))) self.alg_id = None self.recording_volume = os.environ["STORAGE_VOLUME"] self.every_nth_frame = int(os.environ["EVERY_NTH_FRAME"]) #Hosts self.dbhost = os.environ["DBHOST"] self.vahost = "http://localhost:8080/pipelines" self.mqtthost = os.environ["MQTTHOST"] #Clients self.db_alg = DBIngest(host=self.dbhost, index="algorithms", office=self.office) self.db_inf = DBIngest(host=self.dbhost, index="analytics", office=self.office) self.db_sensors = DBQuery(host=self.dbhost, index="sensors", office=self.office) self.mqttclient = None self.mqtttopic = None self.observer = Observer() self.batchsize = 300 self.inference_cache = [] self._threadflag = False def start(self): logger.info(" ### Starting Feeder ### ") logger.debug("Waiting for VA startup") r = requests.Response() r.status_code = 400 while r.status_code != 200 and r.status_code != 201: try: r = requests.get(self.vahost) except Exception as e: r = requests.Response() r.status_code = 400 time.sleep(10) # Register Algorithm logger.debug("Registering as algorithm in the DB") while True: try: self.alg_id = self.db_alg.ingest({ "name": "object_detection", "office": { "lat": self.office[0], "lon": self.office[1] }, "status": "idle", "skip": self.every_nth_frame, })["_id"] break except Exception as e: logger.debug("Register algo exception: " + str(e)) time.sleep(10) self.mqtttopic = "smtc_va_inferences_" + self.alg_id camera_monitor_thread = Thread(target=self.monitor_cameras, daemon=True) logger.debug("Starting working threads") self._threadflag = True self.startmqtt() self.observer.start() camera_monitor_thread.start() logger.debug("Waiting for interrupt...") camera_monitor_thread.join() self.observer.join() def stop(self): logger.info(" ### Stopping Feeder ### ") self._threadflag = False logger.debug("Unregistering algorithm from DB") self.db_alg.delete(self.alg_id) self.mqttclient.loop_stop() self.observer.stop() def startmqtt(self): self.mqttclient = mqtt.Client("feeder_" + self.alg_id) self.mqttclient.connect(self.mqtthost) self.mqttclient.on_message = self.mqtt_handler self.mqttclient.loop_start() self.mqttclient.subscribe(self.mqtttopic) def mqtt_handler(self, client, userdata, message): m_in = json.loads(str(message.payload.decode("utf-8", "ignore"))) for tag in m_in["tags"]: m_in[tag] = m_in["tags"][tag] del m_in["tags"] m_in["time"] = m_in["real_base"] + m_in["timestamp"] # convert to milliseconds m_in["time"] = int(m_in["time"] / 1000000) self.inference_cache.append(m_in) if len(self.inference_cache) >= self.batchsize: try: self.db_inf.ingest_bulk(self.inference_cache[:self.batchsize]) self.inference_cache = self.inference_cache[self.batchsize:] except Exception as e: logger.debug("Ingest Error: " + str(e)) def monitor_cameras(self): logger.debug("Starting Sensor Monitor Thread") while self._threadflag: logger.debug("Searching for sensors...") try: for sensor in self.db_sensors.search( "sensor:'camera' and status:'idle' and office:[" + str(self.office[0]) + "," + str(self.office[1]) + "]"): logger.debug(sensor) try: fswatch = None logger.debug("Sensor found! " + sensor["_id"]) logger.debug("Setting sensor " + sensor["_id"] + " to streaming") r = self.db_sensors.update(sensor["_id"], {"status": "streaming"}, version=sensor["_version"]) logger.debug( "Setting algorithm to streaming from sensor " + sensor["_id"]) r = self.db_alg.update(self.alg_id, { "source": sensor["_id"], "status": "processing" }) # Attempt to POST to VA service jsonData = { "source": { "uri": sensor["_source"]["url"], "type": "uri" }, "tags": { "algorithm": self.alg_id, "sensor": sensor["_id"], "office": { "lat": self.office[0], "lon": self.office[1], }, }, "parameters": { "every-nth-frame": self.every_nth_frame, "recording_prefix": "recordings/" + sensor["_id"], "method": "mqtt", "address": self.mqtthost, "clientid": self.alg_id, "topic": self.mqtttopic }, } folderpath = os.path.join( os.path.realpath(self.recording_volume), sensor["_id"]) if not os.path.exists(folderpath): os.makedirs(folderpath) logger.debug("Adding folder watch for " + folderpath) filehandler = FSHandler( sensor=sensor["_id"], office=self.office, dbhost=self.dbhost, rec_volume=self.recording_volume) fswatch = self.observer.schedule(filehandler, folderpath, recursive=True) try: logger.info("Posting Request to VA Service") r = requests.post(self.vahost + "/object_detection/2", json=jsonData, timeout=10) r.raise_for_status() pipeline_id = None if r.status_code == 200: logger.debug("Started pipeline " + r.text) pipeline_id = int(r.text) while r.status_code == 200: logger.debug("Querying status of pipeline") r = requests.get(self.vahost + "/object_detection/2/" + str(pipeline_id) + "/status", timeout=10) r.raise_for_status() jsonValue = r.json() if "avg_pipeline_latency" not in jsonValue: jsonValue["avg_pipeline_latency"] = 0 state = jsonValue["state"] try: logger.debug("fps: ") logger.debug(str(jsonValue)) except: logger.debug("error") logger.debug("Pipeline state is " + str(state)) if state == "COMPLETED" or state == "ABORTED" or state == "ERROR": logger.debug("Pipeline ended") break self.db_alg.update( self.alg_id, { "performance": jsonValue["avg_fps"], "latency": jsonValue["avg_pipeline_latency"] * 1000 }) time.sleep(10) logger.debug("Setting sensor " + sensor["_id"] + " to disconnected") r = self.db_sensors.update( sensor["_id"], {"status": "disconnected"}) except requests.exceptions.RequestException as e: logger.error( "Feeder: Request to VA Service Failed: " + str(e)) logger.debug("Setting sensor " + sensor["_id"] + " to idle") r = self.db_sensors.update(sensor["_id"], {"status": "idle"}) except Exception as e: logger.error("Feeder Exception: " + str(e)) if fswatch: self.observer.unschedule(fswatch) del (filehandler) logger.debug("Setting algorithm to idle") r = self.db_alg.update(self.alg_id, {"status": "idle"}) break except Exception as e: print(e, flush=True) time.sleep(5) logger.debug("Sensor monitor thread done")
print(traceback.format_exc(), flush=True) def quit_service(signum, sigframe): stop.set() signal(SIGTERM, quit_service) dba=DBIngest(host=dbhost, index="algorithms", office=office) dbs=DBQuery(host=dbhost, index="sensors", office=office) # register algorithm (while waiting for db to startup) dba.wait(stop) algorithm=dba.ingest({ "name": text["entrance-counting"], "office": { "lat": office[0], "lon": office[1], }, "status": "processing", "skip": every_nth_frame, })["_id"] # compete for a sensor connection while not stop.is_set(): try: print("Searching...", flush=True) for sensor in dbs.search("type:'camera' and status:'idle' and algorithm='entrance-counting' and office:["+str(office[0])+","+str(office[1])+"]"): try: # compete (with other va instances) for a sensor r=dbs.update(sensor["_id"],{"status":"streaming"},seq_no=sensor["_seq_no"],primary_term=sensor["_primary_term"]) # stream from the sensor
try: if dbt and rt: dbt.delete(rt["_id"]) except Exception as e: pass exit(143) signal(SIGTERM, quit_service) # register trigger dbt = DBIngest(index="services", office=office, host=dbhost) while True: try: rt = dbt.ingest({ "name": "triggers", "service": "alert trigger", "status": "active", }) break except Exception as e: print("Exception: " + str(e), flush=True) time.sleep(10) imbalance = ImbalanceTrigger() occupency = OccupencyTrigger() cpu = CPUTrigger() with ThreadPoolExecutor(3) as e: e.submit(imbalance.loop) e.submit(occupency.loop) e.submit(cpu.loop)
dbs = None rs = None def quit_service(signum, sigframe): if dbs and rs: dbs.delete(rs["_id"]) exit(143) signal(SIGTERM, quit_service) dbs = DBIngest(index="services", office=office, host=dbhost) while isinstance(office, list): try: rs = dbs.ingest({ "name": "cleanup", "service": "maintanence", "status": "active", }) break except Exception as e: print("Exception: " + str(e), flush=True) time.sleep(10) while True: time.sleep(service_interval) print("Searching...", flush=True) for index in indexes: db = DBQuery(index=index, office=office, host=dbhost) try: for r in db.search("time<now-" + str(retention_time * 1000),
class Handler(FileSystemEventHandler): def __init__(self, sensor): super(Handler, self).__init__() self._sensor = sensor self._db_rec = DBIngest(host=dbhost, index="recordings", office=office) self._db_cam = DBQuery(host=dbhost, index="sensors", office=office) self._last_file = None def on_created(self, event): print("on_created: " + event.src_path, flush=True) if event.is_directory: return if event.src_path.endswith(".png"): return if self._last_file: if self._last_file == event.src_path: return try: if psutil.cpu_percent() >= 80: time.sleep(2) # yield on busy self._process_file(self._last_file) except Exception as e: print("Exception: " + str(e), flush=True) self._last_file = event.src_path def _ffmpeg_convert(self, filename): print("post-processing " + filename, flush=True) with tempfile.TemporaryDirectory() as tmpdirname: filename = os.path.abspath(filename) tmpfilename = os.path.abspath( os.path.join(tmpdirname, os.path.basename(filename))) try: list( run([ "/usr/bin/ffmpeg", "-i", filename, "-c", "copy", tmpfilename ])) shutil.move(tmpfilename, filename) list( run([ "/usr/bin/ffmpeg", "-i", filename, "-vf", "thumbnail,scale=640:360", "-frames:v", "1", filename + ".png" ])) return filename, probe(filename) except Exception as e: print("Exception: " + str(e), flush=True) return None, None def _get_timestamp(self, filename): parsed = os.path.basename(filename).split('_') return int(int(parsed[-2]) / 1000000) def _process_file(self, filename): converted_file, sinfo = self._ffmpeg_convert(filename) if not converted_file: return sinfo.update({ "sensor": self._sensor, "office": { "lat": office[0], "lon": office[1], }, "time": self._get_timestamp(converted_file), "path": os.path.abspath(converted_file).split( os.path.abspath(storage) + "/")[1], }) # calculate total bandwidth bandwidth = 0 for stream1 in sinfo["streams"]: if "bit_rate" in stream1: bandwidth = bandwidth + stream1["bit_rate"] if bandwidth: self._db_cam.update(self._sensor, {"bandwidth": bandwidth}) self._db_rec.ingest(sinfo)
"time": timestamp, "office": str(office[0]) + "," + str(office[1]), "sensor": sensor, }, files={ "file": fd, }, verify=False) signal(SIGTERM, quit_service) dbs = DBIngest(index="services", office=office, host=dbhost) dbs.wait(stop) rs = dbs.ingest({ "name": text["smart-upload"], "service": text["maintanence"], "status": "active", }) dbq = DBQuery(index="recordings", office=office, host=dbhost) dbs = DBQuery(index="sensors", office=office, host=dbhost) dba = DBQuery(index="analytics", office=office, host=dbhost) dbsq_c = DBQuery(index="sensors", office="", host=dbchost) dbsi_c = DBIngest(index="sensors", office="", host=dbchost) dba_c = DBIngest(index="analytics", office="", host=dbchost) while not stop.is_set(): print("Searching...", flush=True) try: for q in dbq.search("not evaluated=true", size=25):
"time": timestamp, "office": str(office[0]) + "," + str(office[1]), "sensor": sensor, }, files={ "file": fd, }, verify=False) signal(SIGTERM, quit_service) dbs = DBIngest(index="services", office=office, host=dbhost) dbs.wait(stop) rs = dbs.ingest({ "name": text["smart-upload"], "service": text["maintanence"], "status": "active", }) dbq = DBQuery(index="recordings", office=office, host=dbhost) dba = DBQuery(index="analytics", office=office, host=dbhost) while not stop.is_set(): print("Searching...", flush=True) try: for q in dbq.search("evaluated=false", size=25): if stop.is_set(): break # mark it as evaluated dbq.update(q["_id"], {"evaluated": True})
dbs = None rs = None def quit_service(signum, sigframe): if dbs and rs: dbs.delete(rs["_id"]) exit(143) signal(SIGTERM, quit_service) dbs = DBIngest(index="services", office=office, host=dbhost) while True: try: dbs.ingest({ "name": "where-indexing", "service": "maintanence", "status": "active", }) break except Exception as e: print("Exception: " + str(e), flush=True) time.sleep(10) dbq = DBQuery(index=indexes[0], office=office, host=dbhost) dba = DBQuery(index=indexes[1], office=office, host=dbhost) while True: print("Sleeping...") time.sleep(service_interval) print("Searching...", flush=True) try:
def quit_service(signum, sigframe): try: if dbt and rt: dbt.delete(rt["_id"]) except Exception as e: pass exit(143) signal(SIGTERM, quit_service) # register trigger dbt=DBIngest(index="services",office=office,host=dbhost) while True: try: rt=dbt.ingest({ "name": "health_check", "service": "trigger", "status": "active", }) break except Exception as e: print("Exception: "+str(e), flush=True) time.sleep(10) dbs=DBQuery(index="sensors",office=office,host=dbhost) dba=DBQuery(index="algorithms",office=office,host=dbhost) dbat=DBIngest(index="alerts",office=office,host=dbhost) while True: try: nsensors={ "total": dbs.count("sensor:*"),
stop = Event() def quit_service(signum, sigframe): stop.set() signal(SIGTERM, quit_service) dbs = DBIngest(index="services", office=office, host=dbhost) rs = None if isinstance(office, list): dbs.wait(stop) rs = dbs.ingest({ "name": text["cleanup"], "service": text["maintenance"], "status": "active", }) while not stop.is_set(): print("Searching...", flush=True) for index in indexes: if stop.is_set(): break db = DBQuery(index=index, office=office, host=dbhost) try: for r in db.search("time<now-" + str(retention_time * 1000), size=500): if stop.is_set(): break # delete the record
_include_type_name = {"include_type_name": "false"} for index in settings: print("Initialize index " + index, flush=True) host = dbhost if index.endswith(officestr) else dbchost if host: r = requests.put(host + "/" + index, json=settings[index], params=_include_type_name) print(r.json(), flush=True) officeinfo = Provision(officestr) if dbchost: print("Register office {}".format(office), flush=True) dbo_c = DBIngest(index="offices", office="", host=dbchost) dbo_c.ingest(officeinfo, id1=officestr) print("Signal starting up", flush=True) dbs.ingest({"type": "startup"}) break except Exception as e: print(traceback.format_exc(), flush=True) print("Waiting for DB...", flush=True) time.sleep(1) print("DB Initialized", flush=True) while True: time.sleep(10000)
"sensor": sensor, }, files={ "file": fd, }, verify=False) os.remove(filename) signal(SIGTERM, quit_service) dbs = DBIngest(index="services", office=office, host=dbhost) while True: try: dbs.ingest({ "name": "smart-upload", "service": "maintanence", "status": "active", }) break except Exception as e: print("Exception: " + str(e), flush=True) time.sleep(10) dbq = DBQuery(index=indexes, office=office, host=dbhost) while True: print("Searching...", flush=True) print("query = ", query) try:
scenario = os.environ["SCENARIO"] zone = os.environ["ZONE"] print("Provisioning...", flush=True) # populate db with simulated offices and provisionings with open("/run/secrets/sensor-info.json", encoding='utf-8') as fd: data = json.load(fd) dbo = DBIngest(index="offices", office="", host=dbhost) dbp = DBIngest(index="provisions", office=office, host=dbhost) for office1 in data: if scenario != office1["scenario"]: continue location1 = office1["location"] if location1["lat"] != office[0] or location1["lon"] != office[1]: continue sensors = office1.pop("sensors") for s in sensors: s["office"] = location1 if "ip" in s: # convert IP to CIDR if s["ip"].find("/") < 0: s["ip"] = s["ip"] + "/32" dbp.ingest_bulk(sensors) office1.pop("scenario") office1["uri"] = proxyhost office1["zone"] = zone dbo.ingest(office1, officestr) print("DB Initialized", flush=True)
cameras[mac]=camera_count camera_count=camera_count+1 location = locations[int(cameras[mac] % (len(locations)))] print("location: ["+str(location[0])+","+str(location[1])+"]",flush=True) try: print("Checking for preexistance", flush=True) found=list(dbs.search("sensor:'camera' and model:'ip_camera' and mac='"+mac+"'", size=1)) if not found: print("Ingesting", flush=True) db.ingest({ 'sensor': 'camera', 'icon': 'camera.gif', 'office': { 'lat': office[0], 'lon': office[1] }, 'model': 'ip_camera', 'resolution': { 'width': width, 'height': height }, 'location': { "lat": location[0], "lon": location[1] }, 'url': rtspuri, 'mac': mac, 'theta': 105.0, 'mnth': 75.0, 'alpha': 45.0, 'fovh': 90.0, 'fovv': 68.0, 'status': 'idle', }) except Exception as e: print("Exception: "+str(e), flush=True) time.sleep(service_interval)
if rec2db: rec2db.stop() if runva: runva.stop() signal(SIGTERM, quit_service) dba = DBIngest(host=dbhost, index="algorithms", office=office) dbs = DBQuery(host=dbhost, index="sensors", office=office) # register algorithm (while waiting for db to startup) while True: try: algorithm = dba.ingest({ "name": "object_detection", "office": { "lat": office[0], "lon": office[1], }, "status": "processing", "skip": every_nth_frame, })["_id"] break except Exception as e: print("Exception: " + str(e), flush=True) time.sleep(10) # compete for a sensor connection while not stop: try: print("Searching...", flush=True) for sensor in dbs.search( "sensor:'camera' and status:'idle' and algorithm='object-detection' and office:["
db = DBIngest(index="sensors", office=office, host=dbhost) while True: try: r = db.ingest({ "sensor": "camera", "icon": "camera.gif", "office": { "lat": office[0], "lon": office[1] }, "model": "simulation", "resolution": { "width": resolution[0], "height": resolution[1] }, "location": { "lat": location[0], "lon": location[1] }, "url": "rtsp://" + hostname + ":8554/live.sdp", "mac": "0098c00" + str(6963 + sensor_id), 'theta': theta, 'mnth': mnth, 'alpha': alpha, 'fovh': fovh, 'fovv': fovv, "status": "idle", }) break except Exception as e: print("Exception: " + str(e), flush=True)
class FSHandler(FileSystemEventHandler): def __init__(self, sensor, office, dbhost, rec_volume): self.sensor = sensor self.office = office self.db_rec = DBIngest(host=dbhost, index="recordings", office=office) self.db_sensors = DBQuery(host=dbhost, index="sensors", office=office) self.recording_volume = rec_volume self.last_file = None self.finalize_timer = None self.record_cache = [] self.timeout = 80 #set to 20 seconds... this should change according to recording chunk length def on_created(self, event): if event.is_directory: return if event.src_path.endswith(".png"): return if self.last_file and (self.last_file == event.src_path): return if self.finalize_timer: self.finalize_timer.cancel() if self.last_file: try: self.ingest() except Exception as error: logger.error("Failed to ingest: %s %s\n" % (self.last_file, error)) logger.debug("Started recording new file! " + event.src_path) self.last_file = event.src_path del (self.finalize_timer) self.finalize_timer = Timer(self.timeout, self.ingest) self.finalize_timer.start() logger.debug("Started file watch timer for " + str(self.timeout) + " seconds") def ffmpeg_convert(self, filename): with tempfile.TemporaryDirectory() as tmpdirname: filename = os.path.abspath(filename) tmpfilename = os.path.abspath( os.path.join(tmpdirname, os.path.basename(filename))) output = "" try: list( run([ "/usr/bin/ffmpeg", "-i", filename, "-c", "copy", tmpfilename ])) shutil.move(tmpfilename, filename) list( run([ "/usr/bin/ffmpeg", "-i", filename, "-vf", "thumbnail,scale=640:360", "-frames:v", "1", filename + ".png" ])) return filename, probe(filename) except Exception as error: logger.error("Error converting mp4 with ffmpeg: %s %s" % (error, error.output)) raise def get_timestamp(self, filename): parsed = os.path.basename(filename).split('_') return int(int(parsed[-2]) / 1000000) def ingest(self): logger.debug("Finished recording file " + self.last_file) converted_file, sinfo = self.ffmpeg_convert(self.last_file) sinfo.update({ "sensor": self.sensor, "office": { "lat": self.office[0], "lon": self.office[1], }, "time": self.get_timestamp(converted_file), "path": os.path.abspath(converted_file).split( os.path.abspath(self.recording_volume) + "/")[1], }) # calculate total bandwidth bandwidth = 0 for stream1 in sinfo["streams"]: if "bit_rate" in stream1: bandwidth = bandwidth + stream1["bit_rate"] self.db_sensors.update(self.sensor, {"bandwidth": bandwidth}) self.db_rec.ingest(sinfo) self.record_cache.append(sinfo)
'status': 'idle', }) if not dbhost: continue try: if not r: # new camera print("Searching for template", flush=True) template = list( dbp.search(" or ".join( ['{}="{}"'.format(id1[0], id1[1]) for id1 in camids]), size=1)) if not template: template = list( dbp.search("ip={} and port={}".format(ip, port), size=1)) if template: print("Ingesting", flush=True) record = template[0]["_source"] record.update(sinfo) dbi.ingest(record) else: print("Template not found", flush=True) else: # camera re-connect dbs.update(r[0]["_id"], sinfo) except Exception as e: print(traceback.format_exc(), flush=True) if not dbhost: break time.sleep(service_interval)
def quit_service(signum, sigframe): try: if dbt and rt: dbt.delete(rt["_id"]) except Exception as e: pass exit(143) signal(SIGTERM, quit_service) # register trigger dbt=DBIngest(index="triggers",office=office,host=dbhost) while True: try: rt=dbt.ingest({ "name": "health_check", "status": "processing", }) break except Exception as e: print("Exception: "+str(e), flush=True) time.sleep(10) dbs=DBQuery(index="sensors",office=office,host=dbhost) dba=DBQuery(index="algorithms",office=office,host=dbhost) dbat=DBIngest(index="alerts",office=office,host=dbhost) while True: try: nsensors={ "total": dbs.count("sensor:*"), "streaming": dbs.count("status:'streaming'"),
def _rec2db(self, office, sensor, timestamp, path): disk_usage=psutil.disk_usage(self._storage)[3] if disk_usage<halt_rec_th: dt=datetime.datetime.fromtimestamp(timestamp/1000) officestr=(str(office[0])+"c"+str(office[1])).replace("-","n").replace(".","d") mp4path=self._storage+"/"+officestr+"/"+sensor+"/"+str(dt.year)+"/"+str(dt.month)+"/"+str(dt.day) os.makedirs(mp4path,exist_ok=True) mp4file=mp4path+"/"+str(timestamp)+".mp4" # perform a straight copy to fix negative timestamp for chrome list(run(["/usr/local/bin/ffmpeg","-f","mp4","-i",path,"-c","copy",mp4file])) sinfo=probe(mp4file) sinfo.update({ "sensor": sensor, "office": { "lat": office[0], "lon": office[1], }, "time": timestamp, "path": mp4file[len(self._storage)+1:], }) else: print("Disk full: recording halted", flush=True) sinfo=None if local_office: if sinfo["bandwidth"]: db_cam=DBQuery(host=dbhost, index="sensors", office=office) db_cam.update(sensor, {"bandwidth": sinfo["bandwidth"]}) # check disk usage and send alert disk_usage=psutil.disk_usage(self._storage).percent if disk_usage>=warn_disk_th: level="fatal" if disk_usage>=fatal_disk_th else "warning" db_alt=DBIngest(host=dbhost, index="alerts", office=office) message=text["halt recording"].format(disk_usage) if disk_usage>=halt_rec_th else text["disk usage"].format(disk_usage) db_alt.ingest({ "time": int(time.time()*1000), "office": { "lat": office[0], "lon": office[1], }, "location": { "lat": office[0], "lon": office[1], }, level: [{ "message": message, "args": { "disk": disk_usage, } }] }) # ingest recording local if sinfo: db_rec=DBIngest(host=dbhost, index="recordings", office=office) db_rec.ingest(sinfo) else: # ingest recording cloud if sinfo: db_s=DBQuery(host=dbhost, index="sensors", office=sinfo["office"]) sensor=list(db_s.search("_id='"+sinfo["sensor"]+"'",size=1)) if sensor: # remove status sensor[0]["_source"].pop("status",None) # denormalize address sinfo["address"]=sensor[0]["_source"]["address"] # calcualte hash code for the sensor m=hashlib.md5() m.update(json.dumps(sensor[0]["_source"],ensure_ascii=False).encode('utf-8')) md5=m.hexdigest() # locate the sensor record in cloud db_sc=DBQuery(host=dbhost, index="sensors", office="") sensor_c=list(db_sc.search("md5='"+md5+"'",size=1)) if not sensor_c: # if not available, ingest a sensor record in cloud sensor_c=[{ "_source": sensor[0]["_source"].copy() }] sensor_c[0]["_source"]["md5"]=md5 db_sc=DBIngest(host=dbhost, index="sensors", office="") print("Ingest sensor: {}".format(sensor_c[0]["_source"]), flush=True) sensor_c[0]=db_sc.ingest(sensor_c[0]["_source"]) # replace cloud sensor id and ingest recording sinfo["sensor"]=sensor_c[0]["_id"] print("Ingest recording: {}".format(sinfo), flush=True) db_rec=DBIngest(host=dbhost, index="recordings", office="") db_rec.ingest(sinfo) # copy local analytics to cloud db_a=DBQuery(host=dbhost, index="analytics", office=sinfo["office"]) data=[] for r in db_a.search('sensor="'+sensor[0]["_id"]+'" and office:['+str(office[0])+','+str(office[1])+'] and time>='+str(sinfo["time"])+' and time<='+str(sinfo["time"]+sinfo["duration"]*1000),size=10000): r["_source"]["sensor"]=sinfo["sensor"] data.append(r["_source"]) db_ac=DBIngest(host=dbhost, index="analytics", office="") print("Ingest analytics: {}".format(len(data)), flush=True) db_ac.ingest_bulk(data)
def _rec2db(self, office, sensor, timestamp, path): dt = datetime.datetime.fromtimestamp(timestamp / 1000) officestr = (str(office[0]) + "c" + str(office[1])).replace( "-", "n").replace(".", "d") mp4path = self._storage + "/" + officestr + "/" + sensor + "/" + str( dt.year) + "/" + str(dt.month) + "/" + str(dt.day) os.makedirs(mp4path, exist_ok=True) mp4file = mp4path + "/" + str(timestamp) + ".mp4" list( run([ "/usr/bin/ffmpeg", "-f", "mp4", "-i", path, "-c", "copy", mp4file ])) list( run([ "/usr/bin/ffmpeg", "-i", mp4file, "-vf", "scale=640:360", "-frames:v", "1", mp4file + ".png" ])) sinfo = probe(mp4file) sinfo.update({ "sensor": sensor, "office": { "lat": office[0], "lon": office[1], }, "time": timestamp, "path": mp4file[len(self._storage) + 1:], }) if local_office: # calculate total bandwidth bandwidth = 0 for stream1 in sinfo["streams"]: if "bit_rate" in stream1: bandwidth = bandwidth + stream1["bit_rate"] if bandwidth: db_cam = DBQuery(host=dbhost, index="sensors", office=office) db_cam.update(sensor, {"bandwidth": bandwidth}) # check disk usage and send alert disk_usage = psutil.disk_usage(self._storage)[3] if disk_usage > 75 and sensor_index: level = "fatal" if disk_uage > 85 else "warning" db_alt = DBIngest(host=dbhost, index="alerts", office=office) db_alt.ingest({ "time": int( time.mktime(datetime.datetime.now().timetuple()) * 1000), "office": { "lat": office[0], "lon": office[1], }, "location": { "lat": office[0], "lon": office[1], }, level: [{ "message": "Disk usage: " + str(disk_usage) + "%", "args": { "disk": disk_usage, } }] }) # ingest recording local db_rec = DBIngest(host=dbhost, index="recordings", office=office) db_rec.ingest(sinfo) else: # ingest recording cloud db_rec = DBIngest(host=dbhost, index="recordings_c", office="") db_rec.ingest(sinfo)
# initialize db index settings for index in settings: while True: try: r=requests.put(dbhost+"/"+index,json=settings[index]) break except Exception as e: print("Exception: "+str(e),flush=True) time.sleep(10) # populate db with simulated offices and provisions with open("/run/secrets/sensor-info.json","rt") as fd: data=json.load(fd) dbo=DBIngest(index="offices",office="",host=dbhost) dbp=DBIngest(index="provisions", office=office, host=dbhost) for office1 in data: if scenario != office1["scenario"]: continue location1=office1["location"] if location1["lat"]!=office[0] or location1["lon"]!=office[1]: continue office1.pop("scenario") sensors=office1.pop("sensors") office1["uri"]=proxyhost dbo.ingest(office1,officestr[1:]) for s in sensors: s["office"]=location1 dbp.ingest_bulk(sensors) print("DB Initialized", flush=True)
office = list(map(float, env["OFFICE"].split(","))) dbhost = env["DBHOST"] stop = Event() def quit_service(signum, sigframe): stop.set() signal(SIGTERM, quit_service) # register trigger dbt = DBIngest(index="services", office=office, host=dbhost) dbt.wait(stop) rt = dbt.ingest({ "name": text["alert trigger"], "service": text["triggers"], "status": "active", }) imbalance = ImbalanceTrigger() occupency = OccupencyTrigger() cpu = CPUTrigger() with ThreadPoolExecutor(3) as e: e.submit(imbalance.loop, stop) e.submit(occupency.loop, stop) e.submit(cpu.loop, stop) dbt.delete(rt["_id"])
try: if dbt and rt: dbt.delete(rt["_id"]) except Exception as e: pass exit(143) signal(SIGTERM, quit_service) # register trigger dbt = DBIngest(index="services", office=office, host=dbhost) while True: try: rt = dbt.ingest({ "name": "health_check", "service": "trigger", "status": "active", }) break except Exception as e: print("Exception: " + str(e), flush=True) time.sleep(10) dbs = DBQuery(index="sensors", office=office, host=dbhost) dba = DBQuery(index="algorithms", office=office, host=dbhost) dbat = DBIngest(index="alerts", office=office, host=dbhost) while True: try: nsensors = { "total": dbs.count("sensor:*"),