public IHttpActionResult GetAllDataPointsForDevice([FromBody]string deviceID) { var db = new AirUDBCOE(); Device existingDevice = db.Devices.SingleOrDefault(x => x.DeviceID == deviceID); if (existingDevice != null) { List<Pollutant> pollutants = db.Pollutants.Select(x => x).ToList<Pollutant>(); List<SwaggerPollutantList> data = new List<SwaggerPollutantList>(); StringBuilder msg = new StringBuilder(); foreach (Pollutant p in pollutants) { var amsDataForPollutant = from a in db.Devices_States_and_Datapoints where a.DeviceID == deviceID && a.PollutantName == p.PollutantName orderby a.MeasurementTime select a; /* MOVE ALTITUDE TO STATE */ if (amsDataForPollutant.Count() != 0 && !p.PollutantName.Equals("Altitude")) { SwaggerPollutantList pl = new SwaggerPollutantList(p.PollutantName); foreach (var item in amsDataForPollutant) { pl.values.Add(new object[2]); pl.values.Last()[0] = ConvertDateTimeToMilliseconds(item.MeasurementTime); pl.values.Last()[1] = (decimal)item.Value; } data.Add(pl); } } return Ok(data); } else { // Account register failed. Account with email address: '<user.Email>' already exists. Please try a different email address. return BadRequest("Device with ID: " + deviceID + " does not exist. Please try a different Device ID."); } }
public IHttpActionResult GetDAQChartData([FromBody]string name) { if(swaggerDAQDataDictCache.Count == 0) { swaggerDAQDataDictCache.Add("Box Elder County", null); swaggerDAQDataDictCache.Add("Cache County", null); swaggerDAQDataDictCache.Add("Price", null); swaggerDAQDataDictCache.Add("Davis County", null); swaggerDAQDataDictCache.Add("Duchesne County", null); swaggerDAQDataDictCache.Add("Salt Lake County", null); swaggerDAQDataDictCache.Add("Tooele County", null); swaggerDAQDataDictCache.Add("Uintah County", null); swaggerDAQDataDictCache.Add("Utah County", null); swaggerDAQDataDictCache.Add("Washington County", null); swaggerDAQDataDictCache.Add("Weber County", null); cacheDateTimeStampDict.Add("Box Elder County", new DateTime()); cacheDateTimeStampDict.Add("Cache County", new DateTime()); cacheDateTimeStampDict.Add("Price", new DateTime()); cacheDateTimeStampDict.Add("Davis County", new DateTime()); cacheDateTimeStampDict.Add("Duchesne County", new DateTime()); cacheDateTimeStampDict.Add("Salt Lake County", new DateTime()); cacheDateTimeStampDict.Add("Tooele County", new DateTime()); cacheDateTimeStampDict.Add("Uintah County", new DateTime()); cacheDateTimeStampDict.Add("Utah County", new DateTime()); cacheDateTimeStampDict.Add("Washington County", new DateTime()); cacheDateTimeStampDict.Add("Weber County", new DateTime()); apiUrlDict.Add("Box Elder County", "http://air.utah.gov/xmlFeed.php?id=boxelder"); apiUrlDict.Add("Cache County", "http://air.utah.gov/xmlFeed.php?id=cache"); apiUrlDict.Add("Price", "http://air.utah.gov/xmlFeed.php?id=p2"); apiUrlDict.Add("Davis County", "http://air.utah.gov/xmlFeed.php?id=bv"); apiUrlDict.Add("Duchesne County", "http://air.utah.gov/xmlFeed.php?id=rs"); apiUrlDict.Add("Salt Lake County", "http://air.utah.gov/xmlFeed.php?id=slc"); apiUrlDict.Add("Tooele County", "http://air.utah.gov/xmlFeed.php?id=tooele"); apiUrlDict.Add("Uintah County", "http://air.utah.gov/xmlFeed.php?id=v4"); apiUrlDict.Add("Utah County", "http://air.utah.gov/xmlFeed.php?id=utah"); apiUrlDict.Add("Washington County", "http://air.utah.gov/xmlFeed.php?id=washington"); apiUrlDict.Add("Weber County", "http://air.utah.gov/xmlFeed.php?id=weber"); } List<SwaggerPollutantList> currentData = swaggerDAQDataDictCache[name]; DateTime currentDate = cacheDateTimeStampDict[name]; int timeDiffMinutes = (DateTime.Now.TimeOfDay - currentDate.TimeOfDay).Minutes; if (currentData == null || timeDiffMinutes > 35) { string url = apiUrlDict[name]; HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest; HttpWebResponse response = request.GetResponse() as HttpWebResponse; Stream stream = response.GetResponseStream(); XmlSerializer serializer = new XmlSerializer(typeof(SwaggerDAQData)); StreamReader reader = new StreamReader(stream); SwaggerDAQData data = (SwaggerDAQData)serializer.Deserialize(reader); List<SwaggerPollutantList> pollutantDataList = new List<SwaggerPollutantList>(); List<string> dates = new List<string>(); SwaggerPollutantList ozone = new SwaggerPollutantList("Ozone ppm"); SwaggerPollutantList pm25 = new SwaggerPollutantList("PM 2.5 ug/m^3"); SwaggerPollutantList no2 = new SwaggerPollutantList("NO2 ppm"); SwaggerPollutantList temperature = new SwaggerPollutantList("Temperature F"); SwaggerPollutantList co = new SwaggerPollutantList("CO ppm"); foreach (var dataSet in data.site.data) { DateTime wrongDateTime = DateTime.ParseExact(dataSet.date, "MM/dd/yyyy HH:mm:ss", CultureInfo.InvariantCulture); DateTime correctDateTime = wrongDateTime.AddHours(1); long dateMilliseconds = ConvertDateTimeToMilliseconds(correctDateTime); if (dataSet.ozone != "") { ozone.values.Add(new object[2]); ozone.values.Last()[0] = dateMilliseconds; ozone.values.Last()[1] = Decimal.Parse(dataSet.ozone); } else { ozone.values.Add(new object[2]); ozone.values.Last()[0] = dateMilliseconds; ozone.values.Last()[1] = 0.0; } if (dataSet.pm25 != "") { pm25.values.Add(new object[2]); pm25.values.Last()[0] = dateMilliseconds; pm25.values.Last()[1] = Decimal.Parse(dataSet.pm25); } else { pm25.values.Add(new object[2]); pm25.values.Last()[0] = dateMilliseconds; pm25.values.Last()[1] = 0.0; } if (dataSet.no2 != "") { no2.values.Add(new object[2]); no2.values.Last()[0] = dateMilliseconds; no2.values.Last()[1] = Decimal.Parse(dataSet.no2); } else { no2.values.Add(new object[2]); no2.values.Last()[0] = dateMilliseconds; no2.values.Last()[1] = 0.0; } if (dataSet.temperature != "") { temperature.values.Add(new object[2]); temperature.values.Last()[0] = dateMilliseconds; temperature.values.Last()[1] = Decimal.Parse(dataSet.temperature); } else { temperature.values.Add(new object[2]); temperature.values.Last()[0] = dateMilliseconds; temperature.values.Last()[1] = 0.0; } if (dataSet.co != "") { co.values.Add(new object[2]); co.values.Last()[0] = dateMilliseconds; co.values.Last()[1] = Decimal.Parse(dataSet.co); } else { co.values.Add(new object[2]); co.values.Last()[0] = dateMilliseconds; co.values.Last()[1] = 0.0; } } if (ozone.values.Count != 0) { pollutantDataList.Add(ozone); } if (pm25.values.Count != 0) { pollutantDataList.Add(pm25); } if (no2.values.Count != 0) { pollutantDataList.Add(no2); } if (temperature.values.Count != 0) { pollutantDataList.Add(temperature); } if (co.values.Count != 0) { pollutantDataList.Add(co); } cacheDateTimeStampDict[name] = DateTime.ParseExact(data.site.data[0].date, "MM/dd/yyyy HH:mm:ss", CultureInfo.InvariantCulture); swaggerDAQDataDictCache[name] = pollutantDataList; } return Ok(swaggerDAQDataDictCache[name]); }