mm_os 3.3.1 → 4.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (380) hide show
  1. package/LICENSE +21 -201
  2. package/README.md +491 -99
  3. package/README_EN.md +498 -0
  4. package/adapter/adapter.js +431 -0
  5. package/adapter/custom_persistence.js +660 -0
  6. package/adapter/mqtt.js +273 -0
  7. package/adapter/socket.js +113 -0
  8. package/adapter/web.js +67 -0
  9. package/adapter/websocket.js +146 -0
  10. package/com/api/com.json +5 -0
  11. package/{core/com → com}/api/config.tpl.json +8 -8
  12. package/com/api/drive.js +708 -0
  13. package/com/api/index.js +198 -0
  14. package/com/api/oauth.js +200 -0
  15. package/com/api/script.tpl.js +32 -0
  16. package/com/cmd/README.md +11 -0
  17. package/com/cmd/com.json +5 -0
  18. package/com/cmd/config.tpl.json +122 -0
  19. package/com/cmd/drive.js +1548 -0
  20. package/com/cmd/index.js +1066 -0
  21. package/com/cmd/msg.json +48 -0
  22. package/com/cmd/nlp.js +525 -0
  23. package/com/cmd/script.tpl.js +32 -0
  24. package/com/db/com.json +5 -0
  25. package/com/db/drive.js +1999 -0
  26. package/com/db/index.js +242 -0
  27. package/{core/com → com}/event/README.md +4 -4
  28. package/com/event/com.json +5 -0
  29. package/{core/com → com}/event/config.tpl.json +18 -18
  30. package/com/event/drive.js +59 -0
  31. package/com/event/index.js +409 -0
  32. package/com/event/script.tpl.js +23 -0
  33. package/com/mqtt/com.json +5 -0
  34. package/{core/com → com}/mqtt/config.tpl.json +3 -5
  35. package/com/mqtt/drive.js +676 -0
  36. package/com/mqtt/index.js +822 -0
  37. package/com/mqtt/mm_mqtt.js +425 -0
  38. package/com/mqtt/script.tpl.js +723 -0
  39. package/com/nav/com.json +5 -0
  40. package/com/nav/config.tpl.json +84 -0
  41. package/com/nav/drive.js +702 -0
  42. package/com/nav/index.js +231 -0
  43. package/{core/com → com}/nav/tpl/admin_pc/page_config.vue +280 -280
  44. package/{core/com → com}/nav/tpl/admin_pc/page_config_form.vue +194 -194
  45. package/com/nav/tpl/admin_pc/page_form.vue +180 -0
  46. package/com/nav/tpl/admin_pc/page_view.vue +124 -0
  47. package/com/nav/tpl/dev_pc/page_default.vue +247 -0
  48. package/com/nav/tpl/dev_pc/page_type.vue +313 -0
  49. package/com/nav/tpl/home_pc/page_default.vue +234 -0
  50. package/com/nav/tpl/home_pc/page_form.vue +137 -0
  51. package/com/nav/tpl/home_pc/page_list.vue +234 -0
  52. package/com/nav/tpl/home_pc/page_nav.vue +221 -0
  53. package/com/nav/tpl/home_pc/page_type.vue +234 -0
  54. package/com/nav/tpl/home_pc/page_view.vue +125 -0
  55. package/com/nav/tpl/home_phone/page_channel.vue +234 -0
  56. package/com/nav/tpl/home_phone/page_default.vue +234 -0
  57. package/com/nav/tpl/home_phone/page_form.vue +137 -0
  58. package/com/nav/tpl/home_phone/page_nav.vue +237 -0
  59. package/com/nav/tpl/home_phone/page_type.vue +234 -0
  60. package/com/nav/tpl/home_phone/page_view.vue +125 -0
  61. package/com/nav/viewmodel.js +446 -0
  62. package/com/param/com.json +5 -0
  63. package/{core/com → com}/param/config.tpl.json +7 -1
  64. package/com/param/drive.js +502 -0
  65. package/com/param/index.js +155 -0
  66. package/com/param/script.tpl.js +12 -0
  67. package/com/pendant/com.json +5 -0
  68. package/{core/com/component → com/pendant}/config.tpl.json +15 -13
  69. package/com/pendant/drive.js +204 -0
  70. package/com/pendant/index.js +441 -0
  71. package/com/pendant/pendant.html +16 -0
  72. package/com/pendant/script.tpl.js +18 -0
  73. package/com/socket/com.json +5 -0
  74. package/com/socket/config.tpl.json +12 -0
  75. package/com/socket/drive.js +651 -0
  76. package/com/socket/index.js +351 -0
  77. package/com/socket/script.tpl.js +41 -0
  78. package/com/sql/com.json +5 -0
  79. package/{core/com → com}/sql/config.tpl.json +13 -9
  80. package/com/sql/drive.js +1259 -0
  81. package/com/sql/index.js +150 -0
  82. package/com/sql/script.tpl.js +47 -0
  83. package/com/static/com.json +5 -0
  84. package/{core/com → com}/static/config.tpl.json +10 -6
  85. package/com/static/drive.js +194 -0
  86. package/com/static/index.js +226 -0
  87. package/com/static/script.tpl.js +28 -0
  88. package/com/task/com.json +5 -0
  89. package/{core/com → com}/task/config.tpl.json +4 -6
  90. package/com/task/drive.js +405 -0
  91. package/com/task/index.js +148 -0
  92. package/com/task/script.tpl.js +37 -0
  93. package/com/template/com.json +5 -0
  94. package/com/template/config.tpl.json +16 -0
  95. package/com/template/drive.js +80 -0
  96. package/com/template/index.js +141 -0
  97. package/com.js +156 -0
  98. package/common/README.md +2 -0
  99. package/common/handler/msg/handler.json +22 -0
  100. package/common/handler/msg/index.js +23 -0
  101. package/common/handler/player/handler.json +22 -0
  102. package/common/handler/player/index.js +287 -0
  103. package/common/handler/user/handler.json +22 -0
  104. package/common/handler/user/index.js +23 -0
  105. package/common/middleware/web_after/index.js +29 -0
  106. package/common/middleware/web_after/middleware.json +9 -0
  107. package/common/middleware/web_base/index.js +113 -0
  108. package/common/middleware/web_base/middleware.json +19 -0
  109. package/common/middleware/web_before/index.js +33 -0
  110. package/common/middleware/web_before/middleware.json +9 -0
  111. package/common/middleware/web_cors/index.js +87 -0
  112. package/common/middleware/web_cors/middleware.json +24 -0
  113. package/common/middleware/web_error/index.js +119 -0
  114. package/common/middleware/web_error/middleware.json +18 -0
  115. package/common/middleware/web_ip/index.js +15 -0
  116. package/common/middleware/web_ip/middleware.json +14 -0
  117. package/common/middleware/web_logger/index.js +156 -0
  118. package/common/middleware/web_logger/middleware.json +14 -0
  119. package/common/middleware/web_main/index.js +24 -0
  120. package/common/middleware/web_main/middleware.json +9 -0
  121. package/common/middleware/web_static/index.js +73 -0
  122. package/common/middleware/web_static/middleware.json +54 -0
  123. package/common/middleware/web_waf/index.js +385 -0
  124. package/common/middleware/web_waf/middleware.json +13 -0
  125. package/common/model/msg/index.js +88 -0
  126. package/common/model/msg/model.json +401 -0
  127. package/common/model/player/index.js +63 -0
  128. package/common/model/player/model.json +185 -0
  129. package/common/model/user/index.js +11 -0
  130. package/common/model/user/model.json +219 -0
  131. package/core/app/config.tpl.json +67 -0
  132. package/core/app/index.js +632 -0
  133. package/core/app/script.tpl.js +52 -0
  134. package/core/channel/index.js +899 -0
  135. package/core/channel/matcher.js +585 -0
  136. package/core/com/config.tpl.json +16 -0
  137. package/core/com/index.js +74 -0
  138. package/core/com/script.tpl.js +5 -0
  139. package/core/component/component.js +42 -0
  140. package/core/component/config.tpl.json +63 -0
  141. package/core/component/index.js +273 -0
  142. package/core/component/script.tpl.js +19 -0
  143. package/core/controller/config.tpl.json +14 -0
  144. package/core/controller/index.js +373 -0
  145. package/core/controller/script.tpl.js +27 -0
  146. package/core/factory/config.tpl.json +14 -0
  147. package/core/factory/entity.js +275 -0
  148. package/core/factory/index.js +241 -0
  149. package/core/factory/script.tpl.js +16 -0
  150. package/core/game/bat/index.js +137 -0
  151. package/core/game/bat/world.js +622 -0
  152. package/core/game/config.tpl.json +16 -0
  153. package/core/game/entity_admin.js +230 -0
  154. package/core/game/index.js +186 -0
  155. package/core/handler/config.tpl.json +22 -0
  156. package/core/handler/index.js +181 -0
  157. package/core/handler/script.tpl.js +23 -0
  158. package/core/logic/config.tpl.json +14 -0
  159. package/core/logic/index.js +59 -0
  160. package/core/logic/script.tpl.js +19 -0
  161. package/core/middleware/config.tpl.json +16 -0
  162. package/core/middleware/index.js +125 -0
  163. package/core/middleware/script.tpl.js +37 -0
  164. package/core/mod/config.tpl.json +22 -0
  165. package/core/mod/index.js +130 -0
  166. package/core/mod/script.tpl.js +34 -0
  167. package/core/model/config.tpl.json +219 -0
  168. package/core/model/index.js +272 -0
  169. package/core/model/model.js +27 -0
  170. package/core/model/script.tpl.js +20 -0
  171. package/core/notifier/config.tpl.json +14 -0
  172. package/core/notifier/index.js +77 -0
  173. package/core/notifier/script.tpl.js +20 -0
  174. package/core/plugin/config.tpl.json +24 -0
  175. package/core/plugin/index.js +232 -0
  176. package/core/plugin/script.tpl.js +51 -0
  177. package/core/pusher/config.tpl.json +14 -0
  178. package/core/pusher/index.js +161 -0
  179. package/core/pusher/script.tpl.js +20 -0
  180. package/core/room/bat/index.js +170 -0
  181. package/core/room/bat/room.js +524 -0
  182. package/core/room/config.tpl.json +20 -0
  183. package/core/room/index.js +249 -0
  184. package/core/room/room.js +61 -0
  185. package/core/scene/config.tpl.json +14 -0
  186. package/core/scene/index.js +466 -0
  187. package/core/scene/loop.js +1255 -0
  188. package/core/scene/map.js +28 -0
  189. package/core/scene/script.tpl.js +22 -0
  190. package/core/sender/config.tpl.json +14 -0
  191. package/core/sender/index.js +79 -0
  192. package/core/sender/script.tpl.js +20 -0
  193. package/core/service/config.tpl.json +14 -0
  194. package/core/service/index.js +100 -0
  195. package/core/service/script.tpl.js +25 -0
  196. package/core/store/config.tpl.json +26 -0
  197. package/core/store/index.js +1755 -0
  198. package/core/store/script.tpl.js +22 -0
  199. package/core/store/sql.js +1464 -0
  200. package/core/system/config.tpl.json +18 -0
  201. package/core/system/index.js +312 -0
  202. package/core/system/script.tpl.js +77 -0
  203. package/core/view/config.tpl.json +14 -0
  204. package/core/view/index.js +91 -0
  205. package/core/view/script.tpl.js +20 -0
  206. package/core/zone/bat/index.js +725 -0
  207. package/core/zone/config.tpl.json +54 -0
  208. package/core/zone/index.js +614 -0
  209. package/core/zone/script.tpl.js +10 -0
  210. package/core/zone/zone_bat.js +136 -0
  211. package/core//345/237/272/347/261/273/346/250/241/345/235/227/346/270/205/345/215/225.md +24 -0
  212. package/index.js +17 -333
  213. package/os.js +57 -0
  214. package/package.json +58 -58
  215. package/server.js +598 -0
  216. package/README.en.md +0 -36
  217. package/conf.json +0 -3
  218. package/core/base/mqtt/index.js +0 -1110
  219. package/core/base/mqtt/lib.js +0 -40
  220. package/core/base/web/index.js +0 -245
  221. package/core/com/api/com.json +0 -4
  222. package/core/com/api/drive.js +0 -668
  223. package/core/com/api/index.js +0 -108
  224. package/core/com/api/oauth.js +0 -158
  225. package/core/com/api/script.js +0 -32
  226. package/core/com/app/README.md +0 -3
  227. package/core/com/app/com.json +0 -4
  228. package/core/com/app/config.tpl.json +0 -16
  229. package/core/com/app/drive.js +0 -309
  230. package/core/com/app/index.js +0 -211
  231. package/core/com/app/script.js +0 -155
  232. package/core/com/cmd/com.json +0 -4
  233. package/core/com/cmd/config.tpl.json +0 -66
  234. package/core/com/cmd/drive.js +0 -513
  235. package/core/com/cmd/index.js +0 -354
  236. package/core/com/cmd/old/5w2h.js +0 -54
  237. package/core/com/cmd/old/drive.js +0 -423
  238. package/core/com/cmd/script.js +0 -11
  239. package/core/com/component/README.md +0 -3
  240. package/core/com/component/com.json +0 -4
  241. package/core/com/component/component.html +0 -16
  242. package/core/com/component/drive.js +0 -197
  243. package/core/com/component/index.js +0 -312
  244. package/core/com/component/script.js +0 -18
  245. package/core/com/db/com.json +0 -4
  246. package/core/com/db/drive.js +0 -1160
  247. package/core/com/db/index.js +0 -176
  248. package/core/com/event/com.json +0 -4
  249. package/core/com/event/drive.js +0 -133
  250. package/core/com/event/index.js +0 -345
  251. package/core/com/event/script.js +0 -26
  252. package/core/com/eventer/com.js +0 -477
  253. package/core/com/eventer/com.json +0 -4
  254. package/core/com/middleware/com.js +0 -154
  255. package/core/com/middleware/com.json +0 -4
  256. package/core/com/middleware/config.tpl.json +0 -8
  257. package/core/com/middleware/script.js +0 -9
  258. package/core/com/mqtt/com.json +0 -4
  259. package/core/com/mqtt/drive.js +0 -600
  260. package/core/com/mqtt/index.js +0 -572
  261. package/core/com/mqtt/mm_mqtt.js +0 -330
  262. package/core/com/mqtt/script.js +0 -604
  263. package/core/com/msg/com.js +0 -296
  264. package/core/com/msg/com.json +0 -4
  265. package/core/com/nav/com.json +0 -4
  266. package/core/com/nav/config.tpl.json +0 -75
  267. package/core/com/nav/drive.js +0 -549
  268. package/core/com/nav/index.js +0 -182
  269. package/core/com/nav/tpl/admin_pc/page_form.vue +0 -180
  270. package/core/com/nav/tpl/admin_pc/page_view.vue +0 -124
  271. package/core/com/nav/tpl/dev_pc/page_default.vue +0 -247
  272. package/core/com/nav/tpl/dev_pc/page_type.vue +0 -313
  273. package/core/com/nav/tpl/home_pc/page_default.vue +0 -234
  274. package/core/com/nav/tpl/home_pc/page_form.vue +0 -137
  275. package/core/com/nav/tpl/home_pc/page_list.vue +0 -234
  276. package/core/com/nav/tpl/home_pc/page_nav.vue +0 -221
  277. package/core/com/nav/tpl/home_pc/page_type.vue +0 -234
  278. package/core/com/nav/tpl/home_pc/page_view.vue +0 -125
  279. package/core/com/nav/tpl/home_phone/page_channel.vue +0 -234
  280. package/core/com/nav/tpl/home_phone/page_default.vue +0 -234
  281. package/core/com/nav/tpl/home_phone/page_form.vue +0 -137
  282. package/core/com/nav/tpl/home_phone/page_nav.vue +0 -237
  283. package/core/com/nav/tpl/home_phone/page_type.vue +0 -234
  284. package/core/com/nav/tpl/home_phone/page_view.vue +0 -125
  285. package/core/com/nav/viewmodel.js +0 -296
  286. package/core/com/param/drive.js +0 -366
  287. package/core/com/param/index.js +0 -80
  288. package/core/com/param/script.js +0 -12
  289. package/core/com/param/test.js +0 -98
  290. package/core/com/plugin/README.md +0 -3
  291. package/core/com/plugin/com.json +0 -4
  292. package/core/com/plugin/config.tpl.json +0 -26
  293. package/core/com/plugin/drive.js +0 -536
  294. package/core/com/plugin/index.js +0 -259
  295. package/core/com/plugin/script.js +0 -213
  296. package/core/com/rpc/com.json +0 -4
  297. package/core/com/rpc/drive.js +0 -160
  298. package/core/com/rpc/index.js +0 -87
  299. package/core/com/rpc/rpc.js +0 -118
  300. package/core/com/socket/com.json +0 -4
  301. package/core/com/socket/config.tpl.json +0 -14
  302. package/core/com/socket/drive.js +0 -403
  303. package/core/com/socket/index.js +0 -62
  304. package/core/com/socket/script.js +0 -42
  305. package/core/com/sql/drive.js +0 -1087
  306. package/core/com/sql/index.js +0 -83
  307. package/core/com/sql/script.js +0 -48
  308. package/core/com/static/com.json +0 -4
  309. package/core/com/static/drive.js +0 -220
  310. package/core/com/static/index.js +0 -149
  311. package/core/com/static/script.js +0 -28
  312. package/core/com/task/com.json +0 -4
  313. package/core/com/task/drive.js +0 -403
  314. package/core/com/task/index.js +0 -110
  315. package/core/com/task/script.js +0 -37
  316. package/core/com/timer/com.js +0 -217
  317. package/core/com/timer/com.json +0 -4
  318. package/core/com/tpl/com.js +0 -19
  319. package/core/com/tpl/com.json +0 -4
  320. package/lib/actions.js +0 -50
  321. package/lib/base.js +0 -361
  322. package/lib/com.js +0 -29
  323. package/lib/ref.js +0 -121
  324. package/middleware/cors/index.js +0 -119
  325. package/middleware/cors/middleware.json +0 -20
  326. package/middleware/csrf/index.js +0 -202
  327. package/middleware/csrf/middleware.json +0 -24
  328. package/middleware/ip_firewall/index.js +0 -476
  329. package/middleware/ip_firewall/middleware.json +0 -109
  330. package/middleware/mqtt_base/index.js +0 -10
  331. package/middleware/mqtt_base/middleware.json +0 -11
  332. package/middleware/security_audit/index.js +0 -543
  333. package/middleware/security_audit/middleware.json +0 -48
  334. package/middleware/waf/index.js +0 -343
  335. package/middleware/waf/middleware.json +0 -10
  336. package/middleware/waf_ddos/index.js +0 -520
  337. package/middleware/waf_ddos/middleware.json +0 -38
  338. package/middleware/waf_xss/index.js +0 -269
  339. package/middleware/waf_xss/middleware.json +0 -18
  340. package/middleware/web_after/index.js +0 -33
  341. package/middleware/web_after/middleware.json +0 -10
  342. package/middleware/web_base/index.js +0 -90
  343. package/middleware/web_base/middleware.json +0 -10
  344. package/middleware/web_before/index.js +0 -27
  345. package/middleware/web_before/middleware.json +0 -10
  346. package/middleware/web_check/index.js +0 -28
  347. package/middleware/web_check/middleware.json +0 -10
  348. package/middleware/web_main/index.js +0 -28
  349. package/middleware/web_main/middleware.json +0 -10
  350. package/middleware/web_proxy/index.js +0 -37
  351. package/middleware/web_proxy/middleware.json +0 -10
  352. package/middleware/web_render/index.js +0 -87
  353. package/middleware/web_render/middleware.json +0 -10
  354. package/middleware/web_socket/index.js +0 -34
  355. package/middleware/web_socket/middleware.json +0 -10
  356. package/middleware/web_static/index.js +0 -115
  357. package/middleware/web_static/middleware.json +0 -10
  358. /package/{core/com → com}/api/README.md +0 -0
  359. /package/{core/com → com}/db/README.md +0 -0
  360. /package/{core/com → com}/mqtt/README.md +0 -0
  361. /package/{core/com → com}/nav/README.md +0 -0
  362. /package/{core/com → com}/nav/tpl/admin_pc/page_channel.vue +0 -0
  363. /package/{core/com → com}/nav/tpl/admin_pc/page_default.vue +0 -0
  364. /package/{core/com → com}/nav/tpl/admin_pc/page_lang.vue +0 -0
  365. /package/{core/com → com}/nav/tpl/admin_pc/page_nav.vue +0 -0
  366. /package/{core/com → com}/nav/tpl/admin_pc/page_table.vue +0 -0
  367. /package/{core/com → com}/nav/tpl/admin_pc/page_type.vue +0 -0
  368. /package/{core/com → com}/nav/tpl/dev_pc/page_channel.vue +0 -0
  369. /package/{core/com → com}/nav/tpl/dev_pc/page_config.vue +0 -0
  370. /package/{core/com → com}/nav/tpl/dev_pc/page_form.vue +0 -0
  371. /package/{core/com → com}/nav/tpl/dev_pc/page_nav.vue +0 -0
  372. /package/{core/com → com}/nav/tpl/dev_pc/page_table.vue +0 -0
  373. /package/{core/com → com}/nav/tpl/home_pc/page_channel.vue +0 -0
  374. /package/{core/com → com}/nav/tpl/home_phone/page_list.vue +0 -0
  375. /package/{core/com → com}/param/README.md +0 -0
  376. /package/{core/com/cmd → com/pendant}/README.md +0 -0
  377. /package/{core/com → com}/socket/README.md +0 -0
  378. /package/{core/com → com}/sql/README.md +0 -0
  379. /package/{core/com → com}/static/README.md +0 -0
  380. /package/{core/com → com}/task/README.md +0 -0
@@ -0,0 +1,676 @@
1
+ const os = require('os');
2
+ const util = require('util');
3
+ const Item = require('mm_machine').Drive;
4
+
5
+ /**
6
+ * mqtt驱动类
7
+ * @augments {Item}
8
+ * @class
9
+ */
10
+ class Drive extends Item {
11
+ static config = {
12
+ // 订阅的主题
13
+ 'topic': [],
14
+ // 主题回复
15
+ 'topic_receive': [],
16
+ // 给客户端推送的主题
17
+ 'topic_push': 'mqtt/client/${clientid}',
18
+ // 接收方式
19
+ 'qos': 1,
20
+ // 推送方式
21
+ 'qos_push': 1,
22
+ // 保持接收
23
+ 'retain': true,
24
+ // 超时反馈
25
+ 'longtime': 10000,
26
+ // 消息ID
27
+ 'clientid': 'clientid',
28
+ // 消息ID
29
+ 'msgid': 'msgid',
30
+ // 请求方法
31
+ 'method': 'method',
32
+ // 参数
33
+ 'params': 'params',
34
+ // 结果
35
+ 'result': 'result',
36
+ // 回复方法
37
+ 'method_receive': '${method}-Ack',
38
+ // 服务器地址
39
+ 'host': ''
40
+ };
41
+ /**
42
+ * 构造函数
43
+ * @param {object} config 配置参数
44
+ * @param {object} parent 父对象
45
+ * @class
46
+ */
47
+ constructor(config, parent) {
48
+ super({ ...Drive.config, ...config }, parent);
49
+
50
+ this.mode = 3;
51
+ }
52
+ }
53
+
54
+ /**
55
+ * 配置预设
56
+ */
57
+ Drive.prototype._preset = function () {
58
+ // 开放给前端调用的函数
59
+ this.methods = { ...$.methods };
60
+
61
+ // 客户端列表
62
+ this.clients = {};
63
+ };
64
+
65
+ /**
66
+ * 配置示例
67
+ * @returns {object} 返回配置示例
68
+ */
69
+ Drive.prototype.model = function () {
70
+ // 单层匹配订阅,用于订阅所有客户端要接收的数据 格式:$SYS/<代理服务器>/service/业务/+
71
+ // "topic": "$SYS/mm/service/user/+"
72
+ // 通配方式订阅,用于订阅仅该设备或该客户端要订阅的数据 格式:$SYS/<代理服务器>/service/appid/#
73
+ // "topic": "$SYS/mm/service/idd6877e1561/#"
74
+ // 完全匹配订阅,用于特定的业务订阅,业务压力最小,建议尽可能使用完全匹配
75
+ // "topic": "$SYS/mm/service/state"
76
+ return {
77
+ // 名称, 由中英文和下“_”组成, 用于修改或卸载 例如: demo
78
+ 'name': 'demo_test',
79
+ // mqtt 服务标题
80
+ 'title': '示例MQTT',
81
+ // mqtt 服务介绍
82
+ 'description': '',
83
+ // 状态 0未启用,1启用
84
+ 'state': 1,
85
+ // 订阅的主题
86
+ 'topic': ['mqtt/face/#', 'face/+/response', 'mqtt/server/#'],
87
+ // 回复订阅主题
88
+ 'topic_receive': ['mqtt/face/${clientid}', 'face/${clientid}/request'],
89
+ // 给客户端推送的主题
90
+ 'topic_push': 'mqtt/client/${clientid}',
91
+ // 接收方式
92
+ 'qos': 1,
93
+ // 推送方式
94
+ 'qos_push': 1,
95
+ // 保持接收
96
+ 'retain': true,
97
+ // 超时反馈
98
+ 'longtime': 10000,
99
+ // 消息ID
100
+ 'clientid': 'clientid',
101
+ // 消息ID
102
+ 'msgid': 'msgid',
103
+ // 请求方法
104
+ 'method': 'method',
105
+ // 参数
106
+ 'params': 'params',
107
+ // 结果
108
+ 'result': 'result',
109
+ // 回复方法
110
+ 'method_receive': '${method}-Ack'
111
+ };
112
+ };
113
+
114
+ /**
115
+ * 新建配置
116
+ * @param {string} file 文件名
117
+ */
118
+ Drive.prototype.newConfig = function (file) {
119
+ var fl = __dirname + '/config.tpl.json';
120
+ if (fl.hasFile()) {
121
+ var text = fl.loadText();
122
+ if (text) {
123
+ var l = $.slash;
124
+ if (file.indexOf('mqtt' + l) !== -1) {
125
+ var name = file.between('mqtt' + l, l);
126
+ text = text.replaceAll('{0}', name);
127
+ }
128
+ file.saveText(text);
129
+ }
130
+ }
131
+ };
132
+
133
+ /**
134
+ * 获取IP
135
+ * @returns {string} 返回IP地址
136
+ */
137
+ Drive.prototype.getIp = function () {
138
+ let interfaces = os.networkInte();
139
+ for (let iface of Object.values(interfaces)) {
140
+ for (let config of iface) {
141
+ if (config.family === 'IPv4' && !config.internal) {
142
+ return config.address;
143
+ }
144
+ }
145
+ }
146
+ return '127.0.0.1'; // 如果没有找到,返回本地回环地址
147
+ };
148
+
149
+ /**
150
+ * 新建消息ID
151
+ * @param {string} method 方法
152
+ * @param {string} terminal 终端
153
+ * @returns {string} 返回消息ID
154
+ */
155
+ Drive.prototype.getMsgId = function (method, terminal = 'server') {
156
+ var time = new Date().getTime();
157
+ var ip = this.getIp();
158
+ // var msgid = "ID:localhost-" + clientid + "-" + time;
159
+ var msgid = `${terminal}:${ip}-${time}-${method}`;
160
+ return msgid;
161
+ };
162
+
163
+ /**
164
+ * 回复的方法格式
165
+ * @param {string} method 方法
166
+ * @returns {string} 返回回复方法
167
+ */
168
+ Drive.prototype.getMethod = function (method) {
169
+ var str = this.config.method_receive;
170
+ return str.replace('${method}', method);
171
+ };
172
+
173
+ /**
174
+ * 获取回复KEY
175
+ * @param {string} method 方法
176
+ * @param {string} clientid 客户端ID
177
+ * @param {string} msgid 消息ID
178
+ * @returns {string} 返回KEY
179
+ */
180
+ Drive.prototype.getReceiveKey = function (method, clientid, msgid) {
181
+ return this.getMethod(method) + '_' + clientid + '_' + msgid;
182
+ };
183
+
184
+ /**
185
+ * 获取KEY
186
+ * @param {string} method 方法
187
+ * @param {string} clientid 客户端ID
188
+ * @param {string} msgid 消息ID
189
+ * @returns {string} 返回KEY
190
+ */
191
+ Drive.prototype.getKey = function (method, clientid, msgid) {
192
+ return method + '_' + clientid + '_' + msgid;
193
+ };
194
+
195
+ /**
196
+ * 补全路径
197
+ * @param {string} path 路径
198
+ * @returns {string} 返回完整的路径
199
+ */
200
+ Drive.prototype.fullUrl = function (path) {
201
+ var src = path || '';
202
+ if (src.indexOf('http') !== 0) {
203
+ var host = this.config.host || $.host;
204
+ if (host.indexOf('http') !== 0) {
205
+ host = 'http://' + host;
206
+ }
207
+ if (src.indexOf('~/') === 0) {
208
+ src = src.replace('~/', host);
209
+ } else if (src.indexOf('/') === 0) {
210
+ src = src.replace('/', host);
211
+ }
212
+ }
213
+ return src;
214
+ };
215
+
216
+ /**
217
+ * 推送消息
218
+ * @param {string} topic 推送主题
219
+ * @param {object} msg 推送内容
220
+ * @param {number} qos 接收方式
221
+ * @param {boolean} retain 是否保持推送
222
+ */
223
+ Drive.prototype.send = function (topic, msg, qos = 0, retain = false) {
224
+
225
+ };
226
+
227
+ /**
228
+ * 收到消息
229
+ * @param {string} push_topic 推送过来的主题
230
+ * @param {object} msg 原消息
231
+ * @param {number} index 索引
232
+ * @param {string} topic 订阅的主题
233
+ */
234
+ Drive.prototype.message = async function (push_topic, msg, index, topic) {
235
+ // console.log("请求后", push_topic, msg, index, topic);
236
+ };
237
+
238
+ /**
239
+ * 推送消息到设备
240
+ * @param {string} clientid 客户端ID
241
+ * @param {object} msg 消息主题
242
+ * @param {Function} func 回调函数
243
+ * @param {number} longtime 超时时间
244
+ */
245
+ Drive.prototype.pushSync = function (clientid, msg, func, longtime = 0) {
246
+ var {
247
+ msgid,
248
+ method,
249
+ topic_push,
250
+ qos_push
251
+ } = this.config;
252
+ var id = msg[msgid];
253
+ if (!id) {
254
+ msg[msgid] = this.getMsgId(msg[method], 'server');
255
+ id = msg[msgid];
256
+ }
257
+ var key = this.getReceiveKey(msg[method], clientid, id);
258
+ if (func) {
259
+ if (!this.clients[clientid]) {
260
+ this.clients[clientid] = {};
261
+ }
262
+ this.clients[clientid][key] = {
263
+ clientid,
264
+ id,
265
+ func
266
+ };
267
+ }
268
+
269
+ var topic = this.getTopic(topic_push, clientid);
270
+ this.send(topic, msg, qos_push);
271
+ if (func) {
272
+ setTimeout(async () => {
273
+ // 如果时间到了,回调函数还在队列中,则视为无回复
274
+ if (this.clients[clientid] && this.clients[clientid][key]) {
275
+ try {
276
+ await this.clients[clientid][key].func(null);
277
+ } catch (error) {
278
+ this.log('error', 'MQTT回调错误', key, error);
279
+ //TODO handle the exception
280
+ }
281
+ this.delMsg(clientid, key);
282
+ }
283
+ }, longtime || this.config.longtime || 60000);
284
+ }
285
+ };
286
+
287
+ /**
288
+ * 推送消息
289
+ * @param {string} clientid 客户端ID
290
+ * @param {object} msg 信息
291
+ * @param {number} longtime 超时回馈
292
+ * @returns {object} 返回消息主体
293
+ */
294
+ Drive.prototype.pushS = function (clientid, msg, longtime) {
295
+ var _this = this;
296
+ return new Promise((resolve, reject) => {
297
+ _this.pushSync(clientid, msg, (res) => {
298
+ resolve(res);
299
+ }, longtime);
300
+ });
301
+ };
302
+
303
+ /**
304
+ * 保存设备在线状态
305
+ * @param {string} clientid 客户端ID
306
+ * @param {object} online 是否在线 0为没在线 1为在线
307
+ * @param {object} ip 设备IP
308
+ */
309
+ Drive.prototype.saveOnline = async function (clientid, online, ip) {
310
+ if (!clientid) {
311
+
312
+ }
313
+ };
314
+
315
+ /**
316
+ * 更新设备在线
317
+ * @param {string} clientid 客户端ID
318
+ * @param {boolean} online 是否在线 0为没在线 1为在线
319
+ * @param {string} ip 设备IP
320
+ */
321
+ Drive.prototype.updateOnline = async function (clientid, online = 1, ip = '') {
322
+ if (!this.clients[clientid]) {
323
+ this.clients[clientid] = {};
324
+ }
325
+ var time_last = new Date().getTime(); // 跟新时间戳
326
+ this.clients[clientid].online = online;
327
+ if (online) {
328
+ this.clients[clientid].time_last = time_last;
329
+ }
330
+ var {
331
+ device
332
+ } = await $.server.checkDevice(clientid);
333
+ if (device && online && !device.online) {
334
+ device.online = online;
335
+ if (ip) {
336
+ device.ip = ip;
337
+ }
338
+ }
339
+ };
340
+
341
+ /**
342
+ * 推送消息
343
+ * @param {string} method 方法
344
+ * @param {string} clientid 客户端ID
345
+ * @param {object} params 信息
346
+ * @param {number} longtime 超时回馈
347
+ * @returns {object} 返回消息主体
348
+ */
349
+ Drive.prototype.push = function (method, clientid, params, longtime = 0) {
350
+ var cg = this.config;
351
+ var msg = {};
352
+ msg[cg.msgid] = this.getMsgId(method);
353
+ msg[cg.method] = method;
354
+ msg[cg.params] = params;
355
+ return this.pushS(clientid, msg, longtime);
356
+ };
357
+
358
+ /**
359
+ * 删除消息
360
+ * @param {string} clientid 设备ID
361
+ * @param {string} key 消息键
362
+ */
363
+ Drive.prototype.delMsg = function (clientid, key) {
364
+ if (this.clients[clientid]) {
365
+ var dict = this.clients[clientid];
366
+ delete dict[key];
367
+ }
368
+ };
369
+
370
+ /**
371
+ * 处理事件
372
+ * @param {string} clientid 客户端ID
373
+ * @param {string} key 键
374
+ * @param {object} json 信息
375
+ */
376
+ Drive.prototype.runEvent = function (clientid, key, json) {
377
+ var method = key.left('_');
378
+ if (this[method + '_event']) {
379
+ try {
380
+ this[method + '_event'](clientid, json);
381
+ } catch (err) {
382
+ this.log('error', 'MQTT事件执行失败!', key, err);
383
+ }
384
+ }
385
+ };
386
+
387
+ /**
388
+ * 执行响应
389
+ * @param {string} clientid 客户端ID
390
+ * @param {string} key 键
391
+ * @param {object} json 信息
392
+ * @returns {object} 返回执行结果
393
+ */
394
+ Drive.prototype.run = async function (clientid, key, json) {
395
+ var o;
396
+ if (this.clients[clientid]) {
397
+ o = this.clients[clientid][key];
398
+ }
399
+ if (o) {
400
+ delete this.clients[clientid][key];
401
+ try {
402
+ await o.func(json);
403
+ } catch (err) {
404
+ this.log('error', 'MQTT执行失败!', key, err);
405
+ }
406
+ } else {
407
+ await this.runEvent(clientid, key, json);
408
+ }
409
+ return o;
410
+ };
411
+
412
+ /**
413
+ * 获取客户端ID
414
+ * @param {string} push_topic 推送过来的主题
415
+ * @param {object} body 消息主体
416
+ * @param {object} msg 消息源
417
+ * @param {string} topic 订阅的主题
418
+ * @returns {string} 返回客户端ID
419
+ */
420
+ Drive.prototype.getClientId = function (push_topic, body, msg, topic) {
421
+ var cg = this.config;
422
+ var clientid = body[cg.clientid] || msg[cg.clientid];
423
+ if (!clientid) {
424
+ var str = topic.left('#');
425
+ clientid = push_topic.right(str).left('/', true);
426
+ }
427
+ return clientid;
428
+ };
429
+
430
+ /**
431
+ * 数据格式转换-转入
432
+ * @param {string} push_topic 推送过来的主题
433
+ * @param {object} msg 消息源
434
+ * @param {string} topic 订阅的主题
435
+ * @param {number} index 索引
436
+ * @returns {object} 返回消息主体
437
+ */
438
+ Drive.prototype.toIn = function (push_topic, msg, topic, index) {
439
+ var cg = this.config;
440
+ var method = msg[cg.method];
441
+ var json = {
442
+ clientid: '',
443
+ id: msg[cg.msgid] || this.getMsgId(method, 'client'),
444
+ method
445
+ };
446
+ if (msg[cg.params]) {
447
+ json.params = msg[cg.params];
448
+ }
449
+ if (msg[cg.result]) {
450
+ json.result = msg[cg.result];
451
+ }
452
+ json.clientid = this.getClientId(push_topic, json.params || json.result || {}, msg, topic);
453
+ return json;
454
+ };
455
+
456
+ /**
457
+ * 数据格式转换-转出
458
+ * @param {string} push_topic 推送过来的主题
459
+ * @param {object} msg 消息主体
460
+ * @param {string} topic 订阅的主题
461
+ * @param {number} index 索引
462
+ * @returns {object} 返回消息主体
463
+ */
464
+ Drive.prototype.toOut = function (push_topic, msg, topic, index) {
465
+ return msg;
466
+ };
467
+
468
+ /**
469
+ * 获取回复主题
470
+ * @param {string} topic_tpl 主题模板
471
+ * @param {string} clientid 客户端ID
472
+ * @param {object} params 消息
473
+ * @returns {string} 返回主题
474
+ */
475
+ Drive.prototype.getTopic = function (topic_tpl, clientid, params) {
476
+ return (topic_tpl || '').replace('${clientid}', clientid);
477
+ };
478
+
479
+ /**
480
+ * 处理函数, 用于处理订阅内容
481
+ * @param {string} push_topic 推送过来的主题
482
+ * @param {object} msg 消息正文
483
+ * @param {string} topic 订阅的主题
484
+ * @param {number} index 索引
485
+ * @returns {object} 返回执行结果
486
+ */
487
+ Drive.prototype.run = async function (push_topic, msg, topic, index) {
488
+ var json = this.convertIn(push_topic, msg, topic, index);
489
+
490
+ if (await this._handleMessage(json)) {
491
+ return;
492
+ }
493
+
494
+ if (json.method) {
495
+ await this._handleMethodMessage(json, msg, topic, index);
496
+ }
497
+
498
+ return;
499
+ };
500
+
501
+ /**
502
+ * 处理消息
503
+ * @param {object} json 消息对象
504
+ * @returns {boolean} 是否处理成功
505
+ * @private
506
+ */
507
+ Drive.prototype._handleMessage = async function (json) {
508
+ var id = json.id;
509
+ if (json.result && id) {
510
+ var key = this.getKey(json.method, json.clientid, id);
511
+ var o = await this.run(json.clientid, key, json.result);
512
+ if (o) {
513
+ return true;
514
+ }
515
+ }
516
+ return false;
517
+ };
518
+
519
+ /**
520
+ * 处理方法消息
521
+ * @param {object} json 消息对象
522
+ * @param {object} msg 原始消息
523
+ * @param {string} topic 订阅主题
524
+ * @param {number} index 索引
525
+ * @private
526
+ */
527
+ Drive.prototype._handleMethodMessage = async function (json, msg, topic, index) {
528
+ var func = this._getMethod(json.method);
529
+ if (func) {
530
+ var ret = await this._execMethod(func, json, msg);
531
+ if (ret) {
532
+ await this._sendMethodRes(ret, json, topic, index);
533
+ }
534
+ }
535
+ };
536
+
537
+ /**
538
+ * 获取方法
539
+ * @param {string} methodStr 方法字符串
540
+ * @returns {Function|null} 方法函数
541
+ * @private
542
+ */
543
+ Drive.prototype._getMethod = function (methodStr) {
544
+ var methods = this.methods;
545
+ var arr = methodStr.split('.');
546
+
547
+ for (var i = 0; i < arr.length; i++) {
548
+ var key = arr[i];
549
+ var m = methods[key];
550
+ if (m) {
551
+ methods = m;
552
+ } else {
553
+ return null;
554
+ }
555
+ }
556
+
557
+ return typeof (methods) == 'function' ? methods : null;
558
+ };
559
+
560
+ /**
561
+ * 执行方法
562
+ * @param {Function} func 方法函数
563
+ * @param {object} json 消息对象
564
+ * @param {object} msg 原始消息
565
+ * @returns {*} 方法执行结果
566
+ * @private
567
+ */
568
+ Drive.prototype._execMethod = async function (func, json, msg) {
569
+ var id = json.id;
570
+ var params = json.params;
571
+ var ret;
572
+
573
+ try {
574
+ ret = func(json.clientid, params, msg, id);
575
+ if (util.types.isPromise(ret)) {
576
+ ret = await ret;
577
+ }
578
+ } catch (err) {
579
+ this.log('error', '事件回调失败!', err);
580
+ }
581
+
582
+ return ret;
583
+ };
584
+
585
+ /**
586
+ * 发送方法响应
587
+ * @param {*} ret 方法执行结果
588
+ * @param {object} json 消息对象
589
+ * @param {string} topic 订阅主题
590
+ * @param {number} index 索引
591
+ * @private
592
+ */
593
+ Drive.prototype._sendMethodRes = async function (ret, json, topic, index) {
594
+ var arr_tc = this.config.topic_receive;
595
+ if (arr_tc.length > index) {
596
+ var obj = {};
597
+ var cg = this.config;
598
+ var id = json.id;
599
+
600
+ if (id) {
601
+ obj[cg.msgid] = id;
602
+ }
603
+
604
+ try {
605
+ obj[cg.method] = this.getMethod(json.method);
606
+ obj[cg.result] = ret;
607
+ var topic_receive = this.getTopic(arr_tc[index], json.clientid, json.params);
608
+ var reply_msg = this.convertOut(topic_receive, obj, topic, index);
609
+ await this.send(topic_receive, reply_msg);
610
+ } catch (err) {
611
+ this.log('error', 'MQTT事件响应失败!', err);
612
+ }
613
+ }
614
+ };
615
+
616
+ /**
617
+ * 主函数, 用于接收并处理订阅内容
618
+ * @param {string} push_topic 推送过来的主题
619
+ * @param {object} msg 消息正文
620
+ * @param {string} topic 订阅的主题
621
+ * @param {number} index 索引
622
+ * @returns {object} 返回执行结果
623
+ */
624
+ Drive.prototype.main = async function (push_topic, msg, topic, index) {
625
+ return this.handle(push_topic, msg, topic, index);
626
+ };
627
+
628
+ /**
629
+ * 接收订阅消息
630
+ * @param {object} ret 执行结果
631
+ * @param {object} push_topic 主题
632
+ * @param {object} msg 消息正文
633
+ * @param {string} topic 订阅的主题
634
+ * @param {number} index 索引
635
+ */
636
+ Drive.prototype.mainAfter = async function (ret, push_topic, msg, topic, index) {
637
+ await this.message(push_topic, msg, topic, index);
638
+ };
639
+
640
+ /**
641
+ * 获取插件
642
+ * @param {string} app 应用名称
643
+ * @param {string} name 插件名称
644
+ * @returns {object} 返回获取到的插件
645
+ */
646
+ Drive.prototype.plugin = function (app, name) {
647
+ var l = $.slash;
648
+ var app_name = app || this.config_file.between('app' + l, l);
649
+ var plugin_name = name || this.config_file.between('plugin' + l, l);
650
+ var plus;
651
+ var plugins = $.pool.plugin[app_name];
652
+ if (plugins) {
653
+ plus = plugins.get(plugin_name);
654
+ }
655
+ return plus;
656
+ };
657
+
658
+ /**
659
+ * 获取模型
660
+ * @param {string} type 模型类型
661
+ * @returns {object} 返回获取到的模型
662
+ */
663
+ Drive.prototype.getModel = function (type) {
664
+ let model = { ...this.config };
665
+ let dir = this._getDir();
666
+ let l = $.slash;
667
+ let app_name = dir.between('app' + l, l);
668
+ let plugin_name = dir.between('plugin' + l, l);
669
+ let name = dir.basename();
670
+ model.app = app_name;
671
+ model.plugin = plugin_name;
672
+ model.name = model.name || name;
673
+ return model;
674
+ };
675
+
676
+ module.exports = Drive;