Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 | 4x 4x 4x 4x 4x 4x 38x 38x 38x 38x 38x 1x 1x 38x 8x 6x 6x 38x 2x 2x 2x 1x 1x 2x 2x 38x 38x 38x 3x 3x 3x 38x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 38x 10x 8x 38x 13x 13x 12x 13x 9x 9x 9x 9x 9x 1x 9x 9x 8x 8x 11x 1x 4x 4x | const events = require('events');
const parsers = require('serialport').parsers; // used for parser
const SerialPort = require('serialport');
const ReadLine = require('@serialport/parser-readline');
const util = require('util');
const NL = 0x0a;
function Client() {
let port_address = null;
const sp_open = false;
// const sp_timeout = null;
let current_state = null;
let current_callback = null;
const state_write = (data) => {
// just passes the data up to anything that wants to listen for it.
this.emit('data', data);
// clear any callbacks
current_callback = null;
};
const state_init = (data) => {
// initialising the connection, waits for the magic chars to come out
if (data.indexOf('>>') >=0) {
// now we move to just writing anything from the serial line out.
current_state = state_write;
this.emit('ready');
}
};
const state_dump = (data) => {
// waits for the state dump to come back and then parses the json and
//
current_state = state_write;
let dump;
try {
dump = JSON.parse(data);
} catch (err) {
Eif (current_callback != null) {
current_callback(err);
}
}
Eif (current_callback != null) {
current_callback(null, dump);
}
};
current_state = state_init;
this.serialport = null;
this.get_info = (cb) => {
this.serialport.write('DUMP\n');
current_state = state_dump;
current_callback = cb;
};
this.set_details = async(data, cb) => {
// sets the details of the firmware onto the device
current_state = state_write;
await new Promise((resolve, reject) => {
this.serialport.write('CLR\n');
resolve('CLR');
}).then(() => {
const s = 'FID ' + data.firmwareID + '\n';
this.serialport.write(s);
return ('FID');
}).then(() => {
this.serialport.write('CID ' + data.creatorID + '\n');
return 'CID';
}).then(() => {
this.serialport.write('I2C ' + data.i2c_address + ' ' + data.use_custom_address + '\n');
return 'I2C';
}).then(async() => {
// give the serialport time to finish executing before you close out
await setTimeout(() => {}, 20);
cb();
}).catch(err => {
throw err;
});
};
this.close = () => {
if (this.open) {
this.serialport.close();
}
};
Object.defineProperties(this, {
'open': {
get() {
// gives back whether the serialport is open or not
let open = false
if (this.serialport) {
open = this.serialport.isOpen;
}
return open;
}
},
'port': {
set(p) {
Eif (p != undefined) {
// set up the port here.
const parser = new ReadLine();
const opts = {baudRate: 9600};
this.serialport = new SerialPort(p, opts, (err) => {
if (err) {
this.emit('error', err);
}
});
this.serialport.pipe(parser);
// create handlers for the various serial port actions.
this.serialport.on('open', () => {
// once connection is open, emit that but then this will do nothing
// further. After data comes back in from the SP then the data will
// flow through the parser data event instead.
port_address = p;
this.emit('connected');
});
// These are catch all scenarios to surface an error in a low level
// library. No need for explicit tests.
/* istanbul ignore next */
this.serialport.on('error', (err) => {
this.emit('error', err);
});
/* istanbul ignore next */
parser.on('error', (err) => {
this.emit('error', err);
});
parser.on('data', (data) => current_state(data));
}
},
get() {
return port_address;
}
}
});
}
util.inherits(Client, events.EventEmitter);
module.exports.Client = Client;
|