File size: 4,483 Bytes
0936134
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
'use strict'

const benchmark = require('./utils/stats-benchmark')
const Emitter = require('tiny-emitter')

function Stats (job, config) {
  let cmds = parse(job.buffer)
  let distance = calcDistance(cmds)
  let duration = calcDuration(cmds)

  const em = new Emitter()
  const api = {
    emit: em.emit.bind(em),
    on: em.on.bind(em),
    once: em.once.bind(em),
    off: em.off.bind(em),

    get benchmark () { return benchmark },
    get distance () { return distance },
    get duration () { return duration },

    refresh: () => {
      cmds = parse(job.buffer)
      distance = calcDistance(cmds)
      duration = calcDuration(cmds)
      return api
    },

    pretty: (emoji = true) => {
      return `${emoji ? '❓  ' : ''}${job.name}` + '\n' +
             `${emoji ? '✏️  ' : ''}${distance} mm` + '\n' +
             `${emoji ? '⏳  ' : ''}${duration.estimation.formatted} (${duration.min.formatted} ~ ${duration.max.formatted})`
    }
  }

  return api

  function calcDistance (cmds) {
    let dist = 0
    let px = 0
    let py = 0
    for (let i = 0; i < cmds.length; i++) {
      let cmd = cmds[i]
      if (cmd && cmd.instruction === 'G') {
        let x = cmd.x
        let y = cmd.y
        dist += Math.sqrt((x - px) * (x - px) + (y - py) * (y - py))
        px = x
        py = y
      }
    }
    return round(dist, 3)
  }

  function calcDuration (cmds) {
    let duration = 0
    let servodistance = 0
    let servopos = 0
    let variation = {min: 0, max: 0}
    let dist = 0
    let px = 0
    let py = 0
    let speed

    for (let i = 0; i < cmds.length; i++) {
      let cmd = cmds[i]
      if (cmd) {
        if (cmd.instruction === 'G') {
          let x = cmd.x
          let y = cmd.y
          dist += Math.sqrt((x - px) * (x - px) + (y - py) * (y - py))
          px = x
          py = y
        } else if (cmd.instruction === 'S') {
          duration += calc(dist, speed)

          if (!speed) {
            variation.min += dist / benchmark.speed.values[0].max
            variation.max += dist / benchmark.speed.values[0].min
            speed = null
          } else {
            // @SEE Plotter.Job.setSpeed() for the equation
            speed = (1100 - cmd.speed) / 1000
          }
          dist = 0
        } else if (cmd.instruction === 'M') {
          servodistance += Math.abs(servopos - cmd.position)
          servopos = cmd.position
        }
      }
    }

    if (dist > 0) duration += calc(dist, speed)
    if (servodistance > 0) duration += servodistance * 15 / 1000 // 15ms, @SEE firmware.ino

    function calc (dist, speed) {
      if (speed) return dist / benchmark.speed.interpolate(speed)
      else return dist / benchmark.speed.values[0].mmPerSecond
    }

    function secondToTime (seconds) {
      let date = new Date(null)
      date.setSeconds(seconds)
      return date.toISOString().substr(11, 8)
    }

    duration = round(duration, 0)
    variation.min = round(variation.min, 0)
    variation.max = round(variation.max, 0)

    return {
      estimation: {
        value: duration,
        formatted: secondToTime(duration)
      },
      min: {
        value: variation.min ? variation.min : duration - duration * 0.1,
        formatted: variation.min ? secondToTime(variation.min) : secondToTime(duration - duration * 0.1)
      },
      max: {
        value: variation.max ? variation.max : duration + duration * 0.1,
        formatted: variation.max ? secondToTime(variation.max) : secondToTime(duration + duration * 0.1)
      }
    }
  }

  function parse (buff) {
    let cmds = []
    for (let i = 0; i < buff.length; i++) {
      let cmd = parseCMD(buff[i])
      cmds.push(cmd)
    }

    return cmds
  }

  function parseCMD (message) {
    let params = message.split(' ')
    if (params[0] === ('G1')) {
      let cmd = {
        instruction: 'G',
        x: parseFloat(params[1].split('X').pop()),
        y: parseFloat(params[2].split('Y').pop())
      }
      return cmd
    } else if (params[0] === 'M1') {
      let cmd = {
        instruction: 'M',
        position: params[1]
      }
      return cmd
    } else if (params[0] === 'S0') {
      let cmd = {
        instruction: 'S',
        speed: null
      }
      return cmd
    } else if (params[0] === 'S1') {
      let cmd = {
        instruction: 'S',
        speed: parseFloat(params[1])
      }
      return cmd
    } else return null
  }

  function round (a, d = 3) { return +a.toFixed(d) }
}

module.exports = Stats