public MachineErrorData GetMachineErrorData() { MachineErrorData data = new MachineErrorData(); data.ErrorInfos = new List <MachineErrorItem>(); foreach (KeyValuePair <int, SimMachine> d in _dicSimMachine) { int machineID = d.Key; SimMachine simMachine = d.Value; if (simMachine.IsExistErrorData == true) { MachineErrorItem item = simMachine.GetMachineErrorData(); data.ErrorInfos.Add(item); simMachine.IsExistErrorData = false; } } return(data); }
private void DoWork() { int state_tick = 0; int count = 0; int checkCount = CYCLE_LOOP_INTERVAL / 100; while (!_shouldStop) { if (count < checkCount) { // Wait Thread.Sleep(100); count++; continue; } count = 0; SetMotorCurrent(_currentState); switch (_currentState) { case State.IDLE: { if (IsExistErrorData == true) { continue; } Random rnd = new Random(unchecked ((int)DateTime.Now.Ticks + _machineID)); int randomStateVal = rnd.Next(0, 10); if (randomStateVal == 2) { InitCycleData(); int rndErrorVal = rnd.Next(0, 100); if (rndErrorVal == 38) { // go to error state _cycleErrorPeriod = rnd.Next(30, 50); // Make Error Data CycleErrorData = new MachineErrorItem(); CycleErrorData.MachineID = _machineID; CycleErrorData.MachineType = "SC"; CycleErrorData.JobID = CycleJobID; CycleErrorData.TimeStamp = DateTime.UtcNow.ToString("o"); CycleErrorData.ErrCode = rnd.Next(1, 300); CycleErrorData.ErrMsg = @"Simulation Error Message"; IsExistErrorData = true; _currentState = State.ERROR; state_tick = 0; } else { IsCycleCompleted = false; CycleTotalStartTime = DateTime.UtcNow.ToString("o"); CycleFirstDrivingInfo.MoveStartTime = DateTime.UtcNow.ToString("o"); DateTime hoistStartTime = DateTime.UtcNow.AddSeconds((rnd.Next(2, 3))); CycleFirstHoistingInfo.MoveStartTime = hoistStartTime.ToString("o"); _currentState = State.FIRST_DRIVING; state_tick = 0; } } } break; case State.FIRST_DRIVING: // Driving + Hoisting { if (state_tick >= _cycleFirstDrivingPeriod) { Random rnd = new Random((int)DateTime.Now.Ticks); // Driving DateTime drivingEndTime = DateTime.UtcNow.AddSeconds(-(rnd.Next(1, 3))); CycleFirstDrivingInfo.MoveEndTime = drivingEndTime.ToString("o"); LastDrivingMoveDistance += rnd.Next(100, 2000); // 1 ~ 20m CycleFirstDrivingInfo.MoveDistance = LastDrivingMoveDistance; LastDrivingMotorPowerConsumption += rnd.Next(230, 2000); // 230 W ~ 2KW CycleFirstDrivingInfo.MotorPowerConsumption = LastDrivingMotorPowerConsumption; LastDrivingMotorBreakCount += rnd.Next(1, 3); CycleFirstDrivingInfo.MotorBreakCount = LastDrivingMotorBreakCount; LastDrivingBreakMCCount += rnd.Next(1, 3); CycleFirstDrivingInfo.BreakMCCount = LastDrivingBreakMCCount; // Upper Driving LastUppperDrivingBreakDiscCount += rnd.Next(1, 3); CycleUpperDrivingInfo.BreakDiscCount = LastUppperDrivingBreakDiscCount; LastUppperDrivingBreakRollerCount += rnd.Next(1, 3); CycleUpperDrivingInfo.BreakRollerCount = LastUppperDrivingBreakRollerCount; LastUppperDrivingBreakMCCount += rnd.Next(1, 3); CycleUpperDrivingInfo.BreakMCCount = LastUppperDrivingBreakMCCount; LastUppperDrivingBreakRectifierCount += rnd.Next(1, 3); CycleUpperDrivingInfo.BreakRectifierCount = LastUppperDrivingBreakRectifierCount; // Hoisting CycleFirstHoistingInfo.MoveEndTime = DateTime.UtcNow.ToString("o"); LastHoistingMoveDistance += rnd.Next(100, 1000); // 1 ~ 10m CycleFirstHoistingInfo.MoveDistance = LastHoistingMoveDistance; LastHoistingMotorPowerConsumption += rnd.Next(530, 4000); // 530 W ~ 4KW CycleFirstHoistingInfo.MotorPowerConsumption = LastHoistingMotorPowerConsumption; LastHoistingMotorBreakCount += rnd.Next(1, 3); CycleFirstHoistingInfo.MotorBreakCount = LastHoistingMotorBreakCount; LastHoistingBreakMCCount += rnd.Next(1, 3); CycleFirstHoistingInfo.BreakMCCount = LastHoistingBreakMCCount; // Next CycleForkInfo.MoveStartTime = DateTime.UtcNow.ToString("o"); state_tick = 0; _currentState = State.FORK; } } break; case State.FORK: { if (state_tick >= _cycleForkPeriod) { Random rnd = new Random((int)DateTime.Now.Ticks + 1); // Fork CycleForkInfo.MoveEndTime = DateTime.UtcNow.ToString("o"); LastForkMoveDistance += rnd.Next(50, 200); // 50cm ~ 2m CycleForkInfo.MoveDistance = LastForkMoveDistance; LastForkMotorPowerConsumption += rnd.Next(50, 500); // 230 W ~ 2KW CycleForkInfo.MotorPowerConsumption = LastForkMotorPowerConsumption; LastForkMotorBreakCount += rnd.Next(1, 3); CycleForkInfo.MotorBreakCount = LastForkMotorBreakCount; LastForkBreakMCCount += rnd.Next(1, 3); CycleForkInfo.BreakMCCount = LastForkBreakMCCount; // Next DateTime drivingStartTime = DateTime.UtcNow.AddSeconds(rnd.Next(1, 3)); CycleSecondDrivingInfo.MoveStartTime = drivingStartTime.ToString("o"); CycleSecondHoistingInfo.MoveStartTime = drivingStartTime.ToString("o"); state_tick = 0; _currentState = State.SECOND_DRIVING; } } break; case State.SECOND_DRIVING: // Return { if (state_tick >= _cycleSecondDrivingPeriod) { Random rnd = new Random((int)DateTime.Now.Ticks); // Driving CycleSecondDrivingInfo.MoveEndTime = DateTime.UtcNow.ToString("o"); LastDrivingMoveDistance += rnd.Next(100, 2000); // 1 ~ 20m CycleSecondDrivingInfo.MoveDistance = LastDrivingMoveDistance; LastDrivingMotorPowerConsumption += rnd.Next(130, 2000); // 130 W ~ 1KW CycleSecondDrivingInfo.MotorPowerConsumption = LastDrivingMotorPowerConsumption; LastDrivingMotorBreakCount += rnd.Next(1, 3); CycleSecondDrivingInfo.MotorBreakCount = LastDrivingMotorBreakCount; LastDrivingBreakMCCount += rnd.Next(1, 3); CycleSecondDrivingInfo.BreakMCCount = LastDrivingBreakMCCount; // Hoisting DateTime hoistEndTime = DateTime.UtcNow.AddSeconds(-(rnd.Next(2, 3))); CycleSecondHoistingInfo.MoveEndTime = hoistEndTime.ToString("o"); LastHoistingMoveDistance += rnd.Next(100, 1000); // 1 ~ 10m CycleSecondHoistingInfo.MoveDistance = LastHoistingMoveDistance; LastHoistingMotorPowerConsumption += rnd.Next(230, 2000); // 230 W ~ 2KW CycleSecondHoistingInfo.MotorPowerConsumption = LastHoistingMotorPowerConsumption; LastHoistingMotorBreakCount += rnd.Next(1, 3); CycleSecondHoistingInfo.MotorBreakCount = LastHoistingMotorBreakCount; LastHoistingBreakMCCount += rnd.Next(1, 3); CycleSecondHoistingInfo.BreakMCCount = LastHoistingBreakMCCount; CycleTotalEndTime = DateTime.UtcNow.ToString("o"); DateTime startT = DateTimeOffset.Parse(CycleTotalStartTime).DateTime; DateTime endT = DateTimeOffset.Parse(CycleTotalEndTime).DateTime; TimeSpan diff = endT.Subtract(startT); LastLaserDistanceMeterTotalUsedTime += (int)diff.TotalSeconds; CycleLaserDistanceMeterTotalUsedTime = LastLaserDistanceMeterTotalUsedTime; LastOpticalRepeaterTotalUsedTime += ((int)diff.TotalSeconds + 1); CycleOpticalRepeaterTotalUsedTime = LastOpticalRepeaterTotalUsedTime; CycleWeight = rnd.Next(10, 3000); // 10 ~ 3000 kg if (CycleJobType == 0) { LastInventoryCount++; } else { LastInventoryCount--; } CycleInventoryCount = LastInventoryCount; SaveLastMachineData(); IsCycleCompleted = true; state_tick = 0; _currentState = State.IDLE; } } break; case State.ERROR: { if (state_tick >= _cycleErrorPeriod) { state_tick = 0; _currentState = State.IDLE; } } break; } Trace.WriteLine(string.Format("MachineID : {0} , State : {1}", _machineID, _currentState.ToString())); //PrintCurrent(); state_tick++; } }