public static Dictionary <DateTime, Dictionary <string, Dictionary <DateTime, Dictionary <Enums.MeasureType, double> > > > ReadFromLocal(IAMIAgregator agregator) { Dictionary <DateTime, Dictionary <string, Dictionary <DateTime, Dictionary <Enums.MeasureType, double> > > > agregatorDataLocal = new Dictionary <DateTime, Dictionary <string, Dictionary <DateTime, Dictionary <MeasureType, double> > > >(); Dictionary <string, Dictionary <DateTime, Dictionary <Enums.MeasureType, double> > > deviceMeasurements = new Dictionary <string, Dictionary <DateTime, Dictionary <MeasureType, double> > >(); Dictionary <DateTime, Dictionary <Enums.MeasureType, double> > list = new Dictionary <DateTime, Dictionary <MeasureType, double> >(); using (var data = new LocalBaseDBContex()) { var AgregatorBase = from d in data.LocalBaseData select d; foreach (var lb in AgregatorBase) { if (lb.AgregatorCode == agregator.agregatorCode) { Dictionary <MeasureType, double> measurement = new Dictionary <MeasureType, double>(); measurement.Add(MeasureType.voltage, lb.Voltage); measurement.Add(MeasureType.electricity, lb.Eletricity); measurement.Add(MeasureType.reactivePower, lb.ReactivePower); measurement.Add(MeasureType.activePower, lb.ActivePower); list.Add(DateTime.Parse(lb.TimeStamp), measurement); deviceMeasurements[lb.DeviceCode] = list; } } } agregatorDataLocal[DateTime.Now] = deviceMeasurements; return(agregatorDataLocal); }
public void Send(string code, long timestamp, Dictionary <Enums.MeasureType, double> measurements, string codeAgr) { if (code.Length != 4 || codeAgr.Length != 4) { throw new ArgumentException("Invalid code"); } try{ int.Parse(code); int.Parse(codeAgr); } catch { throw new ArgumentOutOfRangeException("It must be number."); } if (measurements.Count <= 0) { try { } catch (Exception e) { throw new ArgumentException("Dictionary is empty."); } } if (timestamp < 1528236934 || timestamp > 1546300800) { throw new ArgumentOutOfRangeException("Timestamp is out of range."); } using (var data = new LocalBaseDBContex()) { LocalBase lb = new LocalBase() { AgregatorCode = codeAgr, DeviceCode = code, TimeStamp = (Datas.UnixTimeToDateTime(timestamp)).ToString(), Voltage = measurements[MeasureType.voltage], Eletricity = measurements[MeasureType.electricity], ActivePower = measurements[MeasureType.activePower], ReactivePower = measurements[MeasureType.reactivePower] }; data.LocalBaseData.Add(lb); data.SaveChanges(); } Console.WriteLine("Message from [{0}] added in LocalDataBase at {1}.", code, Datas.UnixTimeToDateTime(timestamp)); }
public static void DeleteFromLocal(IAMIAgregator agregator) { using (var data = new LocalBaseDBContex()) { var AgregatorBase = from d in data.LocalBaseData select d; foreach (var lb in AgregatorBase) { if (lb.AgregatorCode == agregator.agregatorCode) { data.LocalBaseData.Remove(lb); } } Console.WriteLine("Measurements for Agregator [{0}] are deleted from LocalDataBase.", agregator.agregatorCode); data.SaveChanges(); } }
public string CheckLocalBase(string myAgregator, string DeviceCode) { if (myAgregator.Length != 4 || DeviceCode.Length != 4) { throw new ArgumentOutOfRangeException("Invalid code"); } try { int.Parse(myAgregator); int.Parse(DeviceCode); } catch { throw new ArgumentException("It must be number."); } using (var lData = new LocalBaseDBContex()) { bool exists = false; do { exists = false; foreach (var lb in lData.LocalBaseData) { if (lb.AgregatorCode == myAgregator && lb.DeviceCode == DeviceCode) { Console.WriteLine("Device with that code already exist-> changing code.."); Random rand = new Random(); DeviceCode = rand.Next(200, 10000).ToString(); Console.WriteLine("New code is : {0}", DeviceCode); exists = true; break; } } } while (exists); } return(DeviceCode); }