/// <summary> /// Trigger all actions /// </summary> /// <param name="args"></param> public virtual void TriggerActions(SensorCollectEventArgs args) { foreach (IAction act in Children) { act.Execute(DataStore, Sensor, this, args); } }
public override void TestCondition(SensorCollectEventArgs args) { // Determine the state we are in ErrorState newState = ErrorState.Normal; if (args.Data == null) { newState = ErrorState.Exception; } else { if (args.Data.Value < LowError) { newState = ErrorState.ErrorLow; } else if (args.Data.Value > HighError) { newState = ErrorState.ErrorHigh; } else if (args.Data.Value < LowWarning) { newState = ErrorState.WarningLow; } else if (args.Data.Value > HighWarning) { newState = ErrorState.WarningHigh; } else { newState = ErrorState.Normal; } } // If the state has changed, notify if (newState != CurrentState) { args.ConditionMessage = String.Format("{0}: {1} is now {2}", newState, Sensor.Name, args.Data.Value); TriggerActions(args); } CurrentState = newState; }
/// <summary> /// Override this in child classes to develop more actions /// </summary> /// <param name="datastore"></param> /// <param name="sensor"></param> /// <param name="condition"></param> /// <param name="args"></param> public virtual void Execute(IDataStore datastore, ISensor sensor, ICondition condition, SensorCollectEventArgs args) { throw new NotImplementedException(); }
/// <summary> /// Execute this condition and return true if it meets the threshold /// </summary> /// <returns></returns> public virtual void TestCondition(SensorCollectEventArgs args) { throw new NotImplementedException(); }
private void Sensor_Collect(ISensor sender, SensorCollectEventArgs e) { _raw_data.Data.Add(e.Data); DrawToImage(); }
/// <summary> /// Collect data for this sensor (with parameter - not used!) /// </summary> public async Task OuterCollect() { DateTime collectStartTime = DateTime.MinValue; DateTime collectFinishTime = DateTime.MinValue; bool success = false; TimeSpan ts = new TimeSpan(); SensorData sd = null; SensorCollectEventArgs args = new SensorCollectEventArgs(); // Collect data and clock how long it took try { collectStartTime = DateTime.UtcNow; args.Raw = await Collect(); collectFinishTime = DateTime.UtcNow; ts = collectFinishTime - collectStartTime; // Record what was collected LastCollectTime = collectFinishTime; sd = AddValue(args.Raw.Value, collectStartTime, (int)ts.TotalMilliseconds); success = true; // If something blew up, keep track of it } catch (Exception ex) { args.Exception = new SensorException() { Cleared = false, Description = ex.Message, ExceptionTime = DateTime.UtcNow, StackTrace = ex.StackTrace }; LastException = ex.ToString(); InError = true; if (PauseOnError) { Enabled = false; SensorProject.LogException("Sensor error & pause: (#" + this.Identity + ") " + this.Name, ex); } else { SensorProject.LogException("Sensor error: (#" + this.Identity + ") " + this.Name, ex); } // Release the inflight status so that this sensor can collect again } finally { // Move forward to next collection time period - skip any number of intermediate time periods if (NextCollectTime < DateTime.UtcNow) { NextCollectTime = DateTime.UtcNow.AddSeconds((int)Frequency); } // Allow this to be recollected again InFlight = false; } // Now, all elements that can safely be moved after the inflight flag is turned off if (success) { args.Data = new Common.SensorData() { CollectionTimeMs = (int)ts.TotalMilliseconds, Time = collectFinishTime, Value = args.Raw.Value }; // Is anyone listening? if (this.SensorCollect != null) { SensorCollect(this, args); } } // Notify everyone of what happened if (Children != null) { foreach (ICondition c in Children) { c.TestCondition(args); } } }
public override void Execute(IDataStore datastore, ISensor sensor, ICondition condition, SensorCollectEventArgs args) { if (!String.IsNullOrEmpty(KlipfolioId)) { try { TimeSpan ts = DateTime.UtcNow - LastUploadTime; if (args.Data != null) { // Filter to just the amount we care about DateTime end = DateTime.UtcNow; DateTime start = DateTime.MinValue; if (UploadDataWindow != ViewTimeframe.AllTime) { start = end.AddMinutes(-(int)UploadDataWindow); } var list = datastore.RetrieveData(sensor, start, end, false); // Determine the correct URL string UploadUrl = String.Format("https://app.klipfolio.com/api/1/datasources/{0}/data", KlipfolioId); //if (SensorProject.Current.Notifications.UploadReport<SensorData>(list.Data, false, UploadUrl, HttpVerb.PUT, // SensorProject.Current.KlipfolioUsername, SensorProject.Current.KlipfolioPassword)) { // LastUploadTime = DateTime.UtcNow; //} } // Catch problems in uploading } catch (Exception ex) { string headline = String.Format("Error uploading {0} ({1}) to Klipfolio", this.Name, this.Identity); SensorProject.LogException(headline, ex); } } }