Skip to content

Commit b238f87

Browse files
Merge pull request #2 from imakeLtd/master
Added device event subscription
2 parents 7b4371a + 4d1ebd2 commit b238f87

12 files changed

+1072
-579
lines changed

demo/app/main-page.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,13 @@
77
<StackLayout orientation="horizontal" horizontalAlignment="center" visibility="{{ loggedIn ? 'visible' : 'collapsed' }}">
88
<Button tap="{{ logout }}" text="Log out" class="btn"/>
99
<Button tap="{{ listDevices }}" text="List devices" class="btn"/>
10+
<Button tap="{{ startwizard }}" text="Add" width="33%" class="btn"/>
1011
</StackLayout>
1112

1213
<Label text="{{ message }}" class="message" textWrap="true"/>
1314

15+
<Button tap="{{ onDeviceSubscribe }}" text="{{ subButtonText }}" class="btn" visibility="{{ selectedDevice && handleEvents ? 'visible' : 'collapsed' }}"/>
16+
1417
<ListView separatorColor="transparent" rowHeight="60" width="95%" class="m-15" items="{{ devices }}" itemTap="{{ onDeviceTap }}" horizontalAlignment="center" visibility="{{ !selectedDevice ? 'visible' : 'collapsed' }}">
1518
<ListView.itemTemplate>
1619
<StackLayout backgroundColor="#eee" class="p-10 m-2">

demo/app/main-view-model.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,29 +6,42 @@ import { prompt } from "tns-core-modules/ui/dialogs";
66
/************ SET THESE FOR QUICK LOGIN ************/
77
const PARTICLE_USERNAME = undefined;
88
const PARTICLE_PASSWORD = undefined;
9+
/************ ALT LOGIN WITH TOKEN ************/
10+
const PARTICLE_TOKEN = undefined;
11+
/************ SET PARTICLE EVENT NAME ************/
12+
const PARTICLE_EVENT_NAME = undefined;
913
/***************************************************/
1014

1115
export class HelloWorldModel extends Observable {
1216
private static MESSAGE_KEY = "message";
1317
private static LOGGED_IN_KEY = "loggedIn";
1418
private static SELECTED_DEVICE_KEY = "selectedDevice";
19+
private static SUBSCRIBE_BUTTON_KEY = "subButtonText";
1520

1621
loggedIn: boolean = false;
1722
message: string = "Log in to get started";
1823
devices: ObservableArray<TNSParticleDevice> = new ObservableArray<TNSParticleDevice>();
1924
selectedDevice: TNSParticleDevice;
25+
handleEvents: boolean = false;
26+
subscribed: boolean = false;
27+
subButtonText: string = "Subscribe to Events";
2028

2129
private particle: Particle;
2230

2331
constructor() {
2432
super();
2533

2634
this.particle = new Particle();
35+
if (PARTICLE_EVENT_NAME) this.handleEvents = true;
2736
}
2837

2938
login(): void {
3039
if (PARTICLE_USERNAME && PARTICLE_PASSWORD) {
3140
this.doLogin(PARTICLE_USERNAME, PARTICLE_PASSWORD);
41+
} else if (PARTICLE_TOKEN){
42+
console.log('login tap, go for loginwithtoken option');
43+
44+
this.doLoginWithToken(PARTICLE_TOKEN);
3245
} else {
3346
prompt({
3447
title: "Particle username",
@@ -58,6 +71,12 @@ export class HelloWorldModel extends Observable {
5871
.catch(error => this.set(HelloWorldModel.MESSAGE_KEY, error));
5972
}
6073

74+
private doLoginWithToken(token: string): void {
75+
this.particle.loginWithToken(PARTICLE_TOKEN);
76+
this.set(HelloWorldModel.LOGGED_IN_KEY, true);
77+
this.set(HelloWorldModel.MESSAGE_KEY, "Logged in");
78+
}
79+
6180
logout(): void {
6281
this.particle.logout();
6382
this.devices.splice(0, this.devices.length);
@@ -113,4 +132,24 @@ export class HelloWorldModel extends Observable {
113132
.then(result => this.set(HelloWorldModel.MESSAGE_KEY, `${vari.name} result: ${result}`))
114133
.catch(error => this.set(HelloWorldModel.MESSAGE_KEY, error));
115134
}
135+
136+
onDeviceSubscribe(args): void {
137+
this.subscribed = !this.subscribed;
138+
this.set(HelloWorldModel.SUBSCRIBE_BUTTON_KEY, this.subscribed ? "Unsubscribe" : "Subscribe to Events");
139+
if (this.subscribed) {
140+
this.selectedDevice.subscribe(PARTICLE_EVENT_NAME, (data) => {
141+
console.log(`selectedDevice.subscribe activity, eventdata: ${data}`);
142+
});
143+
} else {
144+
this.selectedDevice.unsubscribe();
145+
}
146+
}
147+
148+
startwizard(): void {
149+
console.log('start wizard tapped');
150+
this.particle.startDeviceSetupWizard((result) => {
151+
console.log('wizard callback');
152+
});
153+
154+
}
116155
}

demo/tsconfig.json

Lines changed: 41 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,43 @@
11
{
2-
"compilerOptions": {
3-
"target": "es5",
4-
"module": "commonjs",
5-
"declaration": false,
6-
"removeComments": true,
7-
"noLib": false,
8-
"emitDecoratorMetadata": true,
9-
"experimentalDecorators": true,
10-
"lib": [
11-
"es6",
12-
"dom"
2+
"compilerOptions": {
3+
"target": "es5",
4+
"module": "commonjs",
5+
"declaration": false,
6+
"removeComments": true,
7+
"noLib": false,
8+
"emitDecoratorMetadata": true,
9+
"experimentalDecorators": true,
10+
"lib": [
11+
"es6",
12+
"dom"
13+
],
14+
"pretty": true,
15+
"allowUnreachableCode": false,
16+
"allowUnusedLabels": false,
17+
"noEmitHelpers": true,
18+
"noEmitOnError": false,
19+
"noImplicitAny": false,
20+
"noImplicitReturns": true,
21+
"noImplicitUseStrict": false,
22+
"noFallthroughCasesInSwitch": true,
23+
"baseUrl": ".",
24+
"paths": {
25+
"*": [
26+
"./node_modules/*"
27+
],
28+
"~/*": [
29+
"app/*"
30+
]
31+
}
32+
},
33+
"include": [
34+
"../src",
35+
"**/*"
1336
],
14-
"sourceMap": false,
15-
"pretty": true,
16-
"allowUnreachableCode": false,
17-
"allowUnusedLabels": false,
18-
"noEmitHelpers": true,
19-
"noEmitOnError": false,
20-
"noImplicitAny": false,
21-
"noImplicitReturns": true,
22-
"noImplicitUseStrict": false,
23-
"noFallthroughCasesInSwitch": true,
24-
"baseUrl": ".",
25-
"paths": {
26-
"*": [
27-
"./node_modules/*"
28-
],
29-
"~/*": [
30-
"app/*"
31-
]
32-
}
33-
},
34-
"include": [
35-
"../src",
36-
"**/*"
37-
],
38-
"exclude": [
39-
"../src/node_modules",
40-
"node_modules",
41-
"platforms"
42-
],
43-
"compileOnSave": false
44-
}
37+
"exclude": [
38+
"../src/node_modules",
39+
"node_modules",
40+
"platforms"
41+
],
42+
"compileOnSave": false
43+
}

publish/package-lock.json

Lines changed: 12 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)