예제 #1
0
        private void AddPropertyForMapperAndDevice(sconnPropertyMapper maper, Device edited, int DevNo)
        {
            try
            {
                iotContext     cont = new iotContext();
                DeviceProperty prop = new DeviceProperty();
                prop.PropertyName = "Input" + maper.SeqNumber;                            //TODO read from name cfg

                Device storedDevice = cont.Devices.Where(d => d.Id == edited.Id).First(); //devRepo.GetById(edited.Id);
                prop.Device         = storedDevice;
                prop.LastUpdateTime = DateTime.Now;
                cont.Properties.Add(prop);
                cont.SaveChanges();


                DeviceParameter param = new DeviceParameter();
                param.Value = sconnConfigToStringVal(maper, site.siteCfg.deviceConfigs[DevNo]);
                ParameterType extType = ParamTypeForSconnMapper(maper);
                ParameterType inType  = cont.ParamTypes.Where(p => p.Id == extType.Id).First();
                param.Type     = inType;
                param.Property = prop;
                cont.Parameters.Add(param);
                cont.SaveChanges();

                maper.Parameter = param;
                cont.PropertyResultMappers.Add(maper);
                cont.SaveChanges();
            }
            catch (Exception e)
            {
                nlogger.ErrorException(e.Message, e);
            }
        }
예제 #2
0
        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);
        }