private ItemSchedule[] GetScheduledDataItems(Config.IO_Model model) { AdapterBase instance = Instance; if (instance != null && instance.SupportsScheduledReading) { List <Tuple <Config.DataItem, Config.Scheduling> > items = this.Config.GetAllDataItemsWithScheduling(model.Scheduling); var res = new List <ItemSchedule>(); foreach (Tuple <Config.DataItem, Config.Scheduling> tp in items) { Config.DataItem dataItem = tp.Item1; Config.Scheduling scheduling = tp.Item2; if (scheduling.Mode == IO.Config.SchedulingMode.Interval && scheduling.Interval.HasValue) { res.Add(new ItemSchedule() { DataItemID = dataItem.ID, Interval = scheduling.Interval.Value, Offset = scheduling.Offset ?? Duration.FromSeconds(0), UseTimestampFromSource = scheduling.UseTimestampFromSource }); } } return(res.ToArray()); } else { return(new ItemSchedule[0]); } }
public static void ConnectAndRunAdapter(string host, int port, AdapterBase adapter) { var connector = new TcpConnectorSlave(); connector.Connect(host, port); try { SingleThreadedAsync.Run(() => Loop(connector, adapter)); } catch (Exception exp) { Console.Error.WriteLine("EXCEPTION: " + exp.Message); } }
public void CreateInstance(Dictionary <string, Type> mapAdapterTypes) { if (!mapAdapterTypes.ContainsKey(Config.Type)) { throw new Exception($"No adapter type '{Config.Type}' found."); } Type type = mapAdapterTypes[Config.Type]; AdapterBase rawAdapter = (AdapterBase)Activator.CreateInstance(type); Instance = new SingleThreadIOAdapter(rawAdapter); State = State.Created; SetOfPendingReadItems.Clear(); SetOfPendingWriteItems.Clear(); LastError = ""; ItemGroups = new Group[0]; MapItem2GroupID.Clear(); }
private static async Task Loop(TcpConnectorSlave connector, AdapterBase module) { Process?parentProcess = null; using (Request request = await connector.ReceiveRequest(5000)) { if (request.Code != AdapterMsg.ID_ParentInfo) { throw new Exception("Missing ParentInfo request"); } ParentInfoMsg?info = StdJson.ObjectFromUtf8Stream <ParentInfoMsg>(request.Payload); if (info == null) { throw new Exception("ParentInfoMsg is null"); } parentProcess = Process.GetProcessById(info.PID); connector.SendResponseSuccess(request.RequestID, s => { }); } Thread t = new Thread(() => { ParentAliveChecker(parentProcess); }); t.IsBackground = true; t.Start(); var helper = new AdapterHelper(module, connector); bool run = true; while (run) { using (Request request = await connector.ReceiveRequest()) { helper.ExecuteAdapterRequestAsync(request); bool shutdown = request.Code == AdapterMsg.ID_Shutdown; run = !shutdown; } } // Wait until parent process kills us (after Shutdown method completed) while (true) { await Task.Delay(1000); } }
public AdapterHelper(AdapterBase module, TcpConnectorSlave connector) { this.adapter = module; this.connector = connector; }
public SingleThreadIOAdapter(AdapterBase wrapped) { this.adapter = wrapped; }