aboutsummaryrefslogtreecommitdiffstats
path: root/src/js/bluetooth.js
blob: 258a8a7cb1e0343158fd986cb0b938357b4e9619 (plain)
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import { bluetooth } from 'agl-js-api';
import Mustache from 'mustache';
import { load as load_template } from './templates';
import * as app from './app';

var template;
var page = {
    devices: [],
    filter: {
        available: true,
        paired: false,
        connected: false
    },
    powered: false
};

function render(){
    document.body.innerHTML = Mustache.render(template, page);
}

function update_devices(devices) {
    page.devices = [];
    devices.forEach(function(device) {
        if ( page.filter.connected && device.properties.connected ) {
            page.devices.push(device);
        } else if ( page.filter.paired && 
                device.properties.paired && 
                !device.properties.connected ) {
            page.devices.push(device);
        } else if ( page.filter.available & 
                !device.properties.connected && 
                !device.properties.paired) {
            page.devices.push(device);
        }
    });

    render();
}

function refresh_devices() {
    bluetooth.managed_objects().then(function(result){
        update_devices(result.devices);
    });
}

function pair(deviceId) {
    bluetooth.pair(deviceId).then(function() {
        refresh_devices();
    });
}

function connect(deviceId) {
    bluetooth.connect(deviceId).then(function() {
        refresh_devices();
    });
}

function disconnect(deviceId) {
    bluetooth.disconnect(deviceId).then(function() {
        refresh_devices();
    });
}

export function toggle() {
    bluetooth.adapter_state().then(function(result) {
        bluetooth.adapter_state({
            powered: !result.powered
        }).then(function(state) {
            page.powered = state.powered;
        });
    });
}

export function init() {
    load_template('bluetooth.template.html').then(function(result) {
        template = result;
        Mustache.parse(template);
    }, function(error) {
        console.error('ERROR Loading bluetooth template', error);
    });

    bluetooth.adapter_state().then(function(state) {
        page.powered = state.powered;
    });
}

export function getState() {
    return page.powered;
}

export function show() {
    refresh_devices();
}

export function hide() {
    app.show();
}

export function available() {
    page.filter = {
        available: true,
        paired: false,
        connected: false
    };
    refresh_devices();
}

export function connected() {
    page.filter = {
        available: false,
        paired: false,
        connected: true
    };
    refresh_devices();
}

export function paired() {
    page.filter = {
        available: false,
        paired: true,
        connected: false
    };
    refresh_devices();
}

export function remove(deviceId) {
    bluetooth.remove_device(deviceId).then(function() {
        refresh_devices();
    });
}

export function manage(deviceId, paired, connected) {
    if ( connected ) {
        connect(deviceId);
    } else if ( paired ) {
        disconnect(deviceId);
    } else {
        pair(deviceId);
    }
}