private void refresh_outstations(OutstationWrapper updated) { this.BeginInvoke(new Action(refresh_outstations2)); }
public void AddOutstations(List<Outstation> outstations) { foreach (Outstation o in outstations) { if (o.OutstationChannelMappings.Count > 0) { // Only load the first mapping OutstationChannelMapping m = o.OutstationChannelMappings[0]; // Select the first (and should be only) channel that matches the mapping ChannelWrapper cw = null; if ((cw = Channels.SingleOrDefault(x => x.Model.Id == m.ChannelID)) != null) { var config = new MasterStackConfig(); config.master.integrityPeriod = TimeSpan.FromSeconds(60); config.master.taskRetryPeriod = TimeSpan.FromSeconds(60); config.link.localAddr = 30001; config.link.remoteAddr = Convert.ToUInt16(o.Address); config.link.timeout = TimeSpan.FromSeconds(10); config.link.useConfirms = true; //setup your stack configuration here. config.app.rspTimeout = TimeSpan.FromSeconds(50); OutstationWrapper OW = new OutstationWrapper(o); var master = cw.Channel.AddMaster("master", LogLevel.Debug, OW, config); OW.Master = master; OW.OnStateChanged += this.Callback_OutstationStateChanged; OW.OnMeasurementsReceived += this.Callback_OutstationMeasurementReceived; master.AddStateListener(new Action<StackState>(OW.StateChanged)); var classMask = PointClassHelpers.GetMask(PointClass.PC_CLASS_1, PointClass.PC_CLASS_2, PointClass.PC_CLASS_3); var classScan = master.AddClassScan(classMask, TimeSpan.FromSeconds(10), TimeSpan.FromSeconds(60)); Outstations.Add(OW); master.Enable(); // enable communications } } } }
private void update_point_history(OutstationWrapper ow, IndexedValue<DataPoint> point, DateTime update_time) { History H = new History(); // get the master point BasicType b = point.value.GetBasicType(); Point P = TulipContext.Points.SingleOrDefault(X => X.PointIndex == point.index && X.Type == b); if (P != null) { DateTime timestamp; if (point.value.time == TimeStamp.epoch) timestamp = update_time; else timestamp = point.value.time; H.Timestamp = timestamp; H.Quality = point.value.quality; if (P.Type == BasicType.ANALOG_STATUS) H.ValueAnalog = point.value.ToSingle(); else H.ValueDigital = Convert.ToInt32(point.value.ToBoolean()); P.Histories.Add(H); } }
private void update_point_master(OutstationWrapper ow, IndexedValue<DataPoint> point, DateTime update_time) { DateTime timestamp; if (point.value.time == TimeStamp.epoch) timestamp = update_time; else timestamp = point.value.time; /* Check if the point currently exists in the points table */ Point point_model = ow.Model.Points.SingleOrDefault(x => x.PointIndex == point.index && x.Type == point.value.GetBasicType()); /* Create a new point */ if (point_model == null) { point_model = new Point(); point_model.OutstationID = ow.Model.Id; point_model.PointIndex = (int)point.index; point_model.Status = POINT_STATUS.DETECTED; point_model.Type = point.value.GetBasicType(); TulipContext.Points.Add(point_model); } else { // ?? p.Status == POINT_STATUS.UPDATED; } // if a series of measurements come through, only keep the most recent if (timestamp > point_model.LastMeasurement) { if (point_model.Type == BasicType.ANALOG_STATUS) point_model.ValueAnalog = point.value.ToSingle(); else point_model.ValueDigital = Convert.ToInt32(point.value.ToBoolean()); point_model.LastMeasurement = timestamp; point_model.Quality = point.value.quality; point_model.LastUpdate = update_time; } }
public void Callback_OutstationMeasurementReceived(OutstationWrapper ow, IMeasurementUpdate update) { // TODO make async DateTime update_time = DateTime.UtcNow; // TODO: FIX if (update.AnalogUpdates.Count > 0) { foreach (IndexedValue<Analog> ana in update.AnalogUpdates) { // TODO: better way to do this? IndexedValue<DataPoint> dp = new IndexedValue<DataPoint>(ana.value, ana.index); update_point_master(ow, dp, update_time); update_point_history(ow, dp, update_time); } } if (update.BinaryUpdates.Count > 0) { foreach (IndexedValue<Binary> bin in update.BinaryUpdates) { IndexedValue<DataPoint> dp = new IndexedValue<DataPoint>(bin.value, bin.index); update_point_master(ow, dp, update_time); update_point_history(ow, dp, update_time); } } if (OnOutstationMeasurementReceived != null) OnOutstationMeasurementReceived(); TulipContext.SaveChanges(); }