using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.Linq; using System.Net; using System.Web.Http; using TeamDev.SDK.MVVM; using Thermo.Active.CmsConnectGateway; using Thermo.Active.CmsConnectGateway.Events; using Thermo.Active.CmsConnectGateway.Exceptions; using Thermo.Active.Model.DTOModels; using static Thermo.Active.Model.Constants; namespace Thermo.Active.Controllers.WebApi { [RoutePrefix("api/cms_connect")] public class CmsConnectController : ApiController { [Route("runConnectionTest"), HttpGet] public IHttpActionResult RunConnectionTest() { using (GatewayAdapter Adp = new GatewayAdapter(Config.ServerConfig.CmsConnectConfig.Gateway.Address, Config.ServerConfig.CmsConnectConfig.Gateway.Username, Config.ServerConfig.CmsConnectConfig.Gateway.Password)) { try { GatewayConnectionStatus sts = Adp.TestGatewayConnection(1); return Ok(sts.ToString()); } catch (GatewayException e) { return InternalServerError(e); } } } private void endRebootHandler(object sender, GatewayRebootEventHandlerArgs e) { MessageServices.Current.Publish(SEND_CMSCONNECT_GW_REBOOT_STATUS, null, e.errorMessage); } [Route("rebootGateway"), HttpGet] public IHttpActionResult RebootGateway() { using (GatewayAdapter Adp = new GatewayAdapter(Config.ServerConfig.CmsConnectConfig.Gateway.Address, Config.ServerConfig.CmsConnectConfig.Gateway.Username, Config.ServerConfig.CmsConnectConfig.Gateway.Password)) { try { Adp.RebootGatewayAsync(1, endRebootHandler); return Ok(); } catch (GatewayException e) { return InternalServerError(e); } } } [Route("getGatewayNetworkSetup"), HttpGet] public IHttpActionResult GetGatewayNetworkSetup() { using (GatewayAdapter Adp = new GatewayAdapter(Config.ServerConfig.CmsConnectConfig.Gateway.Address, Config.ServerConfig.CmsConnectConfig.Gateway.Username, Config.ServerConfig.CmsConnectConfig.Gateway.Password)) { try { GatewayNetworkConfiguration config = Adp.ReadGatewayNetworkConfiguration(); DTOCmsConnectGatewayNetworkModel model = new DTOCmsConnectGatewayNetworkModel() { hasDhcp = config.hasDhcp, ipAddress = config.ipAddress != null ? config.ipAddress.ToString() : "", netMaskAddress = config.netMaskAddress != null ? config.netMaskAddress.ToString() : "", defaultGatewayAddress = config.defaultGatewayAddress != null ? config.defaultGatewayAddress.ToString() : "", dnsAddresses = config.dnsAddresses != null ? config.dnsAddresses.Select(i => i.ToString()) : new List(), dnsPrefixes = config.dnsPrefixes != null ? config.dnsPrefixes : new List() }; return Ok(model); } catch (GatewayException e) { return InternalServerError(e); } } } [Route("getGatewayProxySetup"), HttpGet] public IHttpActionResult GetGatewayProxySetup() { using (GatewayAdapter Adp = new GatewayAdapter(Config.ServerConfig.CmsConnectConfig.Gateway.Address, Config.ServerConfig.CmsConnectConfig.Gateway.Username, Config.ServerConfig.CmsConnectConfig.Gateway.Password)) { try { GatewayProxyConfiguration config = Adp.ReadGatewayProxyConfiguration(); DTOCmsConnectGatewayProxyModel model = new DTOCmsConnectGatewayProxyModel() { hasProxy = config.hasProxy, address = config.address != null ? config.address : "", port = config.port, username = config.address != null ? config.username : "", password = config.address != null ? config.password : "", noproxyAddresses = config.noproxyAddresses != null ? config.noproxyAddresses : new List() }; return Ok(model); } catch (GatewayException e) { return InternalServerError(e); } } } [Route("setGatewayNetworkSetup"), HttpPut] public IHttpActionResult SetGatewayNetworkSetup([FromBody][Required]DTOCmsConnectGatewayNetworkModel dtoGatewayNetworkModel) { using (GatewayAdapter Adp = new GatewayAdapter(Config.ServerConfig.CmsConnectConfig.Gateway.Address, Config.ServerConfig.CmsConnectConfig.Gateway.Username, Config.ServerConfig.CmsConnectConfig.Gateway.Password)) { try { GatewayNetworkConfigurationBuilder builder = new GatewayNetworkConfigurationBuilder(); if (dtoGatewayNetworkModel.hasDhcp) builder.HasDhcp(true); else { builder.HasDhcp(false); IPAddress addr; if (IPAddress.TryParse(dtoGatewayNetworkModel.ipAddress, out addr)) builder.IpAddress(addr); else throw new ArgumentException("IP Address not ok"); if (IPAddress.TryParse(dtoGatewayNetworkModel.netMaskAddress, out addr)) builder.NetMaskAddress(addr); else throw new ArgumentException("Netmask Address not ok"); if (IPAddress.TryParse(dtoGatewayNetworkModel.defaultGatewayAddress, out addr)) builder.DefaultGatewayAddress(addr); else throw new ArgumentException("D.Gateway Address not ok"); } if (dtoGatewayNetworkModel.dnsAddresses != null) { List dnsAddr = new List(); foreach (string addr in dtoGatewayNetworkModel.dnsAddresses) { if (!string.IsNullOrWhiteSpace(addr)) dnsAddr.Add(IPAddress.Parse(addr)); } builder.DnsAddresses(dnsAddr); } if (dtoGatewayNetworkModel.dnsPrefixes != null) { List dnsPref = new List(); foreach (string addr in dtoGatewayNetworkModel.dnsPrefixes) { if (!string.IsNullOrWhiteSpace(addr)) dnsPref.Add(addr.Trim()); } builder.DnsPrefixes(dnsPref); } Adp.WriteGatewayNetworkConfiguration(builder.GenerateConfiguration()); return Ok(dtoGatewayNetworkModel); } catch (Exception e) { return InternalServerError(e); } } } [Route("setGatewayProxySetup"), HttpPut] public IHttpActionResult SetGatewayProxySetup([FromBody][Required]DTOCmsConnectGatewayProxyModel dtoGatewayProxyModel) { using (GatewayAdapter Adp = new GatewayAdapter(Config.ServerConfig.CmsConnectConfig.Gateway.Address, Config.ServerConfig.CmsConnectConfig.Gateway.Username, Config.ServerConfig.CmsConnectConfig.Gateway.Password)) { try { GatewayProxyConfigurationBuilder builder = new GatewayProxyConfigurationBuilder(); if (!dtoGatewayProxyModel.hasProxy) builder.HasProxy(false); else { builder.HasProxy(true); if (dtoGatewayProxyModel.address != null && !string.IsNullOrWhiteSpace(dtoGatewayProxyModel.address)) builder.Address(dtoGatewayProxyModel.address); else throw new ArgumentException("Address not ok"); if (dtoGatewayProxyModel.port != null && dtoGatewayProxyModel.port != 0) builder.Port(dtoGatewayProxyModel.port); else throw new ArgumentException("Port not ok"); } if (dtoGatewayProxyModel.username != null && !string.IsNullOrWhiteSpace(dtoGatewayProxyModel.username)) builder.Username(dtoGatewayProxyModel.username); if (dtoGatewayProxyModel.password != null && !string.IsNullOrWhiteSpace(dtoGatewayProxyModel.password)) builder.Username(dtoGatewayProxyModel.password); if (dtoGatewayProxyModel.noproxyAddresses != null) { List noProx = new List(); foreach (string addr in dtoGatewayProxyModel.noproxyAddresses) { if (!string.IsNullOrWhiteSpace(addr)) noProx.Add(addr.Trim()); } builder.NoproxyAddresses(noProx); } Adp.WriteGatewayProxyConfiguration(builder.GenerateConfiguration()); return Ok(dtoGatewayProxyModel); } catch (Exception e) { return InternalServerError(e); } } } } }