/************** Property ************/ public bool PropertyAdd(DeviceProperty property) { try { if (property != null) { iotContext db = new iotContext(); db.Properties.Add(property); db.SaveChanges(); //TODO verify save } } catch (Exception e) { } return(true); }
public virtual void UpdateWithHistory(T entity) { try { if (entity.GetType() == typeof(Device)) { Device edited = (Device)(object)entity; iotRepository <DeviceParameter> repo = new iotRepository <DeviceParameter>(); iotRepository <Device> devrepo = new iotRepository <Device>(); Device devbefore = devrepo.GetById(edited.Id); foreach (var item in edited.Actions) { foreach (var param in item.ResultParameters) { DeviceParameter stparam = repo.GetById(param.Id); if (stparam != null) { if (!stparam.Value.Equals(param.Value)) { ActionChangeHistory hist = new ActionChangeHistory(); hist.Date = DateTime.Now; hist.Property = param; hist.Value = param.Value; iotRepository <ActionChangeHistory> histrepo = new iotRepository <ActionChangeHistory>(); histrepo.Add(hist); } } } } foreach (var item in edited.Properties) { foreach (var param in item.ResultParameters) { DeviceParameter stparam = repo.GetById(param.Id); if (stparam != null) { if (!stparam.Value.Equals(param.Value)) { ParameterChangeHistory hist = new ParameterChangeHistory(); hist.Date = DateTime.Now; hist.Property = param; hist.Value = param.Value; iotRepository <ParameterChangeHistory> histrepo = new iotRepository <ParameterChangeHistory>(); histrepo.Add(hist); } } } } } else if (entity.GetType() == typeof(DeviceAction)) { iotRepository <DeviceParameter> repo = new iotRepository <DeviceParameter>(); DeviceAction item = (DeviceAction)(object)entity; foreach (var param in item.ResultParameters) { DeviceParameter stparam = repo.GetById(param.Id); if (stparam != null) { if (!stparam.Value.Equals(param.Value)) { ActionChangeHistory hist = new ActionChangeHistory(); hist.Date = DateTime.Now; hist.Property = param; hist.Value = param.Value; iotRepository <ActionChangeHistory> histrepo = new iotRepository <ActionChangeHistory>(); histrepo.Add(hist); } } } } else if (entity.GetType() == typeof(DeviceProperty)) { iotRepository <DeviceParameter> repo = new iotRepository <DeviceParameter>(); DeviceProperty item = (DeviceProperty)(object)entity; foreach (var param in item.ResultParameters) { DeviceParameter stparam = repo.GetById(param.Id); if (stparam != null) { if (!stparam.Value.Equals(param.Value)) { ParameterChangeHistory hist = new ParameterChangeHistory(); hist.Date = DateTime.Now; hist.Property = param; hist.Value = param.Value; iotRepository <ParameterChangeHistory> histrepo = new iotRepository <ParameterChangeHistory>(); histrepo.Add(hist); } } } } DbEntityEntry dbEntityEntry = DbContext.Entry(entity); if (dbEntityEntry.State == EntityState.Detached) { DbSet.Attach(entity); } dbEntityEntry.State = EntityState.Modified; } catch (Exception e) { _logger.Error(e, e.Message); } }