using Step.Model.DTOModels; 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 Step.CmsConnectManager.Exceptions; using Step.CmsConnectManager; using Step.CmsConnectManager.Events; using static Step.Model.Constants; using System.Threading.Tasks; using MConnectSDK; using System.Security.Claims; using Step.Database.Controllers; using Step.Model.DatabaseModels; namespace Step.Controllers.WebApi { [RoutePrefix("api/cms_connect")] public class CmsConnectController : ApiController { [Route("runConnectionTest"), HttpGet] public IHttpActionResult RunConnectionTest() { GatewayController Adp = new GatewayController( 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() { GatewayController Adp = new GatewayController( 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() { GatewayController Adp = new GatewayController( 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() { GatewayController Adp = new GatewayController( 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) { GatewayController Adp = new GatewayController( 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) { GatewayController Adp = new GatewayController( 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 != 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.Password(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); } } [Route("check_license"), HttpPost] public IHttpActionResult CheckLicense() { MConnectSDKWrapper _sdk = new MConnectSDKWrapper(); _sdk.TryActivation(); return Ok(); } [Route("user/not_imported_list"), HttpGet] public async Task GetUserList() { MConnectSDKWrapper _sdk = new MConnectSDKWrapper(); var res = await _sdk.GetUserNotImported(); return Ok(res); } [Route("user/login"), HttpPost] public async Task UserLogin([Required]string username, [Required]string password) { MConnectSDKWrapper _sdk = new MConnectSDKWrapper(); var res = await _sdk.TryUserLogin(username, password); return Ok(res); } [Route("user/import"), HttpPost] public async Task ImportUser(List usersToImport) { MConnectSDKWrapper _sdk = new MConnectSDKWrapper(); var res = await _sdk.ImportConnectUsers(usersToImport); if (res) return Ok(res); else return NotFound(); } [Route("message"), HttpPost] [WebApiAuthorize(FunctionAccess = FUNCTIONALITY_NAMES.NC_DATA, Action = ACTIONS.READ)] public IHttpActionResult SendMessage(DTOMessageModel message) { MConnectSDKWrapper _sdk = new MConnectSDKWrapper(); var identity = User.Identity as ClaimsIdentity; var userId = identity.Claims.FirstOrDefault(c => c.Type == USER_ID_KEY); UsersController userController = new UsersController(); UserModel um = userController.FindById(Convert.ToInt32(userId.Value)); message.UserId = um.Username; message.Id = Guid.NewGuid(); message.TimeStamp = DateTime.Now; var res = _sdk.SendMessage(message); if (res) return Ok(res); else return NotFound(); } } }