-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgateguard.js
More file actions
78 lines (58 loc) · 2.36 KB
/
Copy pathgateguard.js
File metadata and controls
78 lines (58 loc) · 2.36 KB
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
require('console-stamp')(console, { pattern: 'yyyy/mm/dd HH:MM:ss.l' })
const bleno = require('bleno')
const uuid = require('uuid/v4')
const shared = require('./shared_module').shared
const ble = require('./ble_module')
const cloud = require('./cloud_module')
const buttons = require('rpi-gpio-buttons')([11])
const led = require('./led_module')
const relay = require('./relay_module')
const MAX_TOKEN_ID = 0xffff
const tokenService = new cloud.TokenService()
const greenLedManager = new led.LEDManager(led.LEDColorEnum.green)
const redLedManager = new led.LEDManager(led.LEDColorEnum.red)
const relayManager = new relay.RelayManager()
const gateGuardBleService = new ble.GateGuardBLEService(greenLedManager, redLedManager, relayManager)
bleno.on('stateChange', function(state) {
console.log('BLE state is ' + (state ? 'ON' : 'OFF') + ' now')
if (state === 'poweredOn') {
bleno.startAdvertising('GateGuard', [shared.bleServiceUUID])
} else {
bleno.stopAdvertising()
}
})
bleno.on('advertisingStart', function(error) {
console.log('BLE peripheral started advertising with ' + (error ? 'error: ' + error : 'success'))
if (!error) {
bleno.setServices([gateGuardBleService])
}
})
const buttonStateChanged = function() {
console.log('Button pressed')
if (shared.subscribedToCharacteristic) {
if (!tokenService.requestInProgress) {
const tokenId = Math.floor(Math.random() * MAX_TOKEN_ID)
const token = uuid()
greenLedManager.on(led.LEDModeEnum.blinking)
tokenService.registerTokenInCloud(tokenId, token, function (success) {
console.log('Token registering completed with ' + (success ? 'success' : 'failure'))
shared.cache.set(tokenId.toString(), token)
if (success) {
gateGuardBleService.characteristics[0].valueDidChangeCallback(Buffer.from(tokenId.toString(), 'utf8'))
} else {
greenLedManager.off()
redLedManager.on(led.LEDModeEnum.solid, shared.ELECTROLOCK_ON_DURATION)
}
})
}
} else {
redLedManager.on(led.LEDModeEnum.blinking, shared.ELECTROLOCK_ON_DURATION / 3)
}
}
buttons.on('pressed', function(pin) {
switch(pin) {
case 11:
buttonStateChanged()
break
}
})