async def create(request): form = CreationForm(request) tenantInstance = TenantManagement(request.ctx.tenant_id) await tenantInstance.init(request.ctx.tenant_id) if form.validate_on_submit(): print("Form validated") print("eui=", form.eui.data) print("name=", form.name.data) res = await Request.createDevice( tenant_id=request.ctx.tenant_id, eui=form.eui.data, name=form.name.data ) # Checking if request worked if res == Request.REQ_ERROR: raise ServerError("Cannot create device", status_code=500) else: return response.redirect("/devices/list") rendered_template = await render( "devices_creation.html", request, form=form ) return response.html(rendered_template)
async def remove(request, zone_id): tenantInstance = TenantManagement(request.ctx.tenant_id) await tenantInstance.init(request.ctx.tenant_id) zoneInstance = ZoneManagement(zone_id) await zoneInstance.init(zone_id) rendered_template = await render( 'zone_removing.html', request, zone_id=zone_id, zoneInstance=zoneInstance, tenantInstance=tenantInstance ) return response.html(rendered_template)
async def view(request): tenantInstance = TenantManagement(request.ctx.tenant_id) await tenantInstance.init(request.ctx.tenant_id) pagination = Pagination(request) # Handling the two existing tabs (all devices & not assigned ones) tab_all = False tab_notAssigned = False # Not assigned devices data table if request.raw_args.get('type') == "notassigned": tab_notAssigned = True await tenantInstance.setNotAssignedDevices( page=pagination.page_number, pagesize=pagination.page_size ) devices = tenantInstance.notAssignedDevices pagination.setElementsNumber(tenantInstance.devicesNotAssignedCount) else: # All devices of the tenant tab_all = True await tenantInstance.setDevices( page=pagination.page_number, pagesize=pagination.page_size ) devices = tenantInstance.devices pagination.setElementsNumber(tenantInstance.devicesCount) # User wants to delete the device if request.method == 'POST': print("Deletion of device_id : ", request.form.get('device-id')) res = await Request.deleteDevice(request.form.get('device-id')) if res == Request.REQ_ERROR: raise ServerError("impossible de supprimer le capteur") return response.redirect("/dashboard") rendered_template = await render( "devices_template.html", request, devices=devices, pagination=pagination, active_tab_all=tab_all, active_tab_notassigned=tab_notAssigned ) return response.html(rendered_template)
async def maintenance(request, zone_id): tenantInstance = TenantManagement(request.ctx.tenant_id) await tenantInstance.init(request.ctx.tenant_id) zoneInstance = ZoneManagement(zone_id) await zoneInstance.init(zone_id) await zoneInstance.setSpots() rendered_template = await render( 'parking_template.html', request, active_tab_maintenance='true', zone_id=zone_id, zoneInstance=zoneInstance, tenantInstance=tenantInstance ) return response.html(rendered_template)
async def stats(request, zone_id): tenantInstance = TenantManagement(request.ctx.tenant_id) await tenantInstance.init(request.ctx.tenant_id) zoneInstance = ZoneManagement(zone_id) await zoneInstance.init(zone_id) statistics = zoneInstance.getAllStats() rendered_template = await render( 'parking_template.html', request, active_tab_stats='true', zone_id=zone_id, zoneInstance=zoneInstance, tenantInstance=tenantInstance, statistics=statistics ) return response.html(rendered_template)
async def map(request): tenantInstance = TenantManagement(request.ctx.tenant_id) await tenantInstance.init(request.ctx.tenant_id) await tenantInstance.setZones() if tenantInstance.zones is None: raise ServerError("No zones in DB", status_code=500) for zone in tenantInstance.zones: await zone.setSpots() zonesJson = Tooling.jsonList(tenantInstance.zones) rendered_template = await render("map_template.html", request, tenantInstance=tenantInstance, zoneList=zonesJson) return response.html(rendered_template)
async def remove_check(request, zone_id): # removing the zone # removing the spots if needed # temporary redirection to dashboard of tenant tenantInstance = TenantManagement(request.ctx.tenant_id) await tenantInstance.init(request.ctx.tenant_id) await tenantInstance.setZones() if tenantInstance.zones is None: raise ServerError("No zones in DB", status_code=500) rendered_template = await render( "dashboard_template.html", request, tenantInstance=tenantInstance, removed_zone=True ) return response.html(rendered_template)
async def overview(request, spot_id, zone_id): tenantInstance = TenantManagement(request.ctx.tenant_id) await tenantInstance.init(request.ctx.tenant_id) zoneInstance = ZoneManagement(zone_id) await zoneInstance.init(zone_id) spotInstance = SpotManagement(spot_id) await spotInstance.init(spot_id) rendered_template = await render('spot_template.html', request, active_tab_overview='true', zone_id=zone_id, spot_id=spot_id, tenantInstance=tenantInstance, zoneInstance=zoneInstance, spotInstance=spotInstance) return response.html(rendered_template)
async def dashboard(request): tenantInstance = TenantManagement(request.ctx.tenant_id) await tenantInstance.init(request.ctx.tenant_id) await tenantInstance.setZones() if tenantInstance.zones is None: raise ServerError("No zones in DB", status_code=500) for zone in tenantInstance.zones: res = await zone.setSpots() if res == Request.REQ_ERROR: raise ServerError("Spots not init", status_code=500) zonesJson = Tooling.jsonList(tenantInstance.zones) rendered_template = await render("dashboard_template.html", request, tenantInstance=tenantInstance, zonesJson=zonesJson) return response.html(rendered_template)
async def spots(request, zone_id): tenantInstance = TenantManagement(request.ctx.tenant_id) await tenantInstance.init(request.ctx.tenant_id) # Calculating and initialization of pagination pagination = Pagination(request) zoneInstance = ZoneManagement(zone_id) await zoneInstance.init(zone_id) await zoneInstance.setSpots( page=pagination.page_number, pagesize=pagination.page_size ) # Handling request and tab tab_map = True if request.raw_args.get('type') == "table": tab_map = False for spot in zoneInstance.spots: await spot.setDevice() pagination.setElementsNumber(zoneInstance.spotsCount) spotsJson = Tooling.jsonList(zoneInstance.spots) rendered_template = await render( 'parking_template.html', request, active_tab_list='true', zone_id=zone_id, zoneInstance=zoneInstance, tenantInstance=tenantInstance, spotList=spotsJson, pagination = pagination, tab_map=tab_map ) return response.html(rendered_template)
async def stats(request, spot_id, zone_id): tenantInstance = TenantManagement(123) await tenantInstance.init(request.ctx.tenant_id) zoneInstance = ZoneManagement(zone_id) await zoneInstance.init(zone_id) spotInstance = SpotManagement(spot_id) await spotInstance.init(spot_id) rendered_template = await render( 'spot_template.html', request, active_tab_stats='true', zone_id=zone_id, spot_id=spot_id, tenantInstance=tenantInstance, zoneInstance=zoneInstance, spotInstance=spotInstance, statistics=spotInstance.getAllStats(), ) return response.html(rendered_template)
async def zones(request): tenantInstance = TenantManagement(request.ctx.tenant_id) await tenantInstance.init(request.ctx.tenant_id) # Calculating and initialization of pagination pagination = Pagination(request) # Requesting zone from database res = await tenantInstance.setZones(page=pagination.page_number, pagesize=pagination.page_size) if tenantInstance.zones is None: raise ServerError("No zones in DB", status_code=500) pagination.setElementsNumber(tenantInstance.zonesCount) zonesJson = Tooling.jsonList(tenantInstance.zones) rendered_template = await render("tenant_zone_data_table.html", request, tenantInstance=tenantInstance, zonesList=zonesJson, pagination=pagination) return response.html(rendered_template)
async def config(request, zone_id): changes = False tenantInstance = TenantManagement(request.ctx.tenant_id) await tenantInstance.init(request.ctx.tenant_id) zoneInstance = ZoneManagement(zone_id) await zoneInstance.init(zone_id) await zoneInstance.setSpots() formGeneral = ConfigurationForm(request, zoneInstance, prefix="genForm") formSpots = SpotsAddingForm(request, prefix="spotsForm") # We need the not assigned devices from this tenant deviceList = await tenantInstance.getNotAssignedDevices() if deviceList == Request.REQ_ERROR: raise ServerError("Impossible de charger les capteurs non assignés.") # Creating form and setting device selection formSpots.deviceSelect.choices = deviceList # Informing user if no device is available if deviceList == []: formSpots.deviceSelect.description = "ATTENTION : aucun capteur disponible !" # Handling general configuration if formGeneral.validate_on_submit(): # Deletion clicked if formGeneral.delete.data: print("Zone deletion") res = await Request.deleteZone(zoneInstance.id) if res == Request.REQ_ERROR: raise ServerError("erreur lors de la suppression de la zone") else: print("RES: ", res) return response.redirect("/dashboard") # General form validated elif formGeneral.submitGen.data: print("General form validated") res = await Request.updateZone( zone_id=zone_id, tenant_id=request.ctx.tenant_id, name=formGeneral.name.data, type=formGeneral.type.data, color=Tooling.formatColor(formGeneral.color.data), polygon="" ) if res == Request.REQ_ERROR: raise ServerError("impossible de mettre à jour la zone", 500) changes = True # Checking if user wants to add spots if formSpots.validate_on_submit(): print("Spot adding form validated") lnglat = formSpots.coordinatesInput.data.split(',') res = await Request.createSpot( zone_id=zone_id, device_id=formSpots.deviceSelect.data, type=formSpots.typeSelect.data, coordinates=[float(lnglat[0]) ,float(lnglat[1])] ) if res == Request.REQ_ERROR: raise ServerError("impossible d'ajouter une place", 500) return response.redirect("/parking/zone/"+zone_id+"/spots") rendered_template = await render( 'parking_template.html', request, active_tab_config='true', zone_id=zone_id, zoneInstance=zoneInstance, tenantInstance=tenantInstance, formGeneral=formGeneral, formSpots=formSpots, spotList=Tooling.jsonList(zoneInstance.spots), changesApplied=changes ) return response.html(rendered_template)
async def config(request, spot_id, zone_id): tenantInstance = TenantManagement(request.ctx.tenant_id) res = await tenantInstance.init(request.ctx.tenant_id) if res == Request.REQ_ERROR: raise ServerError("impossible de récupérer le tenant", status_code=500) zoneInstance = ZoneManagement(zone_id) res = await zoneInstance.init(zone_id) if res == Request.REQ_ERROR: raise ServerError("impossible de récupérer la zone", status_code=500) spotInstance = SpotManagement(spot_id) res = await spotInstance.init(spot_id) if res == Request.REQ_ERROR: raise ServerError("impossible de récupérer la place", status_code=500) res = await spotInstance.setDevice() if res == Request.REQ_ERROR: raise ServerError("impossible de récupérer le capteur", status_code=500) # Creating and filing formular form = ConfigurationForm(request, spotInstance) # performing adaptation of coordinates string (remove '[' and ']') if '[' and ']' in form.coordinates.data: form.coordinates.data = form.coordinates.data[1:len(form.coordinates. data) - 2] # Making the device select choices deviceList = [] deviceList.append((spotInstance.device.id, spotInstance.device.eui)) res = await tenantInstance.getNotAssignedDevices() if res == Request.REQ_ERROR: raise ServerError("Impossible de charger les capteurs non assignés.") deviceList = deviceList + res form.device.choices = deviceList # Form validation check & request if form.validate_on_submit(): # The actual update request to the queue if form.submit.data: print("Submit validated") lnglat = form.coordinates.data.split(',') res = await Request.updateSpot( spot_id=spotInstance.id, device_id=form.device.data, type=form.type.data, coordinates=[float(lnglat[0]), float(lnglat[1])]) if res == Request.REQ_ERROR: raise ServerError("Impossible de mettre à jour la place.") return response.redirect("/parking/zone/" + zone_id + "/spot/" + spot_id) # Handling place deletion elif form.delete.data: print("spot deletion") res = await Request.deleteSpot(spotInstance.id) if res == Request.REQ_ERROR: raise ServerError("impossible de supprimer la place.") return response.redirect("/parking/zone/" + zone_id) rendered_template = await render('spot_template.html', request, active_tab_config='true', zone_id=zone_id, spot_id=spot_id, tenantInstance=tenantInstance, zoneInstance=zoneInstance, spotInstance=spotInstance, form=form) return response.html(rendered_template)