|
| 1 | +var doesProtocolMatch, helper, protocols, sortIndices; |
| 2 | + |
| 3 | +helper = require('./helper'); |
| 4 | + |
| 5 | +protocols = ['weather1', 'weather2', 'switch1', 'switch2', 'switch3', 'switch4']; |
| 6 | + |
| 7 | +protocols = protocols.map((function(_this) { |
| 8 | + return function(p) { |
| 9 | + return require("../protocols/" + p + ".coffee")(helper); |
| 10 | + }; |
| 11 | +})(this)); |
| 12 | + |
| 13 | +doesProtocolMatch = function(pulseLengths, pulses, protocol) { |
| 14 | + var i, maxDelta; |
| 15 | + if (pulses.length !== protocol.pulseCount) { |
| 16 | + return false; |
| 17 | + } |
| 18 | + if (pulseLengths.length !== protocol.pulseLengths.length) { |
| 19 | + return false; |
| 20 | + } |
| 21 | + i = 0; |
| 22 | + while (i < pulseLengths.length) { |
| 23 | + maxDelta = pulseLengths[i] * 0.25; |
| 24 | + if (Math.abs(pulseLengths[i] - protocol.pulseLengths[i]) > maxDelta) { |
| 25 | + return false; |
| 26 | + } |
| 27 | + i++; |
| 28 | + } |
| 29 | + return true; |
| 30 | +}; |
| 31 | + |
| 32 | +sortIndices = function(array) { |
| 33 | + var e, i, indices, j, tuple, tuples, _i, _j, _len, _len1; |
| 34 | + tuples = new Array(array.length); |
| 35 | + for (i = _i = 0, _len = array.length; _i < _len; i = ++_i) { |
| 36 | + e = array[i]; |
| 37 | + tuples[i] = [e, i]; |
| 38 | + } |
| 39 | + tuples.sort(function(left, right) { |
| 40 | + if (left[0] < right[0]) { |
| 41 | + return -1; |
| 42 | + } else { |
| 43 | + return 1; |
| 44 | + } |
| 45 | + }); |
| 46 | + indices = new Array(array.length); |
| 47 | + for (j = _j = 0, _len1 = tuples.length; _j < _len1; j = ++_j) { |
| 48 | + tuple = tuples[j]; |
| 49 | + indices[tuple[1]] = j; |
| 50 | + } |
| 51 | + return indices; |
| 52 | +}; |
| 53 | + |
| 54 | +module.exports = { |
| 55 | + compressTimings: function(timings) { |
| 56 | + var bucket, buckets, counts, hasMatch, i, j, pulses, sums, timing, _i, _j, _k, _len, _len1, _len2; |
| 57 | + pulses = ''; |
| 58 | + buckets = []; |
| 59 | + sums = []; |
| 60 | + counts = []; |
| 61 | + for (i = _i = 0, _len = timings.length; _i < _len; i = ++_i) { |
| 62 | + timing = timings[i]; |
| 63 | + hasMatch = false; |
| 64 | + for (j = _j = 0, _len1 = buckets.length; _j < _len1; j = ++_j) { |
| 65 | + bucket = buckets[j]; |
| 66 | + if (Math.abs(bucket - timing) < bucket * 0.5) { |
| 67 | + pulses += j; |
| 68 | + sums[j] += timing; |
| 69 | + counts[j]++; |
| 70 | + hasMatch = true; |
| 71 | + } |
| 72 | + } |
| 73 | + if (!hasMatch) { |
| 74 | + pulses += buckets.length; |
| 75 | + buckets.push(timing); |
| 76 | + sums.push(timing); |
| 77 | + counts.push(1); |
| 78 | + } |
| 79 | + } |
| 80 | + for (j = _k = 0, _len2 = buckets.length; _k < _len2; j = ++_k) { |
| 81 | + bucket = buckets[j]; |
| 82 | + buckets[j] = Math.round(sums[j] / counts[j]); |
| 83 | + } |
| 84 | + return { |
| 85 | + buckets: buckets, |
| 86 | + pulses: pulses |
| 87 | + }; |
| 88 | + }, |
| 89 | + prepareCompressedPulses: function(input) { |
| 90 | + var parts, pulseLengths, pulses; |
| 91 | + parts = input.split(' '); |
| 92 | + pulseLengths = parts.slice(0, 8); |
| 93 | + pulses = parts[8]; |
| 94 | + pulseLengths = pulseLengths.filter(function(puls) { |
| 95 | + return puls !== '0'; |
| 96 | + }).map(function(puls) { |
| 97 | + return parseInt(puls, 10); |
| 98 | + }); |
| 99 | + sortIndices = sortIndices(pulseLengths); |
| 100 | + pulseLengths.sort(function(l, r) { |
| 101 | + return l - r; |
| 102 | + }); |
| 103 | + pulses = helper.mapByArray(pulses, sortIndices); |
| 104 | + return { |
| 105 | + pulseLengths: pulseLengths, |
| 106 | + pulses: pulses |
| 107 | + }; |
| 108 | + }, |
| 109 | + parsePulseSquence: function(pulseLengths, pulses) { |
| 110 | + var p, results, values, _i, _len; |
| 111 | + results = []; |
| 112 | + for (_i = 0, _len = protocols.length; _i < _len; _i++) { |
| 113 | + p = protocols[_i]; |
| 114 | + if (doesProtocolMatch(pulseLengths, pulses, p)) { |
| 115 | + values = p.parse(pulses); |
| 116 | + results.push({ |
| 117 | + protocol: p.name, |
| 118 | + values: values |
| 119 | + }); |
| 120 | + } |
| 121 | + } |
| 122 | + return results; |
| 123 | + } |
| 124 | +}; |
0 commit comments