public static string ToString(DeviceDataType value) { if (value == DeviceDataType.Quantity) { return("Quantity"); } else if (value == DeviceDataType.Range) { return("Range"); } else if (value == DeviceDataType.Coding) { return("Coding"); } else if (value == DeviceDataType.Array) { return("Array"); } else if (value == DeviceDataType.String) { return("string"); } else { throw new ArgumentException("Unrecognized DeviceDataType value: " + value.ToString()); } }
public static string ToString(DeviceDataType value) { if( value==DeviceDataType.Quantity ) return "Quantity"; else if( value==DeviceDataType.Range ) return "Range"; else if( value==DeviceDataType.Coding ) return "Coding"; else if( value==DeviceDataType.Array ) return "Array"; else if( value==DeviceDataType.String ) return "string"; else throw new ArgumentException("Unrecognized DeviceDataType value: " + value.ToString()); }
public ActionResult Monthly(string macaddress, DeviceDataType type = DeviceDataType.EnergyUsage) { ViewBag.Title = "WattchDog - Monthly Device Data"; TempData["data_type"] = type; string table = ""; switch (type) { case DeviceDataType.EnergyUsage: table = "EnergyUsages"; break; case DeviceDataType.Irms: table = "RmsCurrents"; break; case DeviceDataType.PowerFactor: table = "PowerFactors"; break; case DeviceDataType.RealPower: table = "RealPowers"; break; default: table = "RmsVoltages"; break; } var repo = new TempRepo(); var aggregatetdData = new MonthlyDataViewModel(); var device = repo.GetDevice("mac_address", macaddress).Result; if (device.UserId != Session["UID"] as int?) { return(RedirectToAction("Index")); } aggregatetdData.Device = (DeviceViewModel)device; var queryTime = TimeZoneInfo.ConvertTime(DateTime.Now, TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time")); List <MonthlyDatapointViewModel> outputData; var curTime = new DateTime(queryTime.Year, queryTime.Month, 1); var refTime = new DateTime(queryTime.Year, queryTime.Month, 1).AddYears(-1); var cache = MemoryCache.Default; var key = $"{macaddress.ToUpper()}-{type.ToString()}-{curTime.ToString()}-monthly"; if (cache.Contains(key)) { outputData = (List <MonthlyDatapointViewModel>)cache[key]; } else { outputData = new List <MonthlyDatapointViewModel>(); var data = repo.GetAggregatedData(table, device.ID, queryTime, DateGrouping.Monthly).Result.Reverse().ToList(); while (refTime < curTime) { if (data.ElementAtOrDefault(0)?.GroupedDate == refTime) { outputData.Add(new MonthlyDatapointViewModel() { Month = data[0].GroupedDate, NumSamples = data[0].NumSamples, Value = type == DeviceDataType.EnergyUsage ? Math.Round(data[0].AvgValue, 7) : Math.Round(data[0].AvgValue, 2) }); data.RemoveAt(0); } else { outputData.Add(new MonthlyDatapointViewModel() { Month = refTime, NumSamples = 0, Value = 0 }); } refTime = refTime.AddMonths(1); } cache.Add(key, outputData, DateTimeOffset.Now.AddMinutes(5)); } aggregatetdData.Data = outputData; aggregatetdData.Type = type; return(View(aggregatetdData)); }