public static async Task <List <Device> > GetCabinets() { try { string serverIP = Properties.Settings.Default.ServerIp; int serverPort = Properties.Settings.Default.ServerPort; string urlServer = "http://" + serverIP + ":" + serverPort; var client = new RestClient(urlServer); client.Authenticator = new HttpBasicAuthenticator(publicApiLogin, publicApiMdp); var request = new RestRequest("cabinets", Method.GET); client.Timeout = timeout; client.ReadWriteTimeout = timeout; request.Timeout = timeout; request.ReadWriteTimeout = timeout; var response = await client.ExecuteTaskAsync(request); if (response.IsSuccessful) { List <Device> lstDevice = new List <Device>(); var devices = JsonDevice.DeserializedJsonList(response.Content); if ((devices != null) && (devices.Count() > 0)) { foreach (JsonDevice jd in devices) { Device newDev = new Device { DeviceName = jd.name, DeviceSerial = jd.serial_num, DeviceLocation = jd.Location, IpAddress = jd.IP_addr, }; lstDevice.Add(newDev); } } return(lstDevice); } else { return(null); } } catch (Exception error) { ExceptionMessageBox exp = new ExceptionMessageBox(error, "Get Cabinet "); exp.ShowDialog(); return(null); } }
public static async Task <Device> GetCabinet(string serial) { try { Properties.Settings.Default.Reload(); string serverIP = Properties.Settings.Default.ServerIp; int serverPort = Properties.Settings.Default.ServerPort; string urlServer = "http://" + serverIP + ":" + serverPort; var client = new RestClient(urlServer); client.Authenticator = new HttpBasicAuthenticator(publicApiLogin, publicApiMdp); var request = new RestRequest("cabinets/" + serial, Method.GET); client.Timeout = timeout; client.ReadWriteTimeout = timeout; request.Timeout = timeout; request.ReadWriteTimeout = timeout; var response = await client.ExecuteTaskAsync(request); if (response.IsSuccessful) { var device = JsonDevice.DeserializedJsonAlone(response.Content); if (device != null) { Device newDev = new Device { DeviceName = device.name, DeviceSerial = device.serial_num, DeviceLocation = device.Location, IpAddress = device.IP_addr, }; return(newDev); } return(null); } else { return(null); } } catch (Exception error) { ExceptionMessageBox exp = new ExceptionMessageBox(error, "Error in Get Cabinet"); exp.ShowDialog(); return(null); } }
public static string SerializedJsonAlone(JsonDevice jsl) { JavaScriptSerializer jsSerializer = new JavaScriptSerializer(); return(jsSerializer.Serialize(jsl)); }