Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions dist/farmbot-min.js

Large diffs are not rendered by default.

19 changes: 12 additions & 7 deletions dist/farmbot.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,15 @@ return /******/ (function(modules) { // webpackBootstrap
this.setState("mqttServer", "ws://" + mqttUrl + ":3002");
this.setState("uuid", token.bot || "UUID MISSING FROM TOKEN");
};
Farmbot.prototype.listState = function () {
return Object.keys(this._state);
};
;
Farmbot.prototype.getState = function (key) {
return this._state[key];
if (key) {
return this._state[key];
}
else {
// Create a copy of the state object to prevent accidental mutation.
return JSON.parse(JSON.stringify(this._state));
}
;
};
;
Farmbot.prototype.setState = function (key, val) {
Expand Down Expand Up @@ -234,14 +237,16 @@ return /******/ (function(modules) { // webpackBootstrap
that.on(msg.id, function (response) {
console.log("Got " + response.id);
var hasResult = !!(response || {}).result;
(hasResult) ? p.resolve(that) : p.reject(response);
// TODO : If bot returns a status update, update bot's internal state.
// Probably can use a "type guard" for this sort of thing.
(hasResult) ? p.resolve(response) : p.reject(response);
});
return p;
};
;
Farmbot.prototype._onmessage = function (channel, buffer, message) {
var msg = JSON.parse(buffer.toString());
var id = (msg.id || msg.name);
var id = (msg.id || "*");
this.emit(id, msg);
};
;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "farmbot",
"version": "0.3.2",
"version": "0.4.1",
"description": "Javascript wrapper for Farmbot's authentication and RPC instructions",
"main": "dist/farmbot.js",
"scripts": {
Expand Down
53 changes: 11 additions & 42 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ Works on any browser that supports:

* [Native Promise objects](http://caniuse.com/#feat=promises).
* [Websockets](http://caniuse.com/#feat=websockets).
* JSON (any browser made after 1942).

## Installation

Expand All @@ -21,8 +22,6 @@ Raise an issue if you require support with other package managers such as Bower.

## Login with an API Token

:+1:

Login using your API token from the [Farmbot Web App](my.farmbot.io).

[Click here for instructions on how to generate an API token](https://github.com/FarmBot/farmbot-web-app#generating-an-api-token)
Expand All @@ -43,6 +42,10 @@ Example:
* There's no need to mention the MQTT server, it's in the token.
* There's no need to mention the bot's UUID, it's in the token.

# Publishing

0. `webpack`
0. `npm publish`

# Sending Commands to a Farmbot Object

Expand All @@ -69,16 +72,6 @@ To run it off of a private server, you will need to change the `meshServer` url
var bot = Farmbot({uuid: "123", token: "456", meshServer: "//myMeshBluServer.org"});
```

## Installation (Library Developers)

1. `git clone https://github.com/FarmBot/farmbot-js`
2. `cd farmbot-js`
3. `npm install` or `sudo npm install` if required

To run tests: `npm test`

To minify and convert to a [UMD module](https://github.com/umdjs/umd): `gulp build`

## Basic RPC Commands

Call RPC commands using the corresponding method on `bot`. All RPC commands return a promise. Timeout is set at `1000 ms` by default and can be reconfigured by changing the bot `timeout` propery on instantiation or via `bot.setState("timeout", 999)`.
Expand Down Expand Up @@ -144,30 +137,14 @@ Currently supported commands:

The bot object keeps all state in one place for sanity. This includes things like configuration options, current position, etc. All updates to the bot's state are broadcast with a `change` event, that reports current and previous state value as it changes.

* `bot.listState()`: See all relevant state record names.
* `bot.getState()`: Get copy of Farmbot status variables (read only).
* `bot.getState(attribute_name)`: Fetch a state record such as `timeout` or `meshServer` URL.
* `bot.setState(name, value)`: Set state value 'x' to 'y'. Ex: `bot.setState('uuid', '---')`. Emits a `change` event.

## Common Config Options

If you are running your own servers, you may want to use other options.

### meshServer (String)

URL for the mesh server used by the bot. Defaults to `ws://mesh.farmbot.io`.

```javascript

var options = {
uuid: "123",
token: "456",
meshServer: 'wss://localhost:443'
};

var bot = Farmbot(options);

```

### Timeout (Number)

Time (in milliseconds) to wait before deeming an RPC command to be unacknowledged. Relevant promise objects will be rejected if the bot does not respond in the timeframe provided. Defaults to `1000`.
Expand All @@ -184,18 +161,10 @@ bot.options.timeout = 5000 // 5 seconds

## TODO

- [ ] Get feature parity with old version.
- [ ] Convert hardcoded strings, "magic numbers" and event names to constants.
- [ ] Get compliant with A+ promise spec.
- [ ] Download REST server URL off of bot on connect (avoids un-DRY configuration)
- [ ] Convert library to literate javascript?
- [X] Add getState() amd getState(key) function
- [X] Add setState(key, value) function
- [X] Add support for UMD modules
- [X] Add build tool / pre built `farmbot.min.js`
- [X] Get off of socket.io after meshblu upgrade.
- [X] Upgrade to support latest MeshBlu
- [X] DRY up repetitious promise code via helper in `Farmbot.util`
- [X] Ability to generate guest UUID / Token.
- [X] Add test suite
- [X] Add test coverage reporter
- [ ] Track state changes when bot returns a status object.
- define an `isStatusUpdate` type guard.
- Add `maybeUpdateState` method.
- emit `change` event in there.
- Call it inside of `send()`.
3 changes: 1 addition & 2 deletions src/farmbot.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ export declare class Farmbot {
client: MqttClient;
constructor(input: any);
_decodeThatToken(): void;
listState(): string[];
getState(key: any): any;
getState(key?: any): any;
setState(key: any, val: any): any;
emergencyStop(): Promise<{}>;
execSequence(sequence: any): Promise<{}>;
Expand Down
19 changes: 11 additions & 8 deletions src/farmbot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,13 @@ export class Farmbot {
this.setState("uuid", token.bot || "UUID MISSING FROM TOKEN");
}

listState() {
return Object.keys(this._state);
};

getState(key) {
return this._state[key];
getState(key?) {
if (key) {
return this._state[key];
} else {
// Create a copy of the state object to prevent accidental mutation.
return JSON.parse(JSON.stringify(this._state));
};
};

setState(key, val) {
Expand Down Expand Up @@ -203,14 +204,16 @@ export class Farmbot {
that.on(msg.id, function(response) {
console.log(`Got ${response.id}`);
let hasResult = !!(response || {}).result;
(hasResult) ? p.resolve(that) : p.reject(response);
// TODO : If bot returns a status update, update bot's internal state.
// Probably can use a "type guard" for this sort of thing.
(hasResult) ? p.resolve(response) : p.reject(response);
});
return p;
};

_onmessage(channel: String, buffer: Uint8Array, message) {
let msg = JSON.parse(buffer.toString());
let id = (msg.id || msg.name);
let id = (msg.id || "*");
this.emit(id , msg);
};

Expand Down
4 changes: 3 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,7 @@

"exclude": [
"node_modules"
]
],
"compileOnSave": false,
"buildOnSave": false
}