From 16c67f0e302106a4c9d7318a9dbe079408a150f6 Mon Sep 17 00:00:00 2001 From: Peli de Halleux Date: Mon, 29 Jan 2018 19:46:54 -0800 Subject: [PATCH] Refactoring datalog in common packages (#249) * using datalog from common packages * upgrading common package link * updated block signatures * more docs --- libs/datalog/README.md | 3 - .../_locales/datalog-jsdoc-strings.json | 8 +- libs/datalog/_locales/datalog-strings.json | 2 +- libs/datalog/datalog.ts | 122 ------------------ libs/datalog/pxt.json | 15 +-- libs/datalog/targetoverrides.ts | 57 ++++++++ libs/datalog/test.ts | 6 - package.json | 2 +- 8 files changed, 68 insertions(+), 147 deletions(-) delete mode 100644 libs/datalog/README.md delete mode 100644 libs/datalog/datalog.ts create mode 100644 libs/datalog/targetoverrides.ts delete mode 100644 libs/datalog/test.ts diff --git a/libs/datalog/README.md b/libs/datalog/README.md deleted file mode 100644 index c29e22b5..00000000 --- a/libs/datalog/README.md +++ /dev/null @@ -1,3 +0,0 @@ -# Datalog - -A tiny libraty to create CSV datalog files. \ No newline at end of file diff --git a/libs/datalog/_locales/datalog-jsdoc-strings.json b/libs/datalog/_locales/datalog-jsdoc-strings.json index cd0b0749..ce94588a 100644 --- a/libs/datalog/_locales/datalog-jsdoc-strings.json +++ b/libs/datalog/_locales/datalog-jsdoc-strings.json @@ -1,11 +1,15 @@ { + "datalog": "A data logging framework", + "datalog.DatalogStorage": "A storage for datalog data", + "datalog.DatalogStorage.appendHeaders": "Appends the headers in datalog", + "datalog.DatalogStorage.appendRow": "Appends a row of data", + "datalog.DatalogStorage.flush": "Flushes any buffered data", + "datalog.DatalogStorage.init": "Initializes the storage", "datalog.addRow": "Starts a row of data", "datalog.addValue": "Adds a cell to the row of data", "datalog.addValue|param|name": "name of the cell, eg: \"x\"", "datalog.addValue|param|value": "value of the cell, eg: 0", "datalog.flush": "Commits any buffered row to disk", "datalog.setEnabled": "Turns on or off datalogging", - "datalog.setFile": "Starts a new data logger for the given file", - "datalog.setFile|param|filename": "the filename, eg: \"datalog.csv\"", "datalog.setStorage": "* @param storage custom storage solution" } \ No newline at end of file diff --git a/libs/datalog/_locales/datalog-strings.json b/libs/datalog/_locales/datalog-strings.json index 7316d4be..003a4e46 100644 --- a/libs/datalog/_locales/datalog-strings.json +++ b/libs/datalog/_locales/datalog-strings.json @@ -1,7 +1,7 @@ { "datalog.addRow|block": "datalog add row", "datalog.addValue|block": "datalog add %name|=%value", - "datalog.setEnabled|block": "datalog %enabled", + "datalog.setEnabled|block": "datalog %enabled=toggleOnOff", "datalog|block": "datalog", "{id:category}Datalog": "Datalog" } \ No newline at end of file diff --git a/libs/datalog/datalog.ts b/libs/datalog/datalog.ts deleted file mode 100644 index 4a930903..00000000 --- a/libs/datalog/datalog.ts +++ /dev/null @@ -1,122 +0,0 @@ -//% weight=100 color=#0fbc11 icon="" -namespace datalog { - let _headers: string[] = undefined; - let _headersLength: number; - let _values: number[]; - let _buffer: string = ""; - let _start: number; - let _filename = "datalog.csv"; - let _storage: storage.Storage = storage.temporary; - let _enabled = true; - - function clear() { - _headers = undefined; - _values = undefined; - _buffer = ""; - } - - function init() { - if (!_headers) { - _headers = []; - _headersLength = 0; - _start = control.millis(); - _storage.remove(_filename); - } - _values = []; - } - - function commit() { - // write row if any data - if (_values && _values.length > 0) { - // write headers for the first row - if (!_headersLength) { - _storage.appendCSVHeaders(_filename, _headers); - _headersLength = _storage.size(_filename); - } - // commit row data - _buffer += storage.toCSV(_values, _storage.csvSeparator); - // buffered writes - if (_buffer.length > 1024) - flush(); - } - - // clear values - _values = undefined; - } - - /** - * Starts a row of data - */ - //% weight=100 - //% blockId=datalogAddRow block="datalog add row" - export function addRow(): void { - if (!_enabled) return; - - commit(); - init(); - const s = (control.millis() - _start) / 1000; - addValue("time (s)", s); - } - - /** - * Adds a cell to the row of data - * @param name name of the cell, eg: "x" - * @param value value of the cell, eg: 0 - */ - //% weight=99 - //% blockId=datalogAddValue block="datalog add %name|=%value" - export function addValue(name: string, value: number) { - if (!_values) return; - let i = _headers.indexOf(name); - if (i < 0) { - _headers.push(name); - i = _headers.length - 1; - } - _values[i] = value; - } - - /** - * Starts a new data logger for the given file - * @param filename the filename, eg: "datalog.csv" - */ - //% - export function setFile(filename: string) { - flush(); - _filename = filename; - clear(); - } - - /** - * - * @param storage custom storage solution - */ - //% - export function setStorage(storage: storage.Storage) { - flush(); - _storage = storage; - clear(); - } - - /** - * Commits any buffered row to disk - */ - //% - export function flush() { - if (_buffer) { - const b = _buffer; - _buffer = ""; - _storage.append(_filename, b); - } - } - - /** - * Turns on or off datalogging - * @param enabled - */ - //% blockId=datalogEnabled block="datalog %enabled" - //% enabled.fieldEditor=fieldonoff - export function setEnabled(enabled: boolean) { - flush(); - _enabled = enabled; - } -} diff --git a/libs/datalog/pxt.json b/libs/datalog/pxt.json index 150fa05d..4bea453f 100644 --- a/libs/datalog/pxt.json +++ b/libs/datalog/pxt.json @@ -1,16 +1,7 @@ { - "name": "datalog", - "description": "Tiny data logging framework", - "files": [ - "README.md", - "datalog.ts" - ], - "testFiles": [ - "test.ts" - ], - "public": true, + "additionalFilePath": "../../node_modules/pxt-common-packages/libs/datalog", "dependencies": { - "core": "file:../core", - "storage": "file:../storage" + "core": "file:../core", + "storage": "file:../storage" } } \ No newline at end of file diff --git a/libs/datalog/targetoverrides.ts b/libs/datalog/targetoverrides.ts new file mode 100644 index 00000000..943cb42d --- /dev/null +++ b/libs/datalog/targetoverrides.ts @@ -0,0 +1,57 @@ +namespace datalog.ev3 { + /** + * A datalog storage for the EV3 + */ + export class EV3DatalogStorage extends DatalogStorage { // implements DatalogStorage { + private _filename: string; + private _buffer: string; + private _storage: storage.Storage; + + /** + * Creates a new storage for the datalog + * @param storage + * @param filename + */ + constructor(storage: storage.Storage, filename: string) { + super(); + this._filename = filename; + this._storage = storage; + } + + /** + * Initializes the storage + */ + init(): void { + this._storage.remove(this._filename); + this._buffer = ""; + } + /** + * Appends the headers in datalog + */ + appendHeaders(headers: string[]): void { + this._storage.appendCSVHeaders(this._filename, headers); + } + /** + * Appends a row of data + */ + appendRow(values: number[]): void { + // commit row data + this._buffer += storage.toCSV(values, this._storage.csvSeparator); + // buffered writes + if (this._buffer.length > 1024) + this.flush(); + } + /** + * Flushes any buffered data + */ + flush(): void { + if (this._buffer) { + const b = this._buffer; + this._buffer = ""; + this._storage.append(this._filename, b); + } + } + } + // automatic hook up + datalog.setStorage(new datalog.ev3.EV3DatalogStorage(storage.temporary, "datalog.csv")); +} diff --git a/libs/datalog/test.ts b/libs/datalog/test.ts deleted file mode 100644 index dede3632..00000000 --- a/libs/datalog/test.ts +++ /dev/null @@ -1,6 +0,0 @@ - -loops.forever(function () { - datalog.addRow() - datalog.addValue("x", Math.random()) - datalog.addValue("y", Math.random()) -}) diff --git a/package.json b/package.json index dd0c6fab..1ae7b1c7 100644 --- a/package.json +++ b/package.json @@ -44,7 +44,7 @@ "webfonts-generator": "^0.4.0" }, "dependencies": { - "pxt-common-packages": "0.17.11", + "pxt-common-packages": "0.17.12", "pxt-core": "3.0.26" }, "scripts": {