public ServiceFunctionResult <DeviceConfig> GetDeviceConfig(string deviceId, string applicationName) { try { DeviceConfig result = null; /*We need a separate to get the entity in order to be able to update its LastConnectionDate, otherwise the underlying LINQConext * will not update the database because the "original entity" will be the same object as the "new entity we're getting here.*/ DeviceConfigContext ctx = new DeviceConfigContext(); List <DeviceConfig> deviceConfigQuery = (from d in ctx.DB.GetTable <DeviceConfig>() where d.DeviceId == deviceId && d.ApplicationName == applicationName select d).ToList(); result = deviceConfigQuery.Count < 1 ? null : deviceConfigQuery[0]; if (result == null) { SaveDevicePendingApproval(deviceId, applicationName); throw new ServiceException( string.Format("Could not retrieve settings for application on device {0}. Device not registered.", deviceId), ServiceResultCode.FatalError); } if (result.LicenseExpiryDate.HasValue && result.LicenseExpiryDate.Value < DateTime.Now) { throw new ServiceException( string.Format("License for device ID {0} has expired.", deviceId), ServiceResultCode.FatalError); } using (TransactionScope t = new TransactionScope()) { User systemUser = GetSystemUser(); result.LastConnectionDate = DateTime.Now; if (result.ActionCount.HasValue) { result.ActionCount++; } else { result.ActionCount = 1; } Save <DeviceConfig>(result, false).ForEach(c => HandleChange(c, systemUser)); GlobalVariable g = GetGlobalVariable(GlobalVariableName.LogActionGetDeviceConfig); if (bool.Parse(g.VariableValue)) { SaveDeviceConfigAction(result, "GetDeviceConfig", null, systemUser); } t.Complete(); } return(new ServiceFunctionResult <DeviceConfig>() { Contents = result }); } catch (Exception ex) { return(new ServiceFunctionResult <DeviceConfig>(HandleException(ex))); } }
public ServiceFunctionResult <DeviceConfigAction> LogDeviceConfigAction( string function, string deviceId, string applicationName, string tag) { try { DeviceConfigAction result = null; using (TransactionScope t = new TransactionScope()) { /*We need a separate to get the entity in order to be able to update its LastConnectionDate, otherwise the underlying LINQConext * will not update the database because the "original entity" will be the same object as the "new entity we're getting here.*/ DeviceConfigContext ctx = new DeviceConfigContext(); List <DeviceConfig> deviceConfigQuery = (from d in ctx.DB.GetTable <DeviceConfig>() where d.DeviceId == deviceId && d.ApplicationName == applicationName select d).ToList(); DeviceConfig deviceConfig = deviceConfigQuery.Count < 1 ? null : deviceConfigQuery[0]; if (deviceConfig == null) { throw new ServiceException( string.Format( "Could not retrieve settings for application on device {0}. Device not registered. Attempt to get device configuration for device to be added as a device pending approval.", deviceId), ServiceResultCode.FatalError); } User systemUser = GetSystemUser(); deviceConfig.LastConnectionDate = DateTime.Now; if (deviceConfig.ActionCount.HasValue) { deviceConfig.ActionCount++; } else { deviceConfig.ActionCount = 1; } Save <DeviceConfig>(deviceConfig, false).ForEach(c => HandleChange(c, systemUser)); SaveDeviceConfigAction(deviceConfig, function, tag, systemUser); t.Complete(); } return(new ServiceFunctionResult <DeviceConfigAction>() { Contents = result }); } catch (Exception ex) { return(new ServiceFunctionResult <DeviceConfigAction>(HandleException(ex))); } }