Skip to content

Commit 0bc57ba

Browse files
Added device event subscription #2
1 parent b238f87 commit 0bc57ba

File tree

6 files changed

+686
-61
lines changed

6 files changed

+686
-61
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,4 +132,8 @@ myDevice.getVariable("analogvalue")
132132
.catch(error => console.log(`Error in getVariable: ${error}`));
133133
```
134134

135+
## Thanks!
136+
[markoImake](https://github.com/markoImake) for adding a few [very cool features](https://github.com/EddyVerbruggen/nativescript-particle/pull/2).
137+
138+
135139
Happy IoT'ing! 🕹🤖🚪🖲💡📸🎙⛈🚦🛎🔊

demo/app/main-view-model.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@ import { ObservableArray } from "tns-core-modules/data/observable-array";
44
import { prompt } from "tns-core-modules/ui/dialogs";
55

66
/************ SET THESE FOR QUICK LOGIN ************/
7-
const PARTICLE_USERNAME = undefined;
8-
const PARTICLE_PASSWORD = undefined;
7+
const PARTICLE_USERNAME = "eddyverbruggen@gmail.com";
8+
const PARTICLE_PASSWORD = "XS4alles";
99
/************ ALT LOGIN WITH TOKEN ************/
1010
const PARTICLE_TOKEN = undefined;
1111
/************ SET PARTICLE EVENT NAME ************/
1212
const PARTICLE_EVENT_NAME = undefined;
13+
1314
/***************************************************/
1415

1516
export class HelloWorldModel extends Observable {
@@ -38,9 +39,9 @@ export class HelloWorldModel extends Observable {
3839
login(): void {
3940
if (PARTICLE_USERNAME && PARTICLE_PASSWORD) {
4041
this.doLogin(PARTICLE_USERNAME, PARTICLE_PASSWORD);
41-
} else if (PARTICLE_TOKEN){
42-
console.log('login tap, go for loginwithtoken option');
43-
42+
} else if (PARTICLE_TOKEN) {
43+
console.log("login tap, go for loginwithtoken option");
44+
4445
this.doLoginWithToken(PARTICLE_TOKEN);
4546
} else {
4647
prompt({
@@ -72,7 +73,7 @@ export class HelloWorldModel extends Observable {
7273
}
7374

7475
private doLoginWithToken(token: string): void {
75-
this.particle.loginWithToken(PARTICLE_TOKEN);
76+
this.particle.loginWithToken(PARTICLE_TOKEN);
7677
this.set(HelloWorldModel.LOGGED_IN_KEY, true);
7778
this.set(HelloWorldModel.MESSAGE_KEY, "Logged in");
7879
}
@@ -111,7 +112,7 @@ export class HelloWorldModel extends Observable {
111112
const fnc = this.selectedDevice.functions[args.index];
112113
prompt({
113114
title: fnc,
114-
message: "Enter a comma-sep list of commands to send to this function",
115+
message: "Enter a comma-sep list of commands to send to this function (fi: 'on').",
115116
cancelButtonText: "cancel",
116117
okButtonText: "Send!"
117118
}).then(paramsResult => {
@@ -144,12 +145,11 @@ export class HelloWorldModel extends Observable {
144145
this.selectedDevice.unsubscribe();
145146
}
146147
}
147-
148+
148149
startwizard(): void {
149-
console.log('start wizard tapped');
150+
console.log("start wizard tapped");
150151
this.particle.startDeviceSetupWizard((result) => {
151-
console.log('wizard callback');
152+
console.log("wizard callback: " + result);
152153
});
153-
154154
}
155155
}

src/package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,16 @@
1818
"test.android": "npm run tsc && npm run tslint && cd ../demo && tns build android && tns test android --justlaunch",
1919
"test.ios": "npm run tsc && npm run tslint && cd ../demo && tns build ios && tns test ios --justlaunch",
2020
"tslint": "cd .. && tslint \"**/*.ts\" --config tslint.json --exclude \"**/node_modules/**\" --exclude \"**/platforms/**\"",
21-
"demo.ios": "npm run tsc && cd ../demo && tns run ios --syncAllFiles --emulator",
21+
"demo.ios": "npm run tsc && cd ../demo && tns run ios --syncAllFiles",
2222
"demo.ios.bundled": "npm run tsc && cd ../demo && tns run ios --bundle --syncAllFiles --emulator",
2323
"demo.android": "npm run tsc && cd ../demo && tns run android --syncAllFiles",
2424
"demo.android.bundled": "npm run tsc && cd ../demo && tns run android --bundle --syncAllFiles",
2525
"demo.reset": "cd ../demo && npx rimraf -- hooks node_modules platforms package-lock.json",
2626
"plugin.prepare": "npm run build && cd ../demo && tns plugin remove nativescript-particle && tns plugin add ../src",
2727
"clean": "npm run demo.reset && npx rimraf -- node_modules package-lock.json && npm i",
2828
"ci.tslint": "npm i && tslint '**/*.ts' --config '../tslint.json' --exclude '**/node_modules/**'",
29-
"prepack": "npm run build.native"
29+
"prepack": "npm run build.native",
30+
"generate-typings.ios": "cd ../demo && TNS_DEBUG_METADATA_PATH=\"$(pwd)/metadata\" tns build ios && TNS_TYPESCRIPT_DECLARATIONS_PATH=\"$(pwd)/typings\" tns build ios && echo 'Now look for your library typings in demo/typings. Afterwards, remove that folder, and the (intermediate) metadata-* folders.'"
3031
},
3132
"keywords": [
3233
"ecosystem:NativeScript",

src/particle.ios.ts

Lines changed: 43 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import {
66
TNSParticleDeviceVariable,
77
TNSParticleLoginOptions
88
} from "./particle.common";
9-
import * as frameModule from 'tns-core-modules/ui/frame';
109

1110
const toJsArray = (nativeArray: NSArray<any>): Array<any> => {
1211
const result: Array<any> = [];
@@ -18,7 +17,6 @@ const toJsArray = (nativeArray: NSArray<any>): Array<any> => {
1817
return result;
1918
};
2019

21-
2220
const toJsonVariables = (nativeDictionary: NSDictionary<string, string>): Array<TNSParticleDeviceVariable> => {
2321
const result: Array<TNSParticleDeviceVariable> = [];
2422
if (nativeDictionary) {
@@ -54,7 +52,7 @@ class MyTNSParticleDevice implements TNSParticleDevice {
5452
functions: Array<string>;
5553
variables: Array<TNSParticleDeviceVariable>;
5654
eventIds: string[] = [];
57-
55+
5856
constructor(public nativeDevice: ParticleDevice) {
5957
this.id = nativeDevice.id;
6058
this.name = nativeDevice.name;
@@ -85,15 +83,15 @@ class MyTNSParticleDevice implements TNSParticleDevice {
8583
});
8684
}
8785

88-
subscribe(name: string, eventHandler:any): void {
89-
const id = this.nativeDevice.subscribeToEventsWithPrefixHandler(name, (event: ParticleEvent, error: NSError) => {
90-
if (!error){
91-
if (event.data) eventHandler(event.data);
92-
} else {
93-
console.log(`Error subscribing to event: ${error}`);
94-
}
95-
});
96-
this.eventIds.push(id);
86+
subscribe(name: string, eventHandler: any): void {
87+
const id = this.nativeDevice.subscribeToEventsWithPrefixHandler(name, (event: ParticleEvent, error: NSError) => {
88+
if (!error) {
89+
if (event.data) eventHandler(event.data);
90+
} else {
91+
console.log(`Error subscribing to event: ${error}`);
92+
}
93+
});
94+
this.eventIds.push(id);
9795
}
9896

9997
unsubscribe(): void {
@@ -105,7 +103,7 @@ class MyTNSParticleDevice implements TNSParticleDevice {
105103

106104
export class Particle implements TNSParticleAPI {
107105

108-
private wizardDelegate : ParticleSetupControllerDelegateImpl;
106+
private wizardDelegate: ParticleSetupControllerDelegateImpl;
109107

110108
public login(options: TNSParticleLoginOptions): Promise<void> {
111109
return new Promise<void>((resolve, reject) => {
@@ -124,7 +122,7 @@ export class Particle implements TNSParticleAPI {
124122
ParticleCloud.sharedInstance().injectSessionAccessToken(token);
125123
}
126124

127-
public setOAuthConfig(id:string, secret:string): void{
125+
public setOAuthConfig(id: string, secret: string): void {
128126
ParticleCloud.sharedInstance().oAuthClientId = id;
129127
ParticleCloud.sharedInstance().oAuthClientSecret = secret;
130128
}
@@ -135,10 +133,10 @@ export class Particle implements TNSParticleAPI {
135133

136134
public isAuthenticated(): Boolean {
137135
return ParticleCloud.sharedInstance().isAuthenticated;
138-
}
139-
136+
}
137+
140138
public accessToken(): string {
141-
return ParticleCloud.sharedInstance().accessToken;
139+
return ParticleCloud.sharedInstance().accessToken;
142140
}
143141

144142
public listDevices(): Promise<Array<TNSParticleDevice>> {
@@ -158,45 +156,43 @@ export class Particle implements TNSParticleAPI {
158156
});
159157
}
160158

161-
public startDeviceSetupWizard(finishHandler: any): void{
162-
console.log('Particle.startDeviceSetup');
163-
const setupController = new ParticleSetupMainController();
164-
this.wizardDelegate = new ParticleSetupControllerDelegateImpl();
165-
this.wizardDelegate.cb = finishHandler;
166-
setupController.delegate = this.wizardDelegate;
167-
var ctrl = frameModule.topmost().ios.controller;
168-
ctrl.navigationBarHidden = true;
169-
ctrl.pushViewControllerAnimated(setupController, true);
159+
public startDeviceSetupWizard(finishHandler: any): void {
160+
const setupController = ParticleSetupMainController.new();
161+
console.log(">> bundle: " + ParticleSetupMainController.getResourcesBundle());
162+
console.log(">> bundle img: " + ParticleSetupMainController.loadImageFromResourceBundle("spinner"));
163+
this.wizardDelegate = ParticleSetupControllerDelegateImpl.createWithOwnerAndCallback(new WeakRef(this), finishHandler);
164+
setupController.delegate = <any>this.wizardDelegate;
165+
UIApplication.sharedApplication.keyWindow.rootViewController.presentViewControllerAnimatedCompletion(setupController, true, null);
170166
}
171167

172-
public getDeviceSetupCustomizer(): any{
168+
public getDeviceSetupCustomizer(): any {
173169
return ParticleSetupCustomization.sharedInstance();
174170
}
175171
}
176172

177-
178-
declare var ParticleSetupMainController : any;
179-
declare var ParticleSetupCustomization : any;
180173
class ParticleSetupControllerDelegateImpl extends NSObject implements ParticleSetupMainControllerDelegate {
181-
static ObjCProtocols = [ParticleSetupMainControllerDelegate] // define our native protocalls
174+
static ObjCProtocols = [ParticleSetupMainControllerDelegate]; // define our native protocols
175+
176+
private owner: WeakRef<Particle>;
177+
private cb: (result: ParticleSetupMainControllerResult) => void;
182178

183-
static new(): ParticleSetupControllerDelegateImpl {
184-
return <ParticleSetupControllerDelegateImpl>super.new() // calls new() on the NSObject
179+
public static new(): ParticleSetupControllerDelegateImpl {
180+
return <ParticleSetupControllerDelegateImpl>super.new(); // calls new() on the NSObject
185181
}
186-
cb :any;
187182

188-
public particleSetupViewControllerDidFinishWithResultDevice(controller, result, device){
189-
console.log('particleSetupViewControllerDidFinishWithResultDevice');
190-
191-
if(this.cb){
192-
this.cb(result);
193-
}
183+
public static createWithOwnerAndCallback(owner: WeakRef<Particle>, callback: (result: ParticleSetupMainControllerResult) => void): ParticleSetupControllerDelegateImpl {
184+
const delegate = <ParticleSetupControllerDelegateImpl>ParticleSetupControllerDelegateImpl.new();
185+
delegate.owner = owner;
186+
delegate.cb = callback;
187+
return delegate;
188+
}
189+
190+
public particleSetupViewControllerDidFinishWithResultDevice(controller: ParticleSetupMainController, result: ParticleSetupMainControllerResult, device: ParticleDevice): void {
191+
console.log("particleSetupViewControllerDidFinishWithResultDevice, result: " + result);
192+
this.cb && this.cb(result);
193+
}
194+
195+
particleSetupViewControllerDidNotSucceeedWithDeviceID(controller: ParticleSetupMainController, deviceID: string): void {
196+
console.log("particleSetupViewControllerDidFinishWithResultDevice");
194197
}
195198
}
196-
interface ParticleSetupMainControllerDelegate {
197-
particleSetupViewControllerDidFinishWithResultDevice(controller: any, result: any, device: any): void;
198-
particleSetupViewControllerDidNotSucceeedWithDeviceID?(controller: any, deviceID: string): void;
199-
}
200-
declare var ParticleSetupMainControllerDelegate: {
201-
prototype: ParticleSetupMainControllerDelegate;
202-
};

src/platforms/ios/Podfile

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
1+
platform :ios, '9.0'
2+
13
pod 'Particle-SDK', '~> 0.8'
2-
pod 'ParticleSetup'
4+
pod 'ParticleSetup', '~> 0.9'

0 commit comments

Comments
 (0)