コード例 #1
0
        public V0100.Objects.Credentials RegisterDevice(string locationUid, string serialNumber)
        {
            UserEntity user = GetUser();

            if (user == null)
            {
                throw new WebFaultException <string>(V0100.Constants.StatusSubcode.USER_AUTH_REQUIRED, HttpStatusCode.Forbidden);
            }

            LocationEntity location = LocationUtils.GetByUid(locationUid);

            if (location == null)
            {
                throw new WebFaultException <string>(V0100.Constants.StatusSubcode.LOCATION_NOT_FOUND, HttpStatusCode.PreconditionFailed);
            }
            if (location.OrganizationId != user.OrganizationId)
            {
                throw new WebFaultException <string>(V0100.Constants.StatusSubcode.LOCATION_INVALID, HttpStatusCode.PreconditionFailed);
            }

            DeviceEntity device = location.Devices.FirstOrDefault(d => d.SerialNumber == serialNumber);

            if (device == null)
            {
                throw new WebFaultException <string>(V0100.Constants.StatusSubcode.DEVICE_NOT_FOUND, HttpStatusCode.NotFound);
            }

            if (device.DeviceState != DeviceState.New && device.DeviceState != DeviceState.Transitioning)
            {
                throw new WebFaultException <string>(V0100.Constants.StatusSubcode.DEVICE_STATE_INVALID, HttpStatusCode.NotAcceptable);
            }

            V0100.Objects.Credentials credentials = new V0100.Objects.Credentials
            {
                OrganizationUid     = location.Organization.UniqueIdentifier,
                LocationUid         = location.UniqueIdentifier,
                DeviceUid           = device.UniqueIdentifier,
                UidQualifier        = DeviceUtils.GetUidQualifier(device),
                AuthenticationToken = DeviceUtils.GetAuthenticationToken(device, false)
            };

            // Set new state and create tracking record. This will update the database.
            DeviceUtils.SetState(device, DeviceState.Active, "Device registered at location " + location.Name);

            return(credentials);
        }