Example #1
0
		/// <summary>
		/// Comunica como servidor de Licencas e atualiza repositorio.
		/// Esse metodo sera transferido para o server...............................   TODO
		/// </summary>
		public async Task atualizaLicencas (bool nova)
		{
			if (CrossConnectivity.Current.IsConnected)
				try {
					consultarLicencas = false;
					ObservableCollection<ResponseObject> licenses;
					Object req;
					if (nova) {
						var objNewLicense = new RequestObjectNew ();
						objNewLicense.nome = Settings.nome;
						objNewLicense.email = Settings.email;
						objNewLicense.imei = Settings.imei;
						objNewLicense.os = Settings.os;
						objNewLicense.phoneModel = Settings.phoneModel;
						objNewLicense.bleidcelular = mysafetyDll.localBleAddress;
						req = objNewLicense;
					} else {
						var objRenew = new RequestObjectRenew ();
						objRenew.email = Settings.email;
						objRenew.imei = Settings.imei;
						objRenew.bleidcelular = mysafetyDll.localBleAddress;
						req = objRenew;
					}
					licenses = consultaServidorLicencas (req).Result;
					if (licenses.Count == 0)
						Console.WriteLine ("*** Sem licenças para usuario*****");   //TODO: verificar se user tem licencas no repositorio e se for o caso, deletar
                    else {
						Console.WriteLine ("***Com Licencas para usuario:" + licenses.Count);

						ObservableCollection<Licenses> LicensesValidas = new ObservableCollection<Licenses> ();

						ObservableCollection<Licenses> LicensesRepositorio = new ObservableCollection<Licenses> (tableLicencas.GetAllAsync ().
                                                 Result.Where (gs => gs.UserId == Settings.user_Id));

						if (LicensesRepositorio == null)
							LicensesRepositorio = new ObservableCollection<Licenses> ();

						int pos;
						Licenses licRepositorio;
						foreach (var resp in licenses) {   //Monta as novas licencas validas 
							try {
								Licenses license = new Licenses ();
								license.UserId = Settings.user_Id;
								license.IsOwner = true;
								license.Type = resp.Type;
								license.Id = "" + resp.serialId;
								license.Expire = resp.expiredate;
								///Apenas se gate
								license.Code = resp.authorizationCode;
								license.ECount = resp.elementCount;
								license.GateBleId = resp.gateBleId;
								LicensesValidas.Add (license);

								if ((pos = LicensesRepositorio.IndexOf (license)) < 0) {
									await tableLicencas.CreateAsync (license);
									LicensesRepositorio.Add (license);
									Licenses.Add (license);
								} else {
									licRepositorio = LicensesRepositorio [pos];
									license.Email = licRepositorio.Email; //Mantem as associacoes atuais de licenca
									if (!string.Equals (licRepositorio.Expire, license.Expire) ||
									    licRepositorio.Code != license.Code ||
									    licRepositorio.ECount != license.ECount ||
									    licRepositorio.GateBleId != license.GateBleId) {
										licRepositorio.Expire = license.Expire;
										licRepositorio.Code = license.Code;
										licRepositorio.ECount = license.ECount;
										licRepositorio.GateBleId = license.GateBleId;
										await tableLicencas.UpdateAsync (license);
									}
								}

								if (resp.Type.Equals ("GATE_CONTROL"))
									Debug.WriteLine (resp.serialId + "***licença Portao:" + license.GateBleId + " qte Lista: " + license.ECount + "  " + license.Code + "  " + resp.expiredate);
								else
									Debug.WriteLine (resp.serialId + "***licença Panico:" + "  " + resp.expiredate);
							} catch (Exception ex) {
								Console.WriteLine ("responseObject in licenses" + ex);
							}
						}

						//Verifica se tem que deletar alguma licenca do repositorio
						bool todos = false;
						while (!todos) {
							todos = false;
							foreach (var lic in Licenses) {
								if (LicensesValidas.IndexOf (lic) < 0) {  //Essa licenca nao existe mais
									await tableLicencas.RemoveAsync (lic);
									break;
								}
								todos = true;
							}
						}

						Licenses = LicensesValidas;

						try {   //Se o user desse cel nao tiver licenca de panico associado e tiver uma livre, faz a associacao

							Licenses licLivre = null;
							foreach (var lic in Licenses) {
								if (lic.Email != null && lic.Email.Length > 0) {
									Console.WriteLine ("Licenca panico:" + lic.Email);
									if (lic.Email.Equals (Settings.email)) {  //ja tem licenca associado para o user desse celular
										Settings.licencaPanicoAssociada = true;
										licLivre = null;
										break;
									}
								} else
									licLivre = lic;
							}

							if (licLivre != null) {
								Console.WriteLine (licLivre.Id + " Associando licenca de panico para:" + Settings.email);
								Settings.licencaPanicoAssociada = true;
								licLivre.Email = Settings.email;
								await tableLicencas.UpdateAsync (licLivre);
							}

						} catch (Exception ex) {
							Console.WriteLine ("var PanicLicense =" + ex);
						}

						await renovaMeusPortoes ();

					}

				} catch (Exception ex) {
					Console.WriteLine ("***Exception ao consultar licencas....." + ex);
				}
			else
				Console.WriteLine ("***Sem rede para consultar licencas.....");
			if (meuSkey != null) {
				mysafetyDll.meusKey = meuSkey;
				if (!mysafetyDll.meusBLEs.ContainsKey (meuSkey.deviceAddress))
					mysafetyDll.meusBLEs.Add (meuSkey.deviceAddress, meuSkey);
			}
			PreparaDll ();
			mysafetyDll.scanBleDevice (15000);


		}
Example #2
0
 public bool Equals(Licenses other)
 {
     if (other == null)
         return false;
     return ((this.Type + this.Id).Equals (other.Type + other.Id));
 }