Esempio n. 1
0
        public override void HandleMessage(ICDEThing sender, object pIncoming)
        {
            TheProcessMessage pMsg = pIncoming as TheProcessMessage;

            if (pMsg == null || pMsg.Message == null)
            {
                return;
            }

            var cmd = TheCommonUtils.cdeSplit(pMsg.Message.TXT, ":", false, false);

            switch (cmd[0])
            {
            case nameof(MsgAddConnectionThing <TConnectionThingParam>):
                var addMsg = TheCommRequestResponse.ParseRequestMessageJSON <MsgAddConnectionThing <TConnectionThingParam> >(pMsg.Message);

                var responseMsg = new MsgAddConnectionThingResponse {
                    Error = "Unexpected"
                };
                if (addMsg != null)
                {
                    var thingToAdd = addMsg.ThingToAdd;

                    if (thingToAdd != null)
                    {
                        var currentThing = MyConnectionThings.MyMirrorCache.GetEntryByID(thingToAdd.cdeMID);
                        var newThing     = new TConnectionThing();
                        newThing.Initialize(thingToAdd);

                        if (currentThing == null)
                        {
                            MyConnectionThings.AddAnItem(newThing);
                            responseMsg.Error = null;
                        }
                        else
                        {
                            if (!newThing.IsEqual(currentThing))
                            {
                                UpdateConnectionThing(currentThing, newThing);
                                MyConnectionThings.UpdateItem(newThing);
                                Connect();
                            }
                            responseMsg.Error = null;
                        }
                    }
                    else
                    {
                        responseMsg.Error = "INVALIDARGS";
                    }
                }
                TheCommRequestResponse.PublishResponseMessageJson(pMsg.Message, responseMsg);
                break;

            default:
                base.HandleMessage(sender, pIncoming);
                break;
            }
        }
Esempio n. 2
0
        internal TheStorageMirror <TheEventLogData> MyRuleEventLog;   //The Storage Container for data to store

        public override bool LogEvent(TheEventLogData pData)
        {
            if (!MyRuleEventLog.IsReady)
            {
                return(false);
            }
            MyRuleEventLog.AddAnItem(pData);
            return(true);
        }
Esempio n. 3
0
        public void Write(TheNMILiveTag pTag, string pEventLabel, string pComment)
        {
            TheNMITagLogEntry tEntry = new TheNMITagLogEntry
            {
                ID         = pTag.GetBaseThing().ID,
                Value      = TheCommonUtils.CDbl(pTag.GetBaseThing().Value),
                EngineName = pTag.GetBaseThing().EngineName,
                EventLabel = pEventLabel,
                Comment    = pComment
            };

            MyTagLog.AddAnItem(tEntry);
        }
Esempio n. 4
0
        void sinkStorageReady(StoreEventArgs pArgs)
        {
            string             FileToReturn1 = TheCommonUtils.cdeFixupFileName(Address);
            DirectoryInfo      di            = new DirectoryInfo(FileToReturn1);
            List <TheFileInfo> tList         = new List <TheFileInfo>();

            TheISMManager.ProcessDirectory(di, ref tList, "", ".TXT", false, true);
            MyLogFiles.MyMirrorCache.Clear(false);
            foreach (TheFileInfo t in tList)
            {
                MyLogFiles.AddAnItem(new TheBackupDefinition()
                {
                    FileName = t.FileName, Title = t.Name, BackupSize = t.FileSize, BackupTime = t.CreateTime
                });
            }
        }
        private void Add(int pEventID, long pSerial, string pSource, TSM AMessage)
        {
            TheEventLogEntry tLog = new TheEventLogEntry
            {
                Message = AMessage,
                Serial  = pSerial,
                Source  = string.IsNullOrEmpty(pSource) ? AMessage.ENG : pSource,
                EventID = pEventID
            };

            MyMessageLog.AddAnItem(tLog);
            if (IsEventRegistered("NewLogEntry"))
            {
                FireEvent("NewLogEntry", new TheProcessMessage()
                {
                    Message = AMessage, Cookie = tLog
                }, true);                                                                                       //Prevents a call to AddScopeID for the TOPIC
            }
        }
Esempio n. 6
0
        //Helper to update storage list
        private void UpdateStorageList(string name, string status, int step, TheScript script, TheThing context, bool replay)
        {
            ScriptSnapshot existingSnapshot = null;

            // No longer adding a new entry on replay because the list is now used to disable script/steps etc.
            existingSnapshot = MyScriptTableStorage.MyMirrorCache.GetEntryByFunc(snapshot => snapshot.ScriptName == name && snapshot.ScriptStep == step);
            var stepName = step > 0 && step <= script?.Steps.Length ? script?.Steps[step - 1]?.GetName() ?? "" : "";

            if (existingSnapshot == null)
            {
                var newSnapshot = new ScriptSnapshot
                {
                    ScriptName    = name,
                    ScriptStatus  = status,
                    StepName      = stepName,
                    ScriptStep    = step,
                    ContextScript = script,
                    FileName      = script?.FileName,
                    ScriptRaw     = script?.ScriptRaw,
                    Context       = context,
                    LastUpdate    = DateTimeOffset.Now,
                };
                MyScriptTableStorage.AddAnItem(newSnapshot);
            }
            else
            {
                existingSnapshot.ScriptName    = name;
                existingSnapshot.ScriptStatus  = status;
                existingSnapshot.StepName      = stepName;
                existingSnapshot.ScriptStep    = step;
                existingSnapshot.ContextScript = script;
                existingSnapshot.FileName      = script?.FileName;
                existingSnapshot.ScriptRaw     = script?.ScriptRaw;
                existingSnapshot.Context       = context;
                existingSnapshot.LastUpdate    = DateTimeOffset.Now;
                MyScriptTableStorage.UpdateItem(existingSnapshot);
            }
        }
Esempio n. 7
0
        /// <summary>
        /// Handles Messages sent from a host sub-engine to its clients
        /// </summary>
        /// <param name="Command"></param>
        /// <param name="pMessage"></param>
        public override void HandleMessage(ICDEThing sender, object pIncoming)
        {
            TheProcessMessage pMsg = pIncoming as TheProcessMessage;

            if (pMsg == null)
            {
                return;
            }

            string[] cmd = pMsg.Message.TXT.Split(':');
            switch (cmd[0])
            {
            case nameof(TheThing.MsgBrowseSensors):
                var browseRequest  = TheCommRequestResponse.ParseRequestMessageJSON <TheThing.MsgBrowseSensors>(pMsg.Message);
                var browseResponse = new TheThing.MsgBrowseSensorsResponse {
                    Error = "Internal error", Sensors = new List <TheThing.TheSensorSourceInfo>()
                };
                foreach (FieldMapping fld in MyModFieldStore.TheValues)
                {
                    browseResponse.Sensors.Add(new TheThing.TheSensorSourceInfo
                    {
                        SourceType    = fld.SourceType,
                        cdeType       = ePropertyTypes.TNumber,
                        SensorId      = TheCommonUtils.CStr(fld.cdeMID),
                        ExtensionData = new Dictionary <string, object>
                        {
                            { nameof(FieldMapping.SourceOffset), fld.SourceOffset },
                            { nameof(FieldMapping.SourceSize), fld.SourceSize },
                            { nameof(FieldMapping.AllowWrite), fld.AllowWrite }
                        },
                        DisplayNamePath = new string[] { MyBaseEngine.GetEngineName(), MyBaseThing.FriendlyName, fld.PropertyName }
                    });
                }
                browseResponse.Error = null;
                TheCommRequestResponse.PublishResponseMessageJson(pMsg.Message, browseResponse);
                break;

            case nameof(TheThing.MsgSubscribeSensors):
                var subscribeRequest  = TheCommRequestResponse.ParseRequestMessageJSON <TheThing.MsgSubscribeSensors>(pMsg.Message);
                var subscribeResponse = new TheThing.MsgSubscribeSensorsResponse {
                    Error = "Internal error", SubscriptionStatus = new List <TheThing.TheSensorSubscriptionStatus>()
                };
                if (subscribeRequest.ReplaceAll)
                {
                    MyModFieldStore.RemoveAllItems();
                }
                var subscriptionStatus = new List <TheThing.TheSensorSubscriptionStatus>();
                foreach (TheThing.TheSensorSubscription sub in subscribeRequest.SubscriptionRequests)
                {
                    FieldMapping fld = new FieldMapping()
                    {
                        PropertyName = sub.TargetProperty,
                        cdeMID       = TheCommonUtils.CGuid(sub.SensorId)
                    };
                    if (fld.cdeMID == Guid.Empty)
                    {
                        fld.cdeMID = Guid.NewGuid();
                    }
                    object sourceType;
                    if (sub.ExtensionData != null)
                    {
                        if (sub.ExtensionData.TryGetValue(nameof(TheThing.TheSensorSourceInfo.SourceType), out sourceType))
                        {
                            fld.SourceType = TheCommonUtils.CStr(sourceType);
                        }
                        object offset;
                        if (sub.ExtensionData.TryGetValue("SourceOffset", out offset))
                        {
                            fld.SourceOffset = TheCommonUtils.CInt(offset);
                        }
                        object size;
                        if (sub.ExtensionData.TryGetValue("SourceSize", out size))
                        {
                            fld.SourceSize = TheCommonUtils.CInt(size);
                        }
                        object allowWrite;
                        if (sub.ExtensionData.TryGetValue("AllowWrite", out allowWrite))
                        {
                            fld.AllowWrite = TheCommonUtils.CBool(allowWrite);
                        }
                        MyModFieldStore.AddAnItem(fld);
                        subscriptionStatus.Add(CreateSubscriptionStatusFromFieldMapping(fld));
                    }
                    else
                    {
                        subscriptionStatus.Add(new TheThing.TheSensorSubscriptionStatus
                        {
                            Error        = "Missing source info",
                            Subscription = sub,
                        });
                    }
                }
                subscribeResponse.SubscriptionStatus = subscriptionStatus;
                subscribeResponse.Error = null;
                TheCommRequestResponse.PublishResponseMessageJson(pMsg.Message, subscribeResponse);
                break;

            case nameof(TheThing.MsgGetSensorSubscriptions):
                var getResponse = new TheThing.MsgGetSensorSubscriptionsResponse {
                    Error = "Internal error"
                };
                getResponse.Subscriptions = MyModFieldStore.TheValues.Select(fld => CreateSubscriptionStatusFromFieldMapping(fld).Subscription).ToList();
                getResponse.Error         = null;
                TheCommRequestResponse.PublishResponseMessageJson(pMsg.Message, getResponse);
                break;

            case nameof(TheThing.MsgUnsubscribeSensors):
                var unsubscribeRequest  = TheCommRequestResponse.ParseRequestMessageJSON <TheThing.MsgUnsubscribeSensors>(pMsg.Message);
                var unsubscribeResponse = new TheThing.MsgUnsubscribeSensorsResponse {
                    Error = "Internal error", Failed = new List <TheThing.TheSensorSubscriptionStatus>()
                };
                if (unsubscribeRequest.UnsubscribeAll)
                {
                    MyModFieldStore.RemoveAllItems();
                    if (MyModFieldStore.GetCount() > 0)
                    {
                        unsubscribeResponse.Failed = MyModFieldStore.TheValues.Select(fld => CreateSubscriptionStatusFromFieldMapping(fld)).ToList();
                    }
                }
                else
                {
                    List <FieldMapping> toRemove = MyModFieldStore.TheValues.FindAll(fld => unsubscribeRequest.SubscriptionIds.Contains(fld.cdeMID));
                    MyModFieldStore.RemoveItems(toRemove, null);
                    foreach (FieldMapping fld in MyModFieldStore.TheValues)
                    {
                        if (toRemove.Any(t => t.cdeMID == fld.cdeMID))
                        {
                            unsubscribeResponse.Failed.Add(CreateSubscriptionStatusFromFieldMapping(fld));
                        }
                    }
                }
                unsubscribeResponse.Error = null;
                TheCommRequestResponse.PublishResponseMessageJson(pMsg.Message, unsubscribeResponse);
                break;

            case nameof(TheThing.MsgExportConfig):
                var exportRequest  = TheCommRequestResponse.ParseRequestMessageJSON <TheThing.MsgExportConfig>(pMsg.Message);
                var exportResponse = new TheThing.MsgExportConfigResponse {
                    Error = "Internal error"
                };

                // No custom config beyond config properties and subscriptions
                exportResponse.Error = null;

                TheCommRequestResponse.PublishResponseMessageJson(pMsg.Message, exportResponse);
                break;

            case nameof(TheThing.MsgApplyConfig):
                break;

            default:
                break;
            }

            TheThing.TheSensorSubscriptionStatus CreateSubscriptionStatusFromFieldMapping(FieldMapping fld)
            {
                return(new TheThing.TheSensorSubscriptionStatus
                {
                    Subscription = new TheThing.TheSensorSubscription
                    {
                        TargetProperty = fld.PropertyName,
                        SensorId = TheCommonUtils.CStr(fld.cdeMID),
                        SubscriptionId = fld.cdeMID,
                        ExtensionData = new Dictionary <string, object>
                        {
                            { nameof(FieldMapping.SourceType), fld.SourceType },
                            { nameof(FieldMapping.SourceOffset), fld.SourceOffset },
                            { nameof(FieldMapping.SourceSize), fld.SourceSize },
                            { nameof(FieldMapping.AllowWrite), fld.AllowWrite }
                        },
                        TargetThing = new TheThingReference(MyBaseThing),
                        SampleRate = (int?)this.Interval
                    },
                    Error = null,
                });
            }
        }
Esempio n. 8
0
        public void ApplyLoadToTheStorageMirrorTest()
        {
            #region ARRANGE

            PerformanceCounter         counterCPU;
            PerformanceCounter         counterRAM;
            BlockingCollection <float> metricsCPU;
            BlockingCollection <float> metricsRAM;

            counterCPU = new PerformanceCounter("Processor", "% Processor Time", "_Total");
            counterRAM = new PerformanceCounter("Memory", "Available MBytes");
            metricsCPU = new BlockingCollection <float>(100000);
            metricsRAM = new BlockingCollection <float>(100000);

            Timer timerCPU;
            timerCPU = new Timer((state) =>
            {
                if (metricsCPU.IsAddingCompleted)
                {
                    float oldest = metricsCPU.FirstOrDefault();
                    metricsCPU.TryTake(out oldest);
                }
                metricsCPU.TryAdd(counterCPU.NextValue());
            }, null, 1000, 10);

            Timer timerRAM;
            timerRAM = new Timer((state) =>
            {
                if (metricsRAM.IsAddingCompleted)
                {
                    float oldest = metricsRAM.FirstOrDefault();
                    metricsRAM.TryTake(out oldest);
                }
                metricsRAM.TryAdd(counterRAM.NextValue());
            }, null, 1000, 10);

            counterCPU.NextValue();
            var memoryBefore = counterRAM.NextValue();
            Thread.Sleep(1000); // Per MSDN suggestion when using PerformanceCounter mechanism...

            int            totalCandidates = 1000000;
            int            countCallbacks  = 0;
            var            random          = new Random();
            var            data            = Enumerable.Range(1, totalCandidates).OrderBy(i => random.Next(1, totalCandidates));
            CountdownEvent countdown       = new CountdownEvent(totalCandidates);
            TheStorageMirror <TheStorageEngineTSM> mirror;
            mirror = new TheStorageMirror <TheStorageEngineTSM>(TheCDEngines.MyIStorageService)
            {
                IsRAMStore               = true,
                CacheStoreInterval       = 1,
                IsStoreIntervalInSeconds = true,
                IsCachePersistent        = true,
                UseSafeSave              = true,
                AllowFireUpdates         = true,
            };



            #endregion

            #region ACT

            Stopwatch stopwatch = new Stopwatch();
            stopwatch.Start();

            try
            {
                Parallel.ForEach(data, currentNumber =>
                {
                    mirror.AddAnItem(
                        new TheStorageEngineTSM()
                    {
                        TXTPattern = currentNumber.ToString()
                    },
                        state =>
                    {
                        Interlocked.Increment(ref countCallbacks);
                        countdown.Signal();
                    });
                });
            }
            catch (Exception ex)
            {
                Assert.IsTrue(true, ex.Message);
            }

            countdown.Wait();

            stopwatch.Stop();
            timerCPU.Change(Timeout.Infinite, Timeout.Infinite);
            timerCPU.Dispose();
            timerRAM.Change(Timeout.Infinite, Timeout.Infinite);
            timerRAM.Dispose();
            counterCPU?.Dispose();
            counterRAM?.Dispose();
            countdown?.Dispose();
            mirror?.Dispose();

            #endregion

            #region ASSERT

            float avgCPU = metricsCPU.Average();
            float maxCPU = 99f;

            float avgRAM = metricsRAM.Average();
            float maxRAM = memoryBefore + 1000f;

            Assert.IsTrue(
                (avgCPU <= maxCPU && avgRAM <= maxRAM && totalCandidates == countCallbacks),
                buildSummary(totalCandidates,
                             stopwatch.ElapsedMilliseconds,
                             avgCPU,
                             maxCPU,
                             avgRAM,
                             maxRAM));

            #endregion
        }
Esempio n. 9
0
 public bool LogVisitor(TheVisitorLogData pData)
 {
     MyVisitorLogStore?.AddAnItem(pData);
     return(MyVisitorLogStore != null);
 }