Example #1
0
        public async Task <IActionResult> GetMapData(MapSettingsInput input)
        {
            MapDataJson dataJson = new MapDataJson();

            var calls = await _callsService.GetActiveCallsByDepartmentAsync(DepartmentId);

            var department = await _departmentsService.GetDepartmentByIdAsync(DepartmentId, false);

            var stations = await _departmentGroupsService.GetAllStationGroupsForDepartmentAsync(DepartmentId);

            var lastUserActionlogs = await _actionLogsService.GetLastActionLogsForDepartmentAsync(DepartmentId);

            var personnelNames = await _departmentsService.GetAllPersonnelNamesForDepartmentAsync(DepartmentId);

            var unitStates = await _unitsService.GetAllLatestStatusForUnitsByDepartmentIdAsync(DepartmentId);

            var userLocationPermission = await _permissionsService.GetPermissionByDepartmentTypeAsync(DepartmentId, PermissionTypes.CanSeePersonnelLocations);

            if (userLocationPermission != null)
            {
                var userGroup = await _departmentGroupsService.GetGroupForUserAsync(UserId, DepartmentId);

                int?groupId = null;

                if (userGroup != null)
                {
                    groupId = userGroup.DepartmentGroupId;
                }

                var roles = await _personnelRolesService.GetRolesForUserAsync(UserId, DepartmentId);

                var allowedUsers = _permissionsService.GetAllowedUsers(userLocationPermission, DepartmentId, groupId, ClaimsAuthorizationHelper.IsUserDepartmentAdmin(), ClaimsAuthorizationHelper.IsUserDepartmentAdmin(), roles);

                lastUserActionlogs.RemoveAll(x => !allowedUsers.Contains(x.UserId));
            }

            if (input.ShowDistricts)
            {
                foreach (var station in stations)
                {
                    if (!String.IsNullOrWhiteSpace(station.Geofence))
                    {
                        GeofenceJson geofence = new GeofenceJson();
                        geofence.Name  = station.Name;
                        geofence.Color = station.GeofenceColor;
                        geofence.Fence = station.Geofence;

                        dataJson.Geofences.Add(geofence);
                    }
                }
            }

            if (input.ShowStations)
            {
                foreach (var station in stations)
                {
                    try
                    {
                        MapMakerInfo info = new MapMakerInfo();
                        info.ImagePath         = "Station";
                        info.Title             = station.Name;
                        info.InfoWindowContent = station.Name;

                        if (station.Address != null)
                        {
                            string coordinates =
                                await _geoLocationProvider.GetLatLonFromAddress(string.Format("{0} {1} {2} {3}", station.Address.Address1,
                                                                                              station.Address.City,
                                                                                              station.Address.State,
                                                                                              station.Address.PostalCode));

                            if (!String.IsNullOrEmpty(coordinates))
                            {
                                info.Latitude  = double.Parse(coordinates.Split(char.Parse(","))[0]);
                                info.Longitude = double.Parse(coordinates.Split(char.Parse(","))[1]);

                                dataJson.Markers.Add(info);
                            }
                        }
                        else if (!String.IsNullOrWhiteSpace(station.Latitude) && !String.IsNullOrWhiteSpace(station.Longitude))
                        {
                            info.Latitude  = double.Parse(station.Latitude);
                            info.Longitude = double.Parse(station.Longitude);

                            dataJson.Markers.Add(info);
                        }
                    }
                    catch (Exception ex)
                    {
                        //Logging.LogException(ex);
                    }
                }
            }

            if (input.ShowCalls)
            {
                foreach (var call in calls)
                {
                    MapMakerInfo info = new MapMakerInfo();
                    info.ImagePath         = "Call";
                    info.Title             = call.Name;
                    info.InfoWindowContent = call.NatureOfCall;

                    if (!String.IsNullOrEmpty(call.GeoLocationData))
                    {
                        try
                        {
                            info.Latitude  = double.Parse(call.GeoLocationData.Split(char.Parse(","))[0]);
                            info.Longitude = double.Parse(call.GeoLocationData.Split(char.Parse(","))[1]);

                            dataJson.Markers.Add(info);
                        }
                        catch
                        {
                        }
                    }
                    else if (!String.IsNullOrEmpty(call.Address))
                    {
                        string coordinates = await _geoLocationProvider.GetLatLonFromAddress(call.Address);

                        if (!String.IsNullOrEmpty(coordinates))
                        {
                            info.Latitude  = double.Parse(coordinates.Split(char.Parse(","))[0]);
                            info.Longitude = double.Parse(coordinates.Split(char.Parse(","))[1]);
                        }

                        dataJson.Markers.Add(info);
                    }
                }
            }

            if (input.ShowUnits)
            {
                foreach (var unit in unitStates)
                {
                    if (unit.Latitude.HasValue && unit.Latitude.Value != 0 && unit.Longitude.HasValue &&
                        unit.Longitude.Value != 0)
                    {
                        MapMakerInfo info = new MapMakerInfo();
                        info.ImagePath         = "Engine_Responding";
                        info.Title             = unit.Unit.Name;
                        info.InfoWindowContent = "";
                        info.Latitude          = double.Parse(unit.Latitude.Value.ToString());
                        info.Longitude         = double.Parse(unit.Longitude.Value.ToString());

                        dataJson.Markers.Add(info);
                    }
                }
            }

            if (input.ShowPersonnel)
            {
                foreach (var person in lastUserActionlogs)
                {
                    if (!String.IsNullOrWhiteSpace(person.GeoLocationData))
                    {
                        MapMakerInfo info = new MapMakerInfo();
                        info.ImagePath = "Person";

                        var name = personnelNames.FirstOrDefault(x => x.UserId == person.UserId);
                        if (name != null)
                        {
                            info.Title             = name.Name;
                            info.InfoWindowContent = "";
                        }
                        else
                        {
                            info.Title             = "";
                            info.InfoWindowContent = "";
                        }

                        var infos = person.GeoLocationData.Split(char.Parse(","));
                        if (infos != null && infos.Length == 2)
                        {
                            info.Latitude  = double.Parse(infos[0]);
                            info.Longitude = double.Parse(infos[1]);

                            dataJson.Markers.Add(info);
                        }
                    }
                }
            }

            if (input.ShowPOIs)
            {
                var poiTypes = await _mappingService.GetPOITypesForDepartmentAsync(DepartmentId);

                foreach (var poiType in poiTypes)
                {
                    foreach (var poi in poiType.Pois)
                    {
                        MapMakerInfo info = new MapMakerInfo();
                        info.ImagePath         = poiType.Image;
                        info.Marker            = poiType.Marker;
                        info.Title             = poiType.Name;
                        info.InfoWindowContent = "";
                        info.Latitude          = poi.Latitude;
                        info.Longitude         = poi.Longitude;
                        info.Color             = poiType.Color;

                        dataJson.Pois.Add(info);
                    }
                }
            }

            return(Json(dataJson));
        }