public IActionResult StatusUpdate(DeviceDetails details) { var output = new DeviceStatusUpdate(); output.Device = details; try { if (details.LoanStatus == null) { details.LoanStatus = "false"; details.LoanedTeam = ""; } var data = JsonConvert.SerializeObject(details); var path = "./Data/" + details.OwnerTeam.Replace("+", " ") + "/" + details.DeviceName + ".txt"; if (!System.IO.File.Exists(path)) { using (StreamWriter sw = System.IO.File.CreateText(path)) { sw.WriteLine(data); } } else { System.IO.File.WriteAllText(path, data); } return(View(output)); } catch { output.Error = "There was some error while updating the device status, please try again later."; return(View(output)); } }
public IActionResult Add(string teamName, string deviceName, string serialNumber) { var identity = System.Security.Principal.WindowsIdentity.GetCurrent().Name.Split('\\')[1]; string TeamAdminList; using (StreamReader sr = new StreamReader("./Roles/TeamAdmin.txt")) { TeamAdminList = sr.ReadToEnd(); } List <TeamAdminList> adminList = JsonConvert.DeserializeObject <List <TeamAdminList> >(TeamAdminList); var output = new DeviceStatusUpdate(); foreach (var team in adminList) { if (team.Admin == identity) { output.Teams.Add(team.Team); } } if (!string.IsNullOrEmpty(teamName) && !string.IsNullOrEmpty(deviceName)) { teamName = teamName.Replace("+", " "); deviceName = deviceName.Replace("+", " "); if (System.IO.File.Exists("./Data/" + teamName + "/" + deviceName + ".txt")) { output.Error = "This device is already associated to your account, please contact component owner to know more information info"; } else { using (StreamWriter sw = System.IO.File.CreateText("./Data/" + teamName + "/" + deviceName + ".txt")) { var deviceDetails = new DeviceDetails(); deviceDetails.DeviceName = deviceName; deviceDetails.SerialNumber = serialNumber?.Replace("+", " "); deviceDetails.LastUpdatedDate = DateTime.Now.ToString(); deviceDetails.DeviceOwner = identity; deviceDetails.OwnerTeam = teamName; deviceDetails.Status = ""; deviceDetails.LoanStatus = ""; deviceDetails.LoanedTeam = ""; sw.WriteLine(JsonConvert.SerializeObject(deviceDetails)); output.Message = "Device name: " + deviceName + ", is created successfully, and associated to your account"; } } } return(View(output)); }
public IActionResult Index(string team = null, string serialNumber = null) { var Data = new DeviceStatusUpdate(); try { team = team.Replace("+", " "); Data.TeamName = team; } catch { Data.Error = "Team name can't be empty"; return(View(Data)); } //string line; Data.Devices = Directory.GetFiles("./Data/" + team).ToList(); for (var i = 0; i < Data.Devices.Count(); i++) { Data.Devices[i] = Data.Devices[i].Replace("./Data/" + team + "\\", "").Replace(".txt", ""); } Data.Teams = Directory.GetDirectories("./Data/").ToList(); for (var j = 0; j < Data.Teams.Count(); j++) { Data.Teams[j] = Data.Teams[j].Replace("./Data/", ""); } if (!string.IsNullOrEmpty(serialNumber)) { Data.SerialNumber = serialNumber; try { var deviceFile = new StreamReader("./Data/" + team + "/" + serialNumber + ".txt"); Data.Device = new DeviceDetails(); var deviceDetails = deviceFile.ReadToEnd(); Data.Device = JsonConvert.DeserializeObject <DeviceDetails>(deviceDetails); deviceFile.Close(); } catch { Data.Error = "Device info not found"; } } return(View(Data)); }
public async Task <RestResponseStatus> PostDeviceStatusUpdate(RestSession session, DeviceStatusUpdate statusUpdate) { var headers = SessionToHeaders(session); var message = new StatusUpdateMessage { Country = statusUpdate.Country.ToString(), Traffic = statusUpdate.Traffic, Device = new Device { ClassName = "Device", Type = "Pointer", ObjectId = session.ObjectId } }; var content = JsonSerializer.Serialize(message); var result = await _restService.SendRestPostRequest(PostDeviceStatusLink, headers, content); return(result.Status); }