private void AddActionForMapperAndDevice(sconnActionResultMapper maper, Device edited, int DevNo) { try { iotContext cont = new iotContext(); Device storedDevice = cont.Devices.Where(d => d.Id == edited.Id).First(); DeviceAction action = new DeviceAction(); action.RequiredParameters = new List <ActionParameter>(); action.ResultParameters = new List <DeviceActionResult>(); action.ActionName = "Output" + maper.SeqNumber; //TODO read from name cfg action.Device = storedDevice; action.LastActivationTime = DateTime.Now; cont.Actions.Add(action); cont.SaveChanges(); //copy maper for action sconnActionResultMapper actionResultMaper = new sconnActionResultMapper(); actionResultMaper.ConfigType = maper.ConfigType; actionResultMaper.SeqNumber = maper.SeqNumber; sconnActionMapper actionInputMaper = new sconnActionMapper(); actionInputMaper.ConfigType = maper.ConfigType; actionInputMaper.SeqNumber = maper.SeqNumber; ParameterType extType = ParamTypeForSconnMapper(maper); ParameterType inType = cont.ParamTypes.Where(p => p.Id == extType.Id).First(); ActionParameter inparam = new ActionParameter(); inparam.Value = sconnConfigToStringVal(actionInputMaper, site.siteCfg.deviceConfigs[DevNo]); inparam.Type = inType; inparam.Action = action; cont.ActionParameters.Add(inparam); actionInputMaper.ActionParam = inparam; cont.ActionParamMappers.Add(actionInputMaper); cont.SaveChanges(); //create result parameter and bind mapper to it DeviceActionResult param = new DeviceActionResult(); param.Value = sconnConfigToStringVal(maper, site.siteCfg.deviceConfigs[DevNo]); param.Type = inType; param.Action = action; cont.ActionResultParameters.Add(param); cont.SaveChanges(); actionResultMaper.ActionParam = param; cont.ActionResultMappers.Add(actionResultMaper); cont.SaveChanges(); } catch (Exception e) { nlogger.ErrorException(e.Message, e); } }
private bool LoadConfigToDevice(int devId) { try { //iotConnector connt = new iotConnector(); //Device edited = connt.DeviceList().Where(n => n.Id == dev.Id).First(); //iotRepository<Device> devrep = new iotRepository<Device>(); Stopwatch watch = new Stopwatch(); watch.Start(); iotContext cont = new iotContext(); //cont.Configuration.LazyLoadingEnabled = false; //Device edited = cont.Devices.Where(d => d.Id == dev.Id) // .Include(d => d.Actions.Select(a => a.ResultParameters.Select(r => r.sconnMappers))) // .Include(d => d.Properties.Select(p => p.ResultParameters.Select(r => r.sconnMappers))) // .First(); Device edited = cont.Devices.First(d => d.Id == devId); watch.Stop(); Debug.WriteLine("Execution time : " + watch.ElapsedMilliseconds + " ms @ device query"); watch.Reset(); int devAddr = 0; if (site.siteCfg.deviceNo == 0) { return(false); } //Reload properties and actions if they changed //Update int inputs = site.siteCfg.deviceConfigs[devAddr].memCFG[ipcDefines.mAdrInputsNO]; if (edited.Properties.Count != inputs) { watch.Start(); //clear current properies for (int i = 0; i < edited.Properties.Count; i++) { cont.Properties.Remove(edited.Properties[i]); } cont.SaveChanges(); //load for (int i = 0; i < inputs; i++) { sconnPropertyMapper maper = new sconnPropertyMapper(); maper.ConfigType = ipcDefines.mAdrInput; maper.SeqNumber = i; AddPropertyForMapperAndDevice(maper, edited, devAddr); } watch.Stop(); Debug.WriteLine("Execution time : " + watch.ElapsedMilliseconds + " ms @ readd prop"); watch.Reset(); } else //update { watch.Reset(); watch.Start(); foreach (var item in edited.Properties) { //get parameter Stopwatch watch4 = new Stopwatch(); watch4.Start(); DeviceParameter param = item.ResultParameters.FirstOrDefault(); watch4.Stop(); Debug.WriteLine("Execution time : " + watch4.ElapsedMilliseconds + " ms @ prop update dev param query"); watch4.Restart(); param.Value = sconnConfigToStringVal(param.sconnMappers.FirstOrDefault(), site.siteCfg.deviceConfigs[devAddr]); watch4.Stop(); Debug.WriteLine("Execution time : " + watch4.ElapsedMilliseconds + " ms @ prop update dev param parse"); watch4.Reset(); if (param != null) { //get input mapper Stopwatch watch2 = new Stopwatch(); watch2.Start(); sconnConfigMapper maper = param.sconnMappers.FirstOrDefault(); watch2.Stop(); Debug.WriteLine("Execution time : " + watch2.ElapsedMilliseconds + " ms @ prop update mapper query"); watch2.Reset(); Stopwatch watch3 = new Stopwatch(); watch3.Start(); if (maper != null) { param.Value = sconnConfigToStringVal(maper, site.siteCfg.deviceConfigs[devAddr]); } watch3.Stop(); Debug.WriteLine("Execution time : " + watch3.ElapsedMilliseconds + " ms @ prop update cfg to str"); watch3.Reset(); } } Stopwatch watch1 = new Stopwatch(); watch1.Start(); cont.SaveChanges(); //apply watch1.Stop(); Debug.WriteLine("Execution time : " + watch1.ElapsedMilliseconds + " ms @ prop update save"); watch1.Reset(); watch.Stop(); Debug.WriteLine("Execution time : " + watch.ElapsedMilliseconds + " ms @ prop update"); watch.Reset(); } int outputs = site.siteCfg.deviceConfigs[devAddr].memCFG[ipcDefines.mAdrOutputsNO]; int relays = site.siteCfg.deviceConfigs[devAddr].memCFG[ipcDefines.mAdrRelayNO]; if (edited.Actions.Count != outputs + relays) { watch.Start(); //remove existing if (edited.Actions.Count > 0) { for (int i = 0; i < edited.Actions.Count; i++) { cont.Actions.Remove(edited.Actions[i]); } cont.SaveChanges(); } for (int i = 0; i < outputs; i++) { sconnActionResultMapper maper = new sconnActionResultMapper(); maper.ConfigType = ipcDefines.mAdrOutput; maper.SeqNumber = i; AddActionForMapperAndDevice(maper, edited, devAddr); } for (int i = 0; i < relays; i++) { sconnActionResultMapper maper = new sconnActionResultMapper(); maper.ConfigType = ipcDefines.mAdrRelay; maper.SeqNumber = i; AddActionForMapperAndDevice(maper, edited, devAddr); } watch.Stop(); Debug.WriteLine("Execution time : " + watch.ElapsedMilliseconds + " ms @ action readd"); watch.Reset(); } else { watch.Start(); foreach (var item in edited.Actions) { //get action DeviceActionResult 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(); //save updates watch.Stop(); Debug.WriteLine("Execution time : " + watch.ElapsedMilliseconds + " ms @ action update"); watch.Reset(); } } catch (Exception e) { nlogger.ErrorException(e.Message, e); return(false); } return(true); }