Exemplo n.º 1
0
        public async Task Handle(eFormCompleted message)
        {
            #region get case information

            WriteLogEntry($"eFormCompletedHandler.Handle: we got called for message.caseId {message.caseId} and message.checkId {message.checkId}");
            CaseDto caseDto = await _sdkCore.CaseLookup(message.caseId, message.checkId).ConfigureAwait(false);

            ReplyElement replyElement = await _sdkCore.CaseRead(message.caseId, message.checkId).ConfigureAwait(false);

            OuterInnerResourceSite machineAreaSite =
                _dbContext.OuterInnerResourceSites.SingleOrDefault(x =>
                                                                   x.MicrotingSdkCaseId == message.caseId);

            ResourceTimeRegistration machineAreaTimeRegistration =
                await _dbContext.ResourceTimeRegistrations.SingleOrDefaultAsync(x =>
                                                                                x.DoneAt == replyElement.DoneAt &&
                                                                                x.SDKCaseId == (int)caseDto.CaseId &&
                                                                                x.SDKSiteId == machineAreaSite.MicrotingSdkSiteId).ConfigureAwait(false);

            if (machineAreaTimeRegistration == null)
            {
                machineAreaTimeRegistration = new ResourceTimeRegistration();
                if (machineAreaSite != null)
                {
                    machineAreaTimeRegistration.OuterResourceId = machineAreaSite.OuterInnerResource.OuterResourceId;
                    machineAreaTimeRegistration.InnerResourceId = machineAreaSite.OuterInnerResource.InnerResourceId;
                    machineAreaTimeRegistration.DoneAt          = replyElement.DoneAt;
                    if (caseDto.CaseId != null)
                    {
                        machineAreaTimeRegistration.SDKCaseId = (int)caseDto.CaseId;
                    }
                    machineAreaTimeRegistration.SDKSiteId = machineAreaSite.MicrotingSdkSiteId;
                }

                CheckListValue dataElement = (CheckListValue)replyElement.ElementList[0];
                foreach (var field in dataElement.DataItemList)
                {
                    Field f = (Field)field;
                    if (f.Label.ToLower().Contains("start/stop tid"))
                    {
                        Console.WriteLine($"The field is {f.Label}");
                        FieldValue fv         = f.FieldValues[0];
                        String     fieldValue = fv.Value;
                        Console.WriteLine($"Current field_value is {fieldValue}");
                        int registeredTime = int.Parse(fieldValue.Split("|")[3]);
                        Console.WriteLine($"We are setting the registered time to {registeredTime.ToString()}");

                        machineAreaTimeRegistration.SDKFieldValueId = fv.Id;
                        machineAreaTimeRegistration.TimeInSeconds   = (registeredTime / 1000);
                        machineAreaTimeRegistration.TimeInMinutes   = ((registeredTime / 1000) / 60);
                        machineAreaTimeRegistration.TimeInHours     = ((registeredTime / 1000) / 3600);
                    }
                }
                #endregion

                await machineAreaTimeRegistration.Create(_dbContext).ConfigureAwait(false);
            }
        }
Exemplo n.º 2
0
        public void MachineAreaTimeRegistration_Create_DoesCreate()
        {
            //Arrange

            OuterResource outerResource = new OuterResource()
            {
                Name = Guid.NewGuid().ToString()
            };

            outerResource.Create(DbContext);

            InnerResource innerResource = new InnerResource()
            {
                Name = Guid.NewGuid().ToString()
            };

            innerResource.Create(DbContext);

            Random rnd = new Random();
            ResourceTimeRegistration matr = new ResourceTimeRegistration();

            matr.DoneAt          = DateTime.Now;
            matr.TimeInHours     = 10;
            matr.TimeInMinutes   = 60;
            matr.TimeInSeconds   = 100;
            matr.SDKCaseId       = rnd.Next(1, 100);
            matr.SDKSiteId       = rnd.Next(1, 100);
            matr.SDKFieldValueId = rnd.Next(1, 100);
            matr.OuterResourceId = outerResource.Id;
            matr.InnerResourceId = innerResource.Id;

            //Act

            matr.Create(DbContext);


            ResourceTimeRegistration        dbMatr   = DbContext.ResourceTimeRegistrations.AsNoTracking().First();
            List <ResourceTimeRegistration> matrList = DbContext.ResourceTimeRegistrations.AsNoTracking().ToList();

            //Assert

            Assert.NotNull(dbMatr);

            Assert.AreEqual(matr.DoneAt.ToString(), dbMatr.DoneAt.ToString());
            Assert.AreEqual(matr.TimeInHours, dbMatr.TimeInHours);
            Assert.AreEqual(matr.TimeInMinutes, dbMatr.TimeInMinutes);
            Assert.AreEqual(matr.TimeInSeconds, dbMatr.TimeInSeconds);
            Assert.AreEqual(matr.SDKCaseId, dbMatr.SDKCaseId);
            Assert.AreEqual(matr.SDKSiteId, dbMatr.SDKSiteId);
            Assert.AreEqual(matr.SDKFieldValueId, dbMatr.SDKFieldValueId);
            Assert.AreEqual(matr.OuterResourceId, dbMatr.OuterResourceId);
            Assert.AreEqual(matr.InnerResourceId, dbMatr.InnerResourceId);

            Assert.AreEqual(1, matrList.Count());
        }
        public async Task Handle(eFormCompleted message)
        {
            WriteLogEntry($"eFormCompletedHandler.Handle: we got called for message.caseId {message.caseId} and message.checkId {message.checkId}");
            await using MicrotingDbContext microtingDbContext = _sdkCore.DbContextHelper.GetDbContext();
            Language language = await microtingDbContext.Languages.SingleAsync(x => x.LanguageCode == "da");

            CaseDto caseDto = await _sdkCore.CaseLookup(message.caseId, message.checkId).ConfigureAwait(false);

            ReplyElement replyElement = await _sdkCore.CaseRead(message.caseId, message.checkId, language).ConfigureAwait(false);

            OuterInnerResourceSite machineAreaSite =
                _dbContext.OuterInnerResourceSites.SingleOrDefault(x =>
                                                                   x.MicrotingSdkCaseId == message.caseId);

            var machineAreaTimeRegistrations =
                await _dbContext.ResourceTimeRegistrations.Where(x =>
                                                                 // x.DoneAt == replyElement.DoneAt &&
                                                                 x.SDKCaseId == (int)caseDto.CaseId &&
                                                                 x.SDKSiteId == machineAreaSite.MicrotingSdkSiteId).ToListAsync().ConfigureAwait(false);

            if (machineAreaTimeRegistrations.Count == 0)
            {
                ResourceTimeRegistration machineAreaTimeRegistration = new ResourceTimeRegistration();
                if (machineAreaSite != null)
                {
                    var outerInnerResource =
                        await _dbContext.OuterInnerResources.SingleOrDefaultAsync(x =>
                                                                                  x.Id == machineAreaSite.OuterInnerResourceId);

                    machineAreaTimeRegistration.OuterResourceId = outerInnerResource.OuterResourceId;
                    machineAreaTimeRegistration.InnerResourceId = outerInnerResource.InnerResourceId;
                    machineAreaTimeRegistration.DoneAt          = replyElement.DoneAt;
                    if (caseDto.CaseId != null)
                    {
                        machineAreaTimeRegistration.SDKCaseId = (int)caseDto.CaseId;
                    }
                    machineAreaTimeRegistration.SDKSiteId = machineAreaSite.MicrotingSdkSiteId;
                }

                CheckListValue dataElement = (CheckListValue)replyElement.ElementList[0];
                foreach (var field in dataElement.DataItemList)
                {
                    Field f = (Field)field;
                    if (f.Label.ToLower().Contains("start/stop tid"))
                    {
                        try
                        {
                            Console.WriteLine($"The field is {f.Label}");
                            FieldValue fv         = f.FieldValues[0];
                            String     fieldValue = fv.Value;
                            if (!string.IsNullOrEmpty(fieldValue))
                            {
                                Console.WriteLine($"Current field_value is {fieldValue}");
                                int registeredTime = int.Parse(fieldValue.Split("|")[3]);
                                Console.WriteLine($"We are setting the registered time to {registeredTime.ToString()}");

                                machineAreaTimeRegistration.SDKFieldValueId = fv.Id;
                                machineAreaTimeRegistration.TimeInSeconds   = (registeredTime / 1000);
                                machineAreaTimeRegistration.TimeInMinutes   = ((registeredTime / 1000) / 60);
                                machineAreaTimeRegistration.TimeInHours     = ((registeredTime / 1000) / 3600);
                            }
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine(ex.Message);
                        }
                    }
                }

                await machineAreaTimeRegistration.Create(_dbContext).ConfigureAwait(false);
            }
            else
            {
                if (machineAreaTimeRegistrations.Count > 1)
                {
                    int i = 0;
                    foreach (ResourceTimeRegistration machineAreaTimeRegistration in machineAreaTimeRegistrations)
                    {
                        if (i > 0)
                        {
                            await machineAreaTimeRegistration.Delete(_dbContext);
                        }

                        i++;
                        Console.WriteLine("More than one time registration found");
                    }
                }
                else
                {
                    Console.WriteLine("One time registration found");
                }
            }
        }