public async Task <string> RegisterDeviceAsync(ManagedDevice device) { string jsonObject = JsonConvert.SerializeObject(device); var content = new StringContent(jsonObject, Encoding.UTF8); content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json"); var response = await _httpClient.PostAsync(Constants.RegisterFunctionUri, content); if (response.IsSuccessStatusCode) { return(await response.Content.ReadAsStringAsync()); } return(null); }
public static void SaveIoTClientState(ManagedDevice iotClientState) { lock (objLock) { try { using (var file = File.CreateText(ShoppingStateFile)) { var serializer = new JsonSerializer(); serializer.Serialize(file, iotClientState); } } catch { } } }
protected override void OnStartup(StartupEventArgs e) { this.DeviceState = FileHelper.ReadIoTClientState(); if(DeviceState == null || !DeviceState.IsRegistered) { StartupUri = new System.Uri("/PhotoStoreDemo;component/Manage.xaml", System.UriKind.Relative); } else { StartupUri = new System.Uri("/PhotoStoreDemo;component/MainWindow.xaml", System.UriKind.Relative); Application.Current.Properties["State"] = DeviceState; } base.OnStartup(e); }
private async void Register_Click(object sender, RoutedEventArgs e) { try { this.Cursor = Cursors.Wait; var email = this.txtEmail.Text; string pattern = @"^(([\w-]+\.)+[\w-]+|([a-zA-Z]{1}|[\w-]{2,}))@" + @"((([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])\.([0-1]? [0-9]{1,2}|25[0-5]|2[0-4][0-9])\." + @"([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])\.([0-1]? [0-9]{1,2}|25[0-5]|2[0-4][0-9])){1}|" + @"([a-zA-Z0-9]+[\w-]+\.)+[a-zA-Z]{1}[a-zA-Z0-9-]{1,23})$"; Regex reg = new Regex(pattern); if (!reg.Match(email).Success) { MessageBox.Show("Invalid email"); return; } ManagedDevice device = new ManagedDevice() { Email = email }; var token = await HttpHelperService.Instance.RegisterDeviceAsync(device); if (token != null) { device.IoTToken = token; device.IsRegistered = true; Application.Current.Properties["State"] = device; FileHelper.SaveIoTClientState(device); Application.Current.MainWindow = new MainWindow(); Application.Current.MainWindow.Show(); this.Close(); } } catch (Exception ex) { Console.WriteLine($"{ex}"); } finally { this.Cursor = Cursors.Arrow; } }
/// <summary> /// Create new navigation property to managedDevices for users /// <param name="body"></param> /// <param name="requestConfiguration">Configuration for the request such as headers, query parameters, and middleware options.</param> /// </summary> public RequestInformation CreatePostRequestInformation(ManagedDevice body, Action <ManagedDevicesRequestBuilderPostRequestConfiguration> requestConfiguration = default) { _ = body ?? throw new ArgumentNullException(nameof(body)); var requestInfo = new RequestInformation { HttpMethod = Method.POST, UrlTemplate = UrlTemplate, PathParameters = PathParameters, }; requestInfo.SetContentFromParsable(RequestAdapter, "application/json", body); if (requestConfiguration != null) { var requestConfig = new ManagedDevicesRequestBuilderPostRequestConfiguration(); requestConfiguration.Invoke(requestConfig); requestInfo.AddRequestOptions(requestConfig.Options); requestInfo.AddHeaders(requestConfig.Headers); } return(requestInfo); }
public static ManagedDevice ReadIoTClientState() { lock (objLock) { ManagedDevice iotClientState = null; try { if (File.Exists(ShoppingStateFile)) { using (var file = File.OpenText(ShoppingStateFile)) { var serializer = new JsonSerializer(); iotClientState = (ManagedDevice)serializer.Deserialize(file, typeof(ManagedDevice)); } } } catch { } return(iotClientState); } }