private async Task RegisterDevice(MetaDataTag[] MetaInfo) { string Key = await RuntimeSettings.GetAsync("ThingRegistry.Key", string.Empty); if (string.IsNullOrEmpty(Key)) { byte[] Bin = new byte[32]; using (RandomNumberGenerator Rnd = RandomNumberGenerator.Create()) { Rnd.GetBytes(Bin); } Key = Hashes.BinaryToString(Bin); await RuntimeSettings.SetAsync("ThingRegistry.Key", Key); } int c = MetaInfo.Length; Array.Resize <MetaDataTag>(ref MetaInfo, c + 1); MetaInfo[c] = new MetaDataStringTag("KEY", Key); this.registryClient.RegisterThing(false, MetaInfo, async(sender, e) => { try { if (e.Ok) { await RuntimeSettings.SetAsync("ThingRegistry.Location", true); await RuntimeSettings.SetAsync("ThingRegistry.Owner", e.OwnerJid); if (string.IsNullOrEmpty(e.OwnerJid)) { string ClaimUrl = registryClient.EncodeAsIoTDiscoURI(MetaInfo); string FilePath = ApplicationData.Current.LocalFolder.Path + Path.DirectorySeparatorChar + "Actuator.iotdisco"; Log.Informational("Registration successful."); Log.Informational(ClaimUrl, new KeyValuePair <string, object>("Path", FilePath)); File.WriteAllText(FilePath, ClaimUrl); } else { await RuntimeSettings.SetAsync("ThingRegistry.Key", string.Empty); Log.Informational("Registration updated. Device has an owner.", new KeyValuePair <string, object>("Owner", e.OwnerJid)); } } else { Log.Error("Registration failed."); await this.RegisterDevice(); } } catch (Exception ex) { Log.Critical(ex); } }, null); }
internal static Task RegistrationSuccessfulAsConsole(MetaDataTag[] MetaData, RegistrationEventArgs e) { if (!e.IsClaimed && Types.TryGetModuleParameter("Registry", out object Obj) && Obj is ThingRegistryClient ThingRegistryClient) { SimpleXmppConfiguration.PrintQRCode(ThingRegistryClient.EncodeAsIoTDiscoURI(MetaData)); } return(RegistrationSuccessfulAsService(MetaData, e)); }
private static async Task RegistrationSuccessful(MetaDataTag[] MetaData, RegistrationEventArgs e) { if (!e.IsClaimed && Types.TryGetModuleParameter("Registry", out object Obj) && Obj is ThingRegistryClient ThingRegistryClient) { string ClaimUrl = ThingRegistryClient.EncodeAsIoTDiscoURI(MetaData); string FilePath = Path.Combine(Gateway.AppDataFolder, "Gateway.iotdisco"); Log.Informational("Registration successful."); Log.Informational(ClaimUrl, new KeyValuePair <string, object>("Path", FilePath)); await File.WriteAllTextAsync(FilePath, ClaimUrl); } }
private void Register() { key = Guid.NewGuid().ToString().Replace("-", string.Empty); // For info on tag names, see: http://xmpp.org/extensions/xep-0347.html#tags metaData = new MetaDataTag[] { new MetaDataStringTag("KEY", key), new MetaDataStringTag("CLASS", "Temperature Sensor"), new MetaDataStringTag("MAN", "waher.se"), new MetaDataStringTag("MODEL", "Waher.Mock.Temperature.UWP"), new MetaDataStringTag("PURL", "https://github.com/PeterWaher/IoTGateway/tree/master/Mocks/Waher.Mock.Temperature.UWP"), new MetaDataNumericTag("V", 1.0) }; qrCodeUrl = SimpleXmppConfiguration.GetQRCodeURL(thingRegistryClient.EncodeAsIoTDiscoURI(metaData), 400, 400); thingRegistryClient.RegisterThing(metaData, (sender2, e2) => { if (e2.Ok) { this.registered = true; if (e2.IsClaimed) { ownerJid = e2.OwnerJid; } else { ownerJid = string.Empty; } this.RaiseOwnershipChanged(); } return(Task.CompletedTask); }, null); }