iot.go 2.8 KB

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