Пример #1
0
        /// <summary>
        /// Initializes a new session and adds it to the dictionary. Does not begin streaming data.
        /// </summary>
        /// <param name="args"></param>
        /// <returns>A string container with the SessionKey or a failure message</returns>
        public static Container <string> InitializeSession(SessionInitializer args)
        {
            var validation = args.Validate();

            if (validation.Success)
            {
                // Construct new session from args
                var session = new Session(GetRandomKey().Data[0], args);
                // Create instrument on CHORDS and set session's instrument ID
                var createInstContainer = ChordsBot.CreateInstrument(session.SessionKey);
                if (!createInstContainer.Success)
                {
                    return(new Container <string>("", false, createInstContainer.Message));
                }
                int id = createInstContainer.Data[0];
                session.SetInstrument(id);
                // Map session streams to CHORDS variables
                var confVarsContainer = ChordsBot.ConfigureVariables(session);
                if (!confVarsContainer.Success)
                {
                    return(new Container <string>("", false, confVarsContainer.Message));
                }
                // Add session to dict
                SessionDict.Add(session.SessionKey, session);
                return(new Container <string>(session.SessionKey, true));
            }
            else
            {
                return(new Container <string>("", false, validation.Message));
            }
        }
        public void ValidateTest()
        {
            var sinit = new SessionInitializer();

            // Valid data - null endtime
            sinit.StreamIDs = new int[] { 1, 2, 3 };
            sinit.StartTime = DateTime.UtcNow.AddHours(-1).ToString("s");
            sinit.EndTime   = null;

            Assert.IsTrue(sinit.Validate().Success);

            // Valid - provided endtime
            sinit.EndTime = DateTime.UtcNow.ToString("s");

            Assert.IsTrue(sinit.Validate().Success);

            // Null IDs
            var ids = sinit.StreamIDs;

            sinit.StreamIDs = null;

            Assert.IsFalse(sinit.Validate().Success);

            // Null startTime
            var start = sinit.StartTime;

            sinit.StreamIDs = ids;
            sinit.StartTime = null;

            Assert.IsFalse(sinit.Validate().Success);

            // Invalid ID
            sinit.StartTime    = start;
            sinit.StreamIDs[0] = -1;

            Assert.IsFalse(sinit.Validate().Success);

            // StartTime > EndTime
            sinit.StreamIDs[0] = 1;
            sinit.StartTime    = DateTime.UtcNow.AddHours(1).ToString("s");

            Assert.IsFalse(sinit.Validate().Success);
        }