public DeviceDesc RegisterDevice(string projectId, DeviceDesc device) { try { string url = string.Format("{0}v1alpha2/projects/{1}/devices/", apiEndpoint, projectId); HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); request.Headers.Add(HttpRequestHeader.Authorization, "Bearer " + accessToken); string body = JsonConvert.SerializeObject(device); var data = Encoding.ASCII.GetBytes(body); request.Method = "POST"; request.ContentType = "application/json"; request.ContentLength = data.Length; using (var stream = request.GetRequestStream()) { stream.Write(data, 0, data.Length); } var response = (HttpWebResponse)request.GetResponse(); var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd(); return(JsonConvert.DeserializeObject <DeviceDesc>(responseString)); } catch (WebException ex) { Logger.Get().Error("Error during RegisterDevice : " + ex); return(null); } }
private DeviceDesc RegisterInstance(string projectId, string modelId) { DeviceDesc optionalDevice = ReadFromFile <DeviceDesc>(deviceRegisterConf.deviceInstanceFilePath, typeof(DeviceDesc)); if (optionalDevice != null) { return(optionalDevice); } DeviceDesc dev = new DeviceDesc(); dev.id = Guid.NewGuid().ToString(); dev.modelId = modelId; // Here we use the Google Assistant Service dev.clientType = "SDK_SERVICE"; try { DeviceDesc response = deviceInterface.RegisterDevice(projectId, dev); if (response != null) { using (StreamWriter file = File.CreateText(deviceRegisterConf.deviceInstanceFilePath)) { JsonSerializer serializer = new JsonSerializer(); serializer.Formatting = Formatting.Indented; serializer.Serialize(file, response); } return(response); } } catch (IOException e) { Logger.Get().Error("Error during RegisterInstance : " + e); } return(null); }
public void Register() { string projectId = deviceRegisterConf.projectId; deviceModel = RegisterModel(projectId); device = RegisterInstance(projectId, deviceModel.deviceModelId); }