public ProcessChannelContext(ChannelInfo channelInfo, MicroserviceDescription description, IChannelFactory factory, IBusDataAdapter dataAdapter) { _channelInfo = channelInfo ?? throw new ArgumentNullException(nameof(channelInfo)); _description = description ?? throw new ArgumentNullException(nameof(description)); _factory = factory ?? throw new ArgumentNullException(nameof(factory)); _dataAdapter = dataAdapter ?? throw new ArgumentNullException(nameof(dataAdapter)); }
private byte[] LoadIconFile(MicroserviceDescription description) { if (!IconFileExist(description)) { return(null); } string filePath = System.IO.Path.Combine(description.BinPath, "wwwroot", description.Icon); return(System.IO.File.ReadAllBytes(filePath)); }
/// <summary> /// /// </summary> /// <param name="channelInfo"></param> /// <returns></returns> public IChannelContext CreateContext(ChannelInfo channelInfo) { #region Validate parameters if (channelInfo == null) { throw new ArgumentNullException(nameof(channelInfo)); } #endregion //channelInfo.Description.Type MicroserviceDescription description = _addinManager.FindMicroservice(channelInfo.Provider); return(new ProcessChannelContext(channelInfo, description, _factory, _dataAdapter)); }
//[AdminAccess] public IActionResult ChannelIcon(string provider) { MicroserviceDescription description = _addinManager.FindMicroservice(provider); byte[] data = LoadIconFile(description); if (data == null) { return(null); } string contentType = MediaType.GetMimeByFileName(description.Icon); return(File(data, contentType)); }
public static void SetDescription(this ChannelInfo channelInfo, MicroserviceDescription description) { #region Validate parameters if (channelInfo == null) { throw new ArgumentNullException(nameof(channelInfo)); } if (description == null) { throw new ArgumentNullException(nameof(description)); } #endregion //if (this.Description != null) // throw new InvalidOperationException("Описание канала уже задано."); //this.Description = description; //this.Provider = description.Provider; IDictionary <string, ChannelInfoProperty> properties = channelInfo.Properties; List <string> existProps = properties.Values.Select(p => p.Name).Where(prop => description.Properties.Values.Select(p => p.Name).Contains(prop)).ToList(); List <string> delProps = properties.Values.Select(p => p.Name).Where(prop => !description.Properties.Values.Select(p => p.Name).Contains(prop)).ToList(); List <string> newProps = description.Properties.Values.Select(p => p.Name).Where(prop => !properties.Values.Select(p => p.Name).Contains(prop)).ToList(); existProps.ForEach(p => { MicroserviceDescriptionProperty dp = description.GetProperty(p); ChannelInfoProperty prop = channelInfo.GetProperty(p); string value = prop.Value; dp.CopyTo(prop); prop.Value = value; }); delProps.ForEach(p => channelInfo.RemoveProperty(p)); newProps.ForEach(p => { MicroserviceDescriptionProperty dp = description.GetProperty(p); var prop = new ChannelInfoProperty(); dp.CopyTo(prop); channelInfo.AddNewProperty(prop); }); }
private bool IconFileExist(MicroserviceDescription description) { if (description == null) { return(false); } if (String.IsNullOrWhiteSpace(description.BinPath)) { return(false); } if (String.IsNullOrWhiteSpace(description.Icon)) { return(false); } string filePath = System.IO.Path.Combine(description.BinPath, "wwwroot", description.Icon); return(System.IO.File.Exists(filePath)); }
public void LoadChannels() { var errors = new List <Exception>(); GroupInfo deafaultGroup = GetOrCreateDefaultGroup(); List <ChannelInfo> allChannels = _dataAdapter.GetChannels(); List <GroupInfo> allGroups = _dataAdapter.GetGroups(); allChannels.ForEach(channelInfo => { try { // Если канал не принадлежит ни одной группе, то включаем его в группу по умолчанию if (!allGroups.Any(group => group.Channels.Contains(channelInfo.LINK))) { var map = new GroupChannelMap() { GroupLINK = deafaultGroup.LINK, ChannelLINK = channelInfo.LINK }; _dataAdapter.SaveGroupMap(map); } } catch (Exception ex) { lock (errors) { errors.Add(ex); } } }); RefreshGroups(); // Создание и запуск каналов allChannels.ForEach(channelInfo => //allChannels.AsParallel().ForAll(channelInfo => { try { MicroserviceDescription description = _addinManager.FindMicroservice(channelInfo.Provider); if (description == null) { channelInfo.Enabled = false; } else { channelInfo.SetDescription(description); } if (channelInfo.IsSystem()) { channelInfo.SetRealAddress(_busSettings.Database.ConnectionString); } _dataAdapter.SaveChannelInfo(channelInfo); if (channelInfo.Enabled) { IChannelContext context = _contextFactory.CreateContext(channelInfo); if (!_channels.TryAdd(channelInfo.VirtAddress, context)) { context.Dispose(); throw new InvalidOperationException($"Канал {channelInfo.VirtAddress} уже существует."); } } } catch (Exception ex) { lock (errors) { errors.Add(ex); } } }); _channels.Values.ToList().ForEach(context => //_channels.Values.AsParallel().ForAll(context => { ChannelInfo channelInfo = context.ChannelInfo; ChannelSettings settings = channelInfo.ChannelSettings(); if (settings.AutoOpen) { try { IChannel channel = context.CreateChannelAsync().Result; //channel.SetSettingsAsync(); channel.OpenAsync().Wait(); } catch (Exception ex) { lock (errors) { errors.Add(ex); } } } }); _channels.Values.ToList().ForEach(context => //_channels.Values.AsParallel().ForAll(context => { ChannelInfo channelInfo = context.ChannelInfo; IChannel channel = context.Channel; if (channel != null && channel.IsOpened) { ChannelSettings settings = channelInfo.ChannelSettings(); if (settings.AutoRun) { try { channel.RunAsync().Wait(); } catch (Exception ex) { lock (errors) { errors.Add(ex); } } } } }); if (errors.Count > 0) { throw new AggregateException(errors); } }
public static MicroserviceDescriptionProperty GetProperty(this MicroserviceDescription description, string propName) { return(description.Properties[propName]); }
//[AdminAccess] //[NoCache] public IActionResult Channels(int?groupLink) { GroupInfo[] channelGroups = _channelManager.ChannelsGroups; IChannelContext[] runtimeChannels = _channelManager.RuntimeChannels; if (groupLink == null) { groupLink = GetGroupLink(); } var channels = runtimeChannels.Select(context => { ChannelInfo channelInfo = context.ChannelInfo; IChannel channel = context.Channel; ExceptionWrapper error = context.LastError.Wrap(); MicroserviceDescription description = _addinManager.FindMicroservice(channelInfo.Provider); return(new { channelInfo.LINK, channelInfo.Name, channelInfo.Provider, channelInfo.VirtAddress, channelInfo.SID, channelInfo.RealAddress, channelInfo.Timeout, IsSystem = channelInfo.IsSystem(), channelInfo.Enabled, channelInfo.Comment, Opened = (channel != null ? channel.IsOpened : false), //channelInfo.Opened, //channelInfo.Running, //channelInfo.Online, CanSyncContacts = description.CanSyncContacts, LastError = (error != null ? error.Time.Value.ToString("[dd.MM.yyyy HH:mm:ss]") + ' ' + error.Message.Split('\n')[0] : ""), //IconCss = (string)null //(IconFileExist(channelInfo.Description) ? null : (channelInfo.Description != null ? channelInfo.Description.IconCss : null)) }); }).ToList(); var groups = channelGroups.Select(group => new { group.LINK, group.Name, group.Image, Channels = channels.Where(channelInfo => group.Channels.Contains(channelInfo.LINK)).ToArray() } ).ToList(); if (this.Request.IsAjaxRequest()) { if (groupLink != null) { var group = groups.Single(g => g.LINK == groupLink); return(Json(group.Channels)); } else { return(Json(channels)); } } else { if (_serviceInfo.StartupError != null) { return(RedirectToAction("Home")); } bool systemExist = runtimeChannels.Any(context => context.ChannelInfo.IsSystem()); var registeredChannels = _addinManager.RegisteredMicroservices.Select(desc => new { desc.Provider, //IconCss = (string)null, //(IconFileExist(desc) ? null : desc.IconCss), desc.Comment, Disabled = (desc.Provider == "SYSTEM" && systemExist || desc.Provider != "SYSTEM" && !systemExist) } ).ToList(); this.ViewBag.ChannelGroups = groups; this.ViewBag.RegisteredChannels = registeredChannels; return(View("Channels")); } }