-
Notifications
You must be signed in to change notification settings - Fork 0
/
devices.go
63 lines (56 loc) · 1.67 KB
/
devices.go
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
package main
import (
"fmt"
"net/http"
)
func (devices *NetworkDevices) Reserve(devId, cusId string) *AppError {
d, ok := devices.DeviceTable[devId]
if !ok {
return appErr(fmt.Sprintf("device id %v not found", devId), http.StatusNotFound)
}
d.Mutex.Lock()
defer d.Mutex.Unlock()
if d.reserved && d.customerId != cusId {
return appErr("device unavailable for reservation", http.StatusConflict)
}
d.reserved = true
d.customerId = cusId
devices.Channel <- &ChannelOp{op: []uint8{AddClaimedChannel, RemoveUnClaimedChannel}, data: d}
return nil
}
func (devices *NetworkDevices) Release(devId, cusId string) *AppError {
d, ok := devices.DeviceTable[devId]
if !ok {
return appErr(fmt.Sprintf("customer id %v not found", cusId), http.StatusNotFound)
}
if d.reserved == false {
return nil
}
d.Mutex.Lock()
defer d.Mutex.Unlock()
if d.customerId != cusId {
return appErr(fmt.Sprintf("unauthorized: customer does not own the device"), http.StatusUnauthorized)
}
d.reserved = false
d.customerId = ""
devices.Channel <- &ChannelOp{op: []uint8{RemoveClaimedChannel, AddUnClaimedChannel}, data: d}
return nil
}
func (devices *NetworkDevices) GetDevices(claimed bool) []string {
if !claimed {
return devices.UnClaimedBuffer
}
return devices.ClaimedBuffer
}
func (devices *NetworkDevices) GetDevice(devId, cusId string) (*Device, *AppError) {
d := devices.DeviceTable[devId]
if d == nil {
return nil, appErr(fmt.Sprintf("device id %v not found", devId), http.StatusNotFound)
}
d.Mutex.Lock()
defer d.Mutex.Unlock()
if d.customerId != "" && d.customerId != cusId {
return nil, appErr("customer does not own the device", http.StatusUnauthorized)
}
return d, nil
}