public void TestRepoUpdate() { iotRepository <Location> repo = new iotRepository <Location>(); //get List <Location> locs = repo.GetAll().ToList(); //add if required Location loc = new Location(); if (locs != null) { loc.LocationName = Guid.NewGuid().ToString(); loc.Lat = 0; loc.Lng = 0; repo.Add(loc); //update locs = repo.GetAll().ToList(); loc = locs.Where(l => { return(l.LocationName == loc.LocationName); }).First(); Location StoredBefore = repo.GetById(loc.Id); StoredBefore.Lat = 53.325241; repo.Update(StoredBefore); //verify Location stored = repo.GetById(loc.Id); Assert.IsTrue(loc.Lat == stored.Lat); } }
public EndpointInfo EndpointWithId(int id) { try { iotRepository <EndpointInfo> repo = new iotRepository <EndpointInfo>(); return(repo.GetById(id)); } catch (Exception e) { nlogger.ErrorException(e.Message, e); return(new EndpointInfo()); } }
public bool TestRepoSingleRead(int id) { try { iotRepository <Location> repo = new iotRepository <Location>(); Location locs = repo.GetById(id); return(locs != null); } catch (Exception ex) { return(false); } }
public Device GetDevice(string id) { try { int DeviceId = Convert.ToInt32(id); iotRepository <Device> repo = new iotRepository <Device>(); return(repo.GetById(DeviceId)); } catch (Exception ex) { throw new FaultException(ex.Message); } }
public ActionParameter ActionParamWithId(int id) { try { iotRepository <ActionParameter> repo = new iotRepository <ActionParameter>(); return(repo.GetById(id)); } catch (Exception e) { nlogger.ErrorException(e.Message, e); return(new ActionParameter()); } }
public DeviceCredentials DeviceCredentialWithId(int id) { try { iotRepository <DeviceCredentials> repo = new iotRepository <DeviceCredentials>(); return(repo.GetById(id)); } catch (Exception e) { nlogger.ErrorException(e.Message, e); return(new DeviceCredentials()); } }
public DeviceType DeviceTypeWithId(int id) { try { iotRepository <DeviceType> repo = new iotRepository <DeviceType>(); return(repo.GetById(id)); } catch (Exception e) { nlogger.ErrorException(e.Message, e); return(new DeviceType()); } }
public iotDomain DomainWithId(int id) { try { iotRepository <iotDomain> repo = new iotRepository <iotDomain>(); return(repo.GetById(id)); } catch (Exception e) { nlogger.ErrorException(e.Message, e); return(new iotDomain()); } }
public String GetSomeJson() { try { } catch (Exception ex) { _logger.ErrorException(ex.Message, ex); } iotRepository <Device> repo = new iotRepository <Device>(); Device dev = repo.GetById(52); return(JsonConvert.SerializeObject(dev)); }
public bool DeleteDevice(string id) { try { int DeviceId = Convert.ToInt32(id); iotRepository <Device> repo = new iotRepository <Device>(); Device dev = repo.GetById(DeviceId); repo.Delete(dev); return(true); } catch (Exception ex) { throw new FaultException(ex.Message); } }
public Location LocationWithId(int id) { try { iotRepository <Location> repo = new iotRepository <Location>(); return(repo.GetById(id)); // iotConnector connector = new iotConnector(); // return connector.LocationList().Where(s => { return s.Id == id; }).First(); } catch (Exception e) { nlogger.ErrorException(e.Message, e); return(new Location()); } }
static public void StoreParamChange(DeviceParameter param) { try { //add history only if param already in DB iotRepository <DeviceParameter> repo = new iotRepository <DeviceParameter>(); DeviceParameter stparam = repo.GetById(param.Id); if (stparam != null) { ParameterChangeHistory hist = new ParameterChangeHistory(); hist.Date = DateTime.Now; hist.Property = param; hist.Value = param.Value; iotRepository <ParameterChangeHistory> histrepo = new iotRepository <ParameterChangeHistory>(); histrepo.Add(hist); } } catch (Exception e) { } }
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); } } } } DbSet.Attach(entity); } catch (Exception e) { _logger.Error(e, e.Message); } }
private bool LoadConfigToDevice(Device dev) { try { //iotConnector connt = new iotConnector(); //Device edited = connt.DeviceList().Where(n => n.DeviceId == dev.DeviceId).First(); iotRepository <Device> devrep = new iotRepository <Device>(); Device edited = devrep.GetById(dev.DeviceId); int devAddr = IndexForHostDevice(); //Reload properties and actions if they changed //Update int inputs = site.siteCfg.deviceConfigs[devAddr].memCFG[ipcDefines.mAdrInputsNO]; if (edited.Properties.Count != inputs) { //clear current properies //load for (int i = 0; i < inputs; i++) { sconnConfigMapper maper = new sconnConfigMapper(); maper.ConfigType = ipcDefines.mAdrInput; maper.SeqNumber = i; AddPropertyForMapperAndDevice(maper, edited, devAddr); } } else //update { foreach (var item in edited.Properties) { //get parameter // List<DeviceParameter> propparams = (from par in item.ResultParameters // select par).ToList(); DeviceParameter param = item.ResultParameters.ElementAt(0); param.Value = sconnConfigToStringVal(param.sconnMappers.ElementAt(0), site.siteCfg.deviceConfigs[devAddr]); //iotRepository<DeviceParameter> repo = new iotRepository<DeviceParameter>(); //repo.Update(param); //cont.SaveChanges(); if (param != null) { //get input mapper sconnConfigMapper maper = (from cm in param.sconnMappers select cm).FirstOrDefault(); if (maper != null) { param.Value = sconnConfigToStringVal(maper, site.siteCfg.deviceConfigs[devAddr]); // cont.SaveChanges(); } } } } int outputs = site.siteCfg.deviceConfigs[devAddr].memCFG[ipcDefines.mAdrOutputsNO]; int relays = site.siteCfg.deviceConfigs[devAddr].memCFG[ipcDefines.mAdrRelayNO]; if (edited.Actions.Count != outputs + relays) { //remove existing if (edited.Actions.Count > 0) { iotRepository <DeviceAction> actrep = new iotRepository <DeviceAction>(); List <DeviceAction> acts = actrep.GetAll().ToList(); for (int i = 0; i < acts.Count; i++) { actrep.Delete(acts.ElementAt(i)); } } for (int i = 0; i < outputs; i++) { sconnConfigMapper maper = new sconnConfigMapper(); maper.ConfigType = ipcDefines.mAdrOutput; maper.SeqNumber = i; AddActionForMapperAndDevice(maper, edited, devAddr); } for (int i = 0; i < relays; i++) { sconnConfigMapper maper = new sconnConfigMapper(); maper.ConfigType = ipcDefines.mAdrRelay; maper.SeqNumber = i; AddActionForMapperAndDevice(maper, edited, devAddr); } } else { foreach (var item in edited.Actions) { //get action DeviceParameter param = (from par in item.ResultParameters select par).FirstOrDefault(); if (param != null) { //get input mapper sconnConfigMapper maper = (from cm in param.sconnMappers select cm).FirstOrDefault(); if (maper != null) { param.Value = sconnConfigToStringVal(maper, site.siteCfg.deviceConfigs[devAddr]); param.Type = param.Type; // cont.SaveChanges(); } } } } } catch (Exception e) { return(false); } 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 = iotGenericGlobalContext <T> .DbContext.Entry(entity); if (dbEntityEntry.State == EntityState.Detached) { iotGenericGlobalContext <T> .DbSet.Attach(entity); } dbEntityEntry.State = EntityState.Modified; //iotGenericGlobalContext<T>.DbContext.SaveChanges(); } catch (Exception exc) { throw; } }