Пример #1
0
        public Task <MsgUnsubscribeSensorsResponse> UnsubscribeSensorsAsync(MsgUnsubscribeSensors unsubscribeRequest, bool bypassCapabilityCheck)
        {
            if (!bypassCapabilityCheck && !this.Capabilities.Contains(eThingCaps.SensorProvider))
            {
                return(TheCommonUtils.TaskFromResult(new MsgUnsubscribeSensorsResponse {
                    Error = "Thing is not a sensor provider"
                }));
            }
            ;
            var unsubscribeResponseTask = TheCommRequestResponse.PublishRequestJSonAsync <MsgUnsubscribeSensors, MsgUnsubscribeSensorsResponse>(this, unsubscribeRequest);

            return(unsubscribeResponseTask);
        }
Пример #2
0
        public Task <MsgGetSensorSubscriptionsResponse> GetSensorProviderSubscriptionsAsync(MsgGetSensorSubscriptions getSubscriptionsRequest, bool bypassCapabilityCheck)
        {
            if (!bypassCapabilityCheck && !this.Capabilities.Contains(eThingCaps.SensorProvider))
            {
                return(TheCommonUtils.TaskFromResult(new MsgGetSensorSubscriptionsResponse {
                    Error = "Thing is not a sensor provider"
                }));
            }
            ;
            var getSubscriptionResponseTask = TheCommRequestResponse.PublishRequestJSonAsync <MsgGetSensorSubscriptions, MsgGetSensorSubscriptionsResponse>(this, getSubscriptionsRequest);

            return(getSubscriptionResponseTask);
        }
Пример #3
0
        public Task <MsgSubscribeSensorsResponse <subscriptionT> > SubscribeSensorsAsync <subscriptionT>(MsgSubscribeSensors <subscriptionT> subscribeRequest, bool bypassCapabilityCheck) where subscriptionT : TheSensorSubscription
        {
            if (!bypassCapabilityCheck && !this.Capabilities.Contains(eThingCaps.SensorProvider))
            {
                return(TheCommonUtils.TaskFromResult(new MsgSubscribeSensorsResponse <subscriptionT> {
                    Error = "Thing is not a sensor provider"
                }));
            }
            ;
            var subscribeResponseTask = TheCommRequestResponse.PublishRequestJSonAsync <TheThing.MsgSubscribeSensors <subscriptionT>, TheThing.MsgSubscribeSensorsResponse <subscriptionT> >(this, subscribeRequest);

            return(subscribeResponseTask);
        }
Пример #4
0
        public Task <MsgBrowseSensorsResponse> ProviderBrowseSensorsAsync(TheThing.MsgBrowseSensors browseRequest, bool bypassCapabilityCheck)
        {
            if (!bypassCapabilityCheck && !this.Capabilities.Contains(eThingCaps.SensorProvider))
            {
                return(TheCommonUtils.TaskFromResult(new MsgBrowseSensorsResponse {
                    Error = "Thing is not a sensor provider"
                }));
            }
            ;
            var browseResponseTask = TheCommRequestResponse.PublishRequestJSonAsync <TheThing.MsgBrowseSensors, TheThing.MsgBrowseSensorsResponse>(this, browseRequest);

            return(browseResponseTask);
        }
Пример #5
0
        private static Task <IEnumerable <object> > LoadThingUpdatesAsync(string fileName)
        {
            if (fileName.ToLowerInvariant() != "meshsenderdata.log") // We allow this filename so one can use the immediate local output from the mesh sender, all others are sanitized and must be effectively uploaded via the file uploader
            {
                fileName = TheCommonUtils.cdeFixupFileName(fileName);
            }
            var fileContent = File.ReadAllText(fileName, Encoding.UTF8);

            if (!fileContent.StartsWith("["))
            {
                fileContent = "[" + fileContent + "]";
            }
            var logEntries = TheCommonUtils.DeserializeJSONStringToObject <List <Dictionary <string, object> > >(fileContent);

            return(TheCommonUtils.TaskFromResult(logEntries.Where(e => e.ContainsKey("PLS")).Select(e => e["PLS"])));
        }
Пример #6
0
        public static Task <MsgApplyPipelineConfigResponse> ApplyPipelineConfigAsync(MsgApplyPipelineConfig request)
        {
            var pipeLineTasks = ApplyPipelineConfigJsonInternal(request.Config, request.AnswerConfigs, null, null);

            if (pipeLineTasks == null)
            {
                return(TheCommonUtils.TaskFromResult(new MsgApplyPipelineConfigResponse
                {
                    Error = "Error applying configuration",
                }));
            }

            return(TheCommonUtils.TaskWhenAll(pipeLineTasks.Select(t => (Task)t))
                   .ContinueWith(t =>
            {
                bool bSuccess = true;
                int failedFileCount = 0;
                var thingsConfigured = new List <TheThingIdentity>();
                foreach (var task in pipeLineTasks)
                {
                    if (task.IsFaulted)
                    {
                        failedFileCount++;
                        bSuccess = false;
                        thingsConfigured.Add(null);
                    }
                    else
                    {
                        var thingIdentities = task.Result?.Select(thing => new TheThingIdentity(thing));
                        if (thingIdentities != null)
                        {
                            thingsConfigured.AddRange(thingIdentities);
                        }
                        else
                        {
                            thingsConfigured.Add(null);
                        }
                    }
                }
                return new MsgApplyPipelineConfigResponse {
                    Error = bSuccess ? null : "Error applying configuration", ThingsConfigured = thingsConfigured,
                };
            }));
        }
Пример #7
0
        internal static Task <ApplyConfigFilesResult> ApplyConfigurationFilesAsync()
        {
            bool bSuccess        = true;
            int  fileCount       = 0;
            int  failedFileCount = 0;
            var  pipeLineTasks   = new List <Task <List <TheThing> > >();

            try
            {
                var configDir = TheCommonUtils.cdeFixupFileName(Path.Combine("ClientBin", "config"));
                if (!Directory.Exists(configDir))
                {
                    return(TheCommonUtils.TaskFromResult(new ApplyConfigFilesResult
                    {
                        Success = true,
                    }));
                }

                var configDirs = Directory.GetParent(configDir).GetDirectories("config");
                if (configDirs.Length != 1 || configDirs[0].FullName != configDir)
                {
                    TheBaseAssets.MySYSLOG.WriteToLog(7721, TSM.L(eDEBUG_LEVELS.OFF) ? null : new TSM(eEngineName.ThingService, "ClientBin/config directory has invalid casing. Must be lower case.", eMsgLevel.l1_Error));
                    return(TheCommonUtils.TaskFromResult(new ApplyConfigFilesResult
                    {
                        Success = false,
                    }));
                }

                var configFiles = Directory.GetFiles(configDir, "*.cdeconfig");
                fileCount = configFiles.Length;
                foreach (var configFile in configFiles)
                {
                    var pipelineJson = File.ReadAllText(configFile);
                    pipelineJson = TheCommonUtils.GenerateFinalStr(pipelineJson);
                    var pipelineConfig = TheCommonUtils.DeserializeJSONStringToObject <ThePipelineConfiguration>(pipelineJson);

                    var answerFileNames = Directory.GetFiles(configDir, $"{Path.GetFileNameWithoutExtension(configFile)}*.cdeanswer");
                    var answerConfigs   = answerFileNames.Select(af =>
                    {
                        var answerJson   = File.ReadAllText(af);
                        answerJson       = TheCommonUtils.GenerateFinalStr(answerJson);
                        var answerConfig = TheCommonUtils.DeserializeJSONStringToObject <ThePipelineConfiguration>(answerJson);
                        return(answerConfig);
                    });
                    var tasks = ApplyPipelineConfigJsonInternal(pipelineConfig, answerConfigs, configFile, answerFileNames);
                    if (tasks?.Count == 0)
                    {
                        failedFileCount++;
                    }
                    pipeLineTasks.AddRange(tasks);
                }
            }
            catch (Exception e)
            {
                TheBaseAssets.MySYSLOG.WriteToLog(7722, TSM.L(eDEBUG_LEVELS.OFF) ? null : new TSM(eEngineName.ThingService, "Error processing config files", eMsgLevel.l1_Error, $"{e.Message}"));
                bSuccess = false;
            }

            return(TheCommonUtils.TaskWhenAll(pipeLineTasks.Select(t => (Task)t))
                   .ContinueWith(t =>
            {
                foreach (var task in pipeLineTasks)
                {
                    if (task.IsFaulted)
                    {
                        failedFileCount++;
                        bSuccess = false;
                    }
                }
                return new ApplyConfigFilesResult {
                    Success = bSuccess, NumberOfFiles = fileCount, NumberOfFailedFiles = failedFileCount
                };
            }));
        }