public static int AddRoomDeviceTimeOn(DeviceOnTime reg) { using (HomeAutomationEntities context = new HomeAutomationEntities()) { context.DeviceOnTime.Add(reg); int result = context.SaveChanges(); return(result); } }
/// <summary> /// Adds a User register. /// </summary> /// <returns></returns> /// Version: SPI V5G /// Author: Felipe Botero public static int Add(User reg) { using (HomeAutomationEntities context = new HomeAutomationEntities()) { context.User.Add(reg); int result = context.SaveChanges(); return(result); } }
public static int UpdateRoomDeviceTimeOn(DeviceOnTime reg) { using (HomeAutomationEntities context = new HomeAutomationEntities()) { context.DeviceOnTime.Attach(reg); context.Entry(reg).State = EntityState.Modified; int result = context.SaveChanges(); return(result); } }
public static int Update(User reg) { using (HomeAutomationEntities context = new HomeAutomationEntities()) { context.User.Attach(reg); context.Entry(reg).State = EntityState.Modified; int result = context.SaveChanges(); return(result); } }
public SchemeViewModel() { #region 初始化方案 using (var db = new HomeAutomationEntities()) { Schemes = db.t_Scheme.ToList(); } if (Schemes.Count > 0) { Scheme = Schemes[0]; SchemeName = Schemes[0].SchemeName; } #endregion #region 初始化详细方案//test RefreshDetailScheme(); #endregion #region 初始化电器 using (var db = new HomeAutomationEntities()) { HomeAppliances = db.t_HomeAppliances.ToList(); } #endregion #region 新建方案 CreateSchemeCommand = new RelayCommand(() => { if (SchemeState) { UMessageBox.Show($"请先停止方案!", false); return; } if (SchemeNameEidtState) { return; } using (var db = new HomeAutomationEntities()) { var count = db.t_Scheme.ToList().Count; db.t_Scheme.Add(new t_Scheme() { SchemeName = $"方案{++count}" }); db.SaveChanges(); Schemes = db.t_Scheme.ToList(); Scheme = Schemes.Find(x => x.SchemeName == $"方案{count}"); DetailSchemes = new ObservableCollection <DetailSchemeModel>(); } }); #endregion #region 除方案 DeleteSchemeCommand = new RelayCommand(() => { if (SchemeState) { UMessageBox.Show($"请先停止方案!", false); return; } if (SchemeNameEidtState) { return; } var result = UMessageBox.Show("确定删除当前方案吗?"); if (result == false) { return; } using (var db = new HomeAutomationEntities()) { if (Scheme == null) { return; } var scheme = db.t_Scheme.FirstOrDefault(x => x.Id == Scheme.Id); if (scheme != null) { db.t_Scheme.Remove(scheme); } var detailScheme = db.t_DetailScheme.Where(x => x.SchemeId == Scheme.Id); db.t_DetailScheme.RemoveRange(detailScheme); db.SaveChanges(); Schemes = db.t_Scheme.ToList(); if (Schemes.Count <= 0) { return; } Scheme = Schemes[0]; SchemeName = Schemes[0].SchemeName; } }); #endregion #region 刷新方案列表命令 HomeApplianceScheme = new RelayCommand(() => { if (SchemeNameEidtState) { return; } RefreshDetailScheme(); }); #endregion #region 方案定时开关 (开关定时任务) StartSchemeCommand = new RelayCommand((() => { if (!UMessageBox.Result) { return; } //是否选择方案,执行方案是否为空 if (Scheme == null) { UMessageBox.Show($"当前没有选中方案!", false); return; } //是否正在编辑方案 if (SchemeNameEidtState) { return; } //定时方案停止 if (SchemeState) { // //等待提示框 //强制关闭所有电器 //var driver =new Rs485Driver(); //try //{ // driver.Connect(new NetArg("192.168.0.233", 10001)); //} //catch (Exception e) //{ // MessageBox.Show($"连接不上设备", "提示", MessageBoxButton.OK, MessageBoxImage.Information); // return; //} //var state= driver.Read32RelayOutputState(); //foreach (var value in state) //{ // if (value.Value==true) // { // ControllerHomeAutomation(value.Key, false); // } //} foreach (var value in DetailSchemes) { value.RunSchemeState = false; value.SchemevisibiState = "Hidden"; value.Cancel.Cancel(); } SchemeState = false; SchemeList = true; } //定时方案启动 else { SchemeState = true; SchemeList = false; foreach (var value in DetailSchemes) { value.Cancel = new CancellationTokenSource(); value.Task = new Task(() => { //等待开始 var waitTime = 0; while (waitTime < value.DtailScheme.WaitTime) { try { value.Cancel.Token.ThrowIfCancellationRequested(); } catch (Exception e) { value.RunSchemeState = false; value.SchemevisibiState = "Hidden"; return; } waitTime++; Thread.Sleep(1000); } value.RunSchemeState = true; value.SchemevisibiState = "Visible"; //启动电器 //try //{ // // AsyncControllerHomeAutomation(value.DtailScheme.HomeAppliancesId, true); //} //catch (Exception e) //{ // MessageBox.Show($"连接不上设备", "提示", MessageBoxButton.OK, MessageBoxImage.Information); // value.RunSchemeState = false; // value.SchemevisibiState = "Hidden"; // return; //} //执行时长 var runTime = 0; //等待结束 while (runTime < value.DtailScheme.RunTime) { var timeSpan = new TimeSpan(0, 0, value.DtailScheme.RunTime - runTime - 1); value.SchemeTime = timeSpan.Hours.ToString() + " h " + timeSpan.Minutes + " m " + timeSpan.Seconds + " s"; try { value.Cancel.Token.ThrowIfCancellationRequested(); } catch (Exception e) { value.RunSchemeState = false; value.SchemevisibiState = "Hidden"; return; } runTime++; Thread.Sleep(1000); } //关闭电器 //try //{ // AsyncControllerHomeAutomation(value.DtailScheme.HomeAppliancesId, false); //} //catch (Exception e) //{ // MessageBox.Show($"连接不上设备", "提示", MessageBoxButton.OK, MessageBoxImage.Information); //} value.RunSchemeState = false; }, value.Cancel.Token); value.Task.Start(); } } })); #endregion #region 添加 定时命令 AddSchemeCommand = new RelayCommand(() => { if (DetailSchemes.FirstOrDefault(x => x.DtailScheme.HomeAppliancesName == "选择电器") != null) { //提示选择电器 UMessageBox.Show($"请选择电器!", false); return; } using (var db = new HomeAutomationEntities()) { //遍历详细方案 for (var i = 0; i < DetailSchemes.Count; i++) { if (DetailSchemes[i].DtailScheme.WaitTime < 0 || DetailSchemes[i].DtailScheme.RunTime <= 0) { UMessageBox.Show($"{DetailSchemes[i].DtailScheme.HomeAppliancesName} 时间运行太短!", false); return; } //id= 100表示没有选中 if (DetailSchemes[i] == null || DetailSchemes[i].DtailScheme.HomeAppliancesId == 100) { continue; } //判断时间重复 for (var j = 0; j < i; j++) { //根据电器名称相同,断定同一电器 if (DetailSchemes[i].DtailScheme.HomeAppliancesName != DetailSchemes[j].DtailScheme.HomeAppliancesName) { } //判断同一电器启动,一分钟间隔以上 else if (DetailSchemes[i].DtailScheme.WaitTime > (DetailSchemes[j].DtailScheme.RunTime + DetailSchemes[j].DtailScheme.WaitTime) + 60 || DetailSchemes[i].DtailScheme.RunTime + DetailSchemes[i].DtailScheme.WaitTime + 60 < DetailSchemes[j].DtailScheme.WaitTime) { } else { UMessageBox.Show($"{DetailSchemes[i].DtailScheme.HomeAppliancesName} 运行时间冲突或者间隔太近!", false); return; } } var detail = DetailSchemes[i]; var homeAppliance = db.t_HomeAppliances.FirstOrDefault( x => x.Name == detail.DtailScheme.HomeAppliancesName); if (homeAppliance != null) { db.t_DetailScheme.AddOrUpdate(new t_DetailScheme() { Id = DetailSchemes[i].DtailScheme.DetailSchemeId, HomeAppliancesId = homeAppliance.Id, RunTime = DetailSchemes[i].DtailScheme.RunTime, WaitTime = DetailSchemes[i].DtailScheme.WaitTime, SchemeId = DetailSchemes[i].DtailScheme.SchemeId }); } } db.SaveChanges(); Schemes = db.t_Scheme.ToList(); Scheme = Schemes.FirstOrDefault(x => x.Id == SchemmeId); } RefreshDetailScheme(); DetailSchemes.Add(new DetailSchemeModel() { DtailScheme = new v_DtailScheme() { RunTime = 0, WaitTime = 0, SchemeId = Scheme.Id, HomeAppliancesName = "选择电器" } }); }); #endregion #region 更新选中定时 UpdataSchemeCommand = new RelayCommand <long>((detailSchemeId) => { var detailscheme = DetailSchemes.FirstOrDefault(x => x.DtailScheme.DetailSchemeId == detailSchemeId); //id 100表示没有选中 if (detailscheme == null || detailSchemeId == 100) { return; } //判断时间重复 using (var db = new HomeAutomationEntities()) { var homeAppliance = db.t_HomeAppliances.FirstOrDefault(x => x.Name == detailscheme.DtailScheme.HomeAppliancesName); var detailScheme = db.t_DetailScheme.FirstOrDefault(x => x.Id == detailscheme.DtailScheme.DetailSchemeId); if (detailScheme == null || homeAppliance == null) { return; } detailScheme.HomeAppliancesId = homeAppliance.Id; detailScheme.RunTime = detailscheme.DtailScheme.RunTime; detailScheme.WaitTime = detailscheme.DtailScheme.WaitTime; db.SaveChanges(); } RefreshDetailScheme(); }); #endregion #region 除选中定时命令 DeleletChooseDetailScheme = new RelayCommand <long>(((detailSchemeId) => { using (var db = new HomeAutomationEntities()) { //删除定时 var descheme = db.t_DetailScheme.FirstOrDefault(x => x.Id == detailSchemeId); if (descheme != null) { db.t_DetailScheme.Remove(descheme); } db.SaveChanges(); } DetailSchemes.Remove(DetailSchemes.FirstOrDefault(x => x.DtailScheme.DetailSchemeId == detailSchemeId)); })); #endregion #region 编辑选中方案 UpdataChooseDatailScheme = new RelayCommand((() => { if (SchemeState) { UMessageBox.Show($"请先停止方案!", false); return; } if (SchemeName == null) { return; } if (SchemeEidtState) { SchemmeId = Scheme.Id; SchemeEidtState = false; ButtonNameSave = "完成"; SchemeAddState = "Visible"; SchemeNameEidtState = true; } else { for (var i = DetailSchemes.Count() - 1; i >= 0; i--) { if (DetailSchemes[i].DtailScheme.HomeAppliancesName == "选择电器") { DetailSchemes.Remove(DetailSchemes[i]); } } using (var db = new HomeAutomationEntities()) { //更新方案名称 var scheme = db.t_Scheme.FirstOrDefault(x => x.Id == SchemmeId); if (scheme != null) { scheme.SchemeName = SchemeName; } //遍历详细方案 for (var i = 0; i < DetailSchemes.Count; i++) { if (DetailSchemes[i].DtailScheme.WaitTime <= 0 || DetailSchemes[i].DtailScheme.RunTime <= 0) { UMessageBox.Show($"{DetailSchemes[i].DtailScheme.HomeAppliancesName} 时间运行太短!", false); return; } //id= 100表示没有选中 if (DetailSchemes[i] == null || DetailSchemes[i].DtailScheme.HomeAppliancesId == 100) { continue; } //判断时间重复 for (var j = 0; j < i; j++) { //根据电器名称相同,断定同一电器 if (DetailSchemes[i].DtailScheme.HomeAppliancesName != DetailSchemes[j].DtailScheme.HomeAppliancesName) { } //判断同一电器启动,一分钟间隔以上 else if (DetailSchemes[i].DtailScheme.WaitTime > (DetailSchemes[j].DtailScheme.RunTime + DetailSchemes[j].DtailScheme.WaitTime) + 60 || DetailSchemes[i].DtailScheme.RunTime + DetailSchemes[i].DtailScheme.WaitTime + 60 < DetailSchemes[j].DtailScheme.WaitTime) { } else { UMessageBox.Show($"{DetailSchemes[i].DtailScheme.HomeAppliancesName} 时间冲突!", false); return; } } var detail = DetailSchemes[i]; var homeAppliance = db.t_HomeAppliances.FirstOrDefault( x => x.Name == detail.DtailScheme.HomeAppliancesName); if (homeAppliance != null) { db.t_DetailScheme.AddOrUpdate(new t_DetailScheme() { Id = DetailSchemes[i].DtailScheme.DetailSchemeId, HomeAppliancesId = homeAppliance.Id, RunTime = DetailSchemes[i].DtailScheme.RunTime, WaitTime = DetailSchemes[i].DtailScheme.WaitTime, SchemeId = DetailSchemes[i].DtailScheme.SchemeId }); } } db.SaveChanges(); Schemes = db.t_Scheme.ToList(); Scheme = Schemes.FirstOrDefault(x => x.Id == SchemmeId); } RefreshDetailScheme(); SchemeEidtState = true; ButtonNameSave = "方案编辑"; SchemeAddState = "Hidden"; SchemeNameEidtState = false; } })); #endregion #region 编辑运行时间 InstallRunTimeCommand = new RelayCommand <long>(detailSchemeId => { if (SchemeEidtState) { return; } var detailScheme = DetailSchemes.FirstOrDefault(x => x.DtailScheme.DetailSchemeId == detailSchemeId); if (detailScheme == null) { return; } var result = UTimePiker.ChooseEquals(detailScheme.DtailScheme.RunTime); var dtailScheme = new v_DtailScheme() { DetailSchemeId = detailSchemeId, HomeAppliancesId = detailScheme.DtailScheme.HomeAppliancesId, HomeAppliancesName = detailScheme.DtailScheme.HomeAppliancesName, RunTime = result, SchemeId = detailScheme.DtailScheme.SchemeId, SchemeName = detailScheme.DtailScheme.SchemeName, WaitTime = detailScheme.DtailScheme.WaitTime }; detailScheme.DtailScheme = dtailScheme; }); #endregion #region 编辑等待时间 InstallWaitTimeCommand = new RelayCommand <long>(detailSchemeId => { if (SchemeEidtState) { return; } var detailScheme = DetailSchemes.FirstOrDefault(x => x.DtailScheme.DetailSchemeId == detailSchemeId); if (detailScheme == null) { return; } var result = UTimePiker.ChooseEquals(detailScheme.DtailScheme.WaitTime); var dtailScheme = new v_DtailScheme() { DetailSchemeId = detailSchemeId, HomeAppliancesId = detailScheme.DtailScheme.HomeAppliancesId, HomeAppliancesName = detailScheme.DtailScheme.HomeAppliancesName, RunTime = detailScheme.DtailScheme.RunTime, SchemeId = detailScheme.DtailScheme.SchemeId, SchemeName = detailScheme.DtailScheme.SchemeName, WaitTime = result }; detailScheme.DtailScheme = dtailScheme; }); #endregion }