コード例 #1
0
        protected virtual bool InitBase(string friendlyNamePrefix, string deviceType)
        {
            if (TheCommonUtils.CGuid(MyBaseThing.ID) == Guid.Empty)
            {
                MyBaseThing.ID = Guid.NewGuid().ToString();
                if (string.IsNullOrEmpty(MyBaseThing.FriendlyName))
                {
                    MyBaseThing.FriendlyName = $"{friendlyNamePrefix}: {MyBaseThing.ID}";
                }
            }

            MyBaseThing.LastUpdate = DateTimeOffset.Now;

            MyBaseThing.EngineName = MyBaseEngine.GetEngineName();
            MyBaseThing.DeviceType = deviceType;

            TheBaseEngine.WaitForStorageReadiness((pThing, pReady) =>
            {
                if (pReady != null)
                {
                    var receiverThings                      = new TheStorageMirror <TConnectionThing>(TheCDEngines.MyIStorageService);
                    receiverThings.CacheTableName           = nameof(TConnectionThing) + TheThing.GetSafeThingGuid(MyBaseThing, nameof(TConnectionThing));
                    receiverThings.IsRAMStore               = true;
                    receiverThings.CacheStoreInterval       = 1;
                    receiverThings.IsStoreIntervalInSeconds = true;
                    receiverThings.IsCachePersistent        = true;
                    receiverThings.UseSafeSave              = true;
                    receiverThings.RegisterEvent(eStoreEvents.StoreReady, (args) =>
                    {
                        var result = args.Para as TSM;
                        if (result != null && result.LVL == eMsgLevel.l1_Error)
                        {
                            MyBaseThing.SetStatus(3, "Error loading things");
                            TheBaseAssets.MySYSLOG.WriteToLog(98201, TSM.L(eDEBUG_LEVELS.ESSENTIALS) ? null : new TSM(MyBaseThing.EngineName, "Initialization", eMsgLevel.l6_Debug, String.Format("Error loading things for connection {0}", this.GetBaseThing().Address)));
                        }
                        else
                        {
                            MyConnectionThings = receiverThings;
                            if (MyConnectionThingsForm != null)
                            {
                                MyConnectionThingsForm.defDataSource = MyConnectionThings.StoreMID.ToString();
                            }
                            TheBaseAssets.MySYSLOG.WriteToLog(95272, TSM.L(eDEBUG_LEVELS.FULLVERBOSE) ? null : new TSM(MyBaseThing.EngineName, "Initialization", eMsgLevel.l6_Debug, String.Format("Things loaded for connection {0}", this.GetBaseThing().Address)));

                            if (AutoConnect)
                            {
                                Connect();
                                if (!IsConnected)
                                {
                                    TheCommonUtils.cdeRunTaskAsync("receiverAutoConnect", async o =>
                                    {
                                        await TheCommonUtils.TaskDelayOneEye(30000, 100).ConfigureAwait(false);
                                        while (!IsConnected && AutoConnect && TheBaseAssets.MasterSwitch)
                                        {
                                            Connect();
                                            await TheCommonUtils.TaskDelayOneEye(30000, 100).ConfigureAwait(false);
                                        }
                                    }).ContinueWith(t => t.Exception);
                                }
                            }
                            mIsInitialized = true;
                            FireEvent(eThingEvents.Initialized, this, true, true);
                            FireEvent("ServerInit", this, IsConnected.ToString(), true);
                        }
                    }
                                                 );
                    receiverThings.InitializeStore(false, false);
                }
            }, true);
            return(mIsInitialized);
        }