def Load(position, data): data[Areas.AREA_TO_AVOID] = [] data[Areas.SAFE_ZONES] = [] data[Areas.DISPLAY_POLY] = [] for safeZone in Areas.PREDEFINED_SAFE_ZONES: data[Areas.SAFE_ZONES].append({ "radius": Areas.DEFAULT_POSITION_RANGE_IN_DEG, "point": [safeZone[0], safeZone[1]] }) try: cellIDs = Areas.GetCellIDs(position, Areas.DEFAULT_POSITION_RANGE_IN_DEG) for cellID in cellIDs: cell = GetDB().Cells.find_one({"cellID": cellID}) print(cell) if cell: for areaID in cell["areas"]: area = GetDB().Areas.find_one({"areaID": areaID}) if area: areaType = area["type"] if areaType == Areas.DISPLAY_POLY: if area["data"] not in data[areaType]: data[areaType].append(area["data"]) data[Areas.AREA_TO_AVOID].append({ "boundary": Areas.CalculateAreaToAvoid( area["data"]["points"]), "risk": area["data"]["risk"] }) except Exception as e: print("Failed to Load areas: %s, error: %s" % (position, e))
def UpdateCells(boundary, areaID, add=True): try: cellIDs = Areas.GetCellIDsWithBoundary(boundary) for cellID in cellIDs: cell = GetDB().Cells.find_one({"cellID": cellID}) if cell: update = False if add: if areaID not in cell["areas"]: cell["areas"].append(areaID) update = True else: if areaID in cell["areas"]: cell["areas"].remove(areaID) if len(cell) == 0: GetDB().Cells.delete_one({"cellID": cellID}) else: update = True if update: GetDB().Cells.update_one( {"cellID": cellID}, {"$set": { "areas": cell["areas"] }}) elif add: GetDB().Cells.insert_one({ "cellID": cellID, "areas": [areaID] }) except Exception as e: print("Failed to update cells: %s, error: %s" % (boundary, e))
def AddAreaToAvoid(boundary, risk, dataRev): leftTopPoint = [boundary[0], boundary[1]] rightBottomPoint = [boundary[2], boundary[3]] cellsMin = Areas.GetCellIDs(leftTopPoint, 0) cellsMax = Areas.GetCellIDs(rightBottomPoint, 0) areaID = "ATA" + cellsMin[0] + cellsMax[0] boundary = [ leftTopPoint[0], leftTopPoint[1], rightBottomPoint[0], rightBottomPoint[1] ] try: GetDB().Areas.delete_one({"areaID": areaID}) except: pass try: GetDB().Areas.insert_one({ "areaID": areaID, "type": Areas.AREA_TO_AVOID, "rev": dataRev, "data": { "boundary": boundary, "risk": risk } }) Areas.UpdateCells(boundary, areaID, add=True) except Exception as e: print("Failed to add area to avoid: %s, error: %s" % (boundary, e))
def AddDisplayPoly(points, risk, dataRev, type): boundary = Areas.GetBoundary(points) leftTopPoint = [boundary[0], boundary[1]] rightBottomPoint = [boundary[2], boundary[3]] cellsMin = Areas.GetCellIDs(leftTopPoint, 0) cellsMax = Areas.GetCellIDs(leftTopPoint, 0) areaID = "DP" + cellsMin[0] + cellsMax[0] boundary = [ leftTopPoint[0], leftTopPoint[1], rightBottomPoint[0], rightBottomPoint[1] ] try: GetDB().Areas.delete_one({"areaID": areaID}) except: pass try: GetDB().Areas.insert_one({ "areaID": areaID, "type": Areas.DISPLAY_POLY, "data": { "points": points, "risk": risk, "type": type } }) Areas.UpdateCells(boundary, areaID, add=True) except Exception as e: print("Failed to add display poly: %s, error: %s" % (boundary, e))
def EnumerateAlerts(callback): result = None try: alerts = GetDB().Alerts.find() for alert in alerts: clientID = alert["clientID"] dangerLevel = alert[Alerts.DANGER_LEVEL] if callback(clientID, dangerLevel): # Delete processed alert GetDB().Alerts.delete_one({"clientID": clientID}) except Exception as e: print("Failed to enumerate alerts, error: %s" % e)
def FindClient(clientID): result = None try: result = GetDB().ActiveClients.find_one({"clientID": clientID}) except: pass return result
def GetNumberOfClients(): result = None try: clients = GetDB().ActiveClients.find() result = len(clients) except: print("Failed to calculate clients, error: %s" % e) return result
def PushAlert(clientID, dangerLevel): try: GetDB().Alerts.insert_one({ "clientID": clientID, Alerts.DANGER_LEVEL: dangerLevel }) except Exception as e: print("Failed to connect client: %s, error: %s" % (clientID, e))
def EnumerateClients(callback): result = None try: clients = GetDB().ActiveClients.find() for client in clients: callback(client) except Exception as e: print("Failed to enumerate clients, error: %s" % e)
def GetPoints(): result = [] try: firepoints = GetDB().FirePoints.find() for firepoint in firepoints: result.append(firepoint) except Exception as e: print("Failed to enumerate fire points, error: %s" % e) return result
def GetAreas(): result = [] try: areas = GetDB().Areas.find() for area in areas: result.append(area) except Exception as e: print("Failed to enumerate areas, error: %s" % e) return result
def Connect(clientID, reply_channel): try: GetDB().ActiveClients.insert_one({ "clientID": clientID, "notified": 0 }) ActiveClients._responseChannels[clientID] = reply_channel except Exception as e: print("Failed to connect client: %s, error: %s" % (clientID, e))
def Load(position, data): data[Areas.AREA_TO_AVOID] = [] data[Areas.SAFE_ZONES] = Areas.PREDEFINED_SAFE_ZONES data[Areas.DISPLAY_POLY] = [] try: cellIDs = Areas.GetCellIDs(position, Areas.DEFAULT_POSITION_RANGE_IN_DEG) for cellID in cellIDs: cell = GetDB().Cells.find_one({"cellID": cellID}) if cell: for areaID in cell["areas"]: area = GetDB().Areas.find({"areaID": areaID}) areaType = area["type"] data[areaType].append(area["data"]) except Exception as e: print("Failed to Load areas: %s, error: %s" % (position, e))
def GetCell(position): result = None try: cellIDs = Areas.GetCellIDs(position, 0) for cellID in cellIDs: result = GetDB().Cells.find_one({"cellID": cellID}) break except Exception as e: print("Failed to get cell areas: %s, error: %s" % (position, e)) return result
def Load(): try: url = "https://firms.modaps.eosdis.nasa.gov/active_fire/c6/text/MODIS_C6_Australia_and_New_Zealand_24h.csv" dataSource = urllib.request.urlopen(url) lines = dataSource.read() lines = lines.decode('utf-8') firstLine = True linesCounter = 0 linesLoaded = 0 GetDB().FirePoints.delete_many({}) GetDB().Areas.delete_many({}) GetDB().Cells.delete_many({}) for line in lines.split('\n'): linesCounter = linesCounter + 1 if firstLine: firstLine = False continue #latitude,longitude,brightness,scan,track,acq_date,acq_time,satellite,confidence,version,bright_t31,frp,daynight firePoint = {} try: params = line.split(',') latStr = params[0] lngStr = params[1] point = (float(latStr), float(lngStr)) dateStr = params[5] timeStr = params[6] confidence = int(params[8]) if confidence > FirePoints.CONFIDENCE_THRESGHOLD: firePoint["point"] = point firePoint["timeStamp"] = dateStr + " " + timeStr FirePoints.GetWeatherData(firePoint) FirePoints.InsertFirePoint(firePoint) linesLoaded = linesLoaded + 1 except: continue except Exception as e: print("Failed to load firepoint %s, error: %s" % (line, e)) print("Loaded %d out of %d fire points" % (linesLoaded, linesCounter))
def RemoveAreaToAvoid(boundary): leftTopPoint = [boundary[0], boundary[1]] rightBottomPoint = [boundary[2], boundary[3]] cellsMin = Areas.GetCellIDs(leftTopPoint, 0) cellsMax = Areas.GetCellIDs(rightBottomPoint, 0) areaID = "ATA" + cellsMin[0] + cellsMax[0] boundary = [ leftTopPoint[0], leftTopPoint[1], rightBottomPoint[0], rightBottomPoint[1] ] try: GetDB().Areas.delete_one({"areaID": areaID}) Areas.UpdateCells(boundary, areaID, add=False) except Exception as e: print("Failed to remove area to avoid: %s, error: %s" % (boundary, e))
def AddAreaToAvoid(leftTopPoint, rightBottomPoint, risk): cellsMin = GetCellIDs(leftTopPoint, 0) cellsMax = GetCellIDs(leftTopPoint, 0) areaID = "ATA" + cellsMin[0] + cellsMax[0] boundary = [ leftTopPoint[0], leftTopPoint[1], rightBottomPoint[0], rightBottomPoint[1] ] try: GetDB().Areas.delete_one({"areaID": areaID}) except: pass try: GetDB().Areas.insert_one({ "areaID": areaID, "type": Areas.AREA_TO_AVOID, "data": { "boundary": boundary, "risk": risk } }) cellIDs = GetCellIDsWithBoundary(boundary) for cellID in cellIDs: cell = GetDB().Cells.find_one({"cellID": cellID}) if cell: if areaID not in cell["areas"]: cell["areas"].append({"areaID": areaID}) GetDB().Cells.update_one( {"cellID": cellID}, {"$set": { "areas": cell["areas"] }}) else: GetDB().Cells.insert_one({ "cellID": cellID, "type": Areas.AREA_TO_AVOID, "data": { "boundary": boundary, "risk": risk } }) GetDB().Cells.insert_one({"cellID": cellID, "areas": [areaID]}) except Exception as e: print("Failed to add area to avoid: %s, error: %s" % (boundary, e))
def InsertFirePoint(firePoint): try: GetDB().FirePoints.insert_one(firePoint) except Exception as e: print("Failed to insert firepoint, error: %s" % (e))
from datastore.clients import ActiveClients from datastore.db import GetDB # Remove out-of-date clients ActiveClients.RemoveAll() # Connect DB GetDB()
def IncDataRev(): GetDB().Configs.update_one({"config": "areas"}, {"$inc": { "updateRev": 1 }})
def GetDataRev(): config = GetDB().Configs.find_one({"config": "areas"}) if config: return config["updateRev"] GetDB().Configs.insert_one({"config": "areas", "updateRev": 0}) return 0
def Disconnect(clientID): try: GetDB().ActiveClients.delete_one({"clientID": clientID}) ActiveClients._responseChannels[clientID] = None except Exception as e: print("Failed to diconnect client: %s, error: %s" % (clientID, e))
def UpdateClient(clientID, operator, data): try: clientFilter = {"clientID": clientID} GetDB().ActiveClients.update_one(clientFilter, {operator: data}) except Exception as e: print("Failed to update client: %s, error: %s" % (clientID, e))
def RemoveAll(): GetDB().ActiveClients.delete_many({}) ActiveClients._responseChannels = {}