public async Task <IActionResult> Register(RegisterUnitDto registerUnitDto)
        {
            try
            {
                string message;
                Unit   unitExists = await _unitService.GetByMacAddress(registerUnitDto.MacAddress);

                if (unitExists == null)
                {
                    var unit       = _mapper.Map <Unit>(registerUnitDto);
                    var registered = _unitService.InsertUnit(unit);

                    message = "The unit has been registered.";
                    var response = new DeviceApiResponse <bool>(message);
                    return(Ok(response));
                }
                else
                {
                    message = "The Mac Address already exists in the database.";
                    var response = new DeviceApiResponse <bool>(message);
                    return(BadRequest(response));
                }
            }
            catch (Exception ex)
            {
                string message  = "ERROR";
                var    response = new ApiResponse <Exception>(ex, message);
                return(StatusCode(500, response));
            }
        }
Beispiel #2
0
        public async Task <IActionResult> AddFacility(AddFacilityDto addFacilityDto)
        {
            //#MISSING: ADD ORGANIZATIONID TO FACILITY DEPENDING ON USER LOGGED
            try
            {
                string message;
                bool   facilityExists = await _facilityService.ExistsFacility(addFacilityDto.Facility.Name, addFacilityDto.Facility.OrganizationId);

                if (!facilityExists)
                {
                    var address    = _mapper.Map <Address>(addFacilityDto);
                    var facility   = _mapper.Map <Facility>(addFacilityDto);
                    var invites    = _mapper.Map <List <Invite> >(addFacilityDto);
                    var units      = _mapper.Map <List <Unit> >(addFacilityDto);
                    var registered = _facilityService.AddFacility(address, facility, invites, units);

                    message = "The facility has been registered.";
                    var response = new DeviceApiResponse <bool>(message);
                    return(Ok(response));
                }
                else
                {
                    message = "The Facility name already exists in the Organization.";
                    var response = new DeviceApiResponse <bool>(message);
                    return(BadRequest(response));
                }
            }
            catch (Exception ex)
            {
                string message  = "ERROR";
                var    response = new ApiResponse <Exception>(ex, message);
                return(StatusCode(500, response));
            }
        }
Beispiel #3
0
 public IActionResult credentials_digested()
 {
     try
     {
         string message  = "SUCCESS";
         var    response = new DeviceApiResponse <bool>(message);
         return(Ok(response));
     }
     catch (Exception ex)
     {
         string message  = "ERROR";
         var    response = new DeviceApiResponse <Exception>(ex, message);
         return(StatusCode(500, response));
     }
 }
Beispiel #4
0
        public async Task <IActionResult> SetupOrganization(SetupOrganizationDto setupOrganizationDto)
        {
            //#MISSING: ADD ORGANIZATIONID TO ORGANIZATION DEPENDING ON USER LOGGED
            //ASSIGN DATA AND THEN VALIDATE THE WHOLE OBJECT
            try
            {
                string message;
                bool   organizationExists = await _organizationService.ExistsOrganization(setupOrganizationDto.Organization.Name);

                if (!organizationExists)
                {
                    var address   = _mapper.Map <Address>(setupOrganizationDto.Address);
                    var addressId = await _addressService.InsertAddress(address);

                    var organization = _mapper.Map <Organization>(setupOrganizationDto.Organization);
                    organization.AddressId = addressId;
                    var organizationId = await _organizationService.InsertOrganization(organization);

                    var facilities = _mapper.Map <List <Facility> >(setupOrganizationDto.Facilities);
                    facilities.ForEach(e => e.OrganizationId = organizationId);
                    var facilitiesIdDic = await _facilityService.InsertFacilitiesRange(facilities);

                    var invites = _mapper.Map <List <Invite> >(setupOrganizationDto.Users);
                    invites.ForEach(e => e.OrganizationId = organizationId);
                    await _inviteService.InsertInvitesRange(invites);

                    setupOrganizationDto.Units.ForEach(e => e.FacilityId = facilitiesIdDic[e.Facility]);
                    var units = _mapper.Map <List <Unit> >(setupOrganizationDto.Units);
                    await _unitService.InsertUnitsRange(units);

                    message = "The organization has been registered.";
                    var response = new DeviceApiResponse <bool>(message);
                    return(Ok(response));
                }
                else
                {
                    message = "The Organization name already exists in the Organization.";
                    var response = new DeviceApiResponse <bool>(message);
                    return(BadRequest(response));
                }
            }
            catch (Exception ex)
            {
                string message  = "ERROR";
                var    response = new ApiResponse <Exception>(ex, message);
                return(StatusCode(500, response));
            }
        }
Beispiel #5
0
 public IActionResult ota_update()
 {
     try
     {
         string link        = Path.Combine("Files", "esp32.bin");
         var    net         = new System.Net.WebClient();
         var    data        = net.DownloadData(link);
         var    content     = new MemoryStream(data);
         var    contentType = "APPLICATION/octet-stream";
         return(File(content, contentType));
     }
     catch (Exception ex)
     {
         string message  = "ERROR";
         var    response = new DeviceApiResponse <Exception>(ex, message);
         return(StatusCode(500, response));
     }
 }
Beispiel #6
0
        public IActionResult request_access(string encodedDeviceId)
        {
            try
            {
                string message;
                string decodedDeviceId = "";
                if (!String.IsNullOrEmpty(encodedDeviceId))
                {
                    try
                    {
                        byte[] deviceIdBytes = Convert.FromBase64String(encodedDeviceId);
                        decodedDeviceId = Encoding.UTF8.GetString(deviceIdBytes);
                    }
                    catch (Exception) //CREATE CUSTOM EXCEPTION FOR INVALID ENCODED DEVICE ID
                    {
                        message = "INVALID_DEVICEID";
                        var _response = new DeviceApiResponse <bool>(message);
                        return(BadRequest(_response));
                    }

                    message = "REQUEST_ACCESS_SUCCESS";
                    var claimsIdentity = new ClaimsIdentity(new List <Claim>()
                    {
                        new Claim(ClaimTypes.NameIdentifier, decodedDeviceId)
                    }, "Identity.Application");

                    var token    = _passwordService.CreateAccessToken(_passwordService.CreateJwtClaims(claimsIdentity));
                    var response = new DeviceApiResponse <string>(token, message);
                    return(Ok(response));
                }
                else
                {
                    message = "NULL_PARAMETER";
                    var response = new DeviceApiResponse <bool>(message);
                    return(BadRequest(response));
                }
            }
            catch (Exception ex)
            {
                string message  = "ERROR";
                var    response = new DeviceApiResponse <Exception>(ex, message);
                return(StatusCode(500, response));
            }
        }
Beispiel #7
0
 public IActionResult ota_json()
 {
     try
     { //update version
         string     message = "SUCCESS";
         OtaJsonDto info    = new OtaJsonDto()
         {
             version = 2.2F,
             file    = _urlOptions.GetBaseUrl() + "/api/device/ota_update",
             //file = _urlOptions.BaseUrl + "/api/device/ota_update"
         };
         var response = new DeviceApiResponse <OtaJsonDto>(info, message);
         return(Ok(response));
     }
     catch (Exception ex)
     {
         string message  = "ERROR";
         var    response = new DeviceApiResponse <Exception>(ex, message);
         return(StatusCode(500, response));
     }
 }
Beispiel #8
0
        public async Task <IActionResult> register(RegisterDto registerDto)
        {
            try
            {
                string message;
                if (registerDto != null && !String.IsNullOrEmpty(registerDto.device_id))
                {
                    Unit unitExists = await _unitService.GetByMacAddress(registerDto.device_id);

                    if (unitExists == null)
                    {
                        Unit unit = new Unit();
                        unit.MacAddress = registerDto.device_id;
                        var registered = _unitService.InsertUnit(unit);

                        message = "REGISTRATION_SUCCESS";
                        var response = new DeviceApiResponse <bool>(message);
                        return(Ok(response));
                    }
                    else
                    {
                        message = "DEVICE_DUPLICATED";
                        var response = new DeviceApiResponse <bool>(message);
                        return(BadRequest(response));
                    }
                }
                else
                {
                    message = "NULL PARAMETER";
                    var response = new DeviceApiResponse <bool>(message);
                    return(BadRequest(response));
                }
            }
            catch (Exception ex)
            {
                string message  = "ERROR";
                var    response = new DeviceApiResponse <Exception>(ex, message);
                return(StatusCode(500, response));
            }
        }
Beispiel #9
0
        public async Task <IActionResult> flush(FlushDto flushDto)
        {
            try
            { //HANDLE CUSTOM EXCEPTIONS //VALIDATE FORMATS
                string message;
                var    token = Request.Headers["Authorization"].ToString();
                if (!String.IsNullOrEmpty(token))
                {
                    var TokenData = _passwordService.ReadToken(token);

                    if (flushDto != null)
                    {
                        string macAddress = TokenData.Claims.FirstOrDefault().Value;

                        Unit unit = await _unitService.GetByMacAddress(macAddress);

                        if (unit != null)
                        { //USE MAP<> //BATTERY CAN BE FLOAT
                            Flush flush = new Flush();
                            flush.UnitId = unit.Id;
                            flush.Date   = UnixTimeStampToDateTime(double.Parse(flushDto.unix_timestamp));

                            filters_pressure pressure = flushDto.filters_pressure;
                            flush.Filter1 = double.Parse(pressure.f1.Replace("PSI", ""));
                            flush.Filter2 = double.Parse(pressure.f2.Replace("PSI", ""));
                            flush.Filter3 = double.Parse(pressure.f3.Replace("PSI", ""));
                            flush.Filter4 = double.Parse(pressure.f4.Replace("PSI", ""));

                            await _flushService.InsertFlush(flush);

                            message = "SUCCESS";
                            var response = new DeviceApiResponse <bool>(message);
                            return(Ok(response));
                        }
                        else
                        {
                            message = "DEVICE_NOT_FOUND";
                            var response = new DeviceApiResponse <bool>(message);
                            return(NotFound(response));
                        }
                    }
                    else
                    {
                        message = "NULL_PARAMETER";
                        var response = new DeviceApiResponse <bool>(message);
                        return(BadRequest(response));
                    }
                }
                else
                {
                    message = "ACCESS_DENIED";
                    var response = new DeviceApiResponse <bool>(message);
                    return(Unauthorized(response));
                }
            }
            catch (Exception ex)
            {
                string message;
                if (ex.Source == "System.IdentityModel.Tokens.Jwt")
                {
                    message = "INVALID_TOKEN";
                }
                else
                {
                    message = "ERROR";
                }
                var response = new DeviceApiResponse <Exception>(ex, message);
                return(StatusCode(500, response));
            }
        }
Beispiel #10
0
        public async Task <IActionResult> system_status(SystemStatusDto systemStatusDto)
        {
            try
            { //HANDLE CUSTOM EXCEPTIONS
                string message;
                var    token = Request.Headers["Authorization"].ToString();
                if (!String.IsNullOrEmpty(token))
                {
                    var TokenData = _passwordService.ReadToken(token);

                    if (systemStatusDto != null)
                    {
                        string macAddress = TokenData.Claims.FirstOrDefault().Value;

                        Unit unit = await _unitService.GetByMacAddress(macAddress);

                        if (unit != null)
                        { //SEPARATE SYSTEM_STATUS AND FLUSH DATA //USE MAP<> //BATTERY CAN BE FLOAT //VALIDATE FORMATS
                            SystemStatus systemStatus = new SystemStatus();
                            systemStatus.UnitId = unit.Id;
                            systemStatus.SelenoidTemperature = double.Parse(systemStatusDto.case_temperature.Replace("C", ""));
                            systemStatus.BatteryLevel        = int.Parse(systemStatusDto.battery.Replace("%", ""));
                            systemStatus.Date = UnixTimeStampToDateTime(double.Parse(systemStatusDto.unix_timestamp));

                            await _systemStatusService.InsertSystemStatus(systemStatus);

                            message = "SUCCESS";
                            var response = new DeviceApiResponse <bool>(message);
                            return(Ok(response));
                        }
                        else
                        {
                            message = "DEVICE_NOT_FOUND";
                            var response = new DeviceApiResponse <bool>(message);
                            return(NotFound(response));
                        }
                    }
                    else
                    {
                        message = "NULL_PARAMETER";
                        var response = new DeviceApiResponse <bool>(message);
                        return(BadRequest(response));
                    }
                }
                else
                {
                    message = "ACCESS_DENIED";
                    var response = new DeviceApiResponse <bool>(message);
                    return(Unauthorized(response));
                }
            }
            catch (Exception ex)
            {
                string message;
                if (ex.Source == "System.IdentityModel.Tokens.Jwt")
                {
                    message = "INVALID_TOKEN";
                }
                else
                {
                    message = "ERROR";
                }
                var response = new DeviceApiResponse <Exception>(ex, message);
                return(StatusCode(500, response));
            }
        }