iot.go 2.4 KB

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