|
@@ -17,6 +17,7 @@ import (
|
|
|
|
|
|
type Manager struct {
|
|
type Manager struct {
|
|
RegisteredHandler []ProtocolHandler
|
|
RegisteredHandler []ProtocolHandler
|
|
|
|
+ cachedDeviceList []*Device
|
|
}
|
|
}
|
|
|
|
|
|
func NewIoTManager() *Manager {
|
|
func NewIoTManager() *Manager {
|
|
@@ -52,6 +53,35 @@ func (m *Manager) HandleScannerList(w http.ResponseWriter, r *http.Request) {
|
|
sendJSONResponse(w, string(js))
|
|
sendJSONResponse(w, string(js))
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+//Get status of the given device ID
|
|
|
|
+func (m *Manager) HandleGetDeviceStatus(w http.ResponseWriter, r *http.Request) {
|
|
|
|
+ devid, err := mv(r, "devid", true)
|
|
|
|
+ if err != nil {
|
|
|
|
+ sendErrorResponse(w, "Invalid device id")
|
|
|
|
+ return
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ //Search for that device ID
|
|
|
|
+ for _, dev := range m.cachedDeviceList {
|
|
|
|
+ if dev.DeviceUUID == devid {
|
|
|
|
+ //Found. Get it status and return
|
|
|
|
+ status, err := dev.Handler.Status(dev)
|
|
|
|
+ if err != nil {
|
|
|
|
+ sendErrorResponse(w, err.Error())
|
|
|
|
+ return
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ //Return the status
|
|
|
|
+ js, _ := json.Marshal(status)
|
|
|
|
+ sendJSONResponse(w, string(js))
|
|
|
|
+ return
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ //Not found
|
|
|
|
+ sendErrorResponse(w, "Given device ID does not match any scanned devices")
|
|
|
|
+}
|
|
|
|
+
|
|
//Handle IoT Scanning Request
|
|
//Handle IoT Scanning Request
|
|
func (m *Manager) HandleScanning(w http.ResponseWriter, r *http.Request) {
|
|
func (m *Manager) HandleScanning(w http.ResponseWriter, r *http.Request) {
|
|
scannedDevices := []*Device{}
|
|
scannedDevices := []*Device{}
|
|
@@ -68,26 +98,20 @@ func (m *Manager) HandleScanning(w http.ResponseWriter, r *http.Request) {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ //Cache the scan record
|
|
|
|
+ m.cachedDeviceList = scannedDevices
|
|
js, _ := json.Marshal(scannedDevices)
|
|
js, _ := json.Marshal(scannedDevices)
|
|
sendJSONResponse(w, string(js))
|
|
sendJSONResponse(w, string(js))
|
|
}
|
|
}
|
|
|
|
|
|
//Handle IoT Listing Request
|
|
//Handle IoT Listing Request
|
|
func (m *Manager) HandleListing(w http.ResponseWriter, r *http.Request) {
|
|
func (m *Manager) HandleListing(w http.ResponseWriter, r *http.Request) {
|
|
- listedDevices := []*Device{}
|
|
|
|
- for _, ph := range m.RegisteredHandler {
|
|
|
|
- //Scan devices using this handler
|
|
|
|
- thisProtcolDeviceList, err := ph.List()
|
|
|
|
- if err != nil {
|
|
|
|
- continue
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- //Append it to list
|
|
|
|
- for _, dev := range thisProtcolDeviceList {
|
|
|
|
- listedDevices = append(listedDevices, dev)
|
|
|
|
- }
|
|
|
|
|
|
+ if m.cachedDeviceList == nil || len(m.cachedDeviceList) == 0 {
|
|
|
|
+ //Scan results not exists. Scan again
|
|
|
|
+ m.HandleScanning(w, r)
|
|
|
|
+ } else {
|
|
|
|
+ //Cache already exists. Return it
|
|
|
|
+ js, _ := json.Marshal(m.cachedDeviceList)
|
|
|
|
+ sendJSONResponse(w, string(js))
|
|
}
|
|
}
|
|
-
|
|
|
|
- js, _ := json.Marshal(listedDevices)
|
|
|
|
- sendJSONResponse(w, string(js))
|
|
|
|
}
|
|
}
|