소스 검색

Added wip iot manager

TC pushbot 5 4 년 전
부모
커밋
be977625e9

BIN
documents/ASSP promotion/promo1.png


BIN
documents/ASSP promotion/promo1.psd


BIN
documents/ASSP promotion/sankaku_garland2_line.png


+ 50 - 9
iot.go

@@ -2,9 +2,11 @@ package main
 
 import (
 	"log"
+	"net/http"
 
 	"imuslab.com/arozos/mod/iot"
 	"imuslab.com/arozos/mod/iot/hdsv2"
+	prout "imuslab.com/arozos/mod/prouter"
 )
 
 /*
@@ -12,19 +14,58 @@ import (
 	Author: tobychui
 
 	This script handle the IoT service start up and mangement
+
+	IoT Manager: Manage who can have access to certain IoT devices
+	IoT Panel: The panel for controlling the devices
 */
 
-var hdsv2Handler *hdsv2.Handler
+var iotManager *iot.Manager
 
 func IoTHubInit() {
-	hdsv2Handler = hdsv2.NewProtocolHandler(MDNS)
-	go func() {
-		testScan(hdsv2Handler)
-	}()
+	if *allow_iot && *allow_mdns && MDNS != nil {
+		//Create a new ioT Manager
+		iotManager = iot.NewIoTManager()
 
-}
+		//Register IoT Setting Interfaces
+		registerSetting(settingModule{
+			Name:     "IoT Manager",
+			Desc:     "Manage IoT Devices Permission",
+			IconPath: "SystemAO/iot/img/small_icon.png",
+			Group:    "Device",
+			StartDir: "SystemAO/iot/manager.html",
+		})
+
+		//Register IoT Devices Endpoints
+		router := prout.NewModuleRouter(prout.RouterOption{
+			ModuleName:  "IoT Panel",
+			AdminOnly:   false,
+			UserHandler: userHandler,
+			DeniedHandler: func(w http.ResponseWriter, r *http.Request) {
+				sendErrorResponse(w, "Permission Denied")
+			},
+		})
+
+		adminRouter := prout.NewModuleRouter(prout.RouterOption{
+			ModuleName:  "System Setting",
+			AdminOnly:   true,
+			UserHandler: userHandler,
+			DeniedHandler: func(w http.ResponseWriter, r *http.Request) {
+				sendErrorResponse(w, "Permission Denied")
+			},
+		})
+
+		router.HandleFunc("/system/iot/scan", iotManager.HandleScanning)
+
+		log.Println("Work in progress breakpoint: ", router, adminRouter)
+
+		//Start of the IoT Management Handlers
+
+		//Home Dynamic v2
+		hdsv2Handler := hdsv2.NewProtocolHandler(MDNS)
+		iotManager.RegisterHandler(hdsv2Handler)
+
+		//Add more here if needed
+
+	}
 
-func testScan(m iot.ProtocolHandler) {
-	dev, err := m.Scan()
-	log.Println(dev, err)
 }

+ 1 - 0
main.flags.go

@@ -91,3 +91,4 @@ var maxTempFileKeepTime = flag.Int("tmp_time", 86400, "Time before tmp file will
 
 //Flags related to ArozOS Cluster services
 var allow_clustering = flag.Bool("allow_cluster", true, "Enable cluster operations within LAN. Require allow_mdns=true flag")
+var allow_iot = flag.Bool("allow_iot", true, "Enable IoT related APIs and scanner. Require MDNS enabled")

+ 45 - 0
mod/iot/handlerManager.go

@@ -0,0 +1,45 @@
+package iot
+
+import (
+	"log"
+	"net/http"
+)
+
+/*
+	IoT Handler Manager
+	Author: tobychui
+
+	This manager mange all the existsing / registered ioT Manager.
+	This allow a much more abstract usage in the main code
+
+*/
+
+type Manager struct {
+	RegisteredHandler []ProtocolHandler
+}
+
+func NewIoTManager() *Manager {
+	return &Manager{
+		RegisteredHandler: []ProtocolHandler{},
+	}
+}
+
+//Register the handler as one of the IoT Protocol Handler.
+func (m *Manager) RegisterHandler(h ProtocolHandler) error {
+	//Try to start the handler
+	err := h.Start()
+	if err != nil {
+		//Handler startup failed
+		log.Println("*IoT* Protocol Handler Startup Failed: ", err.Error())
+		return err
+	}
+
+	//Add it to the handlers
+	m.RegisteredHandler = append(m.RegisteredHandler, h)
+	return nil
+}
+
+//Handle IoT Scanning Request
+func (m *Manager) HandleScanning(w http.ResponseWriter, r *http.Request) {
+
+}

+ 19 - 1
mod/iot/hdsv2/hdsv2.go

@@ -37,6 +37,11 @@ func NewProtocolHandler(scanner *mdns.MDNSHost) *Handler {
 	}
 }
 
+func (h *Handler) Start() error {
+	log.Println("*IoT* Home Dynamic v2 Loaded")
+	return nil
+}
+
 //Scan the devices within the LAN
 func (h *Handler) Scan() ([]*iot.Device, error) {
 	foundDevices := []*iot.Device{}
@@ -88,7 +93,7 @@ func (h *Handler) List() ([]*iot.Device, error) {
 }
 
 //Home Dynamic system's devices no need to established conenction before executing anything
-func (h *Handler) Connect(device *iot.Device) error {
+func (h *Handler) Connect(device *iot.Device, authInfo *iot.AuthInfo) error {
 	return nil
 }
 
@@ -108,6 +113,19 @@ func (h *Handler) Execute(device *iot.Device, endpoint *iot.Endpoint, payload in
 	return result, nil
 }
 
+func (h *Handler) Stats() iot.Stats {
+	return iot.Stats{
+		Name:          "Home Dynamic v2",
+		Desc:          "A basic IoT communication protocol for ESP8266 made by Makers",
+		Version:       "2.0",
+		ProtocolVer:   "2.0",
+		Author:        "tobychui",
+		AuthorWebsite: "http://arozos.com",
+		AuthorEmail:   "[email protected]",
+		ReleaseDate:   1614524498,
+	}
+}
+
 //Get endpoint of the given device object
 func getEndpoints(device *iot.Device) ([]*iot.Endpoint, error) {
 	//Parse the URL of the endpoint apis location (eps)

+ 25 - 2
mod/iot/iot.go

@@ -39,11 +39,34 @@ type Device struct {
 	ControlEndpoints []*Endpoint            //Endpoints avabile for this device
 }
 
+type AuthInfo struct {
+	Username string
+	Password string
+	Token    string
+}
+
+type Stats struct {
+	Name          string //Name of the protocol handler (e.g. Home Dynamic v2)
+	Desc          string //Description of the protcol
+	Version       string //Version of the handler (recommend matching the protocol ver for easier maintaince)
+	ProtocolVer   string //Version of the hardware protocol
+	Author        string //Name of the author
+	AuthorWebsite string //Author contact website
+	AuthorEmail   string //Author Email
+	ReleaseDate   int64  //Release Date in unix timestamp
+}
+
+var (
+	NoAuth AuthInfo = AuthInfo{} //Empty struct for quick no auth IoT protocols
+)
+
 type ProtocolHandler interface {
+	Start() error                                                                         //Run Startup check. This IoT Protocl Handler will not load if this return any error (e.g. required wireless hardware not found) **TRY NOT TO USE BLOCKING LOGIC HERE**
 	Scan() ([]*Device, error)                                                             //Scan the nearby devices
 	List() ([]*Device, error)                                                             //Return the previous scanned list
-	Connect(device *Device) error                                                         //Connect to the device
-	Status(device *Device) (map[string]interface{}, error)                                //Get status of the IoT device                                    //Connect to a given device
+	Connect(device *Device, authInfo *AuthInfo) error                                     //Connect to the device
+	Status(device *Device) (map[string]interface{}, error)                                //Get status of the IoT device
 	Execute(device *Device, endpoint *Endpoint, payload interface{}) (interface{}, error) //Execute an endpoint for a device
 	Disconnect(device *Device) error                                                      //Disconnect from a device connection
+	Stats() Stats                                                                         //Return the properties and status of the Protocol Handler
 }

BIN
web/SystemAO/iot/img/small_icon.png


BIN
web/SystemAO/iot/img/small_icon.psd


+ 14 - 0
web/SystemAO/iot/index.html

@@ -0,0 +1,14 @@
+<!DOCTYPE html>
+<html ng-app="App">
+<head>
+    <title>IoT Control Panel</title>
+    <meta charset="UTF-8">
+    <meta name="viewport" content="width=device-width, initial-scale=1.0 user-scalable=no">
+    <link rel="stylesheet" href="../../script/semantic/semantic.min.css">
+    <script type="text/javascript" src="../../script/jquery.min.js"></script>
+    <script type="text/javascript" src="../../script/semantic/semantic.min.js"></script>
+</head>
+<body>
+    Hello World!
+</body>
+</html>