Skip to content

Instantly share code, notes, and snippets.

View adrianfaciu's full-sized avatar
🎯
Focusing

Adrian Fâciu adrianfaciu

🎯
Focusing
View GitHub Profile
@adrianfaciu
adrianfaciu / bulb-control.component.ts
Last active October 3, 2016 19:45
NativeScriptLightBulb
import { Component } from '@angular/core';
@Component({
moduleId: module.id,
selector: 'ns-bulb-control',
templateUrl: 'bulb-control.component.html',
styleUrls: ['bulb-control.component.css']
})
export class BulbControlComponent {
maxValue = 255;
<StackLayout>
<Label text="Magic Blue"></Label>
<Button (tap)="connectToMagicBlue()"
text="Connect">
</Button>
<Slider [minValue]="minValue"
[maxValue]="maxValue"
[(ngModel)]="redValue">
</Slider>
<Slider [minValue]="minValue"
export class BleDevice {
constructor(public UUID: string,
public name: string,
public state: string) { }
}
fixPermission(): void {
bluetooth.hasCoarseLocationPermission()
.then((granted) => {
if (!granted) {
bluetooth.requestCoarseLocationPermission()
.then(() => console.log("Location permission requested"));
}
});
}
scan(): Promise<any> {
return bluetooth.startScanning({
serviceUUIDs: [],
seconds: 3,
onDiscovered: (device) => {
const bleDevice = new BleDevice(device.UUID, device.name, device.state);
this.bleDevicesAround.push(bleDevice);
}
});
}
getMagicBlue(): BleDevice {
return this.bluetoothService.bleDevicesAround
.filter(d => d.name && d.name.indexOf('LEDBLE') > -1)[0];
}
connectToMagicBlue() {
this.bluetoothService.scan().then(() => {
this.magicBlue = this.getMagicBlue();
if (this.magicBlue) {
this.bluetoothService.connect(this.magicBlue.UUID)
update(red: number, green: number, blue: number, white: number) {
const color = [86, red, green, blue, white, 240, 170].map(param => {
return this.convertToHexString(param);
}).join(",");
const updateMessage = this.getMessage(this.magicBlue.UUID, color);
this.bluetoothService.write(updateMessage);
}
getMessage(UUID: string, value: string): any {
connectToMagicBlue() {
this.lightBulbCommandService.connectToMagicBlue();
}
updateLightBulb() {
this.lightBulbCommandService.update(this.redValue,
this.greenValue,
this.blueValue,
this.whiteValue);
}
detectChanges(title: string, name: string) {
if (title !== this.title) {
this.title = title;
this.myComponent.title = title;
}
if (name !== this.name) {
this.name = name;
this.myComponent.name = name;
}
}
let myStream$ = Rx.Observable.from([1, 2, 3]);
myStream$.subscribe(value => console.log(value));
let newStream$ = Rx.Observable.interval(1000);
newStream$.subscribe(value => console.log(value));
// Use .take to stop it
let foo$ = Rx.Observable.range(5, 10);
foo$.subscribe(value => console.log(value));