protected CommunicationDirector(EdgeGatewayConfiguration config) { this.config = config; clientId = Guid.NewGuid().ToString(); CreateWebSocketClient(); CreateHttpClient(); }
public void Configure(IApplicationBuilder app, IHostingEnvironment env) { var builder = new ConfigurationBuilder() .AddGatewayConfiguration(); IConfigurationRoot root = builder.Build(); EdgeGatewayConfiguration config = new EdgeGatewayConfiguration(); ConfigurationBinder.Bind(root, config); CommunicationDirector.Create(config); //create the singleton instance if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } app.UseMvc(routes => { routes.MapRoute("default", "{controller=RtuOutput}/{action=Index}/{id?}"); }); app.Run(async(context) => { await context.Response.WriteAsync("Field Gateway Service Running..."); }); }
public static CommunicationDirector Create(EdgeGatewayConfiguration config) { ran = new Random(); if (instance == null) { instance = new CommunicationDirector(config); } return(instance); }
//private string requestUrl; // = "http://echomodule:8889/api/rtuinput"; public RtuOutputController(EdgeGatewayConfiguration config, ILogger <RtuOutputController> logger) { this.logger = logger; try { director = CommunicationDirector.Create(config); } catch (Exception ex) { logger.LogError(ex.Message); } }
private void SetHttpProxyWithUpstreamProtocolDetails(JTokenWrapper module, EdgeGatewayConfiguration edgeGatewayConfiguration) { if (edgeGatewayConfiguration.ProxyUser != null && edgeGatewayConfiguration.ProxyHost != null && edgeGatewayConfiguration.ProxyPassword != null) { IDictionary <string, ModuleEnvironmentValue> envData = new Dictionary <string, ModuleEnvironmentValue>(); var hostProxyDetails = string.Format(Constants.ProxyTemplate, edgeGatewayConfiguration.ProxyUser, edgeGatewayConfiguration.ProxyPassword, edgeGatewayConfiguration.ProxyHost); envData.Add(Constants.HttpsProxy, new ModuleEnvironmentValue { Value = hostProxyDetails }); envData.Add(Constants.UpstreamProtocl, new ModuleEnvironmentValue { Value = Constants.UpstreamProtocolValue }); module.Value[Constants.ModuleEnv] = JObject.FromObject(envData); } }
//public IEnumerable<string> GetChildKeys(IEnumerable<string> earlierKeys, string parentPath) //{ // return null; //} //public IChangeToken GetReloadToken() //{ // return new EdgeChangeToken(); //} public override void Load() { EdgeGatewayConfiguration gwConfig = null; IotHubTwin twin = new IotHubTwin(); gwConfig = twin.GetModuleConfigAsync().GetAwaiter().GetResult(); if (gwConfig != null) { string jsonString = JsonConvert.SerializeObject(config); byte[] buffer = Encoding.UTF8.GetBytes(jsonString); Console.WriteLine("Writing configuration to file."); File.WriteAllBytes(Constants.CONFIG_PATH, buffer); } else { byte[] byteArray = File.ReadAllBytes(Constants.CONFIG_PATH); gwConfig = JsonConvert.DeserializeObject <EdgeGatewayConfiguration>(Encoding.UTF8.GetString(byteArray)); } if (gwConfig == null) { Console.WriteLine("No configuration found for gateway."); throw new Exception("Configuration not found for gateway."); } else { Data.Add("Hostname", gwConfig.Hostname); Data.Add("ModBusContainer", gwConfig.ModBusContainer); Data.Add("ModBusPath", gwConfig.ModBusPath); Data.Add("ModBusPort", gwConfig.ModBusPort.ToString()); Data.Add("RtuInputPiSystem", gwConfig.RtuInputPiSystem); Data.Add("RtuOutputPiSsytem", gwConfig.RtuOutputPiSsytem); Data.Add("SecurityToken", gwConfig.SecurityToken); Data.Add("UnitId", gwConfig.UnitId.ToString()); //config.Add("Hostname", gwConfig.Hostname); //config.Add("ModBusContainer", gwConfig.ModBusContainer); //config.Add("ModBusPath", gwConfig.ModBusPath); //config.Add("ModBusPort", gwConfig.ModBusPort.ToString()); //config.Add("RtuInputPiSystem", gwConfig.RtuInputPiSystem); //config.Add("RtuOutputPiSsytem", gwConfig.RtuOutputPiSsytem); //config.Add("SecurityToken", gwConfig.SecurityToken); //config.Add("UnitId", gwConfig.UnitId.ToString()); } }
private void UpdateSystemModuleCreateOptions(JTokenWrapper createOptionsToken, EdgeConfiguration edgeConfiguration, EdgeGatewayConfiguration edgeGatewayConfiguration) { string[] communicationPort = edgeConfiguration.UpstreamCommunication.Split('/'); IDictionary <string, Dictionary <string, string>[]> edgeHubPortBindingsConfig = new Dictionary <string, Dictionary <string, string>[]>(); Dictionary <string, string>[] matrix = new Dictionary <string, string> [1]; matrix[0] = new Dictionary <string, string>(); matrix[0].Add(Constants.HostPort, communicationPort[0]); edgeHubPortBindingsConfig.Add(edgeConfiguration.UpstreamCommunication, matrix); var createOption = new { HostConfig = new { PortBindings = JObject.FromObject(edgeHubPortBindingsConfig), LogConfig = new { Type = "json-file", Config = JObject.FromObject(GetModuleLogConfigurationType(edgeConfiguration.AdvanceConfiguration)) } } }; createOptionsToken.Value.Replace(JObject.FromObject(createOption).ToString()); }
private void UpdateEdgeAgent(JTokenWrapper deploymentJsonObject, EdgeConfiguration edgeConfiguration, EdgeGatewayConfiguration edgeGatewayConfiguration) { var createOptionsToken = GetEdgeAgentDesiredPropertiesEdgeAgentCreateOption(deploymentJsonObject); UpdateSystemModuleCreateOptions(createOptionsToken, edgeConfiguration, edgeGatewayConfiguration); var edgeAgent = GetEdgeAgentDesiredPropertiesEdgeAgentModule(deploymentJsonObject); SetHttpProxyWithUpstreamProtocolDetails(edgeAgent, edgeGatewayConfiguration); }
private void UpdateEdgeHub(JTokenWrapper deploymentJsonObject, EdgeConfiguration edgeConfiguration, EdgeGatewayConfiguration edgeGatewayConfiguration) { var createOptionsToken = GetEdgeAgentDesiredPropertiesEdgeHubCreateOption(deploymentJsonObject); UpdateSystemModuleCreateOptions(createOptionsToken, edgeConfiguration, edgeGatewayConfiguration); var messageTTLToken = GetEdgeHubDesiredPropertiesMessageTTL(deploymentJsonObject).Value; messageTTLToken.Replace(Convert.ToInt32(edgeConfiguration.MessageTTL)); var edgeHub = GetEdgeAgentDesiredPropertiesEdgeHubModule(deploymentJsonObject); SetHttpProxyWithUpstreamProtocolDetails(edgeHub, edgeGatewayConfiguration); }