-
Notifications
You must be signed in to change notification settings - Fork 5
/
main-view-model.ts
116 lines (101 loc) · 3.98 KB
/
main-view-model.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import { Observable } from "tns-core-modules/data/observable";
import { Particle, TNSParticleDevice } from "nativescript-particle";
import { ObservableArray } from "tns-core-modules/data/observable-array";
import { prompt } from "tns-core-modules/ui/dialogs";
/************ SET THESE FOR QUICK LOGIN ************/
const PARTICLE_USERNAME = undefined;
const PARTICLE_PASSWORD = undefined;
/***************************************************/
export class HelloWorldModel extends Observable {
private static MESSAGE_KEY = "message";
private static LOGGED_IN_KEY = "loggedIn";
private static SELECTED_DEVICE_KEY = "selectedDevice";
loggedIn: boolean = false;
message: string = "Log in to get started";
devices: ObservableArray<TNSParticleDevice> = new ObservableArray<TNSParticleDevice>();
selectedDevice: TNSParticleDevice;
private particle: Particle;
constructor() {
super();
this.particle = new Particle();
}
login(): void {
if (PARTICLE_USERNAME && PARTICLE_PASSWORD) {
this.doLogin(PARTICLE_USERNAME, PARTICLE_PASSWORD);
} else {
prompt({
title: "Particle username",
message: "This is an e-mailaddress",
inputType: "email",
cancelButtonText: "cancel",
okButtonText: "OK"
}).then(usernameResult => {
usernameResult.result && prompt({
title: "Particle password",
inputType: "password",
cancelButtonText: "cancel",
okButtonText: "OK"
}).then(passwordResult => {
passwordResult.result && this.doLogin(usernameResult.text, passwordResult.text);
});
});
}
}
private doLogin(username: string, password: string): void {
this.particle.login({username, password})
.then(() => {
this.set(HelloWorldModel.LOGGED_IN_KEY, true);
this.set(HelloWorldModel.MESSAGE_KEY, "Logged in");
})
.catch(error => this.set(HelloWorldModel.MESSAGE_KEY, error));
}
logout(): void {
this.particle.logout();
this.devices.splice(0, this.devices.length);
this.set(HelloWorldModel.LOGGED_IN_KEY, false);
this.set(HelloWorldModel.SELECTED_DEVICE_KEY, undefined);
this.set(HelloWorldModel.MESSAGE_KEY, "Logged out");
}
listDevices(): void {
// reset
this.devices.splice(0, this.devices.length);
this.set(HelloWorldModel.SELECTED_DEVICE_KEY, undefined);
this.particle.listDevices()
.then(devices => {
if (devices.length === 0) {
this.set(HelloWorldModel.MESSAGE_KEY, "No devices have been claimed in this account. Go to particle.io for details.");
} else {
this.set(HelloWorldModel.MESSAGE_KEY, "Select a device below:");
}
this.devices.push(devices);
})
.catch(error => this.set(HelloWorldModel.MESSAGE_KEY, error));
}
onDeviceTap(args): void {
this.set(HelloWorldModel.SELECTED_DEVICE_KEY, this.devices.getItem(args.index));
this.set(HelloWorldModel.MESSAGE_KEY, `Selected: ${this.selectedDevice.name}`);
}
onFunctionTap(args): void {
const fnc = this.selectedDevice.functions[args.index];
prompt({
title: fnc,
message: "Enter a comma-sep list of commands to send to this function",
cancelButtonText: "cancel",
okButtonText: "Send!"
}).then(paramsResult => {
if (paramsResult.result) {
const params: Array<string> = [];
paramsResult.text.split(",").forEach(param => params.push(param.trim()));
this.selectedDevice.callFunction(fnc, ...params)
.then(result => this.set(HelloWorldModel.MESSAGE_KEY, `${fnc} result: ${result}`))
.catch(error => this.set(HelloWorldModel.MESSAGE_KEY, error));
}
});
}
onVariableTap(args): void {
const vari = this.selectedDevice.variables[args.index];
this.selectedDevice.getVariable(vari.name)
.then(result => this.set(HelloWorldModel.MESSAGE_KEY, `${vari.name} result: ${result}`))
.catch(error => this.set(HelloWorldModel.MESSAGE_KEY, error));
}
}