public virtual bool Init(bool declareInit) { if (mIsInitCalled) { return(false); } mIsInitCalled = true; IsConnected = false; MyBaseThing.StatusLevel = 0; MyBaseThing.LastMessage = "Service is ready"; MyBaseThing.RegisterStatusChanged(sinkUpdateUX); MyBaseThing.RegisterEvent(eEngineEvents.IncomingMessage, HandleMessage); if (string.IsNullOrEmpty(MyBaseThing.ID)) { MyBaseThing.ID = Guid.NewGuid().ToString(); TheThing.SetSafePropertyBool(MyBaseThing, "IsStateSensor", true); TheThing.SetSafePropertyString(MyBaseThing, "StateSensorType", "analog"); TheThing.SetSafePropertyString(MyBaseThing, "StateSensorUnit", "ms"); TheThing.SetSafePropertyNumber(MyBaseThing, "StateSensorMaxValue", 100); TheThing.SetSafePropertyNumber(MyBaseThing, "StateSensorAverage", 50); TheThing.SetSafePropertyNumber(MyBaseThing, "StateSensorMinValue", 0); //TheThing.SetSafePropertyString(MyBaseThing, "StateSensorName", MyBaseThing.FriendlyName); TheRulesFactory.CreateOrUpdateRule(new TheThingRule(Guid.NewGuid(), "NetService:" + MyBaseThing.FriendlyName + " Failed", MyBaseThing, "StatusLevel", eRuleTrigger.Larger, "1", true, true)); } if (declareInit) { mIsInitialized = true; } return(true); }
public string OpenModBus() { if (MyModMaster != null && tcpClient != null && tcpClient.Connected) { return(null); } string error = null; try { CloseModBus(); tcpClient = new TcpClient(); int port = (int)TheThing.GetSafePropertyNumber(MyBaseThing, "CustomPort"); if (port == 0) { port = 502; //MOdbus default Port TheThing.SetSafePropertyNumber(MyBaseThing, "CustomPort", port); } tcpClient.Connect(IPAddress.Parse(MyBaseThing.Address), port); MyModMaster = ModbusIpMaster.CreateIp(tcpClient); } catch (SocketException e) { error = e.Message; CloseModBus(); } return(error); }
private void ShowKPI(int pFldOrder, string pLabel, string pUpdName, int MaxVal = 100, int AveVal = 50, string pUnits = "bytes") { TheNMIEngine.AddSmartControl(MyBaseThing, mMyForm, eFieldType.TileGroup, pFldOrder, 0, 0, null, null, new nmiCtrlTileGroup { ParentFld = 300, TileWidth = 6 }); TheNMIEngine.AddSmartControl(MyBaseThing, mMyForm, eFieldType.SmartLabel, pFldOrder + 1, 0, 0, null, null, new nmiCtrlSmartLabel { ParentFld = pFldOrder, Text = pLabel, NoTE = true, TileFactorY = 2, TileHeight = 1, TileWidth = 5, ClassName = "cdeTileGroupHeaderSmall" }); var tBut = TheNMIEngine.AddSmartControl(MyBaseThing, mMyForm, eFieldType.TileButton, pFldOrder + 2, 2, 0, "V-Tise", null, new nmiCtrlTileButton { ParentFld = pFldOrder, NoTE = true, TileFactorY = 2, TileHeight = 1, TileWidth = 1, ClassName = "cdeGlassButton" }); tBut.RegisterUXEvent(MyBaseThing, eUXEvents.OnClick, $"But{pUpdName}", (sender, para) => { TheThing tT = TheThingRegistry.GetThingByID(MyBaseEngine.GetEngineName(), $"ISO{MyBaseThing.cdeMID}:{pUpdName}"); if (tT == null) { TheKPIReport tRep = new TheKPIReport(null, MyPlugin); TheThing.SetSafePropertyGuid(tRep, "RealSensorThing", MyBaseThing.cdeMID); tRep.GetBaseThing().ID = $"ISO{MyBaseThing.cdeMID}:{pUpdName}"; MyBaseThing.GetProperty(pUpdName, true); TheThing.SetSafePropertyString(tRep, "RealSensorProperty", pUpdName); TheThing.SetSafePropertyNumber(tRep, "StateSensorMaxValue", MaxVal); TheThing.SetSafePropertyNumber(tRep, "StateSensorAverage", AveVal); TheThing.SetSafePropertyNumber(tRep, "StateSensorSteps", MaxVal / 15); TheThing.SetSafePropertyString(tRep, "StateSensorValueName", pLabel); TheThing.SetSafePropertyString(tRep, "StateSensorUnit", pUnits); TheThing.SetSafePropertyString(tRep, "FriendlyName", MyBaseThing.FriendlyName); TheThing.SetSafePropertyString(tRep, "ReportName", $"ISO-KPI: {MyBaseThing.FriendlyName} - {pLabel}"); TheThing.SetSafePropertyString(tRep, "ReportCategory", "ISO KPI Reports"); ThePluginInfo tI = MyPlugin.GetBaseEngine().GetPluginInfo(); if (tI != null) { TheThing.SetSafePropertyString(tRep, "SerialNumber", TheCommonUtils.CStr(tI.CurrentVersion)); TheThing.SetSafePropertyString(tRep, "VendorName", TheCommonUtils.CStr(tI.Developer)); TheThing.SetSafePropertyString(tRep, "ProductName", TheCommonUtils.CStr(tI.ServiceDescription)); TheThing.SetSafePropertyString(tRep, "ProductText", TheCommonUtils.CStr(tI.LongDescription)); TheThing.SetSafePropertyString(tRep, "VendorText", TheCommonUtils.CStr(tI.DeveloperUrl)); TheThing.SetSafePropertyString(tRep, "ProductID", TheCommonUtils.CStr(tI.cdeMID)); } TheThingRegistry.RegisterThing(tRep); MyBaseEngine.ProcessMessage(new TheProcessMessage(new TSM(MyBaseEngine.GetEngineName(), "REFRESH_DASH"))); } else { TheCommCore.PublishToOriginator((para as TheProcessMessage).Message, new TSM(eEngineName.NMIService, "NMI_TTS", tT.cdeMID.ToString())); } }); TheNMIEngine.AddSmartControl(MyBaseThing, mMyForm, eFieldType.SmartLabel, pFldOrder + 3, 0, 0, null, pUpdName, new nmiCtrlSingleEnded { ParentFld = pFldOrder, TileWidth = 2 }); TheNMIEngine.AddSmartControl(MyBaseThing, mMyForm, eFieldType.BarChart, pFldOrder + 4, 0, 0, null, pUpdName, new nmiCtrlBarChart() { ParentFld = pFldOrder, MaxValue = MaxVal, TileWidth = 4, NoTE = true, Foreground = "blue" }); }
public override bool DoInit() { base.DoInit(); IsActive = false; TheThing.SetSafePropertyBool(MyBaseThing, "IsStateSensor", true); MyBaseThing.StatusLevel = 4; if (string.IsNullOrEmpty(MyBaseThing.ID)) { MyBaseThing.ID = Guid.NewGuid().ToString(); TheThing.SetSafePropertyString(MyBaseThing, "StateSensorType", "analog"); TheThing.SetSafePropertyString(MyBaseThing, "StateSensorUnit", "units"); TheThing.SetSafePropertyNumber(MyBaseThing, "StateSensorMaxValue", 100); TheThing.SetSafePropertyNumber(MyBaseThing, "StateSensorAverage", 50); TheThing.SetSafePropertyNumber(MyBaseThing, "StateSensorMinValue", 0); TheThing.SetSafePropertyNumber(MyBaseThing, "Interval", 500); } TheThing.SetSafePropertyString(MyBaseThing, "StateSensorIcon", "/P066/Images/iconVThingsRest.png"); GetProperty("FriendlyName", true).RegisterEvent(eThingEvents.PropertyChanged, sinkNameChanged); cdeP tRW = GetProperty("RawValue", true); tRW.RegisterEvent(eThingEvents.PropertyChanged, sinkPrePro); cdeP.SetSafePropertyBool(tRW, "IsStateSensor", true); cdeP.SetSafePropertyString(tRW, "StateSensorType", "analog"); cdeP.SetSafePropertyString(tRW, "StateSensorUnit", "°F"); cdeP.SetSafePropertyNumber(tRW, "StateSensorMaxValue", 100); cdeP.SetSafePropertyNumber(tRW, "StateSensorAverage", 50); cdeP.SetSafePropertyNumber(tRW, "StateSensorMinValue", 0); if (!string.IsNullOrEmpty(TheThing.GetSafePropertyString(MyBaseThing, "RealSensorThing")) && !string.IsNullOrEmpty(TheThing.GetSafePropertyString(MyBaseThing, "RealSensorProperty"))) { EngageMapper(); } GetProperty("IsGlobal", true).RegisterEvent(eThingEvents.PropertyChanged, (p) => { if (TheCommonUtils.CBool(p.ToString())) { TheThingRegistry.RegisterThingGlobally(MyBaseThing); } else { TheThingRegistry.UnregisterThingGlobally(MyBaseThing); } }); GetProperty("Interval", true).RegisterEvent(eThingEvents.PropertyChanged, (p) => { changeInterval(TheCommonUtils.CInt(p.ToString())); }); MyBaseThing.SetPublishThrottle((int)TheThing.GetSafePropertyNumber(MyBaseThing, "Interval")); //TheQueuedSenderRegistry.RegisterHealthTimer(checkMapperHealth); return(true); }
public void sinkStoreReady(StoreEventArgs e) { if (MyDevice != null) { if (!string.IsNullOrEmpty(MyDevice.Name)) { MyBaseThing.FriendlyName = MyDevice.Name; } if (!string.IsNullOrEmpty(MyDevice.IpAddress)) { MyBaseThing.Address = MyDevice.IpAddress; } if (MyDevice.IpPort == 0) { MyDevice.IpPort = 502; } if (ConnectionType == 0) { ConnectionType = 3; } TheThing.SetSafePropertyNumber(MyBaseThing, "CustomPort", MyDevice.IpPort); TheThing.SetSafePropertyNumber(MyBaseThing, "SlaveAddress", MyDevice.SlaveAddress); if (MyDevice.Mapping != null) { TheThing.SetSafePropertyNumber(MyBaseThing, "Offset", MyDevice.Mapping.Offset); //TODO: Create Storage Mirror with Field Mapps MyModFieldStore.FlushCache(true); foreach (var tFld in MyDevice.Mapping.FieldList) { MyModFieldStore.AddAnItem(tFld); } } } if (Interval < 100) { Interval = 100; } if (AutoConnect) { TheCommonUtils.cdeRunAsync("ModBusAutoConnect", true, async(o) => { while (AutoConnect && TheBaseAssets.MasterSwitch && !IsConnected && !Connect(null)) { await TheCommonUtils.TaskDelayOneEye((int)Interval, 100); } }); } mIsInitialized = true; FireEvent(eThingEvents.Initialized, this, true, true); }
void sinkServiceInfo(TSM pMsgMessage) { if (string.IsNullOrEmpty(pMsgMessage?.PLS)) { return; } var MyInfo = TheCommonUtils.DeserializeJSONStringToObject <List <TheNodeTopics> >(pMsgMessage.PLS); int bk = 0; foreach (TheFootPrints tf in MyFootPrints.MyMirrorCache.TheValues) { tf.Counter = 0; tf.IsDirty = false; } if (MyInfo?.Count > 0) { foreach (var tIn in MyInfo) { if (tIn.NodeType == cdeSenderType.CDE_JAVAJASON) { bk++; } else { foreach (TheFootPrints tf in MyFootPrints.MyMirrorCache.TheValues) { List <string> tFs = TheCommonUtils.CStringToList(tf.Footprint, ';'); if (IsListInList(tIn.Topics, tFs) && (tf.ExactCount == 0 || tf.ExactCount == tIn.Topics.Count)) { tf.Counter++; tf.IsDirty = true; } } } } TheThing.SetSafePropertyNumber(MyBaseThing, "BrowserCount", bk); } foreach (TheFootPrints tf in MyFootPrints.MyMirrorCache.TheValues) { if (tf.IsDirty || tf.Counter == 0) { MyFootPrints.UpdateItem(tf); TheThing.SetSafePropertyNumber(MyBaseThing, $"{tf.NodeType}_Count", tf.Counter); } } }
public static bool CopyStateSensorInfo(TheThing pBaseThing) { if (pBaseThing == null) { return(false); } var t = TheThingRegistry.GetThingByMID("*", TheThing.GetSafePropertyGuid(pBaseThing, "RealSensorThing")); if (t != null && TheThing.GetSafePropertyBool(t, "IsStateSensor")) { if (string.IsNullOrEmpty(TheThing.GetSafePropertyString(pBaseThing, "StateSensorType"))) { TheThing.SetSafePropertyString(pBaseThing, "StateSensorType", TheThing.GetSafePropertyString(t, "StateSensorType")); } if (string.IsNullOrEmpty(TheThing.GetSafePropertyString(pBaseThing, "StateSensorUnit"))) { TheThing.SetSafePropertyString(pBaseThing, "StateSensorUnit", TheThing.GetSafePropertyString(t, "StateSensorUnit")); } if (string.IsNullOrEmpty(TheThing.GetSafePropertyString(pBaseThing, "StateSensorValueName"))) { TheThing.SetSafePropertyString(pBaseThing, "StateSensorValueName", TheThing.GetSafePropertyString(t, "StateSensorValueName")); } if (pBaseThing.GetProperty("StateSensorAverage") == null) { TheThing.SetSafePropertyNumber(pBaseThing, "StateSensorAverage", TheThing.GetSafePropertyNumber(t, "StateSensorAverage")); } if (pBaseThing.GetProperty("StateSensorMinValue") == null) { TheThing.SetSafePropertyNumber(pBaseThing, "StateSensorMinValue", TheThing.GetSafePropertyNumber(t, "StateSensorMinValue")); } if (pBaseThing.GetProperty("StateSensorMaxValue") == null) { TheThing.SetSafePropertyNumber(pBaseThing, "StateSensorMaxValue", TheThing.GetSafePropertyNumber(t, "StateSensorMaxValue")); } if (string.IsNullOrEmpty(TheThing.GetSafePropertyString(pBaseThing, "StateSensorIcon"))) { TheThing.SetSafePropertyString(pBaseThing, "StateSensorIcon", TheThing.GetSafePropertyString(t, "StateSensorIcon")); } return(true); } return(false); }
void sinkTimer(long t) { if (MyIsolator == null) { return; } if (MyIsolator.HasExited) { TheQueuedSenderRegistry.UnregisterHealthTimer(sinkTimer); MyBaseThing.StatusLevel = 0; MyBaseThing.LastMessage = "Plugin no longer running"; return; } try { TheThing.SetSafePropertyString(MyBaseThing, "IORead", string.Format("{0:0.00}", readBytesSec?.NextValue())); //if ((t%15)!=0) return; TheThing.SetSafePropertyString(MyBaseThing, "CPUTime", string.Format("{0:0.00}", MyIsolator.TotalProcessorTime.TotalSeconds)); TheThing.SetSafePropertyNumber(MyBaseThing, "WorkingSet", MyIsolator.WorkingSet64 / 1024); TheThing.SetSafePropertyNumber(MyBaseThing, "Handles", MyIsolator.HandleCount); TheThing.SetSafePropertyNumber(MyBaseThing, "Threads", MyIsolator.Threads.Count); TimeSpan ts = DateTime.Now.Subtract(MyIsolator.StartTime); TheThing.SetSafePropertyNumber(MyBaseThing, "UpTime", ts.TotalMinutes); using (ManagementObjectSearcher searcher = new ManagementObjectSearcher("root\\CIMV2", $"SELECT * FROM Win32_PerfFormattedData_PerfProc_Process where IDProcess='{MyIsolator.Id}'")) { foreach (ManagementObject queryObj in searcher.Get()) { TheThing.SetSafePropertyString(MyBaseThing, "CPU", queryObj["PercentProcessorTime"].ToString()); MyBaseThing.Value = queryObj["PercentProcessorTime"].ToString(); //foreach (var ttt in queryObj.Properties) //{ // Console.WriteLine($"{ttt.Name}: {ttt.Value}"); //} } } } catch (Exception) { } }
public override bool DoInit() { MyBaseThing.StatusLevel = 0; MyBaseThing.LastMessage = "Timer Ready"; IsActive = false; if (string.IsNullOrEmpty(MyBaseThing.ID)) { MyBaseThing.ID = Guid.NewGuid().ToString(); TheThing.SetSafePropertyBool(MyBaseThing, "IsStateSensor", true); TheThing.SetSafePropertyString(MyBaseThing, "StateSensorType", "analog"); TheThing.SetSafePropertyString(MyBaseThing, "StateSensorUnit", "ticks"); TheThing.SetSafePropertyNumber(MyBaseThing, "StateSensorMaxValue", 60000); TheThing.SetSafePropertyNumber(MyBaseThing, "StateSensorAverage", 1000); TheThing.SetSafePropertyNumber(MyBaseThing, "StateSensorMinValue", 0); } if (TheThing.GetSafePropertyBool(MyBaseThing, "AutoStart")) { sinkTriggered(null); } return(true); }
protected override void SensorCyclicCalc(long timer) { base.SensorCyclicCalc(timer); if ((timer % PingFreq) != 0) { return; } if (IsInCyclic) { return; } IsInCyclic = true; PingFreq = (int)TheThing.GetSafePropertyNumber(MyBaseEngine.GetBaseThing(), "CalcAggregation"); if (PingFreq == 0) { PingFreq = 30; } var tVals = MyHistorian?.GetValues(); //TODO: This returns null if the SensorHistorian is in the SQL Server. We need to think about what we do here! if (tVals?.Any() == true) { string tValues = TheThing.GetSafePropertyString(MyBaseThing, "HistoryFields"); if (tValues.Contains("QValue_Ave")) { TheThing.SetSafePropertyNumber(MyBaseThing, "QValue_Ave", tVals.Average(w => w.PB.ContainsKey("QValue") ? TheCommonUtils.CDbl(w.PB["QValue"]) : 0)); } if (tValues.Contains("QValue_Min")) { TheThing.SetSafePropertyNumber(MyBaseThing, "QValue_Min", tVals.Min(w => w.PB.ContainsKey("QValue") ? TheCommonUtils.CDbl(w.PB["QValue"]) : 0)); } if (tValues.Contains("QValue_Max")) { TheThing.SetSafePropertyNumber(MyBaseThing, "QValue_Max", tVals.Max(w => w.PB.ContainsKey("QValue") ? TheCommonUtils.CDbl(w.PB["QValue"]) : 0)); } } IsInCyclic = false; }
void sinkUXUpdatedThing(ICDEThing pThing, object pData) { cdeP tProp = pData as cdeP; if (tProp != null && m_Tag != null) { if (!DontMonitor && tProp != null && tProp.HasChanged && tProp.Name == "Value") { var value = tProp.GetValue(); if (value != null && tProp.cdeT == 4 && TheThing.GetSafePropertyBool(MyBaseThing, "StoreArrayInProperties")) { byte[] bytes = (byte[])value; for (int ii = 0; ii < bytes.Length; ii++) { TheThing.SetSafePropertyNumber(MyBaseThing, $"Value{ii}", bytes[ii]); } } if (PropertyOPCToString((ePropertyTypes)tProp.cdeT, value) != m_Tag.MyLastValue.ToString()) //CODE-REVIEW: Racing can happen here! Both functions can take too long and change during the ToString operation (so far only seen in debugger!) { m_Tag.WriteTag(new DataValue(new Variant(tProp.Value))); } } } }
public override bool DoInit() { if (Frequency < 100) { Frequency = 100; } GetProperty(nameof(StartValue), true).RegisterEvent(eThingEvents.PropertyChanged, sinkTriggered); GetProperty(nameof(StartValue), true).RegisterEvent(eThingEvents.PropertyChanged, sinkStartValueChanged); GetProperty("Value", true).RegisterEvent(eThingEvents.PropertyChanged, sinkValueReset); this.MyBaseThing.DeclareSensorProperty("Value", ePropertyTypes.TNumber, new cdeP.TheSensorMeta { RangeMax = 100, RangeMin = 0, Units = "ticks", }); if (!TheThing.GetSafePropertyBool(MyBaseThing, "IsStateSensor")) { TheThing.SetSafePropertyBool(MyBaseThing, "IsStateSensor", true); TheThing.SetSafePropertyString(MyBaseThing, "StateSensorType", "analog"); TheThing.SetSafePropertyString(MyBaseThing, "StateSensorUnit", "ticks"); TheThing.SetSafePropertyNumber(MyBaseThing, "StateSensorMaxValue", 100); TheThing.SetSafePropertyNumber(MyBaseThing, "StateSensorAverage", 50); TheThing.SetSafePropertyNumber(MyBaseThing, "StateSensorMinValue", 0); } if (!IsActive) { IsActive = false; MyBaseThing.StatusLevel = 0; MyBaseThing.LastMessage = "Countdown ready"; if (AutoStart && mTimer == null) { sinkTriggered(this.GetProperty("Value", false)); } } return(true); }
public override bool DoCreateUX() { var tFlds = TheNMIEngine.AddStandardForm(MyBaseThing, "FACEPLATE", 18, null, null, 0, "...Virtual State Sensors"); SummaryForm = (tFlds["DashIcon"] as TheDashPanelInfo); MyStatusForm = tFlds["Form"] as TheFormInfo; MyStatusForm.ModelID = "VStateSensorForm"; TheNMIEngine.AddSmartControl(MyBaseThing, MyStatusForm, eFieldType.TileButton, 1112, 2, 0, "Details...", null, new nmiCtrlTileButton { OnClick = $"TTS:%RealSensorThing%", RenderTarget = "VSDETBUT%cdeMID%", NoTE = true, Background = "transparent", TileWidth = 3, TileHeight = 1, TileFactorY = 2, ClassName = "cdeEmptyButtoninner" }); var ts = TheNMIEngine.AddStatusBlock(MyBaseThing, MyStatusForm, 100); ts["Group"].SetParent(1); TheNMIEngine.AddSmartControl(MyBaseThing, MyStatusForm, eFieldType.CollapsibleGroup, 200, 2, 0xc0, "V-Sensor Settings...", null, ThePropertyBag.Create(new nmiCtrlCollapsibleGroup() { DoClose = true, MaxTileWidth = 6, IsSmall = true, ParentFld = 1 })); TheNMIEngine.AddSmartControl(MyBaseThing, MyStatusForm, eFieldType.ComboBox, 210, 2, 0xc0, "Sensor Type", "StateSensorType", new nmiCtrlComboBox { ParentFld = 200, FldWidth = 4, TileWidth = 6, Options = "analog;binary;state" }); TheNMIEngine.AddSmartControl(MyBaseThing, MyStatusForm, eFieldType.CollapsibleGroup, 220, 2, 0xc0, "Settings (Analog Sensor Only)...", null, ThePropertyBag.Create(new nmiCtrlCollapsibleGroup() { DoClose = true, MaxTileWidth = 6, ParentFld = 1, IsSmall = true })); TheNMIEngine.AddSmartControl(MyBaseThing, MyStatusForm, eFieldType.SingleEnded, 225, 2, 0xc0, "Sensor Units", "StateSensorUnit", new nmiCtrlSingleEnded { ParentFld = 220, TileWidth = 3 }); TheNMIEngine.AddSmartControl(MyBaseThing, MyStatusForm, eFieldType.SingleEnded, 221, 2, 0x0, "Sensor Value Name", "StateSensorValueName", new nmiCtrlSingleEnded { ParentFld = 220, TileWidth = 6 }); TheNMIEngine.AddSmartControl(MyBaseThing, MyStatusForm, eFieldType.Number, 228, 2, 0xc0, "Average Value", "StateSensorAverage", new nmiCtrlNumber { ParentFld = 220, TileWidth = 3 }); TheNMIEngine.AddSmartControl(MyBaseThing, MyStatusForm, eFieldType.Number, 230, 2, 0xc0, "Min Value", "StateSensorMinValue", new nmiCtrlNumber { ParentFld = 220, TileWidth = 3 }); TheNMIEngine.AddSmartControl(MyBaseThing, MyStatusForm, eFieldType.Number, 240, 2, 0xc0, "Max Value", "StateSensorMaxValue", new nmiCtrlNumber { ParentFld = 220, TileWidth = 3 }); TheNMIEngine.AddSmartControl(MyBaseThing, MyStatusForm, eFieldType.Number, 245, 2, 0xc0, "Value Divider", "StateSensorScaleFactor", new nmiCtrlNumber { TileWidth = 3, ParentFld = 220 }); TheNMIEngine.AddSmartControl(MyBaseThing, MyStatusForm, eFieldType.Number, 246, 2, 0xc0, "Digits", "StateSensorDigits", new nmiCtrlNumber { MinValue = 0, TileWidth = 3, ParentFld = 220 }); TheNMIEngine.AddSmartControl(MyBaseThing, MyStatusForm, eFieldType.SingleCheck, 247, 2, 0xc0, "Force Absolute", "StateSensorIsAbs", new nmiCtrlSingleCheck { TileWidth = 3, ParentFld = 220 }); TheNMIEngine.AddSmartControl(MyBaseThing, MyStatusForm, eFieldType.ThingPicker, 212, 2, 0xc0, "Thing Picker", "RealSensorThing", new nmiCtrlThingPicker() { ParentFld = 200, IncludeEngines = true, IncludeRemotes = true }); TheNMIEngine.AddSmartControl(MyBaseThing, MyStatusForm, eFieldType.PropertyPicker, 213, 2, 0xc0, "Property Picker", "RealSensorProperty", new nmiCtrlPropertyPicker() { ParentFld = 200, ThingFld = 212 }); GetProperty("RealSensorProperty", true).RegisterEvent(eThingEvents.PropertyChanged, (p) => { EngageMapper(); }); var tEngage = TheNMIEngine.AddSmartControl(MyBaseThing, MyStatusForm, eFieldType.TileButton, 214, 2, 0xC0, "Restart Sensor", null, new nmiCtrlNumber { ParentFld = 200, TileWidth = 6, NoTE = true }); tEngage.RegisterUXEvent(MyBaseThing, eUXEvents.OnClick, "reset", (sender, para) => { EngageMapper(); }); TheNMIEngine.AddSmartControl(MyBaseThing, MyStatusForm, eFieldType.CollapsibleGroup, 249, 2, 0xc0, "Advanced Settings...", null, ThePropertyBag.Create(new nmiCtrlCollapsibleGroup() { DoClose = true, MaxTileWidth = 6, ParentFld = 200, IsSmall = true })); TheNMIEngine.AddSmartControl(MyBaseThing, MyStatusForm, eFieldType.SingleCheck, 250, 2, 0xC0, "Replicate", "IsGlobal", new nmiCtrlNumber { ParentFld = 249, TileWidth = 3 }); TheNMIEngine.AddSmartControl(MyBaseThing, MyStatusForm, eFieldType.Number, 255, 2, 0xC0, "NMI Interval", "Interval", new nmiCtrlNumber { ParentFld = 249, TileWidth = 3 }); TheNMIEngine.AddSmartControl(MyBaseThing, MyStatusForm, eFieldType.CollapsibleGroup, 260, 2, 0xc0, "Pre-Processing...", null, ThePropertyBag.Create(new nmiCtrlCollapsibleGroup() { DoClose = true, MaxTileWidth = 6, ParentFld = 220, IsSmall = true })); TheNMIEngine.AddSmartControl(MyBaseThing, MyStatusForm, eFieldType.ComboBox, 261, 2, 0xC0, "Pre-Processing", "PrePro", new nmiCtrlComboBox { Options = "Raw Value:0;Min Value:1;Max Value:2;Average:3", ParentFld = 260, TileWidth = 6, DefaultValue = "0" }); TheNMIEngine.AddSmartControl(MyBaseThing, MyStatusForm, eFieldType.Number, 265, 2, 0xC0, "PrePro Window", "PreProTime", new nmiCtrlNumber { ParentFld = 260, TileWidth = 3 }); TheNMIEngine.AddSmartControl(MyBaseThing, MyStatusForm, eFieldType.SingleEnded, 216, 2, 0x0, "Match Pattern", nameof(ValueMatchPattern), new nmiCtrlSingleEnded { ParentFld = 200, TileWidth = 6 }); TheNMIEngine.AddSmartControl(MyBaseThing, MyStatusForm, eFieldType.SingleEnded, 217, 2, 0x0, "Filter Pattern", nameof(ValueFilterPattern), new nmiCtrlSingleEnded { ParentFld = 200, TileWidth = 6 }); if (TheCommonUtils.CBool(TheBaseAssets.MySettings.GetSetting("RedPill"))) { var t = TheNMIEngine.AddSmartControl(MyBaseThing, MyStatusForm, eFieldType.TileButton, 266, 2, 0xC0, "Test", null, new nmiCtrlTileButton { NoTE = true, Background = "transparent", TileWidth = 3, TileHeight = 1 }); t.RegisterUXEvent(MyBaseThing, eUXEvents.OnClick, "hakko", (sender, para) => { cdeP tRW = GetProperty("RawValue", true); cdeP.SetSafePropertyNumber(tRW, "StateSensorMinValue", TheCommonUtils.GetRandomUInt(0, 100)); TheThing.SetSafePropertyNumber(MyBaseThing, "[RawValue].[StateSensorAverage]", TheCommonUtils.GetRandomUInt(0, 100)); }); TheNMIEngine.AddSmartControl(MyBaseThing, MyStatusForm, eFieldType.Number, 267, 2, 0xc0, "Average Value", "[RawValue].[StateSensorAverage]", new nmiCtrlNumber { TileWidth = 3 }); TheNMIEngine.AddSmartControl(MyBaseThing, MyStatusForm, eFieldType.Number, 268, 2, 0xc0, "Min Value", "[RawValue].[StateSensorMinValue]", new nmiCtrlNumber { TileWidth = 3 }); } return(true); }
private void onPrometheusHttpRequest(TheRequestData tRequest) { try { tRequest.AllowStatePush = false; //No need to track state if (HoldOffOnSenderLoop && (DateTimeOffset.Now - _lastPollTime) > maxMetricSampleRate) { // Using polling: generate all metrics now GenerateMetrics(); _lastPollTime = DateTimeOffset.Now; } using (var outputStream = new MemoryStream()) { try { _registry.CollectAndExportAsTextAsync(outputStream, TheBaseAssets.MasterSwitchCancelationToken).Wait(); } catch (ScrapeFailedException ex) { tRequest.StatusCode = 500; EventsSentErrorCountSinceStart++; if (!string.IsNullOrWhiteSpace(ex.Message)) { tRequest.ResponseBuffer = Encoding.UTF8.GetBytes(ex.Message); } return; } if (tRequest.Header.TryGetValue("Accept", out var acceptHeader)) { var acceptHeaders = acceptHeader?.Split(','); var contentType = "text/plain"; tRequest.ResponseMimeType = contentType; tRequest.StatusCode = 200; tRequest.ResponseBuffer = outputStream.ToArray(); EventsSentSinceStart++; TheThing.SetSafePropertyNumber(MyBaseThing, "QValue", EventsSentSinceStart); LastSendTime = DateTimeOffset.Now; TheThing.SetSafePropertyString(MyBaseThing, "StateSensorUnit", TheCommonUtils.GetDateTimeString(LastSendTime, -1)); } else { tRequest.StatusCode = 406; tRequest.ResponseBuffer = Encoding.UTF8.GetBytes("No accept header"); EventsSentErrorCountSinceStart++; return; } } } catch (Exception ex) when(!(ex is OperationCanceledException)) { EventsSentErrorCountSinceStart++; TheBaseAssets.MySYSLOG.WriteToLog(95307, TSM.L(eDEBUG_LEVELS.OFF) ? null : new TSM(strPrometheusExporter, $"Error in metric server: {this.MyBaseThing?.Address}", eMsgLevel.l3_ImportantMessage, ex.ToString())); try { tRequest.StatusCode = 500; } catch { // Might be too late in request processing to set response code, so just ignore. } } }
public override bool Init() { if (mIsInitCalled) { return(false); } mIsInitCalled = true; MyBaseThing.LastMessage = "Device Ready"; MyBaseThing.StatusLevel = 0; IsConnected = false; MyBaseThing.DeclareConfigProperty(new TheThing.TheConfigurationProperty { Name = nameof(MyBaseThing.Address), cdeT = ePropertyTypes.TString, Required = true, Description = "", Generalize = true }); MyBaseThing.DeclareConfigProperty(new TheThing.TheConfigurationProperty { Name = nameof(MyBaseThing.FriendlyName), cdeT = ePropertyTypes.TString, Description = "" }); MyBaseThing.DeclareConfigProperty(new TheThing.TheConfigurationProperty { Name = nameof(Interval), cdeT = ePropertyTypes.TNumber, DefaultValue = 1000, RangeMin = 100, Description = "Time interval at which to poll the sensor for values" }); MyBaseThing.DeclareConfigProperty(new TheThing.TheConfigurationProperty { Name = nameof(CustomPort), cdeT = ePropertyTypes.TNumber, DefaultValue = 502, Description = "" }); MyBaseThing.DeclareConfigProperty(new TheThing.TheConfigurationProperty { Name = nameof(SlaveAddress), cdeT = ePropertyTypes.TNumber, DefaultValue = 1, Description = "" }); MyBaseThing.DeclareConfigProperty(new TheThing.TheConfigurationProperty { Name = nameof(AutoConnect), cdeT = ePropertyTypes.TBoolean, DefaultValue = false, Description = "" }); MyBaseThing.DeclareConfigProperty(new TheThing.TheConfigurationProperty { Name = nameof(KeepOpen), cdeT = ePropertyTypes.TBoolean, DefaultValue = true, Description = "" }); MyBaseThing.DeclareConfigProperty(new TheThing.TheConfigurationProperty { Name = nameof(Offset), cdeT = ePropertyTypes.TNumber, Description = "" }); MyBaseThing.DeclareConfigProperty(new TheThing.TheConfigurationProperty { Name = nameof(ConnectionType), cdeT = ePropertyTypes.TNumber, DefaultValue = 3, Description = "Read Coils:1, Read Input:2, Holding Registers:3, Input Register:4, Read Multiple Register:23" }); MyModFieldStore = new TheStorageMirror <FieldMapping>(TheCDEngines.MyIStorageService); MyModFieldStore.IsRAMStore = true; MyModFieldStore.IsCachePersistent = true; MyModFieldStore.IsStoreIntervalInSeconds = true; if (string.IsNullOrEmpty(MyBaseThing.ID)) { MyBaseThing.ID = Guid.NewGuid().ToString(); if (MyDevice != null && !string.IsNullOrEmpty(MyDevice.Id)) { MyBaseThing.ID = MyDevice.Id; } if (GetProperty("CustomPort", false) == null) { TheThing.SetSafePropertyNumber(MyBaseThing, "CustomPort", 502); } if (GetProperty("SlaveAddress", false) == null) { TheThing.SetSafePropertyNumber(MyBaseThing, "SlaveAddress", 1); } Interval = 1000; } MyModFieldStore.CacheTableName = $"MBFLDS{MyBaseThing.ID}"; MyModFieldStore.RegisterEvent(eStoreEvents.StoreReady, sinkStoreReady); MyModFieldStore.RegisterEvent(eStoreEvents.UpdateRequested, sinkUpdated); MyModFieldStore.RegisterEvent(eStoreEvents.Inserted, sinkUpdated); MyModFieldStore.InitializeStore(true, false); return(false); }
static public bool CreatePerformanceHeader(TheThing pBaseThing, TheFormInfo pFormInfo, int pFldOrder, int pParent, string pSensorPicSource = "SENSORS/Images/SensorLogo_156x78.png", string pLogo = null) { var tG = TheNMIEngine.AddSmartControl(pBaseThing, pFormInfo, eFieldType.TileGroup, pFldOrder, 0, 0, null, null, new nmiCtrlTileGroup() { ParentFld = pParent, /*MaxTileWidth = 18,*/ Background = "transparent" }); tG.AddOrUpdatePlatformBag(eWebPlatform.Mobile, new nmiPlatBag { MaxTileWidth = 6 }); tG.AddOrUpdatePlatformBag(eWebPlatform.HoloLens, new nmiPlatBag { MaxTileWidth = 12 }); TheNMIEngine.AddSmartControl(pBaseThing, pFormInfo, eFieldType.TileGroup, pFldOrder + 1, 0, 0, null, null, new nmiCtrlTileGroup() { ParentFld = pFldOrder, TileWidth = 6 }); //TheNMIEngine.AddSmartControl(pBaseThing, pFormInfo, eFieldType.StatusLight, pFldOrder + 2, 0, 0, "Current status", "StatusLevel", new TheNMIBaseControl { ParentFld = pFldOrder + 1, TileHeight = 1, TileWidth = 4 }); //TheNMIEngine.AddSmartControl(pBaseThing, pFormInfo, eFieldType.SmartLabel, pFldOrder + 3, 0, 0, null, "LastMessage", new nmiCtrlTextArea() { ParentFld = pFldOrder + 1, TileWidth = 6, TileHeight = 1, Background = "transparent",NoTE=true, FontSize = 20, HorizontalAlignment = "left", VerticalAlignment = "center"/*, Value = pCurrentStatus*/ }); var tMiniStatus = TheNMIEngine.AddSmartControl(pBaseThing, pFormInfo, eFieldType.TileGroup, pFldOrder + 5, 0, 0, null, null, new nmiCtrlTileGroup() { ParentFld = pFldOrder, TileWidth = 6, Background = "transparent" }); if (!string.IsNullOrEmpty(pLogo)) { var tBut2 = TheNMIEngine.AddSmartControl(pBaseThing, pFormInfo, eFieldType.Picture, pFldOrder + 7, 2, 0, null, null, new nmiCtrlPicture() { ParentFld = pFldOrder + 5, TileWidth = 2, TileHeight = 1, NoTE = true, Background = "transparent", Source = pLogo }); tBut2.RegisterUXEvent(pBaseThing, eUXEvents.OnClick, "ImageClick2", (sender, para) => { uint min = TheCommonUtils.CUInt(TheThing.GetSafePropertyNumber(pBaseThing, "StateSensorMinValue")); uint max = TheCommonUtils.CUInt(TheThing.GetSafePropertyNumber(pBaseThing, "StateSensorMaxValue")); TheThing.SetSafePropertyNumber(pBaseThing, "QValue", TheCommonUtils.GetRandomUInt(min, max)); }); } var tFld = TheNMIEngine.AddSmartControl(pBaseThing, pFormInfo, eFieldType.TileGroup, pFldOrder + 8, 0, 0, null, null, new nmiCtrlTileGroup() { ParentFld = pFldOrder, TileWidth = 6, TileHeight = 2, Background = "transparent" }); tFld.AddOrUpdatePlatformBag(eWebPlatform.Any, new nmiPlatBag { Hide = true }); tFld.AddOrUpdatePlatformBag(eWebPlatform.Desktop, new nmiPlatBag { Show = true }); tFld.AddOrUpdatePlatformBag(eWebPlatform.XBox, new nmiPlatBag { Show = true }); TheNMIEngine.AddSmartControl(pBaseThing, pFormInfo, eFieldType.SmartLabel, pFldOrder + 9, 0, 0, null, "FriendlyName", new nmiCtrlSmartLabel() { ParentFld = pFldOrder + 8, NoTE = true, TileWidth = 3, TileHeight = 1, LabelClassName = "cdeSelDevice", FontSize = 20, HorizontalAlignment = "left", VerticalAlignment = "center" /*, Value = pSelectedDeviceName*/ }); if (!string.IsNullOrEmpty(pSensorPicSource)) { var tBut3 = TheNMIEngine.AddSmartControl(pBaseThing, pFormInfo, eFieldType.Picture, pFldOrder + 10, 0, 0, null, null, new nmiCtrlPicture() { NoTE = true, ParentFld = pFldOrder + 8, TileWidth = 3, TileHeight = 1, Source = pSensorPicSource });; tBut3.RegisterUXEvent(pBaseThing, eUXEvents.OnClick, "LogoClick3", (sender, para) => { uint min = TheCommonUtils.CUInt(TheThing.GetSafePropertyNumber(pBaseThing, "StateSensorMinValue")); uint max = TheCommonUtils.CUInt(TheThing.GetSafePropertyNumber(pBaseThing, "StateSensorMaxValue")); TheThing.SetSafePropertyNumber(pBaseThing, "QValue", TheCommonUtils.GetRandomUInt(min, max)); }); } TheNMIEngine.AddSmartControl(pBaseThing, pFormInfo, eFieldType.StatusLight, pFldOrder + 12, 0, 0, null, "StatusLevel", new TheNMIBaseControl { NoTE = true, TileHeight = 1, TileWidth = 1, ParentFld = pFldOrder + 8 }); TheNMIEngine.AddSmartControl(pBaseThing, pFormInfo, eFieldType.TextArea, pFldOrder + 11, 0, 0, null, "LastMessage", new nmiCtrlTextArea { TileHeight = 1, ParentFld = pFldOrder + 8, TileWidth = 5, FontSize = 12, NoTE = true, Foreground = "gray", Background = "Transparent" }); return(true); }
public override bool Init() { if (mIsInitCalled) { return(false); } mIsInitCalled = true; MyBaseThing.StatusLevel = 4; string temp; TheBaseAssets.MyCmdArgs.TryGetValue("IsHealthCollectionOff", out temp); if (!string.IsNullOrEmpty(temp)) { IsHealthCollectionOff = TheCommonUtils.CBool(temp); } if (HealthCollectionCycle == 0) { TheBaseAssets.MyCmdArgs.TryGetValue("HealthCollectionCycle", out temp); if (!string.IsNullOrEmpty(temp)) { HealthCollectionCycle = TheCommonUtils.CInt(temp); } if (HealthCollectionCycle == 0) { HealthCollectionCycle = 15; } } if (SensorDelay == 0) { TheBaseAssets.MyCmdArgs.TryGetValue("SensorDelay", out temp); if (!string.IsNullOrEmpty(temp)) { SensorDelay = TheCommonUtils.CInt(temp); } if (SensorDelay == 0) { SensorDelay = 500; } } if (SensorAccelDeadband < 1) { TheBaseAssets.MyCmdArgs.TryGetValue("SensorAccelDeadband", out temp); if (!string.IsNullOrEmpty(temp)) { SensorAccelDeadband = TheCommonUtils.CDbl(temp); } if (SensorAccelDeadband < 0.1) { SensorAccelDeadband = 3.0; } } int tBS = (int)TheThing.GetSafePropertyNumber(MyBaseThing, "ChartValues"); if (tBS < 10) { tBS = 1000; TheThing.SetSafePropertyNumber(MyBaseThing, "ChartValues", tBS); } tBS = (int)TheThing.GetSafePropertyNumber(MyBaseThing, "ChartSize"); if (tBS < 6) { tBS = 18; TheThing.SetSafePropertyNumber(MyBaseThing, "ChartSize", tBS); } TheUserManager.RegisterNewRole(new TheUserRole(new Guid("{0A254170-D4D4-4B2D-9E05-D471729BE739}"), "ComputerManager", 1, new Guid("{3FB56264-9AA8-4AC9-9208-A01F1142B153}"), true, "Person allowed to view Computer Details")); MyBaseThing.RegisterEvent(eEngineEvents.IncomingMessage, HandleMessage); //Event when C-DEngine has new Telegram for this service as a subscriber (Client Side) MyBaseThing.RegisterEvent("FileReceived", sinkFileReceived); MyBaseThing.RegisterEvent(eEngineEvents.ShutdownEvent, sinkEngineShutdown); StartEngineServices(); if (MyBaseThing.StatusLevel == 4) { MyBaseThing.StatusLevel = 1; } mIsInitialized = true; return(true); }