public bool createFeed(string name, string description, string Key_EndToEnd, string Url, string control) { try { var httpWebRequest = (HttpWebRequest)WebRequest.Create("https://io.adafruit.com/api/v2/" + MqttConnection.UserName + "/feeds?X-AIO-Key=" + MqttConnection.AioKey + "&group_key=default"); httpWebRequest.ContentType = "application/json"; httpWebRequest.Method = "POST"; string key = getCorretName(name); if (key == "") { return(false); } using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream())) { string json = "{\"description\":\"" + description + "\"," + "\"key\":\"" + key + "\"," + "\"license\":\"" + Key_EndToEnd + "*" + Url + "*" + control + "\"," + "\"name\":\"" + name + "\"}"; streamWriter.Write(json); } string datareceived = ""; var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse(); using (var streamReader = new StreamReader(httpResponse.GetResponseStream())) { datareceived = streamReader.ReadToEnd(); } FeedsJson.FeedProprety feedProprety = JsonConvert.DeserializeObject <FeedsJson.FeedProprety>(datareceived); MqttConnection.setChatRoomNameKey(feedProprety.key); return(true); } catch (Exception) { return(false); } }
private async void LogInBtn_Click(object sender, RoutedEventArgs e) { if (ServerAdafruit) { if (!string.IsNullOrWhiteSpace(NickName.Text) && !string.IsNullOrWhiteSpace(UsernameTxt.Text) && !string.IsNullOrWhiteSpace(AioKeyTxt.Password)) { if (NickName.Text.Length < 25 && !NickName.Text.Contains("_")) { ProgressCircle.IsIndeterminate = true; LogInContainer.IsEnabled = false; NickName.Text = NickName.Text.Trim(); UsernameTxt.Text = UsernameTxt.Text.Trim(); AioKeyTxt.Password = AioKeyTxt.Password.Trim(); MqttConnection.setNickname(NickName.Text); MqttConnection.setUser(UsernameTxt.Text); MqttConnection.setAioKey(AioKeyTxt.Password); HttpRequest req = new HttpRequest(); string response = await Task.Run(() => req.getJsonData("https://io.adafruit.com/api/v2/user?X-AIO-Key=" + MqttConnection.AioKey)); if (response != "") { UserDataJsonReceive.Rootobject UserData = JsonConvert.DeserializeObject <UserDataJsonReceive.Rootobject>(response); if (UsernameTxt.Text == UserData.user.username) { if (await Task.Run(() => MqttConnection.MqttTryToConnect())) { SaveConnectDataFile("", Directory.GetCurrentDirectory() + "\\Data", "\\AdaDataConnection.dt"); MqttConnection.setChatRoomNameKey("chat"); MqttConnection.client.MqttMsgPublishReceived += client_MqttMsgPublishReceived; MqttConnection.MqttSubscribe(UsernameTxt.Text + "/feeds/" + MqttConnection.ChatRoomNameKey); MqttConnection.IsSolace = false; MqttConnection.IsGroup = false; MqttConnection.IamAdmin = true; Restore.Start(); } else { ShowSnackBar("Error to connect with server!"); LogInContainer.IsEnabled = true; ProgressCircle.IsIndeterminate = false; } } else { ShowSnackBar("Bad Username or Aio Key"); ProgressCircle.IsIndeterminate = false; LogInContainer.IsEnabled = true; } } else { ShowSnackBar("Bad internet connection!"); ProgressCircle.IsIndeterminate = false; LogInContainer.IsEnabled = true; } } else { ShowSnackBar("Max 25 char for Nickname and not '_'"); } } else { ShowSnackBar("Please, insert data!"); } } else if (ServerSolace) { if (!string.IsNullOrWhiteSpace(SolaceNickName.Text) && !string.IsNullOrWhiteSpace(SolaceHostPort.Text) && !string.IsNullOrWhiteSpace(SolacePassword.Password)) { if (SolaceNickName.Text.Length < 25 && !SolaceNickName.Text.Contains("_")) { ProgressCircle.IsIndeterminate = true; LogInContainer.IsEnabled = false; SolaceNickName.Text = SolaceNickName.Text.Trim(); SolacePassword.Password = SolacePassword.Password.Trim(); SolaceHostPort.Text = SolaceHostPort.Text.Trim(); SolaceHostPort.Text = SolaceHostPort.Text.Remove(0, 6); var SplitAddressPort = SolaceHostPort.Text.Split(':'); MqttConnection.setMqttServer(SplitAddressPort[0]); MqttConnection.setMqttPort(SplitAddressPort[1]); MqttConnection.setNickname(SolaceNickName.Text); MqttConnection.setUser("solace-cloud-client"); MqttConnection.setAioKey(SolacePassword.Password); if (await Task.Run(() => MqttConnection.MqttTryToConnect())) { SaveConnectDataFile("", Directory.GetCurrentDirectory() + "\\Data", "\\SolaceDataConnection.dt"); MqttConnection.setChatRoomNameKey("SolaceRoom"); MqttConnection.client.MqttMsgPublishReceived += client_MqttMsgPublishReceived; MqttConnection.MqttSubscribe(MqttConnection.UserName + "/feeds/" + MqttConnection.ChatRoomNameKey); MqttConnection.MqttUnsecurePublish(MqttConnection.UserName + "/clients", "{\"status\":\"connected\",\"at\":\"2020-10-14T00:23:28.481Z\",\"client\":{\"id\":\"dzbpiydeyvrngch_" + MqttConnection.NickName + "\",\"ip\":\"78.74.65.35\"}}"); MqttConnection.IsSolace = true; MqttConnection.IsGroup = false; MqttConnection.IamAdmin = true; Restore.Start(); } else { ShowSnackBar("Error to connect with server!"); LogInContainer.IsEnabled = true; ProgressCircle.IsIndeterminate = false; } } else { ShowSnackBar("Max 25 char for Nickname and not '_'"); } } else { ShowSnackBar("Please, insert data!"); } } }
private async void PasteKey_Click(object sender, RoutedEventArgs e) { string key_group_enc = Clipboard.GetText(); string key_group_dec = StringCipher.Decrypt(key_group_enc, ""); //MessageBox.Show(key_group_dec); var var_key_group_dec = key_group_dec.Split('|'); HttpRequest req = new HttpRequest(); try { MqttConnection.IsGroup = true; MqttConnection.setChatRoomNameKey(var_key_group_dec[0]); MqttConnection.setUser(var_key_group_dec[1]); MqttConnection.setAioKey(var_key_group_dec[2]); MqttConnection.setKeyEndToEnd(var_key_group_dec[3]); CreatorGroup = var_key_group_dec[4]; UrlStream = var_key_group_dec[5]; MqttConnection.IamAdmin = !bool.Parse(var_key_group_dec[6]); MqttConnection.setMqttServer(var_key_group_dec[7]); MqttConnection.setMqttPort(var_key_group_dec[8]); } catch (Exception) { ShowSnackBar("Invalid Key Room"); return; } ProgressCircle.IsIndeterminate = true; LogInContainer.IsEnabled = false; if (MqttConnection.MqttServer.Contains("adafruit")) { AdaServer_Click(sender, e); if (NickName.Text.Length < 25) { if (string.IsNullOrWhiteSpace(NickName.Text)) { ShowSnackBar("Please insert a NickName"); ProgressCircle.IsIndeterminate = false; LogInContainer.IsEnabled = true; return; } MqttConnection.setNickname(NickName.Text); if (await Task.Run(() => MqttConnection.MqttTryToConnect())) { string data = await req.getJsonData("https://io.adafruit.com/api/v2/" + MqttConnection.UserName + "/feeds/" + MqttConnection.ChatRoomNameKey + "?X-AIO-Key=" + MqttConnection.AioKey); if (data != "") { FeedsJson.FeedProprety feedProprety = JsonConvert.DeserializeObject <FeedsJson.FeedProprety>(data); if (feedProprety.enabled) { NameGroup = feedProprety.name; UsernameTxt.Text = ""; AioKeyTxt.Password = ""; MqttConnection.client.MqttMsgPublishReceived += client_MqttMsgPublishReceived; MqttConnection.MqttSubscribe(MqttConnection.UserName + "/feeds/" + MqttConnection.ChatRoomNameKey); MqttConnection.IsSolace = false; MqttConnection.IsGroup = true; Restore.Start(); } else { restore(); } } else { restore(); } } else { ShowSnackBar("Error to connect!"); LogInContainer.IsEnabled = true; ProgressCircle.IsIndeterminate = false; } } else { ShowSnackBar("Max 25 char for Nickname"); LogInContainer.IsEnabled = true; ProgressCircle.IsIndeterminate = false; } } else if (MqttConnection.MqttServer.Contains("messaging.solace.cloud")) { SolaceServer_Click(sender, e); if (SolaceNickName.Text.Length < 25) { if (string.IsNullOrWhiteSpace(SolaceNickName.Text)) { ShowSnackBar("Please insert a NickName"); ProgressCircle.IsIndeterminate = false; LogInContainer.IsEnabled = true; return; } MqttConnection.setNickname(SolaceNickName.Text); if (await Task.Run(() => MqttConnection.MqttTryToConnect())) { MqttConnection.setChatRoomNameKey("SolaceRoom"); MqttConnection.client.MqttMsgPublishReceived += client_MqttMsgPublishReceived; MqttConnection.MqttSubscribe(MqttConnection.UserName + "/feeds/" + MqttConnection.ChatRoomNameKey); MqttConnection.MqttUnsecurePublish(MqttConnection.UserName + "/clients", "{\"status\":\"connected\",\"at\":\"2020-10-14T00:23:28.481Z\",\"client\":{\"id\":\"dzbpiydeyvrngch_" + MqttConnection.NickName + "\",\"ip\":\"78.74.65.35\"}}"); MqttConnection.IsSolace = true; MqttConnection.IsGroup = true; Restore.Start(); } else { ShowSnackBar("Error to connect!"); LogInContainer.IsEnabled = true; ProgressCircle.IsIndeterminate = false; } } else { ShowSnackBar("Max 25 char for Nickname"); LogInContainer.IsEnabled = true; ProgressCircle.IsIndeterminate = false; } } else { ShowSnackBar("Invalid Server!"); LogInContainer.IsEnabled = true; ProgressCircle.IsIndeterminate = false; } }
private async void GenerateBtn_Click(object sender, RoutedEventArgs e) { if (NameRoom.Text != "" && !string.IsNullOrWhiteSpace(UsernameTxt.Text) && !string.IsNullOrWhiteSpace(AioKeyTxt.Password) && !string.IsNullOrWhiteSpace(Key_EndToEnd.Text) && !Key_EndToEnd.Text.Contains("GENERATE")) { HttpRequest req = new HttpRequest(); string KeyRoomDec = req.getCorretName(NameRoom.Text) + "|" + UsernameTxt.Text + "|" + AioKeyTxt.Password + "|" + Key_EndToEnd.Text + "|" + MqttConnection.NickName + "|" + UrlTxt.Text + "|" + CheckControll.IsChecked + "|" + MqttConnection.MqttServer + "|" + MqttConnection.MqttPort.ToString(); string KeyRoomEnc = StringCipher.Encrypt(KeyRoomDec, ""); if (CheckCurrentData.IsChecked == true) { if (getNumFeed(req.getAllFeeds()) != 10) { MqttConnection.MqttUnsubscribe(MqttConnection.UserName + "/feeds/" + MqttConnection.ChatRoomNameKey); if (req.createFeed(NameRoom.Text, DescriptionTxt.Text, Key_EndToEnd.Text, UrlTxt.Text, CheckControll.IsChecked.ToString())) { MqttConnection.setKeyEndToEnd(Key_EndToEnd.Text); MqttConnection.MqttSubscribe(MqttConnection.UserName + "/feeds/" + MqttConnection.ChatRoomNameKey); MqttConnection.MqttPublish(MqttConnection.UserName + "/feeds/" + MqttConnection.ChatRoomNameKey, MqttConnection.NickName + "_ROOMGROUP_" + NameRoom.Text + "_" + UrlTxt.Text); Clipboard.SetText(KeyRoomEnc); MessageBox.Show("Room created!!!" + '\n' + '\n' + "Don't worry, I copy the access key to the group, share it with your friends, you just have to paste and they just have to copy it and click on \"I have a key\""); Close(); } else { MqttConnection.setKeyEndToEnd(""); MqttConnection.setChatRoomNameKey("chat"); MqttConnection.MqttSubscribe(MqttConnection.UserName + "/feeds/" + MqttConnection.ChatRoomNameKey); ShowSnackBar("Error creating room!"); } } else { ShowSnackBar("Max number of group created, delete one!"); } } else { string tempUser = MqttConnection.UserName; string tempAioKey = MqttConnection.AioKey; MqttConnection.MqttDisconnect(); MqttConnection.setUser(UsernameTxt.Text); MqttConnection.setAioKey(AioKeyTxt.Password); if (!(await MqttConnection.MqttFastConnect())) { MqttConnection.setUser(tempUser); MqttConnection.setAioKey(tempAioKey); ShowSnackBar("Erron to connect with new data!"); UsernameTxt.Focus(); return; } if (getNumFeed(req.getAllFeeds()) != 10) { if (req.createFeed(NameRoom.Text, DescriptionTxt.Text, Key_EndToEnd.Text, UrlTxt.Text, CheckControll.IsChecked.ToString())) { MqttConnection.setKeyEndToEnd(Key_EndToEnd.Text); MqttConnection.MqttSubscribe(MqttConnection.UserName + "/clients"); MqttConnection.MqttSubscribe(MqttConnection.UserName + "/feeds/" + MqttConnection.ChatRoomNameKey); MqttConnection.MqttPublish(MqttConnection.UserName + "/feeds/" + MqttConnection.ChatRoomNameKey, MqttConnection.NickName + "_ROOMGROUP_" + NameRoom.Text + "_" + UrlTxt.Text); Clipboard.SetText(KeyRoomEnc); MessageBox.Show("Room created!!!" + '\n' + '\n' + "Don't worry, I copy the access key to the group, share it with your friends, you just have to paste and they just have to copy it and click on \"I have a key\""); Close(); } else { MqttConnection.setKeyEndToEnd(""); MqttConnection.setChatRoomNameKey("chat"); MqttConnection.MqttSubscribe(MqttConnection.UserName + "/clients"); MqttConnection.MqttSubscribe(MqttConnection.UserName + "/feeds/" + MqttConnection.ChatRoomNameKey); ShowSnackBar("Error creating room!"); } } else { ShowSnackBar("Max number of group created, delete one!"); } } } else { ShowSnackBar("Complete the box please"); } }