You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Alternatively, you can login with an access token.
98
+
99
+
```typescript
100
+
particle.loginWithToken("the_token");
101
+
```
102
+
88
103
### `logout`
89
104
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.
90
105
@@ -94,6 +109,35 @@ There's no reason not to because it couldn't be easier:
94
109
particle.logout();
95
110
```
96
111
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
+
97
141
### `listDevices`
98
142
Make sure you've claimed a device in your Particle account, then do this to list them in your app:
99
143
@@ -116,10 +160,22 @@ The returned list of `TNSParticleDevice` objects has these properties and functi
116
160
| id |`string`| The unique ID of this device. |
117
161
| name |`string`| The given name of this device. |
118
162
| status |`string`| The current status of the device, usually `normal`. |
163
+
| connected |`boolean`| Whether or not the device is currently connected.. |
119
164
| 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`. |
120
165
| functions |`Array<string>`| The list of functions currently available on the device. You can invoke these with `callFunction` (see below). |
121
166
| 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). |
122
167
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
+
123
179
#### `<device>.callFunction`
124
180
You can invoke any of the `functions` you discovered on the device.
.catch(error=>console.log(`Error in getVariable: ${error}`));
156
212
```
157
213
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'
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
+
158
234
## Thanks!
159
235
[markoImake](https://github.com/markoImake) for adding a few [very cool features](https://github.com/EddyVerbruggen/nativescript-particle/pull/2).
0 commit comments