public IPostResponse ReturnCamera([FromContent] ReturnRequest Loan) { var db = new Database.SqliteDB(); var dev = new DeviceLoan { GeneratedPassword = Loan.Password, Email = Loan.Email, Mac = Loan.SerialNumber }; var dbdev = db.ValidLoanReturn(dev); if (dbdev == null) { return(new PostResponse(PostResponse.ResponseStatus.Conflict, null, new ResponseData { Code = 1, Message = "Not your camera" })); } dbdev.Returned = true; dbdev.ExpireDate = 0; db.UpdateLoanInfo(dbdev); var axisdev = new AxisVapixLib.Device(); var device = db.GetDevice(new DeviceList { SerialNumber = dbdev.Mac }); if (device != null) { axisdev.Username = device.Username; axisdev.Password = device.Password; axisdev.Host = device.Host; //Task.Run(() => axisdev.FactoryDefault(AxisVapixLib.Device.FactoryDefaultMode.Hard)).Wait(); return(new PostResponse(PostResponse.ResponseStatus.Created, null, new ResponseData { Code = 0, Message = "Ok" })); } return(new PostResponse(PostResponse.ResponseStatus.Conflict, null, new ResponseData { Code = 3, Message = "Device could not be found in local database" })); }
public IAsyncOperation <IGetResponse> findCameras() { //Start with finding the host list to parse Database.SqliteDB db = new Database.SqliteDB(); //Get current passwords. var validPasswordString = db.GetSetting(new Settings { Name = "validCameraPasswords" }); string[] validPassword; if (validPasswordString == null) { validPassword = new string[] { "pass", "adp2013" } } ; else { validPassword = validPasswordString.Value.Split(new char[] { ',' }); } validPasswordString = null; var hosts = db.GetHostLists(); /// Value is intered like: /// IP Start /// IP End /// Port Start /// Port End if (hosts.Count == 0) { return(Task.FromResult <IGetResponse>(new GetResponse(GetResponse.ResponseStatus.OK, hosts)).AsAsyncOperation()); } List <string> connectionUrls = new List <string>(); foreach (DeviceHostList item in hosts) { var ips = new IPRange(item.IPRange); foreach (IPAddress ip in ips.GetAllIP()) { var startPort = item.StartPort; while (startPort <= item.EndPort) { connectionUrls.Add(ip.ToString() + ":" + startPort); startPort++; } } } //Ip addresses scoped. foreach (string url in connectionUrls) { foreach (string password in validPassword) { Debug.WriteLine(url); AxisVapixLib.Device dev = new AxisVapixLib.Device { Host = url, Username = "******", Password = password, }; dev.CredentialCache = new NetworkCredential("root", password); var valid = ValidateDevice(dev); while (valid.Status != AsyncStatus.Completed) { Task.Delay(10).Wait(); } if (valid.GetResults()) { //Connection exists. Check if it's an Axis camera. Parameter p = new Parameter(dev); List <AxisParameter> parameters = new List <AxisParameter>(); parameters.Add(new AxisParameter() { Name = "Properties.System.SerialNumber" }); parameters.Add(new AxisParameter() { Name = "Properties.Firmware.Version" }); parameters.Add(new AxisParameter() { Name = "Brand.ProdShortName" }); parameters.Add(new AxisParameter() { Name = "Brand.ProdType" }); parameters.Add(new AxisParameter() { Name = "Properties.System.Architecture" }); Debug.WriteLine("Testing Password " + password); dev.Password = password; bool succeed = false; var receivedParameter = GetParameters(p, parameters); while (receivedParameter.Status != AsyncStatus.Completed) { Task.Delay(10).Wait(); } if (receivedParameter.GetResults() != null) { DeviceList device = new DeviceList(); foreach (AxisParameter Params in receivedParameter.GetResults()) { switch (Params.Name) { case "Properties.System.SerialNumber": device.SerialNumber = Params.Value; break; case "Properties.Firmware.Version": device.FirmwareVersion = Params.Value; break; case "Brand.ProdShortName": device.Model = Params.Value; break; case "Properties.System.Architecture": device.Architecture = Params.Value; break; case "Brand.ProdType": device.DeviceType = Params.Value; break; } } Debug.WriteLine("Password in cache " + dev.CredentialCache.Password); device.Username = dev.CredentialCache.UserName; device.Password = dev.CredentialCache.Password; device.Host = dev.Host; Debug.WriteLine(string.Format("Found Device: {0} - {1} ({2}) - {3} {4}", device.Model, device.SerialNumber, dev.Host, dev.CredentialCache.UserName, dev.CredentialCache.Password)); var dbdevice = db.GetDevice(device); if (dbdevice == null) { //Insert db.SaveDevice(device); Debug.WriteLine(string.Format("Inserting device: {0} {1} {2}", device.SerialNumber, device.Model, device.Host)); //Unknown device. Factory default. Debug.WriteLine("Factory defaulting " + device.SerialNumber); Task.Run(() => dev.FactoryDefault(Device.FactoryDefaultMode.Hard)); } else { //Update device.ID = dbdevice.ID; db.SaveDevice(device); Debug.WriteLine(string.Format("Updating device: {0} {1} {2}", device.SerialNumber, device.Model, device.Host)); } dbdevice = null; succeed = true; } if (!succeed) { Debug.WriteLine("Could not connect to " + dev.Host); } else { dev.ResetHTTPClient(); break; } //TODO ADD Password checking //TODO CREATE TABLE FOR POSSIBLE PASSWORDS } dev.ResetHTTPClient(); } //Reset the HTTP client to allow connections to a new IP. } /*Debug.WriteLine("Find cameras"); * AxisVapixLib.Device dev = new AxisVapixLib.Device * { * Host = "96.89.207.211:5014", * Username = "******", * Password = "******", * * }; * * var valid = ValidateDevice(dev); * while(valid.Status != AsyncStatus.Completed) * { * Task.Delay(1000).Wait(); * } * var result = valid.GetResults(); * * return Task.FromResult<IGetResponse>(new GetResponse(GetResponse.ResponseStatus.OK, result)).AsAsyncOperation(); * //var test = AsyncInfoFactory Task.FromResult<bool>(dev.ValidateConnect()).AsAsyncOperation<bool>(); //Task.FromResult<bool>(await dev.ValidateConnect()).AsAsyncOperation(); */ return(null); }