Skip to content

Commit

Permalink
doc++
Browse files Browse the repository at this point in the history
  • Loading branch information
EddyVerbruggen committed Nov 7, 2018
1 parent 77cdf5d commit 2a1b96b
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 19 deletions.
14 changes: 8 additions & 6 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ matrix:
env:
- WebPack="iOS"
osx_image: xcode9.2
language: node_js
language: node_js
node_js: "8"
jdk: oraclejdk8
script: cd demo && npm run build.plugin && npm i && tns build ios --bundle --env.uglify
Expand All @@ -22,22 +22,22 @@ matrix:
before_install: nvm install 8
script: cd demo && npm run build.plugin && npm i && tns build android --bundle --env.uglify --env.snapshot
- language: android
env:
env:
- BuildAndroid="26"
os: linux
jdk: oraclejdk8
before_install: nvm install stable
script:
- cd src && npm i && npm run tsc && cd ../demo && tns build android
- os: osx
env:
env:
- BuildiOS="11"
- Xcode="9.2"
osx_image: xcode9.2
language: node_js
language: node_js
node_js: "8"
jdk: oraclejdk8
script:
script:
- cd src && npm i && npm run tsc && cd ../demo && tns build ios
- os: linux
language: android
Expand Down Expand Up @@ -71,4 +71,6 @@ android:
install:
- echo no | npm install -g nativescript
- tns usage-reporting disable
- tns error-reporting disable
- tns error-reporting disable
- cd src
- npm i
82 changes: 79 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ The [demo app](demo) has this:
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>com.apple.developer.networking.wifi-info</key>
<true/>
<key>com.apple.developer.networking.wifi-info</key>
<true/>
</dict>
</plist>
```
Expand All @@ -67,10 +67,18 @@ Want to see the demo in action? Check out [this short video 📺](https://www.yo
All examples below assume you have these imports and instantiated the `Particle` class:

```typescript
import { Particle, TNSParticleDevice } from "nativescript-particle";
import { Particle, TNSParticleDevice, TNSParticleEvent } from "nativescript-particle";
const particle = new Particle();
```

### `startDeviceSetupWizard`
To help registering devices to your account (and avoid having to use the Particle CLI) you can add devices to your account right from your app! 😎

```typescript
particle.startDeviceSetupWizard()
.then(isSuccessful => console.log("Wizard success? " + isSuccessful));
```

### `login`
Communication between your app and a device is HTTP (REST) based,
so the first step is authenticating yourself with the Particle Cloud:
Expand All @@ -85,6 +93,13 @@ particle.login(
.catch(error => console.log(`Login error: ${error}`));
```

### `loginWithToken`
Alternatively, you can login with an access token.

```typescript
particle.loginWithToken("the_token");
```

### `logout`
Once done interacting with your device(s) it's best to log out as this will do a little cleanup in the plugin and underlying SDK.

Expand All @@ -94,6 +109,35 @@ There's no reason not to because it couldn't be easier:
particle.logout();
```

### `publish`
Publish an event from your app to the Particle Device Cloud.

```typescript
particle.publish(
"ledStatusApp123", // the event name
"ON", // the event data (string)
true, // isPrivate (default true)
30 // ttl (default 60)
);
```

### `subscribe`
Subscribe to the firehose of public events, plus the private events published by devices one owns.
You really want to use a unique prefix, otherwise you'll receive a lot of data (not only from your own devices!).

```typescript
particle.subscribe(
"ledStatusApp123",
(event: TNSParticleEvent) => console.log(`Got a ledStatus event for App 123 from the Particle Cloud: ${JSON.stringify(event)}`));
```

### `unsubscribe`
To stop receiving published events, unsubscribe from the events. Make sure the prefix is equal to the one you previously subscribed with.

```typescript
particle.unsubscribe("ledStatusApp123");
```

### `listDevices`
Make sure you've claimed a device in your Particle account, then do this to list them in your app:

Expand All @@ -116,10 +160,22 @@ The returned list of `TNSParticleDevice` objects has these properties and functi
| id | `string` | The unique ID of this device. |
| name | `string` | The given name of this device. |
| status | `string` | The current status of the device, usually `normal`. |
| connected | `boolean` | Whether or not the device is currently connected.. |
| type | [`TNSParticleDeviceType`](https://github.com/EddyVerbruggen/nativescript-particle/blob/618dea7d0a5d3c1cd9cb287e70142375547faa60/src/particle.common.ts#L1-L10) | One of `Unknown`, `Core`, `Photon`, `P1`, `Electron`, `RaspberryPi`, `DigistumpOak`, `RedBearDuo`, `Bluz`. |
| functions | `Array<string>` | The list of functions currently available on the device. You can invoke these with `callFunction` (see below). |
| variables | `Array<`[`TNSParticleDeviceVariable`](https://github.com/EddyVerbruggen/nativescript-particle/blob/618dea7d0a5d3c1cd9cb287e70142375547faa60/src/particle.common.ts#L38-L41)`>` | The list of variables currently available on the device. You can get their values with `getVariable` (see below). |

#### `<device>.rename`
You can change the device name right from your app! 💪

```typescript
const myDevice: TNSParticleDevice = null; // you got this from 'listDevices'

myDevice.rename("rocket_bubble")
.then(() => console.log("Device renamed"))
.catch(error => console.log(`Error renaming the device: ${error}`));
```

#### `<device>.callFunction`
You can invoke any of the `functions` you discovered on the device.

Expand Down Expand Up @@ -155,6 +211,26 @@ myDevice.getVariable("analogvalue")
.catch(error => console.log(`Error in getVariable: ${error}`));
```

#### `<device>.subscribe`
You can get notified in your app in case an app on one of your devices publishes an event.

To suppress noise you can filter those events by supplying a prefix, in this case `my-prefix-`, so events like `my-prefix-temp` or `my-prefix-sensorOn` are caught:

```typescript
const myDevice: TNSParticleDevice = null; // you got this from 'listDevices'

myDevice.subscribe(
"my-prefix-",
(event: TNSParticleEvent) => console.log(`device event: ${JSON.stringify(event)}`));
```

#### `<device>.unsubscribe`
To stop receiving published events from your devices, unsubscribe from the events. Make sure the prefix is equal to the one you previously subscribed with.

```typescript
myDevice.unsubscribe("my-prefix-");
```

## Thanks!
[markoImake](https://github.com/markoImake) for adding a few [very cool features](https://github.com/EddyVerbruggen/nativescript-particle/pull/2).

Expand Down
18 changes: 9 additions & 9 deletions demo/app/main-view-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { prompt } from "tns-core-modules/ui/dialogs";

/************ SET THESE FOR QUICK LOGIN ************/
const PARTICLE_USERNAME = "eddyverbruggen@gmail.com";
const PARTICLE_PASSWORD = "XS4alles";
const PARTICLE_PASSWORD = "";
/************ ALT LOGIN WITH TOKEN ************/
const PARTICLE_TOKEN = undefined;
/************ SET PARTICLE EVENT NAME ************/
Expand Down Expand Up @@ -139,6 +139,14 @@ export class HelloWorldModel extends Observable {
.catch(error => this.set(HelloWorldModel.MESSAGE_KEY, error));
}

onPublish(): void {
// you will catch this event only in 'onSubscribe' because of the prefix used
this.particle.publish(
PARTICLE_EVENT_NAME,
"Testing 1-2-3",
true);
}

onSubscribe(args): void {
this.subscribed = !this.subscribed;
this.set(HelloWorldModel.SUBSCRIBE_BUTTON_KEY, this.subscribed ? "Unsub." : "Subscr.");
Expand Down Expand Up @@ -175,14 +183,6 @@ export class HelloWorldModel extends Observable {
}
}

onPublish(): void {
// you will catch this event only in 'onSubscribe'
this.particle.publish(
PARTICLE_EVENT_NAME,
"Testing 1-2-3",
true);
}

startwizard(): void {
console.log("start wizard tapped");
this.particle.startDeviceSetupWizard().then(success => console.log("wizard callback: " + success));
Expand Down
2 changes: 1 addition & 1 deletion src/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
}
},
"scripts": {
"tsc": "tsc",
"tsc": "tsc -skipLibCheck",
"build": "npm run tsc && npm run build.native",
"build.native": "node scripts/build-native.js",
"postclone": "npm i && cd ../demo && npm i && npx rimraf -- package-lock.json && cd ../src",
Expand Down

0 comments on commit 2a1b96b

Please sign in to comment.