SurveillanceCameraMonitorSystem.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  1. using System.Linq;
  2. using Content.Server.DeviceNetwork;
  3. using Content.Server.DeviceNetwork.Systems;
  4. using Content.Server.Power.Components;
  5. using Content.Shared.DeviceNetwork;
  6. using Content.Shared.Power;
  7. using Content.Shared.UserInterface;
  8. using Content.Shared.SurveillanceCamera;
  9. using Robust.Server.GameObjects;
  10. using Robust.Shared.Player;
  11. namespace Content.Server.SurveillanceCamera;
  12. public sealed class SurveillanceCameraMonitorSystem : EntitySystem
  13. {
  14. [Dependency] private readonly SurveillanceCameraSystem _surveillanceCameras = default!;
  15. [Dependency] private readonly UserInterfaceSystem _userInterface = default!;
  16. [Dependency] private readonly DeviceNetworkSystem _deviceNetworkSystem = default!;
  17. public override void Initialize()
  18. {
  19. SubscribeLocalEvent<SurveillanceCameraMonitorComponent, SurveillanceCameraDeactivateEvent>(OnSurveillanceCameraDeactivate);
  20. SubscribeLocalEvent<SurveillanceCameraMonitorComponent, PowerChangedEvent>(OnPowerChanged);
  21. SubscribeLocalEvent<SurveillanceCameraMonitorComponent, ComponentShutdown>(OnShutdown);
  22. SubscribeLocalEvent<SurveillanceCameraMonitorComponent, DeviceNetworkPacketEvent>(OnPacketReceived);
  23. SubscribeLocalEvent<SurveillanceCameraMonitorComponent, ComponentStartup>(OnComponentStartup);
  24. SubscribeLocalEvent<SurveillanceCameraMonitorComponent, AfterActivatableUIOpenEvent>(OnToggleInterface);
  25. Subs.BuiEvents<SurveillanceCameraMonitorComponent>(SurveillanceCameraMonitorUiKey.Key, subs =>
  26. {
  27. subs.Event<SurveillanceCameraRefreshCamerasMessage>(OnRefreshCamerasMessage);
  28. subs.Event<SurveillanceCameraRefreshSubnetsMessage>(OnRefreshSubnetsMessage);
  29. subs.Event<SurveillanceCameraDisconnectMessage>(OnDisconnectMessage);
  30. subs.Event<SurveillanceCameraMonitorSubnetRequestMessage>(OnSubnetRequest);
  31. subs.Event<SurveillanceCameraMonitorSwitchMessage>(OnSwitchMessage);
  32. subs.Event<BoundUIClosedEvent>(OnBoundUiClose);
  33. });
  34. }
  35. private const float _maxHeartbeatTime = 300f;
  36. private const float _heartbeatDelay = 30f;
  37. public override void Update(float frameTime)
  38. {
  39. var query = EntityQueryEnumerator<ActiveSurveillanceCameraMonitorComponent, SurveillanceCameraMonitorComponent>();
  40. while (query.MoveNext(out var uid, out _, out var monitor))
  41. {
  42. if (Paused(uid))
  43. {
  44. continue;
  45. }
  46. monitor.LastHeartbeatSent += frameTime;
  47. SendHeartbeat(uid, monitor);
  48. monitor.LastHeartbeat += frameTime;
  49. if (monitor.LastHeartbeat > _maxHeartbeatTime)
  50. {
  51. DisconnectCamera(uid, true, monitor);
  52. EntityManager.RemoveComponent<ActiveSurveillanceCameraMonitorComponent>(uid);
  53. }
  54. }
  55. }
  56. /// ROUTING:
  57. ///
  58. /// Monitor freq: General frequency for cameras, routers, and monitors to speak on.
  59. ///
  60. /// Subnet freqs: Frequency for each specific subnet. Routers ping cameras here,
  61. /// cameras ping back on monitor frequency. When a monitor
  62. /// selects a subnet, it saves that subnet's frequency
  63. /// so it can connect to the camera. All outbound cameras
  64. /// always speak on the monitor frequency and will not
  65. /// do broadcast pings - whatever talks to it, talks to it.
  66. ///
  67. /// How a camera is discovered:
  68. ///
  69. /// Subnet ping:
  70. /// Surveillance camera monitor - [ monitor freq ] -> Router
  71. /// Router -> camera discovery
  72. /// Router - [ subnet freq ] -> Camera
  73. /// Camera -> router ping
  74. /// Camera - [ monitor freq ] -> Router
  75. /// Router -> monitor data forward
  76. /// Router - [ monitor freq ] -> Monitor
  77. #region Event Handling
  78. private void OnComponentStartup(EntityUid uid, SurveillanceCameraMonitorComponent component, ComponentStartup args)
  79. {
  80. RefreshSubnets(uid, component);
  81. }
  82. private void OnSubnetRequest(EntityUid uid, SurveillanceCameraMonitorComponent component,
  83. SurveillanceCameraMonitorSubnetRequestMessage args)
  84. {
  85. if (args.Actor is { Valid: true } actor && !Deleted(actor))
  86. {
  87. SetActiveSubnet(uid, args.Subnet, component);
  88. }
  89. }
  90. private void OnPacketReceived(EntityUid uid, SurveillanceCameraMonitorComponent component,
  91. DeviceNetworkPacketEvent args)
  92. {
  93. if (string.IsNullOrEmpty(args.SenderAddress))
  94. {
  95. return;
  96. }
  97. if (args.Data.TryGetValue(DeviceNetworkConstants.Command, out string? command))
  98. {
  99. switch (command)
  100. {
  101. case SurveillanceCameraSystem.CameraConnectMessage:
  102. if (component.NextCameraAddress == args.SenderAddress)
  103. {
  104. component.ActiveCameraAddress = args.SenderAddress;
  105. TrySwitchCameraByUid(uid, args.Sender, component);
  106. }
  107. component.NextCameraAddress = null;
  108. break;
  109. case SurveillanceCameraSystem.CameraHeartbeatMessage:
  110. if (args.SenderAddress == component.ActiveCameraAddress)
  111. {
  112. component.LastHeartbeat = 0;
  113. }
  114. break;
  115. case SurveillanceCameraSystem.CameraDataMessage:
  116. if (!args.Data.TryGetValue(SurveillanceCameraSystem.CameraNameData, out string? name)
  117. || !args.Data.TryGetValue(SurveillanceCameraSystem.CameraSubnetData, out string? subnetData)
  118. || !args.Data.TryGetValue(SurveillanceCameraSystem.CameraAddressData, out string? address))
  119. {
  120. return;
  121. }
  122. if (component.ActiveSubnet != subnetData)
  123. {
  124. DisconnectFromSubnet(uid, subnetData);
  125. }
  126. if (!component.KnownCameras.ContainsKey(address))
  127. {
  128. component.KnownCameras.Add(address, name);
  129. }
  130. UpdateUserInterface(uid, component);
  131. break;
  132. case SurveillanceCameraSystem.CameraSubnetData:
  133. if (args.Data.TryGetValue(SurveillanceCameraSystem.CameraSubnetData, out string? subnet)
  134. && !string.IsNullOrEmpty(subnet)
  135. && !component.KnownSubnets.ContainsKey(subnet))
  136. {
  137. component.KnownSubnets.Add(subnet, args.SenderAddress);
  138. }
  139. UpdateUserInterface(uid, component);
  140. break;
  141. }
  142. }
  143. }
  144. private void OnDisconnectMessage(EntityUid uid, SurveillanceCameraMonitorComponent component,
  145. SurveillanceCameraDisconnectMessage message)
  146. {
  147. DisconnectCamera(uid, true, component);
  148. }
  149. private void OnRefreshCamerasMessage(EntityUid uid, SurveillanceCameraMonitorComponent component,
  150. SurveillanceCameraRefreshCamerasMessage message)
  151. {
  152. component.KnownCameras.Clear();
  153. RequestActiveSubnetInfo(uid, component);
  154. }
  155. private void OnRefreshSubnetsMessage(EntityUid uid, SurveillanceCameraMonitorComponent component,
  156. SurveillanceCameraRefreshSubnetsMessage message)
  157. {
  158. RefreshSubnets(uid, component);
  159. }
  160. private void OnSwitchMessage(EntityUid uid, SurveillanceCameraMonitorComponent component, SurveillanceCameraMonitorSwitchMessage message)
  161. {
  162. // there would be a null check here, but honestly
  163. // whichever one is the "latest" switch message gets to
  164. // do the switch
  165. TrySwitchCameraByAddress(uid, message.Address, component);
  166. }
  167. private void OnPowerChanged(EntityUid uid, SurveillanceCameraMonitorComponent component, ref PowerChangedEvent args)
  168. {
  169. if (!args.Powered)
  170. {
  171. RemoveActiveCamera(uid, component);
  172. component.NextCameraAddress = null;
  173. component.ActiveSubnet = string.Empty;
  174. }
  175. }
  176. private void OnShutdown(EntityUid uid, SurveillanceCameraMonitorComponent component, ComponentShutdown args)
  177. {
  178. RemoveActiveCamera(uid, component);
  179. }
  180. private void OnToggleInterface(EntityUid uid, SurveillanceCameraMonitorComponent component,
  181. AfterActivatableUIOpenEvent args)
  182. {
  183. AfterOpenUserInterface(uid, args.User, component);
  184. }
  185. // This is to ensure that there's no delay in ensuring that a camera is deactivated.
  186. private void OnSurveillanceCameraDeactivate(EntityUid uid, SurveillanceCameraMonitorComponent monitor, SurveillanceCameraDeactivateEvent args)
  187. {
  188. DisconnectCamera(uid, false, monitor);
  189. }
  190. private void OnBoundUiClose(EntityUid uid, SurveillanceCameraMonitorComponent component, BoundUIClosedEvent args)
  191. {
  192. RemoveViewer(uid, args.Actor, component);
  193. }
  194. #endregion
  195. private void SendHeartbeat(EntityUid uid, SurveillanceCameraMonitorComponent? monitor = null)
  196. {
  197. if (!Resolve(uid, ref monitor)
  198. || monitor.LastHeartbeatSent < _heartbeatDelay
  199. || string.IsNullOrEmpty(monitor.ActiveSubnet)
  200. || !monitor.KnownSubnets.TryGetValue(monitor.ActiveSubnet, out var subnetAddress))
  201. {
  202. return;
  203. }
  204. var payload = new NetworkPayload()
  205. {
  206. { DeviceNetworkConstants.Command, SurveillanceCameraSystem.CameraHeartbeatMessage },
  207. { SurveillanceCameraSystem.CameraAddressData, monitor.ActiveCameraAddress }
  208. };
  209. _deviceNetworkSystem.QueuePacket(uid, subnetAddress, payload);
  210. }
  211. private void DisconnectCamera(EntityUid uid, bool removeViewers, SurveillanceCameraMonitorComponent? monitor = null)
  212. {
  213. if (!Resolve(uid, ref monitor))
  214. {
  215. return;
  216. }
  217. if (removeViewers)
  218. {
  219. RemoveActiveCamera(uid, monitor);
  220. }
  221. monitor.ActiveCamera = null;
  222. monitor.ActiveCameraAddress = string.Empty;
  223. EntityManager.RemoveComponent<ActiveSurveillanceCameraMonitorComponent>(uid);
  224. UpdateUserInterface(uid, monitor);
  225. }
  226. private void RefreshSubnets(EntityUid uid, SurveillanceCameraMonitorComponent? monitor = null)
  227. {
  228. if (!Resolve(uid, ref monitor))
  229. {
  230. return;
  231. }
  232. monitor.KnownSubnets.Clear();
  233. PingCameraNetwork(uid, monitor);
  234. }
  235. private void PingCameraNetwork(EntityUid uid, SurveillanceCameraMonitorComponent? monitor = null)
  236. {
  237. if (!Resolve(uid, ref monitor))
  238. {
  239. return;
  240. }
  241. var payload = new NetworkPayload()
  242. {
  243. { DeviceNetworkConstants.Command, SurveillanceCameraSystem.CameraPingMessage }
  244. };
  245. _deviceNetworkSystem.QueuePacket(uid, null, payload);
  246. }
  247. private void SetActiveSubnet(EntityUid uid, string subnet,
  248. SurveillanceCameraMonitorComponent? monitor = null)
  249. {
  250. if (!Resolve(uid, ref monitor)
  251. || string.IsNullOrEmpty(subnet)
  252. || !monitor.KnownSubnets.ContainsKey(subnet))
  253. {
  254. return;
  255. }
  256. DisconnectFromSubnet(uid, monitor.ActiveSubnet);
  257. DisconnectCamera(uid, true, monitor);
  258. monitor.ActiveSubnet = subnet;
  259. monitor.KnownCameras.Clear();
  260. UpdateUserInterface(uid, monitor);
  261. ConnectToSubnet(uid, subnet);
  262. }
  263. private void RequestActiveSubnetInfo(EntityUid uid, SurveillanceCameraMonitorComponent? monitor = null)
  264. {
  265. if (!Resolve(uid, ref monitor)
  266. || string.IsNullOrEmpty(monitor.ActiveSubnet)
  267. || !monitor.KnownSubnets.TryGetValue(monitor.ActiveSubnet, out var address))
  268. {
  269. return;
  270. }
  271. var payload = new NetworkPayload()
  272. {
  273. {DeviceNetworkConstants.Command, SurveillanceCameraSystem.CameraPingSubnetMessage},
  274. };
  275. _deviceNetworkSystem.QueuePacket(uid, address, payload);
  276. }
  277. private void ConnectToSubnet(EntityUid uid, string subnet, SurveillanceCameraMonitorComponent? monitor = null)
  278. {
  279. if (!Resolve(uid, ref monitor)
  280. || string.IsNullOrEmpty(subnet)
  281. || !monitor.KnownSubnets.TryGetValue(subnet, out var address))
  282. {
  283. return;
  284. }
  285. var payload = new NetworkPayload()
  286. {
  287. {DeviceNetworkConstants.Command, SurveillanceCameraSystem.CameraSubnetConnectMessage},
  288. };
  289. _deviceNetworkSystem.QueuePacket(uid, address, payload);
  290. RequestActiveSubnetInfo(uid);
  291. }
  292. private void DisconnectFromSubnet(EntityUid uid, string subnet, SurveillanceCameraMonitorComponent? monitor = null)
  293. {
  294. if (!Resolve(uid, ref monitor)
  295. || string.IsNullOrEmpty(subnet)
  296. || !monitor.KnownSubnets.TryGetValue(subnet, out var address))
  297. {
  298. return;
  299. }
  300. var payload = new NetworkPayload()
  301. {
  302. {DeviceNetworkConstants.Command, SurveillanceCameraSystem.CameraSubnetDisconnectMessage},
  303. };
  304. _deviceNetworkSystem.QueuePacket(uid, address, payload);
  305. }
  306. // Adds a viewer to the camera and the monitor.
  307. private void AddViewer(EntityUid uid, EntityUid player, SurveillanceCameraMonitorComponent? monitor = null)
  308. {
  309. if (!Resolve(uid, ref monitor))
  310. {
  311. return;
  312. }
  313. monitor.Viewers.Add(player);
  314. if (monitor.ActiveCamera != null)
  315. {
  316. _surveillanceCameras.AddActiveViewer(monitor.ActiveCamera.Value, player, uid);
  317. }
  318. UpdateUserInterface(uid, monitor, player);
  319. }
  320. // Removes a viewer from the camera and the monitor.
  321. private void RemoveViewer(EntityUid uid, EntityUid player, SurveillanceCameraMonitorComponent? monitor = null)
  322. {
  323. if (!Resolve(uid, ref monitor))
  324. {
  325. return;
  326. }
  327. monitor.Viewers.Remove(player);
  328. if (monitor.ActiveCamera != null)
  329. {
  330. _surveillanceCameras.RemoveActiveViewer(monitor.ActiveCamera.Value, player);
  331. }
  332. }
  333. // Sets the camera. If the camera is not null, this will return.
  334. //
  335. // The camera should always attempt to switch over, rather than
  336. // directly setting it, so that the active viewer list and view
  337. // subscriptions can be updated.
  338. private void SetCamera(EntityUid uid, EntityUid camera, SurveillanceCameraMonitorComponent? monitor = null)
  339. {
  340. if (!Resolve(uid, ref monitor)
  341. || monitor.ActiveCamera != null)
  342. {
  343. return;
  344. }
  345. _surveillanceCameras.AddActiveViewers(camera, monitor.Viewers, uid);
  346. monitor.ActiveCamera = camera;
  347. AddComp<ActiveSurveillanceCameraMonitorComponent>(uid);
  348. UpdateUserInterface(uid, monitor);
  349. }
  350. // Switches the camera's viewers over to this new given camera.
  351. private void SwitchCamera(EntityUid uid, EntityUid camera, SurveillanceCameraMonitorComponent? monitor = null)
  352. {
  353. if (!Resolve(uid, ref monitor)
  354. || monitor.ActiveCamera == null)
  355. {
  356. return;
  357. }
  358. _surveillanceCameras.SwitchActiveViewers(monitor.ActiveCamera.Value, camera, monitor.Viewers, uid);
  359. monitor.ActiveCamera = camera;
  360. UpdateUserInterface(uid, monitor);
  361. }
  362. private void TrySwitchCameraByAddress(EntityUid uid, string address,
  363. SurveillanceCameraMonitorComponent? monitor = null)
  364. {
  365. if (!Resolve(uid, ref monitor)
  366. || string.IsNullOrEmpty(monitor.ActiveSubnet)
  367. || !monitor.KnownSubnets.TryGetValue(monitor.ActiveSubnet, out var subnetAddress))
  368. {
  369. return;
  370. }
  371. var payload = new NetworkPayload()
  372. {
  373. {DeviceNetworkConstants.Command, SurveillanceCameraSystem.CameraConnectMessage},
  374. {SurveillanceCameraSystem.CameraAddressData, address}
  375. };
  376. monitor.NextCameraAddress = address;
  377. _deviceNetworkSystem.QueuePacket(uid, subnetAddress, payload);
  378. }
  379. // Attempts to switch over the current viewed camera on this monitor
  380. // to the new camera.
  381. private void TrySwitchCameraByUid(EntityUid uid, EntityUid newCamera, SurveillanceCameraMonitorComponent? monitor = null)
  382. {
  383. if (!Resolve(uid, ref monitor))
  384. {
  385. return;
  386. }
  387. if (monitor.ActiveCamera == null)
  388. {
  389. SetCamera(uid, newCamera, monitor);
  390. }
  391. else
  392. {
  393. SwitchCamera(uid, newCamera, monitor);
  394. }
  395. }
  396. private void RemoveActiveCamera(EntityUid uid, SurveillanceCameraMonitorComponent? monitor = null)
  397. {
  398. if (!Resolve(uid, ref monitor)
  399. || monitor.ActiveCamera == null)
  400. {
  401. return;
  402. }
  403. _surveillanceCameras.RemoveActiveViewers(monitor.ActiveCamera.Value, monitor.Viewers, uid);
  404. UpdateUserInterface(uid, monitor);
  405. }
  406. // This is public primarily because it might be useful to have the ability to
  407. // have this component added to any entity, and have them open the BUI (somehow).
  408. public void AfterOpenUserInterface(EntityUid uid, EntityUid player, SurveillanceCameraMonitorComponent? monitor = null, ActorComponent? actor = null)
  409. {
  410. if (!Resolve(uid, ref monitor)
  411. || !Resolve(player, ref actor))
  412. {
  413. return;
  414. }
  415. AddViewer(uid, player);
  416. }
  417. private void UpdateUserInterface(EntityUid uid, SurveillanceCameraMonitorComponent? monitor = null, EntityUid? player = null)
  418. {
  419. if (!Resolve(uid, ref monitor))
  420. {
  421. return;
  422. }
  423. var state = new SurveillanceCameraMonitorUiState(GetNetEntity(monitor.ActiveCamera), monitor.KnownSubnets.Keys.ToHashSet(), monitor.ActiveCameraAddress, monitor.ActiveSubnet, monitor.KnownCameras);
  424. _userInterface.SetUiState(uid, SurveillanceCameraMonitorUiKey.Key, state);
  425. }
  426. }