Skip to content

Commit 2a1b96b

Browse files
doc++
1 parent 77cdf5d commit 2a1b96b

File tree

4 files changed

+97
-19
lines changed

4 files changed

+97
-19
lines changed

.travis.yml

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ matrix:
1010
env:
1111
- WebPack="iOS"
1212
osx_image: xcode9.2
13-
language: node_js
13+
language: node_js
1414
node_js: "8"
1515
jdk: oraclejdk8
1616
script: cd demo && npm run build.plugin && npm i && tns build ios --bundle --env.uglify
@@ -22,22 +22,22 @@ matrix:
2222
before_install: nvm install 8
2323
script: cd demo && npm run build.plugin && npm i && tns build android --bundle --env.uglify --env.snapshot
2424
- language: android
25-
env:
25+
env:
2626
- BuildAndroid="26"
2727
os: linux
2828
jdk: oraclejdk8
2929
before_install: nvm install stable
3030
script:
3131
- cd src && npm i && npm run tsc && cd ../demo && tns build android
3232
- os: osx
33-
env:
33+
env:
3434
- BuildiOS="11"
3535
- Xcode="9.2"
3636
osx_image: xcode9.2
37-
language: node_js
37+
language: node_js
3838
node_js: "8"
3939
jdk: oraclejdk8
40-
script:
40+
script:
4141
- cd src && npm i && npm run tsc && cd ../demo && tns build ios
4242
- os: linux
4343
language: android
@@ -71,4 +71,6 @@ android:
7171
install:
7272
- echo no | npm install -g nativescript
7373
- tns usage-reporting disable
74-
- tns error-reporting disable
74+
- tns error-reporting disable
75+
- cd src
76+
- npm i

README.md

Lines changed: 79 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ The [demo app](demo) has this:
4242
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
4343
<plist version="1.0">
4444
<dict>
45-
<key>com.apple.developer.networking.wifi-info</key>
46-
<true/>
45+
<key>com.apple.developer.networking.wifi-info</key>
46+
<true/>
4747
</dict>
4848
</plist>
4949
```
@@ -67,10 +67,18 @@ Want to see the demo in action? Check out [this short video 📺](https://www.yo
6767
All examples below assume you have these imports and instantiated the `Particle` class:
6868

6969
```typescript
70-
import { Particle, TNSParticleDevice } from "nativescript-particle";
70+
import { Particle, TNSParticleDevice, TNSParticleEvent } from "nativescript-particle";
7171
const particle = new Particle();
7272
```
7373

74+
### `startDeviceSetupWizard`
75+
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! 😎
76+
77+
```typescript
78+
particle.startDeviceSetupWizard()
79+
.then(isSuccessful => console.log("Wizard success? " + isSuccessful));
80+
```
81+
7482
### `login`
7583
Communication between your app and a device is HTTP (REST) based,
7684
so the first step is authenticating yourself with the Particle Cloud:
@@ -85,6 +93,13 @@ particle.login(
8593
.catch(error => console.log(`Login error: ${error}`));
8694
```
8795

96+
### `loginWithToken`
97+
Alternatively, you can login with an access token.
98+
99+
```typescript
100+
particle.loginWithToken("the_token");
101+
```
102+
88103
### `logout`
89104
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.
90105

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

112+
### `publish`
113+
Publish an event from your app to the Particle Device Cloud.
114+
115+
```typescript
116+
particle.publish(
117+
"ledStatusApp123", // the event name
118+
"ON", // the event data (string)
119+
true, // isPrivate (default true)
120+
30 // ttl (default 60)
121+
);
122+
```
123+
124+
### `subscribe`
125+
Subscribe to the firehose of public events, plus the private events published by devices one owns.
126+
You really want to use a unique prefix, otherwise you'll receive a lot of data (not only from your own devices!).
127+
128+
```typescript
129+
particle.subscribe(
130+
"ledStatusApp123",
131+
(event: TNSParticleEvent) => console.log(`Got a ledStatus event for App 123 from the Particle Cloud: ${JSON.stringify(event)}`));
132+
```
133+
134+
### `unsubscribe`
135+
To stop receiving published events, unsubscribe from the events. Make sure the prefix is equal to the one you previously subscribed with.
136+
137+
```typescript
138+
particle.unsubscribe("ledStatusApp123");
139+
```
140+
97141
### `listDevices`
98142
Make sure you've claimed a device in your Particle account, then do this to list them in your app:
99143

@@ -116,10 +160,22 @@ The returned list of `TNSParticleDevice` objects has these properties and functi
116160
| id | `string` | The unique ID of this device. |
117161
| name | `string` | The given name of this device. |
118162
| status | `string` | The current status of the device, usually `normal`. |
163+
| connected | `boolean` | Whether or not the device is currently connected.. |
119164
| 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`. |
120165
| functions | `Array<string>` | The list of functions currently available on the device. You can invoke these with `callFunction` (see below). |
121166
| 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). |
122167

168+
#### `<device>.rename`
169+
You can change the device name right from your app! 💪
170+
171+
```typescript
172+
const myDevice: TNSParticleDevice = null; // you got this from 'listDevices'
173+
174+
myDevice.rename("rocket_bubble")
175+
.then(() => console.log("Device renamed"))
176+
.catch(error => console.log(`Error renaming the device: ${error}`));
177+
```
178+
123179
#### `<device>.callFunction`
124180
You can invoke any of the `functions` you discovered on the device.
125181

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

214+
#### `<device>.subscribe`
215+
You can get notified in your app in case an app on one of your devices publishes an event.
216+
217+
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:
218+
219+
```typescript
220+
const myDevice: TNSParticleDevice = null; // you got this from 'listDevices'
221+
222+
myDevice.subscribe(
223+
"my-prefix-",
224+
(event: TNSParticleEvent) => console.log(`device event: ${JSON.stringify(event)}`));
225+
```
226+
227+
#### `<device>.unsubscribe`
228+
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.
229+
230+
```typescript
231+
myDevice.unsubscribe("my-prefix-");
232+
```
233+
158234
## Thanks!
159235
[markoImake](https://github.com/markoImake) for adding a few [very cool features](https://github.com/EddyVerbruggen/nativescript-particle/pull/2).
160236

demo/app/main-view-model.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { prompt } from "tns-core-modules/ui/dialogs";
55

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

142+
onPublish(): void {
143+
// you will catch this event only in 'onSubscribe' because of the prefix used
144+
this.particle.publish(
145+
PARTICLE_EVENT_NAME,
146+
"Testing 1-2-3",
147+
true);
148+
}
149+
142150
onSubscribe(args): void {
143151
this.subscribed = !this.subscribed;
144152
this.set(HelloWorldModel.SUBSCRIBE_BUTTON_KEY, this.subscribed ? "Unsub." : "Subscr.");
@@ -175,14 +183,6 @@ export class HelloWorldModel extends Observable {
175183
}
176184
}
177185

178-
onPublish(): void {
179-
// you will catch this event only in 'onSubscribe'
180-
this.particle.publish(
181-
PARTICLE_EVENT_NAME,
182-
"Testing 1-2-3",
183-
true);
184-
}
185-
186186
startwizard(): void {
187187
console.log("start wizard tapped");
188188
this.particle.startDeviceSetupWizard().then(success => console.log("wizard callback: " + success));

src/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
}
1212
},
1313
"scripts": {
14-
"tsc": "tsc",
14+
"tsc": "tsc -skipLibCheck",
1515
"build": "npm run tsc && npm run build.native",
1616
"build.native": "node scripts/build-native.js",
1717
"postclone": "npm i && cd ../demo && npm i && npx rimraf -- package-lock.json && cd ../src",

0 commit comments

Comments
 (0)