iot.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. //IoT Hub Info APIs
  65. adminRouter.HandleFunc("/system/iot/listScanner", iotManager.HandleScannerList)
  66. //Start of the IoT Management Handlers
  67. //Home Dynamic v1 (Legacy)
  68. hdsHandler := hds.NewProtocolHandler()
  69. iotManager.RegisterHandler(hdsHandler)
  70. //Home Dynamic v2
  71. hdsv2Handler := hdsv2.NewProtocolHandler(MDNS)
  72. iotManager.RegisterHandler(hdsv2Handler)
  73. //Add more here if needed
  74. }
  75. }