iot.go 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. package main
  2. import (
  3. "net/http"
  4. "imuslab.com/arozos/mod/iot"
  5. "imuslab.com/arozos/mod/iot/hds"
  6. "imuslab.com/arozos/mod/iot/hdsv2"
  7. module "imuslab.com/arozos/mod/modules"
  8. prout "imuslab.com/arozos/mod/prouter"
  9. )
  10. /*
  11. IoT Hub
  12. Author: tobychui
  13. This script handle the IoT service start up and mangement
  14. IoT Manager: Manage who can have access to certain IoT devices
  15. IoT Panel: The panel for controlling the devices
  16. */
  17. var iotManager *iot.Manager
  18. func IoTHubInit() {
  19. if *allow_iot && *allow_mdns && MDNS != nil {
  20. //Create a new ioT Manager
  21. iotManager = iot.NewIoTManager()
  22. //Register IoT Hub Module
  23. moduleHandler.RegisterModule(module.ModuleInfo{
  24. Name: "IoT Hub",
  25. Group: "System Tools",
  26. IconPath: "SystemAO/iot/hub/img/small_icon.png",
  27. Version: "1.0",
  28. StartDir: "SystemAO/iot/hub/index.html",
  29. SupportFW: true,
  30. InitFWSize: []int{465, 730},
  31. LaunchFWDir: "SystemAO/iot/hub/index.html",
  32. SupportEmb: false,
  33. })
  34. //Register IoT Setting Interfaces
  35. registerSetting(settingModule{
  36. Name: "IoT Hub",
  37. Desc: "Manage IoT Devices Scanners",
  38. IconPath: "SystemAO/iot/img/small_icon.png",
  39. Group: "Device",
  40. StartDir: "SystemAO/iot/info.html",
  41. })
  42. //Register IoT Devices Endpoints
  43. router := prout.NewModuleRouter(prout.RouterOption{
  44. ModuleName: "IoT Panel",
  45. AdminOnly: false,
  46. UserHandler: userHandler,
  47. DeniedHandler: func(w http.ResponseWriter, r *http.Request) {
  48. sendErrorResponse(w, "Permission Denied")
  49. },
  50. })
  51. adminRouter := prout.NewModuleRouter(prout.RouterOption{
  52. ModuleName: "System Setting",
  53. AdminOnly: true,
  54. UserHandler: userHandler,
  55. DeniedHandler: func(w http.ResponseWriter, r *http.Request) {
  56. sendErrorResponse(w, "Permission Denied")
  57. },
  58. })
  59. //IoT Panel control APIs
  60. router.HandleFunc("/system/iot/scan", iotManager.HandleScanning)
  61. router.HandleFunc("/system/iot/list", iotManager.HandleListing)
  62. router.HandleFunc("/system/iot/status", iotManager.HandleGetDeviceStatus)
  63. router.HandleFunc("/system/iot/execute", iotManager.HandleExecute)
  64. router.HandleFunc("/system/iot/icon", iotManager.HandleIconLoad)
  65. //IoT Hub Info APIs
  66. adminRouter.HandleFunc("/system/iot/listScanner", iotManager.HandleScannerList)
  67. //Start of the IoT Management Handlers
  68. //Home Dynamic v1 (Legacy)
  69. hdsHandler := hds.NewProtocolHandler()
  70. iotManager.RegisterHandler(hdsHandler)
  71. //Home Dynamic v2
  72. hdsv2Handler := hdsv2.NewProtocolHandler(MDNS)
  73. iotManager.RegisterHandler(hdsv2Handler)
  74. //Add more here if needed
  75. }
  76. }