Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import QtQuick 2.15
- // Simulates fix amount of Tasks and their activity (random sleep and work)
- Item
- {
- id:taskSimulation
- //public properties:
- property ListModel taskModel: taskModel
- property int ticks: 0
- property int taskCount: 0
- property int intervalInMsec: 100
- //private stuff:
- ListModel {
- id: taskModel
- //[taskName, ranges{[{start, duration},...]},...]
- }
- ListModel {
- id: simModel
- }
- function getRandomInt(min, max) {
- min = Math.ceil(min);
- max = Math.floor(max);
- return Math.floor(Math.random() * (max - min + 1)) + min;
- }
- readonly property int openState: 0
- readonly property int closeState: 1
- function start()
- {
- for (var i = 0; i < taskCount; i++)
- {
- var ranges = []
- taskModel.append({"taskName": "Task"+i.toString(), "ranges":ranges});
- simModel.append({"waitFor": openState, "wait":getRandomInt(3,5)});
- }
- simTimer.running = true
- }
- function proceed()
- {
- for (var i = 0; i < taskModel.count; i++)
- {
- var sim = simModel.get(i);
- var task = taskModel.get(i)
- var task_ranges = task.ranges
- sim.wait = sim.wait - 1;
- if(sim.wait === 0)
- {
- if( sim.waitFor === closeState)
- {
- sim.waitFor = openState;
- }
- else if( sim.waitFor === openState)
- {
- task_ranges.append({"start":ticks, "duration":0})
- sim.waitFor = closeState;
- }
- sim.wait = getRandomInt(10,25);
- }
- else
- {
- if( sim.waitFor === closeState)
- {
- var rangeCount = task_ranges.count
- if(rangeCount > 0)
- {
- var range = task_ranges.get(rangeCount-1)
- var duration = range.duration
- var new_duration = duration+1
- range.duration = new_duration
- }
- }
- }
- }
- }
- Timer {
- id: simTimer
- interval: intervalInMsec
- running: false
- repeat: true
- onTriggered: {
- ticks = ticks + 1
- proceed()
- }
- }
- }
Add Comment
Please, Sign In to add comment