iot.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. "imuslab.com/arozos/mod/iot/sonoff_s2x"
  8. module "imuslab.com/arozos/mod/modules"
  9. prout "imuslab.com/arozos/mod/prouter"
  10. "imuslab.com/arozos/mod/utils"
  11. )
  12. /*
  13. IoT Hub
  14. Author: tobychui
  15. This script handle the IoT service start up and mangement
  16. IoT Manager: Manage who can have access to certain IoT devices
  17. IoT Panel: The panel for controlling the devices
  18. */
  19. var iotManager *iot.Manager
  20. func IoTHubInit() {
  21. if *allow_iot && *allow_mdns && MDNS != nil {
  22. //Create a new ioT Manager
  23. iotManager = iot.NewIoTManager(sysdb)
  24. //Register IoT Hub Module
  25. moduleHandler.RegisterModule(module.ModuleInfo{
  26. Name: "IoT Hub",
  27. Group: "Internet",
  28. IconPath: "SystemAO/iot/hub/img/small_icon.png",
  29. Version: "1.0",
  30. StartDir: "SystemAO/iot/hub/index.html",
  31. SupportFW: true,
  32. InitFWSize: []int{465, 730},
  33. LaunchFWDir: "SystemAO/iot/hub/index.html",
  34. SupportEmb: false,
  35. })
  36. //Register IoT Setting Interfaces
  37. registerSetting(settingModule{
  38. Name: "IoT Hub",
  39. Desc: "Manage IoT Devices Scanners",
  40. IconPath: "SystemAO/iot/img/small_icon.png",
  41. Group: "Device",
  42. StartDir: "SystemAO/iot/info.html",
  43. })
  44. //Register IoT Devices Endpoints
  45. router := prout.NewModuleRouter(prout.RouterOption{
  46. ModuleName: "IoT Hub",
  47. AdminOnly: false,
  48. UserHandler: userHandler,
  49. DeniedHandler: func(w http.ResponseWriter, r *http.Request) {
  50. utils.SendErrorResponse(w, "Permission Denied")
  51. },
  52. })
  53. adminRouter := prout.NewModuleRouter(prout.RouterOption{
  54. ModuleName: "System Setting",
  55. AdminOnly: true,
  56. UserHandler: userHandler,
  57. DeniedHandler: func(w http.ResponseWriter, r *http.Request) {
  58. utils.SendErrorResponse(w, "Permission Denied")
  59. },
  60. })
  61. //IoT Panel control APIs
  62. router.HandleFunc("/system/iot/scan", iotManager.HandleScanning)
  63. router.HandleFunc("/system/iot/list", iotManager.HandleListing)
  64. router.HandleFunc("/system/iot/status", iotManager.HandleGetDeviceStatus)
  65. router.HandleFunc("/system/iot/execute", iotManager.HandleExecute)
  66. router.HandleFunc("/system/iot/icon", iotManager.HandleIconLoad)
  67. router.HandleFunc("/system/iot/nickname", iotManager.HandleNickName)
  68. //IoT Hub Info APIs
  69. adminRouter.HandleFunc("/system/iot/listScanner", iotManager.HandleScannerList)
  70. //Start of the IoT Management Handlers
  71. //Home Dynamic v1 (Legacy)
  72. hdsHandler := hds.NewProtocolHandler()
  73. iotManager.RegisterHandler(hdsHandler)
  74. //Home Dynamic v2
  75. hdsv2Handler := hdsv2.NewProtocolHandler(MDNS)
  76. iotManager.RegisterHandler(hdsv2Handler)
  77. //Tasmota Sonoff S2X
  78. tasmotaSonoffS2x := sonoff_s2x.NewProtocolHandler(MDNS)
  79. iotManager.RegisterHandler(tasmotaSonoffS2x)
  80. //Add more here if needed
  81. //Start the initial scanning
  82. go func() {
  83. iotManager.ScanDevices()
  84. systemWideLogger.PrintAndLog("IoT", "Initial IoT device scanning completed", nil)
  85. }()
  86. //Finally, inject the gateway into the AGI interface
  87. AGIGateway.Option.IotManager = iotManager
  88. }
  89. }