internal string RetryWithTokenUpdate(M2MgoCloudAgentConfiguration config, Func <string> func)
 {
     if (_token == null)
     {
         _token = UpdateAccessToken(config);
     }
     try {
         return(func());
     }
     catch (WebException) {
         _token = UpdateAccessToken(config);
         try
         {
             return(func());
         }
         catch (WebException e)
         {
             if (e.Response != null && e.Response is HttpWebResponse)
             {
                 if (((HttpWebResponse)e.Response).StatusCode == HttpStatusCode.NotFound)
                 {
                     return(string.Empty);
                 }
             }
             throw;
         }
     }
 }
예제 #2
0
        private GatewayGetModel EnsureGateway(M2MgoCloudAgentConfiguration config, string serialId, GatewayProjectGetModel project)
        {
            string result = _apiWrapper.AuthenticateGateway(config, serialId);

            if (result.IsNullOrEmpty())
            {
                _apiWrapper.CreateGateway(config, serialId, project);
                result = _apiWrapper.AuthenticateGateway(config, serialId);
                if (result.IsNullOrEmpty())
                {
                    throw new Exception("Could not create Gateway!");
                }
            }
            var    gatewayId       = _serializer.Deserialize <Guid>(result);
            string gatewayResult   = _apiWrapper.GetGateway(config, gatewayId);
            var    gatewayGetModel = _serializer.Deserialize <GatewayGetModel>(gatewayResult);

            if (gatewayGetModel.GatewayProjectIdentifier == null)
            {
                _apiWrapper.AddGatewayToProject(config, gatewayGetModel.Identifier.ID, project.ID);
                gatewayResult   = _apiWrapper.GetGateway(config, gatewayId);
                gatewayGetModel = _serializer.Deserialize <GatewayGetModel>(gatewayResult);
            }
            return(gatewayGetModel);
        }
예제 #3
0
        public M2mgoGetwayProjectDto RegisterProject(M2MgoCloudAgentConfiguration config, string serialId, UicProject project)
        {
            GatewayProjectGetModel gatewayProject = EnsureProject(config, project);
            GatewayGetModel        gateway        = EnsureGateway(config, serialId, gatewayProject);

            return(new M2mgoGetwayProjectDto(gateway, gatewayProject));
        }
예제 #4
0
 internal string RetryWithTokenUpdate(M2MgoCloudAgentConfiguration config, Func <string> func)
 {
     if (_token == null)
     {
         _token = UpdateAccessToken(config);
     }
     try {
         return(func());
     }
     catch (WebException) {
         _token = UpdateAccessToken(config);
         try
         {
             return(func());
         }
         catch (WebException e)
         {
             var resp = e.Response as HttpWebResponse;
             if (resp != null)
             {
                 if (resp.StatusCode == HttpStatusCode.NotFound)
                 {
                     return(string.Empty);
                 }
                 throw new Exception(WebApiRequestExecutor.ReadDataFrom(resp), e);
             }
             throw;
         }
     }
 }
예제 #5
0
        private BlueprintDto EnsureProjectBlueprint(M2MgoCloudAgentConfiguration config, UicProject project)
        {
            string blueprintCode = _projectBlueprintTranslator.GetBlueprintCodeFrom(project);

            BlueprintDto[] searchResult = _apiWrapper.SearchBlueprint(config, _projectTranslator.GetProjectDomain(project), blueprintCode);

            if (searchResult.Count() > 1)
            {
                throw new Exception(String.Format("found more than 1 project blueprints {0} in Domain {1}", _projectTranslator.GetProjectDomain(project),
                                                  blueprintCode));
            }
            if (searchResult.Any())
            {
                BlueprintDto blueprint = _projectBlueprintTranslator.UpdateProjectDomain(searchResult.Single());
                string       result    = _apiWrapper.UpdateBlueprint(config, blueprint);
                _logger.Information("Updateing Blueprint: " + result);
                return(blueprint);
            }
            else
            {
                BlueprintDto projectBlueprintDto = _projectBlueprintTranslator.GetProjectBlueprintDto(project, _projectTranslator.GetProjectDomain(project));
                projectBlueprintDto.PrepareMasterForCreation();

                string result = _apiWrapper.CreateBlueprint(config, _projectTranslator.GetProjectDomain(project), projectBlueprintDto);
                _logger.Information("CreateBlueprint Blueprint: " + result);
                return(_apiWrapper.SearchBlueprint(config, _projectTranslator.GetProjectDomain(project), blueprintCode).Single());
            }
        }
예제 #6
0
 public string GetGateway(M2MgoCloudAgentConfiguration config, Guid gatewayId)
 {
     return(_userTokenWebApiWrapper.RetryWithTokenUpdate(config, () => {
         HttpWebRequest request = (HttpWebRequest)WebRequest.Create(config.BaseUrl + "api/gateway/" + gatewayId.ToString("D"));
         request.Method = "Get";
         return _webApiRequestExecutor.ExecuteRequest(request, null, _userTokenWebApiWrapper.GetToken(), _logger);
     }));
 }
        public M2mgoCommunicationAgentImpl(ISerializer serializer, ILoggerFactory loggerFactory)
        {
            _serializer    = serializer;
            _loggerFactory = loggerFactory;
            _logger        = loggerFactory.GetLoggerFor(GetType());
            _configuration = ConfigurationProvider.GetConfiguration(serializer, _logger);


            _mqttConnectionWatchdog = new MqttConnectionWatchdog(loggerFactory.GetLoggerFor(typeof(MqttConnectionWatchdog)));
            _mqttWarapper           = new MqttWarapper(_mqttConnectionWatchdog, loggerFactory.GetLoggerFor(typeof(MqttWarapper)));
        }
예제 #8
0
 private void UpdateGatewayAttributes(M2MgoCloudAgentConfiguration config, string serialId, UicProject project, BlueprintDto gatewayBlueprint)
 {
     _apiWrapper.PostAttributes(config, gatewayBlueprint.Identifier.ID, serialId,
                                M2mgoGatewayBlueprintTranslator.AttributeKeySerialId, serialId);
     _apiWrapper.PostAttributes(config, gatewayBlueprint.Identifier.ID, serialId,
                                M2mgoGatewayBlueprintTranslator.AttributeKeyProjectKey, project.ProjectKey);
     _apiWrapper.PostAttributes(config, gatewayBlueprint.Identifier.ID, serialId,
                                M2mgoGatewayBlueprintTranslator.AttributeKeyProjectName, project.Name);
     _apiWrapper.PostAttributes(config, gatewayBlueprint.Identifier.ID, serialId,
                                M2mgoGatewayBlueprintTranslator.AttributeKeyCustomer, project.Owner);
 }
        private M2mgoUserToken UpdateAccessToken(M2MgoCloudAgentConfiguration config)
        {
            var request = (HttpWebRequest)WebRequest.Create(config.BaseUrl + "/api/cms/membership-user/token");

            request.Method      = "POST";
            request.ContentType = "application/json";
            string serializeObject =
                _serializer.Serialize(new { Email = config.User, Password = config.Password });
            string result = _webApiRequestExecutor.ExecuteRequest(request, serializeObject, null, _logger);

            return(_serializer.Deserialize <M2mgoUserToken>(result));
        }
예제 #10
0
        internal BlueprintDto GetBlueprint(M2MgoCloudAgentConfiguration config, Guid blueprintId)
        {
            string result = _userTokenWebApiWrapper.RetryWithTokenUpdate(config, () =>
            {
                var request =
                    (HttpWebRequest)WebRequest.Create(config.BaseUrl + "api/devicetype/" + blueprintId.ToString("D"));
                request.Method = "GET";
                return(_webApiRequestExecutor.ExecuteRequest(request, string.Empty, _userTokenWebApiWrapper.GetToken(), _logger));
            });

            return(_serializer.Deserialize <BlueprintDto>(result));
        }
예제 #11
0
        internal string GetGatewayProject(M2MgoCloudAgentConfiguration config, UicProject project)
        {
            return(_userTokenWebApiWrapper.RetryWithTokenUpdate(config, () => {
                var request =
                    (HttpWebRequest)WebRequest.Create(config.BaseUrl + "api/gatewayProject/" +
                                                      project.CustomerForeignKey.ToString("D") + "/" +
                                                      config.SgetGatewayTypeId.ToString("D") + "/" + project.ProjectKey);
                request.Method = "GET";

                string executeRequest = _webApiRequestExecutor.ExecuteRequest(request, string.Empty, _userTokenWebApiWrapper.GetToken(), _logger);
                return executeRequest;
            }));
        }
예제 #12
0
        public string CreateGatewayProject(M2MgoCloudAgentConfiguration config, GatewayProjectPutModel model)
        {
            return(_userTokenWebApiWrapper.RetryWithTokenUpdate(config, () => {
                if (model == null)
                {
                    throw new ArgumentNullException("model");
                }

                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(config.BaseUrl + "api/gatewayProject");
                request.Method = "PUT";
                string serializeObject = _serializer.Serialize(model);
                return _webApiRequestExecutor.ExecuteRequest(request, serializeObject, _userTokenWebApiWrapper.GetToken(), _logger);
            }));
        }
예제 #13
0
 internal string UpdateBlueprint(M2MgoCloudAgentConfiguration config, BlueprintDto blueprint)
 {
     if (blueprint == null)
     {
         throw new ArgumentNullException("blueprint");
     }
     return(_userTokenWebApiWrapper.RetryWithTokenUpdate(config, () =>
     {
         var request =
             (HttpWebRequest)WebRequest.Create(config.BaseUrl + "api/devicetype/" + blueprint.Identifier.ID.ToString("D"));
         request.Method = "PUT";
         string serializeObject = _serializer.Serialize(blueprint);
         return _webApiRequestExecutor.ExecuteRequest(request, serializeObject, _userTokenWebApiWrapper.GetToken(), _logger);
     }));
 }
예제 #14
0
        public ApplianceBlueprints SynchronizeWithCloud(M2MgoCloudAgentConfiguration config, string serialId, UicProject project, M2mgoGetwayProjectDto m2MgoGetwayProjectDto)
        {
            BlueprintDto gatewayBlueprint = EnsureGatewayBlueprint(config, project);

            EnsureDevice(config, serialId, gatewayBlueprint, m2MgoGetwayProjectDto.Gateway);
            UpdateGatewayAttributes(config, serialId, project, gatewayBlueprint);

            BlueprintDto projectBlueprint = EnsureProjectBlueprint(config, project);

            EnsureDevice(config, serialId, projectBlueprint, m2MgoGetwayProjectDto.Gateway);
            UpdateProjectDeviceAttributes(config, serialId, projectBlueprint);

            SetRelevantDevicesOfGateway(config, m2MgoGetwayProjectDto.Gateway.Identifier.ID, projectBlueprint, serialId);

            return(new ApplianceBlueprints(gatewayBlueprint, projectBlueprint));
        }
예제 #15
0
        public string AuthenticateGateway(M2MgoCloudAgentConfiguration config, string serialId)
        {
            return(_userTokenWebApiWrapper.RetryWithTokenUpdate(config, () => {
                if (serialId.IsNullOrEmpty())
                {
                    throw new ArgumentNullException("serialId");
                }

                var request = (HttpWebRequest)WebRequest.Create(config.BaseUrl
                                                                + "api/gateway/authenticate"
                                                                + "?gatewayTypeId=" + config.SgetGatewayTypeId.ToString("D")
                                                                + "&serial=" + serialId);

                request.Method = "POST";
                return _webApiRequestExecutor.ExecuteRequest(request, null, _userTokenWebApiWrapper.GetToken(), _logger);
            }));
        }
예제 #16
0
        internal BlueprintDto[] SearchBlueprint(M2MgoCloudAgentConfiguration config, Guid domainId, string deviceTypePattern)
        {
            if (deviceTypePattern == null)
            {
                throw new ArgumentNullException("deviceTypePattern");
            }

            string result = _userTokenWebApiWrapper.RetryWithTokenUpdate(config, () =>
            {
                var request =
                    (HttpWebRequest)WebRequest.Create(config.BaseUrl + "api/devicetype/search/" +
                                                      domainId.ToString("D") + "/" + deviceTypePattern);
                request.Method = "GET";
                return(result = _webApiRequestExecutor.ExecuteRequest(request, string.Empty, _userTokenWebApiWrapper.GetToken(), _logger));
            });

            return(_serializer.Deserialize <BlueprintDto[]>(result));
        }
예제 #17
0
        public Guid AuthenticateDevice(M2MgoCloudAgentConfiguration config, Guid deviceType, string customId, Guid gatewayID)
        {
            var result = _userTokenWebApiWrapper.RetryWithTokenUpdate(config, () =>
            {
                var request =
                    (HttpWebRequest)WebRequest.Create(config.BaseUrl
                                                      + "api/device/authentication"
                                                      + "/" + deviceType.ToString("D")
                                                      + "/" + customId
                                                      + "/" + gatewayID.ToString("D"));
                request.Method = "GET";
                return(_webApiRequestExecutor.ExecuteRequest(request, null, _userTokenWebApiWrapper.GetToken(),
                                                             _logger));
            });

            _logger.Information("AuthenticateDevice: " + result);
            return(_serializer.Deserialize <Guid>(result));
        }
예제 #18
0
        private GatewayProjectGetModel EnsureProject(M2MgoCloudAgentConfiguration config, UicProject project)
        {
            string result = _apiWrapper.GetGatewayProject(config, project);

            if (result.IsNullOrEmpty())
            {
                var model = new GatewayProjectPutModel(_projectTranslator.GetProjectDomain(project), config.SgetGatewayTypeId, project.Name, project.ProjectKey, "", project.GetType().ToString());
                _apiWrapper.CreateGatewayProject(config, model);
                result = _apiWrapper.GetGatewayProject(config, project);
                if (result.IsNullOrEmpty())
                {
                    throw new Exception("Could not create GatewayProject!");
                }
            }
            _logger.Information(result);
            var gatewayProjectGetModel = _serializer.Deserialize <GatewayProjectGetModel>(result);

            return(gatewayProjectGetModel);
        }
예제 #19
0
 public string PostAttributes(M2MgoCloudAgentConfiguration config, Guid deviceType, string customId, string attributeName, string value)
 {
     return(_userTokenWebApiWrapper.RetryWithTokenUpdate(config, () =>
     {
         var request =
             (HttpWebRequest)WebRequest.Create(config.BaseUrl
                                               + "api/device/attribute");
         request.Method = "POST";
         PostAttributeSingleValueModel
         attribute = new PostAttributeSingleValueModel
         {
             AttributeName = attributeName,
             DeviceName = customId,
             DeviceType = deviceType,
             Value = value
         };
         string serializeObject = _serializer.Serialize(attribute);
         return _webApiRequestExecutor.ExecuteRequest(request, serializeObject, _userTokenWebApiWrapper.GetToken(), _logger);
     }));
 }
예제 #20
0
        public string PostRelevantDevices(M2MgoCloudAgentConfiguration config, Guid gatewayGuid, IEnumerable <RelevantDevice> relevantDevices)
        {
            if (relevantDevices == null)
            {
                throw new ArgumentNullException("relevantDevices");
            }
            if (gatewayGuid == Guid.Empty)
            {
                throw new Exception("gateway guid must not be empty");
            }

            return(_userTokenWebApiWrapper.RetryWithTokenUpdate(config, () => {
                var request = (HttpWebRequest)WebRequest.Create(config.BaseUrl
                                                                + "api/gateway"
                                                                + "/" + gatewayGuid.ToString("D")
                                                                + "/relevantDevices");
                request.Method = "POST";
                string serializeObject = _serializer.Serialize(relevantDevices);
                return _webApiRequestExecutor.ExecuteRequest(request, serializeObject, _userTokenWebApiWrapper.GetToken(), _logger);
            }));
        }
예제 #21
0
        public string AddGatewayToProject(M2MgoCloudAgentConfiguration config, Guid gatewayGuid, Guid projectGuid)
        {
            if (gatewayGuid == Guid.Empty)
            {
                throw new Exception("gateway guid must not be empty");
            }
            if (projectGuid == Guid.Empty)
            {
                throw new Exception("project guid must not be empty");
            }

            return(_userTokenWebApiWrapper.RetryWithTokenUpdate(config, () => {
                var request = (HttpWebRequest)WebRequest.Create(config.BaseUrl
                                                                + "api/gateway"
                                                                + "/" + gatewayGuid.ToString("D")
                                                                + "/gatewayProject"
                                                                + "?gatewayProjectIdentifier=" + projectGuid.ToString("D"));
                request.Method = "POST";
                return _webApiRequestExecutor.ExecuteRequest(request, null, _userTokenWebApiWrapper.GetToken(), _logger);
            }));
        }
예제 #22
0
        private void SetRelevantDevicesOfGateway(M2MgoCloudAgentConfiguration config, Guid gatewayDevice, BlueprintDto projectBlueprint, string serialId)
        {
            var relevantSensors = projectBlueprint.Sensors.Select(s => new RelevantSensor
            {
                DisplayChartLeftAxis  = true,
                DisplayChartRightAxis = false,
                DisplayInTable        = true,
                Label     = s.Label,
                Path      = "",
                SensorKey = s.SensorKey
            }).ToArray();

            var relevantDevice = new RelevantDevice
            {
                DeviceCustomID   = serialId,
                DeviceTypeDomain = projectBlueprint.DomainIdentifier.ID,
                DeviceTypeName   = projectBlueprint.Code,
                RelevantSensors  = relevantSensors
            };

            _gatewayApiWrapper.PostRelevantDevices(config, gatewayDevice, new [] { relevantDevice });
        }
예제 #23
0
        public string CreateGateway(M2MgoCloudAgentConfiguration config, string serialId, GatewayProjectGetModel project)
        {
            if (serialId == null)
            {
                throw new ArgumentNullException("serialId");
            }
            if (project == null)
            {
                throw new ArgumentNullException("project");
            }

            return(_userTokenWebApiWrapper.RetryWithTokenUpdate(config, () =>
            {
                var request = (HttpWebRequest)WebRequest.Create(config.BaseUrl
                                                                + "api/gateway"
                                                                + "?gatewayTypeId=" + config.SgetGatewayTypeId.ToString("D")
                                                                + "&domainId=" + project.Domain.ID
                                                                + "&serial=" + serialId);
                request.Method = "PUT";
                return _webApiRequestExecutor.ExecuteRequest(request, null, _userTokenWebApiWrapper.GetToken(), _logger);
            }));
        }
예제 #24
0
        private BlueprintDto EnsureGatewayBlueprint(M2MgoCloudAgentConfiguration config, UicProject project)
        {
            BlueprintDto[] searchResult = _apiWrapper.SearchBlueprint(config, config.SgetDomainID, M2mgoGatewayBlueprintTranslator.CloudMapperGatewayAnchorBlueprintCode);

            if (searchResult.Count() > 1)
            {
                throw new Exception(String.Format("found more than 1 masterdefinitions {0} in Domain {1}", _projectTranslator.GetProjectDomain(project),
                                                  M2mgoGatewayBlueprintTranslator.CloudMapperGatewayAnchorBlueprintCode));
            }
            if (searchResult.Any())
            {
                _logger.Information("Gateway Blueprint {0} alredy exists.", M2mgoGatewayBlueprintTranslator.CloudMapperGatewayAnchorBlueprintCode);
                return(searchResult.Single());
            }
            BlueprintDto gatewayBlueprintDefiniton = _m2MgoGatewayBlueprintTranslator.GetGatewayBlueprintDefiniton(project);

            gatewayBlueprintDefiniton.PrepareMasterForCreation();

            string result = _apiWrapper.CreateBlueprint(config, _projectTranslator.GetProjectDomain(project), gatewayBlueprintDefiniton);

            _logger.Information("CreateBlueprint Blueprint: " + result);
            return(_apiWrapper.SearchBlueprint(config, _projectTranslator.GetProjectDomain(project), M2mgoGatewayBlueprintTranslator.CloudMapperGatewayAnchorBlueprintCode).Single());
        }
예제 #25
0
 private void EnsureDevice(M2MgoCloudAgentConfiguration config, string deviceIdentifier, BlueprintDto blueprint, GatewayGetModel gateway)
 {
     _apiWrapper.AuthenticateDevice(config, blueprint.Identifier.ID, deviceIdentifier, gateway.Identifier.ID);
 }
예제 #26
0
 private void UpdateProjectDeviceAttributes(M2MgoCloudAgentConfiguration config, string serialId, BlueprintDto projectBlueprintDto)
 {
     _apiWrapper.PostAttributes(config, projectBlueprintDto.Identifier.ID, serialId,
                                M2mgoGatewayBlueprintTranslator.AttributeKeySerialId, serialId);
 }