diff options
Diffstat (limited to 'Software/Visual_Studio/TCC/Tango.TCC.Service')
5 files changed, 141 insertions, 16 deletions
diff --git a/Software/Visual_Studio/TCC/Tango.TCC.Service/Controllers/ColorDetectionController.cs b/Software/Visual_Studio/TCC/Tango.TCC.Service/Controllers/ColorDetectionController.cs index a9ab84931..a28c2cb12 100644 --- a/Software/Visual_Studio/TCC/Tango.TCC.Service/Controllers/ColorDetectionController.cs +++ b/Software/Visual_Studio/TCC/Tango.TCC.Service/Controllers/ColorDetectionController.cs @@ -11,11 +11,14 @@ using System.Security.Authentication; using System.Web.Http; using Tango.PMR.TCC; using Tango.TCC.BL; +using Tango.TCC.BL.Entities; using Tango.TCC.BL.Web; +using Tango.TCC.Service.DB; using Tango.TCC.Service.Filters; using Tango.TCC.Service.Security; using Tango.Web.Controllers; using Tango.Web.Security; +using System.Data.Entity; namespace Tango.TCC.Service.Controllers { @@ -28,6 +31,23 @@ namespace Tango.TCC.Service.Controllers if (request.AppID == TCCServiceConfig.APP_ID) { + using (var db = TccDbContext.CreateTCC()) + { + var device = db.Devices.SingleOrDefault(x => x.DeviceID == request.DeviceID); + + if (device == null) + { + device = new Device(); + device.DeviceID = request.DeviceID; + device.DeviceModel = request.Device; + device.Email = request.Email; + device.LastLogin = DateTime.UtcNow; + device.OSVersion = request.OSVersion; + db.Devices.Add(device); + db.SaveChanges(); + } + } + response.AccessToken = WebToken<DeviceTokenObject>.CreateNew(TCCServiceConfig.JWT_TOKEN_SECRET, new DeviceTokenObject() { DeviceID = request.DeviceID, @@ -45,20 +65,40 @@ namespace Tango.TCC.Service.Controllers [HttpPost] public DefinitionResponse GetDefinition(DefinitionRequest request) { - String s = RequestToken.Object.DeviceID; - - return new DefinitionResponse() + using (var db = TccDbContext.CreateTCC()) { - TemplateString = TCCServiceConfig.TEMPLATE_STRING, - SampleWidth = TCCServiceConfig.SAMPLE_WIDTH, - SampleHeight = TCCServiceConfig.SAMPLE_HEIGHT, - CameraWidth = TCCServiceConfig.CAMERA_WIDTH, - CameraHeight = TCCServiceConfig.CAMERA_HEIGHT, - HistogramMethod = TCCServiceConfig.HISTOGRAM_METHOD, - SimilarityTolerance = TCCServiceConfig.SIMILARITY_TOLERANCE, - EnableDoubleChecking = TCCServiceConfig.ENABLE_DOUBLE_CHECKING, - EnforceBarcodeDetection = TCCServiceConfig.ENFORCE_BARCODE_DETECTION, - }; + var response = new DefinitionResponse() + { + TemplateString = TCCServiceConfig.TEMPLATE_STRING, + SampleWidth = TCCServiceConfig.SAMPLE_WIDTH, + SampleHeight = TCCServiceConfig.SAMPLE_HEIGHT, + CameraWidth = TCCServiceConfig.CAMERA_WIDTH, + CameraHeight = TCCServiceConfig.CAMERA_HEIGHT, + HistogramMethod = TCCServiceConfig.HISTOGRAM_METHOD, + SimilarityTolerance = TCCServiceConfig.SIMILARITY_TOLERANCE, + EnableDoubleChecking = TCCServiceConfig.ENABLE_DOUBLE_CHECKING, + EnforceBarcodeDetection = TCCServiceConfig.ENFORCE_BARCODE_DETECTION, + }; + + var device = db.Devices.SingleOrDefault(x => x.DeviceID == RequestToken.Object.DeviceID); + + if (device != null && device.MachineRegistered) + { + using (var tangoDb = TccDbContext.CreateTango()) + { + var machines = tangoDb.Machines.Where(x => x.OrganizationGuid == device.OrganizationGuid); + + response.IsRegistered = true; + + foreach (var m in machines) + { + response.OrganizationMachines.Add(m.SerialNumber); + } + } + } + + return response; + } } [JwtTokenFilter] @@ -111,9 +151,37 @@ namespace Tango.TCC.Service.Controllers { MachineRegistrationResponse response = new MachineRegistrationResponse(); - response.OrganizationMachines.Add("0002"); + using (var tangoDB = TccDbContext.CreateTango()) + { + var machine = tangoDB.Machines.SingleOrDefault(x => x.SerialNumber == request.SerialNumber); + + if (machine == null) + { + throw new AuthenticationException("Invalid serial number."); + } + + var all_machines = tangoDB.Machines.Where(x => x.OrganizationGuid == machine.OrganizationGuid); + + foreach (var m in all_machines) + { + response.OrganizationMachines.Add(m.SerialNumber); + } - //throw new AuthenticationException("No such serial number..."); + using (var db = TccDbContext.CreateTCC()) + { + String deviceId = RequestToken.Object.DeviceID; + var device = db.Devices.SingleOrDefault(x => x.DeviceID == deviceId); + + if (device == null) + { + throw new InvalidOperationException("Could not find the device id."); + } + + device.MachineRegistered = true; + device.OrganizationGuid = machine.OrganizationGuid; + db.SaveChanges(); + } + } return response; } diff --git a/Software/Visual_Studio/TCC/Tango.TCC.Service/DB/TccDbContext.cs b/Software/Visual_Studio/TCC/Tango.TCC.Service/DB/TccDbContext.cs new file mode 100644 index 000000000..de38c0b91 --- /dev/null +++ b/Software/Visual_Studio/TCC/Tango.TCC.Service/DB/TccDbContext.cs @@ -0,0 +1,46 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Web; +using Tango.BL; +using Tango.Core; +using Tango.TCC.BL.Entities; +using Tango.Web; +using Tango.Web.Helpers; + +namespace Tango.TCC.Service.DB +{ + public class TccDbContext + { + public static ObservablesContext CreateTango() + { + return ObservablesContextHelper.CreateContext(TCCServiceConfig.TANGO_DB_CATALOG); + } + + public static TCCContext CreateTCC() + { + if (System.Diagnostics.Debugger.IsAttached) + { + return new TCCContext(new Core.DataSource() + { + Address = "localhost\\SQLEXPRESS", + Catalog = "TCC", + IntegratedSecurity = true, + Type = DataSourceType.SQLServer, + }); + } + else + { + return new TCCContext(new Core.DataSource() + { + Address = WebConfig.DB_ADDRESS, + Catalog = WebConfig.DB_CATALOG, + IntegratedSecurity = false, + Type = DataSourceType.SQLServer, + UserName = WebConfig.DB_USER_NAME, + Password = WebConfig.DB_PASSWORD + }); + } + } + } +}
\ No newline at end of file diff --git a/Software/Visual_Studio/TCC/Tango.TCC.Service/TCCServiceConfig.cs b/Software/Visual_Studio/TCC/Tango.TCC.Service/TCCServiceConfig.cs index 14e9ef8cc..343d27e0a 100644 --- a/Software/Visual_Studio/TCC/Tango.TCC.Service/TCCServiceConfig.cs +++ b/Software/Visual_Studio/TCC/Tango.TCC.Service/TCCServiceConfig.cs @@ -76,5 +76,10 @@ namespace Tango.TCC.Service /// Gets the mobile application ID. /// </summary> public static String APP_ID => ConfigurationManager.AppSettings[nameof(APP_ID)].ToString(); + + /// <summary> + /// Gets the database catalog. + /// </summary> + public static String TANGO_DB_CATALOG => ConfigurationManager.AppSettings[nameof(TANGO_DB_CATALOG)].ToString(); } }
\ No newline at end of file diff --git a/Software/Visual_Studio/TCC/Tango.TCC.Service/Tango.TCC.Service.csproj b/Software/Visual_Studio/TCC/Tango.TCC.Service/Tango.TCC.Service.csproj index fe4d6e85b..b7042197c 100644 --- a/Software/Visual_Studio/TCC/Tango.TCC.Service/Tango.TCC.Service.csproj +++ b/Software/Visual_Studio/TCC/Tango.TCC.Service/Tango.TCC.Service.csproj @@ -175,6 +175,7 @@ <Compile Include="Controllers\AccountController.cs" /> <Compile Include="Controllers\ColorDetectionController.cs" /> <Compile Include="Controllers\HomeController.cs" /> + <Compile Include="DB\TccDbContext.cs" /> <Compile Include="Filters\JwtTokenFilter.cs" /> <Compile Include="Global.asax.cs"> <DependentUpon>Global.asax</DependentUpon> @@ -215,6 +216,10 @@ <None Include="Properties\PublishProfiles\TwineTCC - Production.pubxml" /> </ItemGroup> <ItemGroup> + <ProjectReference Include="..\..\Tango.BL\Tango.BL.csproj"> + <Project>{f441feee-322a-4943-b566-110e12fd3b72}</Project> + <Name>Tango.BL</Name> + </ProjectReference> <ProjectReference Include="..\..\Tango.Core\Tango.Core.csproj"> <Project>{a34ee0f0-649d-41c8-8489-b6f1cc6924ee}</Project> <Name>Tango.Core</Name> diff --git a/Software/Visual_Studio/TCC/Tango.TCC.Service/Web.config b/Software/Visual_Studio/TCC/Tango.TCC.Service/Web.config index 37e3e528d..de53d7d14 100644 --- a/Software/Visual_Studio/TCC/Tango.TCC.Service/Web.config +++ b/Software/Visual_Studio/TCC/Tango.TCC.Service/Web.config @@ -18,7 +18,8 @@ <add key="DB_ADDRESS" value="twine.database.windows.net" /> <add key="DB_USER_NAME" value="Roy" /> <add key="DB_PASSWORD" value="Aa123456" /> - <add key="DB_CATALOG" value="TCC_DEV" /> + <add key="DB_CATALOG" value="TCC" /> + <add key="TANGO_DB_CATALOG" value="Tango_TEST" /> <add key="STORAGE_ACCOUNT" value="DefaultEndpointsProtocol=https;AccountName=tangostorage;AccountKey=S4z/D+Yg6mwMis+bs/VpcDLA9yE1iZaYq23shQlRIi2KmM9E7JY8zdZjeAPOPdG3gONHoNDEpsgH6D4cqQ/bsA==;EndpointSuffix=core.windows.net" /> <add key="TENANT_ID" value="2ebd63a5-bc2f-41dc-9066-4409ed5e5dd4" /> <add key="CLIENT_ID" value="ec612854-7abc-457b-808a-5d0c5ba80c57" /> |
