//================================================================ //Setters //Create PrimarySource public async Task <bool> CreatePrimarySourceAsync(PrimarySourceDataModel obj) { //mapping var PriamrySource = MappingToPrimarySource(obj); //Adding To Db _Context.PrimarySource.Add(PriamrySource); await _Context.SaveChangesAsync(); var value = _Context.PrimarySource.Last(); //value.Code = "" + value.Id; _Context.SaveChanges(); // PEAK POWER TABLE powerPeak peak = new powerPeak() { primarySourceId = Convert.ToInt16(value.Code), peakP1 = 0, peakP2 = 0, peakP3 = 0, }; _Context.powerPeak.Add(peak); _Context.SaveChanges(); return(true); }
public void Create(int id, string type) { if (type == "1") { _context.PhasesConnection.Add(new PhasesConnection { SourceType = type, SourceId = id, dN1 = -1, dN2 = -2, dN3 = -2 }); } else { _context.PhasesConnection.Add(new PhasesConnection { SourceType = type, SourceId = id, dN1 = -1, dN2 = -1, dN3 = -1 }); } _context.SaveChanges(); }
public string test() { var reading = new PowerAvg() { P1 = 120, //model.Power1, P2 = 120, //model.Power2, P3 = 120, //model.Power3, primarySourceId = 5, //model.PrimarySourceId, secondrySourceId = null, // model.SecondarySourceId, readingDate = DateTime.Now //model.TimeStamp }; Context.PowerAvg.Add(reading); Context.SaveChanges(); return($"Ok,"); }
public async Task <bool> CreateLoadAsync(LoadDataModel obj) { //mapping var load = MappingtoLoad(obj); //Adding To Db _con.Load.Add(load); await _con.SaveChangesAsync(); Loadparameter par = new Loadparameter { Power = obj.Power, PowerFactor = obj.PowerFactor, RatingCurrent = obj.RatingCurrent, RatingTemp = obj.RatingTemp, RatingVoltage = obj.RatingVoltage, Type = obj.Type, }; _con.Loadparameter.Add(par); var value = _con.Load.Last(); value.code = value.Id; value.LoadInfo = par; _con.SaveChanges(); // lw mtwsl b primary if (obj.SourceId % 2 != 0) { PhasesConnection phase = new PhasesConnection(); phase.SourceId = obj.SourceId; phase.SourceType = new PrimarySourceSerivce(_con).GetPrimarySourceFromDBByCode(obj.SourceId).Type; if (obj.Type == "1") { if (obj.dN1 == 1) { phase.dN1 = 1; phase.sN1 = "l" + value.Id; } else if (obj.dN1 == 2) { phase.dN2 = 1; phase.sN2 = "l" + value.Id; } else { phase.dN3 = 1; phase.sN3 = "l" + value.Id; } } else { if (obj.dN1 == 1) { phase.dN1 = 1; phase.sN1 = "l" + value.Id; } else if (obj.dN1 == 2) { phase.dN2 = 1; phase.sN2 = "l" + value.Id; } else { phase.dN3 = 1; phase.sN3 = "l" + value.Id; } if (obj.dN2 == 1) { phase.dN1 = 2; phase.sN1 = "l" + value.Id; } else if (obj.dN2 == 2) { phase.dN2 = 2; phase.sN2 = "l" + value.Id; } else { phase.dN2 = 2; phase.sN3 = "l" + value.Id; } if (obj.dN3 == 1) { phase.dN1 = 3; phase.sN1 = "l" + value.Id; } else if (obj.dN3 == 2) { phase.dN2 = 3; phase.sN2 = "l" + value.Id; } else { phase.dN3 = 3; phase.sN3 = "l" + value.Id; } } _con.PhasesConnection.Add(phase); await _con.SaveChangesAsync(); } // lw mtwsl bsec else { //connect to secondry source phases PhasesConnection phase = new PhasesConnectionService(_con).getPhaseBySourceId(obj.SourceId); if (obj.PhaseType == "1") { if (obj.dN1 == 1) { phase.dN1 = 1; phase.sN1 = "l" + value.Id; } else if (obj.dN1 == 2) { phase.dN2 = 1; phase.sN2 = "l" + value.Id; } else { phase.dN3 = 1; phase.sN3 = "l" + value.Id; } } else { if (obj.dN1 == 1) { phase.dN1 = 1; phase.sN1 = "l" + value.Id; } else if (obj.dN1 == 2) { phase.dN2 = 1; phase.sN2 = "l" + value.Id; } else { phase.dN3 = 1; phase.sN3 = "l" + value.Id; } if (obj.dN2 == 1) { phase.dN1 = 2; phase.sN1 = "l" + value.Id; } else if (obj.dN2 == 2) { phase.dN2 = 2; phase.sN2 = "l" + value.Id; } else { phase.dN2 = 2; phase.sN3 = "l" + value.Id; } if (obj.dN3 == 1) { phase.dN1 = 3; phase.sN1 = "l" + value.Id; } else if (obj.dN3 == 2) { phase.dN2 = 3; phase.sN2 = "l" + value.Id; } else { phase.dN3 = 3; phase.sN3 = "l" + value.Id; } } _con.PhasesConnection.Update(phase); } await _con.SaveChangesAsync(); return(true); }
//================================================================ //Setters //Create Secondary public async Task <bool> CreateSecoundrySourceAsync(SecoundrySouresDataModelSim obj) { //mapping var secondarySource = MappingToPrimarySource(obj); //Adding To Db _Context.secondarySource.Add(secondarySource); await _Context.SaveChangesAsync(); var value = _Context.secondarySource.Last(); //value.Code = "" + value.Id; _Context.SaveChanges(); // create secondary source phase PhasesConnectionService phs = new PhasesConnectionService(_Context); phs.Create(value.Id, value.Type); // PEAK POWER TABLE powerPeak peak = new powerPeak() { secondrySourceId = Convert.ToInt16(value.Code), peakP1 = 0, peakP2 = 0, peakP3 = 0, }; _Context.powerPeak.Add(peak); _Context.SaveChanges(); //connect to primary source phases PhasesConnection phase = new PhasesConnection(); phase.SourceId = obj.PS_Id; phase.SourceType = new PrimarySourceSerivce(_Context).GetPrimarySourceFromDBByCode(obj.PS_Id).Type; if (obj.Type == "1") { if (obj.dN1 == 1) { phase.dN1 = 1; phase.sN1 = "s" + value.Id; } else if (obj.dN1 == 2) { phase.dN2 = 1; phase.sN2 = "s" + value.Id; } else { phase.dN3 = 1; phase.sN3 = "s" + value.Id; } } else { if (obj.dN1 == 1) { phase.dN1 = 1; phase.sN1 = "s" + value.Id; } else if (obj.dN1 == 2) { phase.dN2 = 1; phase.sN2 = "s" + value.Id; } else { phase.dN3 = 1; phase.sN3 = "s" + value.Id; } if (obj.dN2 == 1) { phase.dN1 = 2; phase.sN1 = "s" + value.Id; } else if (obj.dN2 == 2) { phase.dN2 = 2; phase.sN2 = "s" + value.Id; } else { phase.dN2 = 2; phase.sN3 = "s" + value.Id; } if (obj.dN3 == 1) { phase.dN1 = 3; phase.sN1 = "s" + value.Id; } else if (obj.dN3 == 2) { phase.dN2 = 3; phase.sN2 = "s" + value.Id; } else { phase.dN3 = 3; phase.sN3 = "s" + value.Id; } } _Context.PhasesConnection.Add(phase); await _Context.SaveChangesAsync(); return(true); }
public bool Edit(ReportConstant obj) { _Contect.ReportConstants.Update(obj); _Contect.SaveChanges(); return(true); }
public void InitialRecordData(RecordParams recordParams, Guid stageId) { try { Stage.Add(stageId, RecordProcessStage.OnCreatingRecord); var recordRepo = Repo <PowerRepository <Record> >(); var newRecord = new Record { RecordName = recordParams.RecordName, RecordStartDateTime = DateTime.Parse(recordParams.RecordConfigs["StartDateTime"]), RecordDuration = TimeSpan.Parse(recordParams.RecordConfigs["RecordDuration"]), RecordEndDateTime = DateTime.Parse(recordParams.RecordConfigs["EndDateTime"]) }; recordRepo.AddOrUpdateDoCommit(newRecord); newRecord.ModelState = ModelState.Changed; var configRepo = Repo <PowerRepository <SystemConfig> >(); var configs = configRepo.GetModelList(obj => obj.ConfigType == "RecordInitial") .ToDictionary(obj => obj.ConfigName, item => item.ConfigValue); var initialzation = $"{newRecord.Id}\r\n{recordParams.RecordConfigs["Period"]}\r\n{recordParams.RecordConfigs["Frequency"]}\r\n{recordParams.RecordConfigs["LineType"]}"; foreach (var file in Directory.GetFiles(configs["dataDirectory"])) { if (!recordParams.FileList.Contains(Path.GetFileNameWithoutExtension(file))) { File.Delete(file); } } if (Directory.GetFiles(configs["dataDirectory"]).Length != recordParams.FileList.Count) { Stage[stageId] = RecordProcessStage.MissingFile; return; } File.WriteAllText(configs["InitialzationFile"], initialzation); if (!Globals.IsProcessRunning(configs["MainProcessName"])) { Process.Start(configs["MainProcessPath"]); } File.WriteAllText(configs["FinishFile"], "0"); File.WriteAllText(configs["StartFile"], "1"); Stage[stageId] = RecordProcessStage.OnCaclating; while (File.ReadAllText(configs["FinishFile"]).Substring(0, 1) != "1") { Thread.Sleep(1000); } var config = new RecordConfig { LineType = (LineType)byte.Parse(recordParams.RecordConfigs["LineType"]), CalcPrecision = ushort.Parse(recordParams.RecordConfigs["Period"]), Frequency = ushort.Parse(recordParams.RecordConfigs["SampleRate"]), RecordId = newRecord.Id }; var ctx = new PowerDbContext(); ctx.RecordConfigs.Add(config); ctx.SaveChanges(); using (var connection = new MySqlConnection(configs["MySqlConnString"])) { Stage[stageId] = RecordProcessStage.OnAfterCaclating; connection.Open(); using (var transction = connection.BeginTransaction()) { try { using (var loadCmd = connection.CreateCommand()) { loadCmd.CommandType = CommandType.Text; loadCmd.CommandText = $"LOAD DATA LOCAL INFILE '{configs["ActiveFilePath"]}' INTO TABLE activevalues"; loadCmd.ExecuteNonQuery(); loadCmd.CommandText = $"LOAD DATA LOCAL INFILE '{configs["HarmonicFilePath"]}' INTO TABLE harmonics"; loadCmd.ExecuteNonQuery(); } using (var cmd = connection.CreateCommand()) { cmd.CommandType = CommandType.StoredProcedure; cmd.CommandText = "transferPowers"; cmd.CommandTimeout = int.MaxValue; cmd.Parameters.Add(new MySqlParameter() { DbType = DbType.Int64, Direction = ParameterDirection.Input, ParameterName = "relativeRecordId", Value = newRecord.Id }); cmd.ExecuteNonQuery(); cmd.CommandText = "calcvoltageseconds"; cmd.ExecuteNonQuery(); cmd.CommandText = "calcvoltagethreeseconds"; cmd.ExecuteNonQuery(); } transction.Commit(); } catch (Exception ex) { transction.Rollback(); File.WriteAllText(configs["ActiveFilePath"], string.Empty); File.WriteAllText(configs["HarmonicFilePath"], string.Empty); LogService.Instance.Error("数据库操作执行失败。", ex); Stage[stageId] = RecordProcessStage.Failed; return; } } File.WriteAllText(configs["ActiveFilePath"], string.Empty); File.WriteAllText(configs["HarmonicFilePath"], string.Empty); File.WriteAllText(configs["StartFile"], "0"); } Stage[stageId] = RecordProcessStage.ProcessCompleted; newRecord.Finalized = true; recordRepo.AddOrUpdateDoCommit(newRecord); } catch (Exception ex) { LogService.Instance.Error("生成记录失败。", ex); } }
private static void GenerateData() { var context = new PowerDbContext(); var startDate = DateTime.Now; var duration = TimeSpan.FromMilliseconds(345600 * 250); context.Set <Record>().Add(new Record { Id = 1, RecordName = "测试记录", RecordDateTime = startDate, RecordStartDateTime = startDate, RecordDuration = duration, RecordEndDateTime = startDate + duration }); context.Set <RecordConfig>().Add(new RecordConfig { CalcPrecision = 250, Frequency = 256, LineType = LineType.StarWithMiddle, RecordId = 1 }); context.SaveChanges(); //var recordCount = 345600; //var recordIndexs = new List<int>(); //var current = 0; //while (current < recordCount) //{ // recordIndexs.Add(current); // current += 100; //} //Parallel.ForEach(recordIndexs, (index) => //{ // var done = false; // while (!done) // { // try // { // var dbContext = new PowerDbContext(); // var rd = new Random(); // var activeValues = new List<ActiveValue>(); // var harmonics = new List<Harmonic>(); // for (var i = index; i < index + 100; i++) // { // var avg = Math.Round(rd.Next(1, 100) / 100.0 + 220, 2); // var cur = Math.Round(rd.Next(1, 100) / 100.0 + 120, 2); // var activeValue = new ActiveValue // { // Voltage_AN = avg, // Voltage_BN = avg + 0.2, // Voltage_CN = avg - 0.2, // Voltage_NG = avg - 220, // Voltage_AB = avg + 0.3, // Voltage_BC = avg + 0.2, // Voltage_CA = avg + 0.1, // Current_A = cur, // Current_B = cur + 2, // Current_C = cur - 2, // Current_N = cur - 120, // RecordId = 1, // RecordIndex = i, // RecordTimeTicks = startDate.AddMilliseconds(i * 250).Ticks // }; // activeValues.Add(activeValue); // harmonics.Add(new Harmonic() // { // RecordId = 1, // RecordIndex = i // }); // } // dbContext.Configuration.AutoDetectChangesEnabled = false; // dbContext.Configuration.ValidateOnSaveEnabled = false; // dbContext.ActiveValues.AddRange(activeValues); // dbContext.Harmonics.AddRange(harmonics); // dbContext.SaveChanges(); // } // catch (Exception ex) // { // LogService.Instance.Fail("添加记录数据失败", ex); // Console.WriteLine(ex); // } // done = true; // } //}); }