Add connected controllers hashmap for controller-ws
This commit is contained in:
		
							parent
							
								
									6459804e1f
								
							
						
					
					
						commit
						6536ff0792
					
				
					 17 changed files with 267 additions and 649 deletions
				
			
		
							
								
								
									
										411
									
								
								Cargo.lock
									
										
									
										generated
									
									
									
								
							
							
						
						
									
										411
									
								
								Cargo.lock
									
										
									
										generated
									
									
									
								
							| 
						 | 
				
			
			@ -143,7 +143,7 @@ dependencies = [
 | 
			
		|||
 "futures-core",
 | 
			
		||||
 "futures-util",
 | 
			
		||||
 "mio",
 | 
			
		||||
 "socket2 0.5.5",
 | 
			
		||||
 "socket2",
 | 
			
		||||
 "tokio",
 | 
			
		||||
 "tracing",
 | 
			
		||||
]
 | 
			
		||||
| 
						 | 
				
			
			@ -204,7 +204,7 @@ dependencies = [
 | 
			
		|||
 "serde_json",
 | 
			
		||||
 "serde_urlencoded",
 | 
			
		||||
 "smallvec",
 | 
			
		||||
 "socket2 0.5.5",
 | 
			
		||||
 "socket2",
 | 
			
		||||
 "time",
 | 
			
		||||
 "url",
 | 
			
		||||
]
 | 
			
		||||
| 
						 | 
				
			
			@ -334,131 +334,6 @@ dependencies = [
 | 
			
		|||
 "libc",
 | 
			
		||||
]
 | 
			
		||||
 | 
			
		||||
[[package]]
 | 
			
		||||
name = "async-channel"
 | 
			
		||||
version = "1.9.0"
 | 
			
		||||
source = "registry+https://github.com/rust-lang/crates.io-index"
 | 
			
		||||
checksum = "81953c529336010edd6d8e358f886d9581267795c61b19475b71314bffa46d35"
 | 
			
		||||
dependencies = [
 | 
			
		||||
 "concurrent-queue",
 | 
			
		||||
 "event-listener 2.5.3",
 | 
			
		||||
 "futures-core",
 | 
			
		||||
]
 | 
			
		||||
 | 
			
		||||
[[package]]
 | 
			
		||||
name = "async-channel"
 | 
			
		||||
version = "2.1.0"
 | 
			
		||||
source = "registry+https://github.com/rust-lang/crates.io-index"
 | 
			
		||||
checksum = "d37875bd9915b7d67c2f117ea2c30a0989874d0b2cb694fe25403c85763c0c9e"
 | 
			
		||||
dependencies = [
 | 
			
		||||
 "concurrent-queue",
 | 
			
		||||
 "event-listener 3.1.0",
 | 
			
		||||
 "event-listener-strategy",
 | 
			
		||||
 "futures-core",
 | 
			
		||||
 "pin-project-lite",
 | 
			
		||||
]
 | 
			
		||||
 | 
			
		||||
[[package]]
 | 
			
		||||
name = "async-executor"
 | 
			
		||||
version = "1.7.2"
 | 
			
		||||
source = "registry+https://github.com/rust-lang/crates.io-index"
 | 
			
		||||
checksum = "fc5ea910c42e5ab19012bab31f53cb4d63d54c3a27730f9a833a88efcf4bb52d"
 | 
			
		||||
dependencies = [
 | 
			
		||||
 "async-lock 3.1.1",
 | 
			
		||||
 "async-task",
 | 
			
		||||
 "concurrent-queue",
 | 
			
		||||
 "fastrand 2.0.1",
 | 
			
		||||
 "futures-lite 2.0.1",
 | 
			
		||||
 "slab",
 | 
			
		||||
]
 | 
			
		||||
 | 
			
		||||
[[package]]
 | 
			
		||||
name = "async-global-executor"
 | 
			
		||||
version = "2.3.1"
 | 
			
		||||
source = "registry+https://github.com/rust-lang/crates.io-index"
 | 
			
		||||
checksum = "f1b6f5d7df27bd294849f8eec66ecfc63d11814df7a4f5d74168a2394467b776"
 | 
			
		||||
dependencies = [
 | 
			
		||||
 "async-channel 1.9.0",
 | 
			
		||||
 "async-executor",
 | 
			
		||||
 "async-io",
 | 
			
		||||
 "async-lock 2.8.0",
 | 
			
		||||
 "blocking",
 | 
			
		||||
 "futures-lite 1.13.0",
 | 
			
		||||
 "once_cell",
 | 
			
		||||
]
 | 
			
		||||
 | 
			
		||||
[[package]]
 | 
			
		||||
name = "async-io"
 | 
			
		||||
version = "1.13.0"
 | 
			
		||||
source = "registry+https://github.com/rust-lang/crates.io-index"
 | 
			
		||||
checksum = "0fc5b45d93ef0529756f812ca52e44c221b35341892d3dcc34132ac02f3dd2af"
 | 
			
		||||
dependencies = [
 | 
			
		||||
 "async-lock 2.8.0",
 | 
			
		||||
 "autocfg",
 | 
			
		||||
 "cfg-if",
 | 
			
		||||
 "concurrent-queue",
 | 
			
		||||
 "futures-lite 1.13.0",
 | 
			
		||||
 "log",
 | 
			
		||||
 "parking",
 | 
			
		||||
 "polling",
 | 
			
		||||
 "rustix 0.37.27",
 | 
			
		||||
 "slab",
 | 
			
		||||
 "socket2 0.4.10",
 | 
			
		||||
 "waker-fn",
 | 
			
		||||
]
 | 
			
		||||
 | 
			
		||||
[[package]]
 | 
			
		||||
name = "async-lock"
 | 
			
		||||
version = "2.8.0"
 | 
			
		||||
source = "registry+https://github.com/rust-lang/crates.io-index"
 | 
			
		||||
checksum = "287272293e9d8c41773cec55e365490fe034813a2f172f502d6ddcf75b2f582b"
 | 
			
		||||
dependencies = [
 | 
			
		||||
 "event-listener 2.5.3",
 | 
			
		||||
]
 | 
			
		||||
 | 
			
		||||
[[package]]
 | 
			
		||||
name = "async-lock"
 | 
			
		||||
version = "3.1.1"
 | 
			
		||||
source = "registry+https://github.com/rust-lang/crates.io-index"
 | 
			
		||||
checksum = "655b9c7fe787d3b25cc0f804a1a8401790f0c5bc395beb5a64dc77d8de079105"
 | 
			
		||||
dependencies = [
 | 
			
		||||
 "event-listener 3.1.0",
 | 
			
		||||
 "event-listener-strategy",
 | 
			
		||||
 "pin-project-lite",
 | 
			
		||||
]
 | 
			
		||||
 | 
			
		||||
[[package]]
 | 
			
		||||
name = "async-std"
 | 
			
		||||
version = "1.12.0"
 | 
			
		||||
source = "registry+https://github.com/rust-lang/crates.io-index"
 | 
			
		||||
checksum = "62565bb4402e926b29953c785397c6dc0391b7b446e45008b0049eb43cec6f5d"
 | 
			
		||||
dependencies = [
 | 
			
		||||
 "async-channel 1.9.0",
 | 
			
		||||
 "async-global-executor",
 | 
			
		||||
 "async-io",
 | 
			
		||||
 "async-lock 2.8.0",
 | 
			
		||||
 "crossbeam-utils",
 | 
			
		||||
 "futures-channel",
 | 
			
		||||
 "futures-core",
 | 
			
		||||
 "futures-io",
 | 
			
		||||
 "futures-lite 1.13.0",
 | 
			
		||||
 "gloo-timers",
 | 
			
		||||
 "kv-log-macro",
 | 
			
		||||
 "log",
 | 
			
		||||
 "memchr",
 | 
			
		||||
 "once_cell",
 | 
			
		||||
 "pin-project-lite",
 | 
			
		||||
 "pin-utils",
 | 
			
		||||
 "slab",
 | 
			
		||||
 "wasm-bindgen-futures",
 | 
			
		||||
]
 | 
			
		||||
 | 
			
		||||
[[package]]
 | 
			
		||||
name = "async-task"
 | 
			
		||||
version = "4.5.0"
 | 
			
		||||
source = "registry+https://github.com/rust-lang/crates.io-index"
 | 
			
		||||
checksum = "b4eb2cdb97421e01129ccb49169d8279ed21e829929144f4a22a6e54ac549ca1"
 | 
			
		||||
 | 
			
		||||
[[package]]
 | 
			
		||||
name = "async-trait"
 | 
			
		||||
version = "0.1.74"
 | 
			
		||||
| 
						 | 
				
			
			@ -479,12 +354,6 @@ dependencies = [
 | 
			
		|||
 "num-traits",
 | 
			
		||||
]
 | 
			
		||||
 | 
			
		||||
[[package]]
 | 
			
		||||
name = "atomic-waker"
 | 
			
		||||
version = "1.1.2"
 | 
			
		||||
source = "registry+https://github.com/rust-lang/crates.io-index"
 | 
			
		||||
checksum = "1505bd5d3d116872e7271a6d4e16d81d0c8570876c8de68093a09ac269d8aac0"
 | 
			
		||||
 | 
			
		||||
[[package]]
 | 
			
		||||
name = "autocfg"
 | 
			
		||||
version = "1.1.0"
 | 
			
		||||
| 
						 | 
				
			
			@ -548,22 +417,6 @@ dependencies = [
 | 
			
		|||
 "generic-array",
 | 
			
		||||
]
 | 
			
		||||
 | 
			
		||||
[[package]]
 | 
			
		||||
name = "blocking"
 | 
			
		||||
version = "1.5.1"
 | 
			
		||||
source = "registry+https://github.com/rust-lang/crates.io-index"
 | 
			
		||||
checksum = "6a37913e8dc4ddcc604f0c6d3bf2887c995153af3611de9e23c352b44c1b9118"
 | 
			
		||||
dependencies = [
 | 
			
		||||
 "async-channel 2.1.0",
 | 
			
		||||
 "async-lock 3.1.1",
 | 
			
		||||
 "async-task",
 | 
			
		||||
 "fastrand 2.0.1",
 | 
			
		||||
 "futures-io",
 | 
			
		||||
 "futures-lite 2.0.1",
 | 
			
		||||
 "piper",
 | 
			
		||||
 "tracing",
 | 
			
		||||
]
 | 
			
		||||
 | 
			
		||||
[[package]]
 | 
			
		||||
name = "brotli"
 | 
			
		||||
version = "3.4.0"
 | 
			
		||||
| 
						 | 
				
			
			@ -654,15 +507,6 @@ dependencies = [
 | 
			
		|||
 "windows-sys 0.48.0",
 | 
			
		||||
]
 | 
			
		||||
 | 
			
		||||
[[package]]
 | 
			
		||||
name = "concurrent-queue"
 | 
			
		||||
version = "2.3.0"
 | 
			
		||||
source = "registry+https://github.com/rust-lang/crates.io-index"
 | 
			
		||||
checksum = "f057a694a54f12365049b0958a1685bb52d567f5593b355fbf685838e873d400"
 | 
			
		||||
dependencies = [
 | 
			
		||||
 "crossbeam-utils",
 | 
			
		||||
]
 | 
			
		||||
 | 
			
		||||
[[package]]
 | 
			
		||||
name = "config"
 | 
			
		||||
version = "0.13.4"
 | 
			
		||||
| 
						 | 
				
			
			@ -954,36 +798,6 @@ version = "2.5.3"
 | 
			
		|||
source = "registry+https://github.com/rust-lang/crates.io-index"
 | 
			
		||||
checksum = "0206175f82b8d6bf6652ff7d71a1e27fd2e4efde587fd368662814d6ec1d9ce0"
 | 
			
		||||
 | 
			
		||||
[[package]]
 | 
			
		||||
name = "event-listener"
 | 
			
		||||
version = "3.1.0"
 | 
			
		||||
source = "registry+https://github.com/rust-lang/crates.io-index"
 | 
			
		||||
checksum = "d93877bcde0eb80ca09131a08d23f0a5c18a620b01db137dba666d18cd9b30c2"
 | 
			
		||||
dependencies = [
 | 
			
		||||
 "concurrent-queue",
 | 
			
		||||
 "parking",
 | 
			
		||||
 "pin-project-lite",
 | 
			
		||||
]
 | 
			
		||||
 | 
			
		||||
[[package]]
 | 
			
		||||
name = "event-listener-strategy"
 | 
			
		||||
version = "0.3.0"
 | 
			
		||||
source = "registry+https://github.com/rust-lang/crates.io-index"
 | 
			
		||||
checksum = "d96b852f1345da36d551b9473fa1e2b1eb5c5195585c6c018118bc92a8d91160"
 | 
			
		||||
dependencies = [
 | 
			
		||||
 "event-listener 3.1.0",
 | 
			
		||||
 "pin-project-lite",
 | 
			
		||||
]
 | 
			
		||||
 | 
			
		||||
[[package]]
 | 
			
		||||
name = "fastrand"
 | 
			
		||||
version = "1.9.0"
 | 
			
		||||
source = "registry+https://github.com/rust-lang/crates.io-index"
 | 
			
		||||
checksum = "e51093e27b0797c359783294ca4f0a911c270184cb10f85783b118614a1501be"
 | 
			
		||||
dependencies = [
 | 
			
		||||
 "instant",
 | 
			
		||||
]
 | 
			
		||||
 | 
			
		||||
[[package]]
 | 
			
		||||
name = "fastrand"
 | 
			
		||||
version = "2.0.1"
 | 
			
		||||
| 
						 | 
				
			
			@ -1091,35 +905,6 @@ version = "0.3.29"
 | 
			
		|||
source = "registry+https://github.com/rust-lang/crates.io-index"
 | 
			
		||||
checksum = "8bf34a163b5c4c52d0478a4d757da8fb65cabef42ba90515efee0f6f9fa45aaa"
 | 
			
		||||
 | 
			
		||||
[[package]]
 | 
			
		||||
name = "futures-lite"
 | 
			
		||||
version = "1.13.0"
 | 
			
		||||
source = "registry+https://github.com/rust-lang/crates.io-index"
 | 
			
		||||
checksum = "49a9d51ce47660b1e808d3c990b4709f2f415d928835a17dfd16991515c46bce"
 | 
			
		||||
dependencies = [
 | 
			
		||||
 "fastrand 1.9.0",
 | 
			
		||||
 "futures-core",
 | 
			
		||||
 "futures-io",
 | 
			
		||||
 "memchr",
 | 
			
		||||
 "parking",
 | 
			
		||||
 "pin-project-lite",
 | 
			
		||||
 "waker-fn",
 | 
			
		||||
]
 | 
			
		||||
 | 
			
		||||
[[package]]
 | 
			
		||||
name = "futures-lite"
 | 
			
		||||
version = "2.0.1"
 | 
			
		||||
source = "registry+https://github.com/rust-lang/crates.io-index"
 | 
			
		||||
checksum = "d3831c2651acb5177cbd83943f3d9c8912c5ad03c76afcc0e9511ba568ec5ebb"
 | 
			
		||||
dependencies = [
 | 
			
		||||
 "fastrand 2.0.1",
 | 
			
		||||
 "futures-core",
 | 
			
		||||
 "futures-io",
 | 
			
		||||
 "memchr",
 | 
			
		||||
 "parking",
 | 
			
		||||
 "pin-project-lite",
 | 
			
		||||
]
 | 
			
		||||
 | 
			
		||||
[[package]]
 | 
			
		||||
name = "futures-macro"
 | 
			
		||||
version = "0.3.29"
 | 
			
		||||
| 
						 | 
				
			
			@ -1188,18 +973,6 @@ version = "0.28.0"
 | 
			
		|||
source = "registry+https://github.com/rust-lang/crates.io-index"
 | 
			
		||||
checksum = "6fb8d784f27acf97159b40fc4db5ecd8aa23b9ad5ef69cdd136d3bc80665f0c0"
 | 
			
		||||
 | 
			
		||||
[[package]]
 | 
			
		||||
name = "gloo-timers"
 | 
			
		||||
version = "0.2.6"
 | 
			
		||||
source = "registry+https://github.com/rust-lang/crates.io-index"
 | 
			
		||||
checksum = "9b995a66bb87bebce9a0f4a95aed01daca4872c050bfcb21653361c03bc35e5c"
 | 
			
		||||
dependencies = [
 | 
			
		||||
 "futures-channel",
 | 
			
		||||
 "futures-core",
 | 
			
		||||
 "js-sys",
 | 
			
		||||
 "wasm-bindgen",
 | 
			
		||||
]
 | 
			
		||||
 | 
			
		||||
[[package]]
 | 
			
		||||
name = "h2"
 | 
			
		||||
version = "0.3.21"
 | 
			
		||||
| 
						 | 
				
			
			@ -1371,26 +1144,6 @@ dependencies = [
 | 
			
		|||
 "hashbrown 0.14.2",
 | 
			
		||||
]
 | 
			
		||||
 | 
			
		||||
[[package]]
 | 
			
		||||
name = "instant"
 | 
			
		||||
version = "0.1.12"
 | 
			
		||||
source = "registry+https://github.com/rust-lang/crates.io-index"
 | 
			
		||||
checksum = "7a5bbe824c507c5da5956355e86a746d82e0e1464f65d862cc5e71da70e94b2c"
 | 
			
		||||
dependencies = [
 | 
			
		||||
 "cfg-if",
 | 
			
		||||
]
 | 
			
		||||
 | 
			
		||||
[[package]]
 | 
			
		||||
name = "io-lifetimes"
 | 
			
		||||
version = "1.0.11"
 | 
			
		||||
source = "registry+https://github.com/rust-lang/crates.io-index"
 | 
			
		||||
checksum = "eae7b9aee968036d54dce06cebaefd919e4472e753296daccd6d344e3e2df0c2"
 | 
			
		||||
dependencies = [
 | 
			
		||||
 "hermit-abi",
 | 
			
		||||
 "libc",
 | 
			
		||||
 "windows-sys 0.48.0",
 | 
			
		||||
]
 | 
			
		||||
 | 
			
		||||
[[package]]
 | 
			
		||||
name = "is-terminal"
 | 
			
		||||
version = "0.4.9"
 | 
			
		||||
| 
						 | 
				
			
			@ -1398,7 +1151,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
 | 
			
		|||
checksum = "cb0889898416213fab133e1d33a0e5858a48177452750691bde3666d0fdbaf8b"
 | 
			
		||||
dependencies = [
 | 
			
		||||
 "hermit-abi",
 | 
			
		||||
 "rustix 0.38.21",
 | 
			
		||||
 "rustix",
 | 
			
		||||
 "windows-sys 0.48.0",
 | 
			
		||||
]
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -1446,15 +1199,6 @@ dependencies = [
 | 
			
		|||
 "serde",
 | 
			
		||||
]
 | 
			
		||||
 | 
			
		||||
[[package]]
 | 
			
		||||
name = "kv-log-macro"
 | 
			
		||||
version = "1.0.7"
 | 
			
		||||
source = "registry+https://github.com/rust-lang/crates.io-index"
 | 
			
		||||
checksum = "0de8b303297635ad57c9f5059fd9cee7a47f8e8daa09df0fcd07dd39fb22977f"
 | 
			
		||||
dependencies = [
 | 
			
		||||
 "log",
 | 
			
		||||
]
 | 
			
		||||
 | 
			
		||||
[[package]]
 | 
			
		||||
name = "language-tags"
 | 
			
		||||
version = "0.3.2"
 | 
			
		||||
| 
						 | 
				
			
			@ -1499,12 +1243,6 @@ version = "0.5.6"
 | 
			
		|||
source = "registry+https://github.com/rust-lang/crates.io-index"
 | 
			
		||||
checksum = "0717cef1bc8b636c6e1c1bbdefc09e6322da8a9321966e8928ef80d20f7f770f"
 | 
			
		||||
 | 
			
		||||
[[package]]
 | 
			
		||||
name = "linux-raw-sys"
 | 
			
		||||
version = "0.3.8"
 | 
			
		||||
source = "registry+https://github.com/rust-lang/crates.io-index"
 | 
			
		||||
checksum = "ef53942eb7bf7ff43a617b3e2c1c4a5ecf5944a7c1bc12d7ee39bbb15e5c1519"
 | 
			
		||||
 | 
			
		||||
[[package]]
 | 
			
		||||
name = "linux-raw-sys"
 | 
			
		||||
version = "0.4.10"
 | 
			
		||||
| 
						 | 
				
			
			@ -1543,9 +1281,6 @@ name = "log"
 | 
			
		|||
version = "0.4.20"
 | 
			
		||||
source = "registry+https://github.com/rust-lang/crates.io-index"
 | 
			
		||||
checksum = "b5e6163cb8c49088c2c36f57875e58ccd8c87c7427f7fbd50ea6710b2f3f2e8f"
 | 
			
		||||
dependencies = [
 | 
			
		||||
 "value-bag",
 | 
			
		||||
]
 | 
			
		||||
 | 
			
		||||
[[package]]
 | 
			
		||||
name = "md-5"
 | 
			
		||||
| 
						 | 
				
			
			@ -1698,12 +1433,6 @@ dependencies = [
 | 
			
		|||
 "hashbrown 0.12.3",
 | 
			
		||||
]
 | 
			
		||||
 | 
			
		||||
[[package]]
 | 
			
		||||
name = "parking"
 | 
			
		||||
version = "2.2.0"
 | 
			
		||||
source = "registry+https://github.com/rust-lang/crates.io-index"
 | 
			
		||||
checksum = "bb813b8af86854136c6922af0598d719255ecb2179515e6e7730d468f05c9cae"
 | 
			
		||||
 | 
			
		||||
[[package]]
 | 
			
		||||
name = "parking_lot"
 | 
			
		||||
version = "0.12.1"
 | 
			
		||||
| 
						 | 
				
			
			@ -1811,17 +1540,6 @@ version = "0.1.0"
 | 
			
		|||
source = "registry+https://github.com/rust-lang/crates.io-index"
 | 
			
		||||
checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184"
 | 
			
		||||
 | 
			
		||||
[[package]]
 | 
			
		||||
name = "piper"
 | 
			
		||||
version = "0.2.1"
 | 
			
		||||
source = "registry+https://github.com/rust-lang/crates.io-index"
 | 
			
		||||
checksum = "668d31b1c4eba19242f2088b2bf3316b82ca31082a8335764db4e083db7485d4"
 | 
			
		||||
dependencies = [
 | 
			
		||||
 "atomic-waker",
 | 
			
		||||
 "fastrand 2.0.1",
 | 
			
		||||
 "futures-io",
 | 
			
		||||
]
 | 
			
		||||
 | 
			
		||||
[[package]]
 | 
			
		||||
name = "pkcs1"
 | 
			
		||||
version = "0.7.5"
 | 
			
		||||
| 
						 | 
				
			
			@ -1849,22 +1567,6 @@ version = "0.3.27"
 | 
			
		|||
source = "registry+https://github.com/rust-lang/crates.io-index"
 | 
			
		||||
checksum = "26072860ba924cbfa98ea39c8c19b4dd6a4a25423dbdf219c1eca91aa0cf6964"
 | 
			
		||||
 | 
			
		||||
[[package]]
 | 
			
		||||
name = "polling"
 | 
			
		||||
version = "2.8.0"
 | 
			
		||||
source = "registry+https://github.com/rust-lang/crates.io-index"
 | 
			
		||||
checksum = "4b2d323e8ca7996b3e23126511a523f7e62924d93ecd5ae73b333815b0eb3dce"
 | 
			
		||||
dependencies = [
 | 
			
		||||
 "autocfg",
 | 
			
		||||
 "bitflags 1.3.2",
 | 
			
		||||
 "cfg-if",
 | 
			
		||||
 "concurrent-queue",
 | 
			
		||||
 "libc",
 | 
			
		||||
 "log",
 | 
			
		||||
 "pin-project-lite",
 | 
			
		||||
 "windows-sys 0.48.0",
 | 
			
		||||
]
 | 
			
		||||
 | 
			
		||||
[[package]]
 | 
			
		||||
name = "powerfmt"
 | 
			
		||||
version = "0.2.0"
 | 
			
		||||
| 
						 | 
				
			
			@ -2019,20 +1721,6 @@ dependencies = [
 | 
			
		|||
 "semver",
 | 
			
		||||
]
 | 
			
		||||
 | 
			
		||||
[[package]]
 | 
			
		||||
name = "rustix"
 | 
			
		||||
version = "0.37.27"
 | 
			
		||||
source = "registry+https://github.com/rust-lang/crates.io-index"
 | 
			
		||||
checksum = "fea8ca367a3a01fe35e6943c400addf443c0f57670e6ec51196f71a4b8762dd2"
 | 
			
		||||
dependencies = [
 | 
			
		||||
 "bitflags 1.3.2",
 | 
			
		||||
 "errno",
 | 
			
		||||
 "io-lifetimes",
 | 
			
		||||
 "libc",
 | 
			
		||||
 "linux-raw-sys 0.3.8",
 | 
			
		||||
 "windows-sys 0.48.0",
 | 
			
		||||
]
 | 
			
		||||
 | 
			
		||||
[[package]]
 | 
			
		||||
name = "rustix"
 | 
			
		||||
version = "0.38.21"
 | 
			
		||||
| 
						 | 
				
			
			@ -2042,7 +1730,7 @@ dependencies = [
 | 
			
		|||
 "bitflags 2.4.1",
 | 
			
		||||
 "errno",
 | 
			
		||||
 "libc",
 | 
			
		||||
 "linux-raw-sys 0.4.10",
 | 
			
		||||
 "linux-raw-sys",
 | 
			
		||||
 "windows-sys 0.48.0",
 | 
			
		||||
]
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -2175,16 +1863,6 @@ version = "1.11.1"
 | 
			
		|||
source = "registry+https://github.com/rust-lang/crates.io-index"
 | 
			
		||||
checksum = "942b4a808e05215192e39f4ab80813e599068285906cc91aa64f923db842bd5a"
 | 
			
		||||
 | 
			
		||||
[[package]]
 | 
			
		||||
name = "socket2"
 | 
			
		||||
version = "0.4.10"
 | 
			
		||||
source = "registry+https://github.com/rust-lang/crates.io-index"
 | 
			
		||||
checksum = "9f7916fc008ca5542385b89a3d3ce689953c143e9304a9bf8beec1de48994c0d"
 | 
			
		||||
dependencies = [
 | 
			
		||||
 "libc",
 | 
			
		||||
 "winapi",
 | 
			
		||||
]
 | 
			
		||||
 | 
			
		||||
[[package]]
 | 
			
		||||
name = "socket2"
 | 
			
		||||
version = "0.5.5"
 | 
			
		||||
| 
						 | 
				
			
			@ -2251,8 +1929,6 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
 | 
			
		|||
checksum = "8d6753e460c998bbd4cd8c6f0ed9a64346fcca0723d6e75e52fdc351c5d2169d"
 | 
			
		||||
dependencies = [
 | 
			
		||||
 "ahash 0.8.6",
 | 
			
		||||
 "async-io",
 | 
			
		||||
 "async-std",
 | 
			
		||||
 "atoi",
 | 
			
		||||
 "byteorder",
 | 
			
		||||
 "bytes",
 | 
			
		||||
| 
						 | 
				
			
			@ -2261,7 +1937,7 @@ dependencies = [
 | 
			
		|||
 "crossbeam-queue",
 | 
			
		||||
 "dotenvy",
 | 
			
		||||
 "either",
 | 
			
		||||
 "event-listener 2.5.3",
 | 
			
		||||
 "event-listener",
 | 
			
		||||
 "futures-channel",
 | 
			
		||||
 "futures-core",
 | 
			
		||||
 "futures-intrusive",
 | 
			
		||||
| 
						 | 
				
			
			@ -2281,6 +1957,8 @@ dependencies = [
 | 
			
		|||
 "smallvec",
 | 
			
		||||
 "sqlformat",
 | 
			
		||||
 "thiserror",
 | 
			
		||||
 "tokio",
 | 
			
		||||
 "tokio-stream",
 | 
			
		||||
 "tracing",
 | 
			
		||||
 "url",
 | 
			
		||||
]
 | 
			
		||||
| 
						 | 
				
			
			@ -2304,7 +1982,6 @@ version = "0.7.2"
 | 
			
		|||
source = "registry+https://github.com/rust-lang/crates.io-index"
 | 
			
		||||
checksum = "0a4ee1e104e00dedb6aa5ffdd1343107b0a4702e862a84320ee7cc74782d96fc"
 | 
			
		||||
dependencies = [
 | 
			
		||||
 "async-std",
 | 
			
		||||
 "dotenvy",
 | 
			
		||||
 "either",
 | 
			
		||||
 "heck",
 | 
			
		||||
| 
						 | 
				
			
			@ -2321,6 +1998,7 @@ dependencies = [
 | 
			
		|||
 "sqlx-sqlite",
 | 
			
		||||
 "syn 1.0.109",
 | 
			
		||||
 "tempfile",
 | 
			
		||||
 "tokio",
 | 
			
		||||
 "url",
 | 
			
		||||
]
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -2476,9 +2154,9 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
 | 
			
		|||
checksum = "7ef1adac450ad7f4b3c28589471ade84f25f731a7a0fe30d71dfa9f60fd808e5"
 | 
			
		||||
dependencies = [
 | 
			
		||||
 "cfg-if",
 | 
			
		||||
 "fastrand 2.0.1",
 | 
			
		||||
 "fastrand",
 | 
			
		||||
 "redox_syscall",
 | 
			
		||||
 "rustix 0.38.21",
 | 
			
		||||
 "rustix",
 | 
			
		||||
 "windows-sys 0.48.0",
 | 
			
		||||
]
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -2562,7 +2240,7 @@ dependencies = [
 | 
			
		|||
 "parking_lot",
 | 
			
		||||
 "pin-project-lite",
 | 
			
		||||
 "signal-hook-registry",
 | 
			
		||||
 "socket2 0.5.5",
 | 
			
		||||
 "socket2",
 | 
			
		||||
 "tokio-macros",
 | 
			
		||||
 "windows-sys 0.48.0",
 | 
			
		||||
]
 | 
			
		||||
| 
						 | 
				
			
			@ -2578,6 +2256,17 @@ dependencies = [
 | 
			
		|||
 "syn 2.0.38",
 | 
			
		||||
]
 | 
			
		||||
 | 
			
		||||
[[package]]
 | 
			
		||||
name = "tokio-stream"
 | 
			
		||||
version = "0.1.14"
 | 
			
		||||
source = "registry+https://github.com/rust-lang/crates.io-index"
 | 
			
		||||
checksum = "397c988d37662c7dda6d2208364a706264bf3d6138b11d436cbac0ad38832842"
 | 
			
		||||
dependencies = [
 | 
			
		||||
 "futures-core",
 | 
			
		||||
 "pin-project-lite",
 | 
			
		||||
 "tokio",
 | 
			
		||||
]
 | 
			
		||||
 | 
			
		||||
[[package]]
 | 
			
		||||
name = "tokio-tungstenite"
 | 
			
		||||
version = "0.20.1"
 | 
			
		||||
| 
						 | 
				
			
			@ -2736,12 +2425,6 @@ dependencies = [
 | 
			
		|||
 "serde",
 | 
			
		||||
]
 | 
			
		||||
 | 
			
		||||
[[package]]
 | 
			
		||||
name = "value-bag"
 | 
			
		||||
version = "1.4.2"
 | 
			
		||||
source = "registry+https://github.com/rust-lang/crates.io-index"
 | 
			
		||||
checksum = "4a72e1902dde2bd6441347de2b70b7f5d59bf157c6c62f0c44572607a1d55bbe"
 | 
			
		||||
 | 
			
		||||
[[package]]
 | 
			
		||||
name = "vcpkg"
 | 
			
		||||
version = "0.2.15"
 | 
			
		||||
| 
						 | 
				
			
			@ -2754,12 +2437,6 @@ version = "0.9.4"
 | 
			
		|||
source = "registry+https://github.com/rust-lang/crates.io-index"
 | 
			
		||||
checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f"
 | 
			
		||||
 | 
			
		||||
[[package]]
 | 
			
		||||
name = "waker-fn"
 | 
			
		||||
version = "1.1.1"
 | 
			
		||||
source = "registry+https://github.com/rust-lang/crates.io-index"
 | 
			
		||||
checksum = "f3c4517f54858c779bbcbf228f4fca63d121bf85fbecb2dc578cdf4a39395690"
 | 
			
		||||
 | 
			
		||||
[[package]]
 | 
			
		||||
name = "wasi"
 | 
			
		||||
version = "0.11.0+wasi-snapshot-preview1"
 | 
			
		||||
| 
						 | 
				
			
			@ -2791,18 +2468,6 @@ dependencies = [
 | 
			
		|||
 "wasm-bindgen-shared",
 | 
			
		||||
]
 | 
			
		||||
 | 
			
		||||
[[package]]
 | 
			
		||||
name = "wasm-bindgen-futures"
 | 
			
		||||
version = "0.4.37"
 | 
			
		||||
source = "registry+https://github.com/rust-lang/crates.io-index"
 | 
			
		||||
checksum = "c02dbc21516f9f1f04f187958890d7e6026df8d16540b7ad9492bc34a67cea03"
 | 
			
		||||
dependencies = [
 | 
			
		||||
 "cfg-if",
 | 
			
		||||
 "js-sys",
 | 
			
		||||
 "wasm-bindgen",
 | 
			
		||||
 "web-sys",
 | 
			
		||||
]
 | 
			
		||||
 | 
			
		||||
[[package]]
 | 
			
		||||
name = "wasm-bindgen-macro"
 | 
			
		||||
version = "0.2.87"
 | 
			
		||||
| 
						 | 
				
			
			@ -2832,44 +2497,12 @@ version = "0.2.87"
 | 
			
		|||
source = "registry+https://github.com/rust-lang/crates.io-index"
 | 
			
		||||
checksum = "ca6ad05a4870b2bf5fe995117d3728437bd27d7cd5f06f13c17443ef369775a1"
 | 
			
		||||
 | 
			
		||||
[[package]]
 | 
			
		||||
name = "web-sys"
 | 
			
		||||
version = "0.3.64"
 | 
			
		||||
source = "registry+https://github.com/rust-lang/crates.io-index"
 | 
			
		||||
checksum = "9b85cbef8c220a6abc02aefd892dfc0fc23afb1c6a426316ec33253a3877249b"
 | 
			
		||||
dependencies = [
 | 
			
		||||
 "js-sys",
 | 
			
		||||
 "wasm-bindgen",
 | 
			
		||||
]
 | 
			
		||||
 | 
			
		||||
[[package]]
 | 
			
		||||
name = "whoami"
 | 
			
		||||
version = "1.4.1"
 | 
			
		||||
source = "registry+https://github.com/rust-lang/crates.io-index"
 | 
			
		||||
checksum = "22fc3756b8a9133049b26c7f61ab35416c130e8c09b660f5b3958b446f52cc50"
 | 
			
		||||
 | 
			
		||||
[[package]]
 | 
			
		||||
name = "winapi"
 | 
			
		||||
version = "0.3.9"
 | 
			
		||||
source = "registry+https://github.com/rust-lang/crates.io-index"
 | 
			
		||||
checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419"
 | 
			
		||||
dependencies = [
 | 
			
		||||
 "winapi-i686-pc-windows-gnu",
 | 
			
		||||
 "winapi-x86_64-pc-windows-gnu",
 | 
			
		||||
]
 | 
			
		||||
 | 
			
		||||
[[package]]
 | 
			
		||||
name = "winapi-i686-pc-windows-gnu"
 | 
			
		||||
version = "0.4.0"
 | 
			
		||||
source = "registry+https://github.com/rust-lang/crates.io-index"
 | 
			
		||||
checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
 | 
			
		||||
 | 
			
		||||
[[package]]
 | 
			
		||||
name = "winapi-x86_64-pc-windows-gnu"
 | 
			
		||||
version = "0.4.0"
 | 
			
		||||
source = "registry+https://github.com/rust-lang/crates.io-index"
 | 
			
		||||
checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
 | 
			
		||||
 | 
			
		||||
[[package]]
 | 
			
		||||
name = "windows-core"
 | 
			
		||||
version = "0.51.1"
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -20,6 +20,6 @@ serde = "1.0"
 | 
			
		|||
serde_json = "1.0"
 | 
			
		||||
serde_derive = "1.0"
 | 
			
		||||
 | 
			
		||||
sqlx = { version = "0.7", features = ["sqlite", "runtime-async-std", "macros", "chrono"] }
 | 
			
		||||
sqlx = { version = "0.7", features = ["sqlite", "runtime-tokio", "macros", "chrono"] }
 | 
			
		||||
 | 
			
		||||
futures = "0.3"
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -2,11 +2,11 @@ use std::str;
 | 
			
		|||
 | 
			
		||||
use crate::relay_loop::run_relay_loop;
 | 
			
		||||
use crate::settings::Settings;
 | 
			
		||||
use emgauwa_lib::db;
 | 
			
		||||
use emgauwa_lib::db::{DbController, DbRelay};
 | 
			
		||||
use emgauwa_lib::handlers::v1::ws::controllers::ControllerWsAction;
 | 
			
		||||
use emgauwa_lib::models::convert_db_list;
 | 
			
		||||
use emgauwa_lib::models::{Controller, FromDbModel};
 | 
			
		||||
use emgauwa_lib::types::ControllerUid;
 | 
			
		||||
use emgauwa_lib::{db, models};
 | 
			
		||||
use futures::channel::mpsc;
 | 
			
		||||
use futures::{future, pin_mut, SinkExt, StreamExt};
 | 
			
		||||
use sqlx::pool::PoolConnection;
 | 
			
		||||
| 
						 | 
				
			
			@ -64,37 +64,23 @@ async fn main() {
 | 
			
		|||
			futures::executor::block_on(create_this_controller(&mut conn, &settings))
 | 
			
		||||
		});
 | 
			
		||||
 | 
			
		||||
	let db_relays: Vec<DbRelay> = settings
 | 
			
		||||
		.relays
 | 
			
		||||
		.iter()
 | 
			
		||||
		.map(|relay| {
 | 
			
		||||
			futures::executor::block_on(async {
 | 
			
		||||
				match DbRelay::get_by_controller_and_num(
 | 
			
		||||
					&mut conn,
 | 
			
		||||
					&db_controller,
 | 
			
		||||
					relay.number.unwrap(),
 | 
			
		||||
				)
 | 
			
		||||
	for relay in &settings.relays {
 | 
			
		||||
		if DbRelay::get_by_controller_and_num(&mut conn, &db_controller, relay.number.unwrap())
 | 
			
		||||
			.await
 | 
			
		||||
			.expect("Failed to get relay from database")
 | 
			
		||||
			.is_none()
 | 
			
		||||
		{
 | 
			
		||||
					None => create_this_relay(&mut conn, &db_controller, relay).await,
 | 
			
		||||
					Some(relay) => relay,
 | 
			
		||||
			create_this_relay(&mut conn, &db_controller, relay).await;
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
			})
 | 
			
		||||
		})
 | 
			
		||||
		.collect();
 | 
			
		||||
 | 
			
		||||
	let db_controller = db_controller
 | 
			
		||||
		.update(&mut conn, &db_controller.name, db_relays.len() as i64)
 | 
			
		||||
		.update(&mut conn, &db_controller.name, settings.relays.len() as i64)
 | 
			
		||||
		.await
 | 
			
		||||
		.unwrap();
 | 
			
		||||
 | 
			
		||||
	let relays = convert_db_list(&mut conn, db_relays).expect("Failed to convert relays");
 | 
			
		||||
 | 
			
		||||
	let this = models::Controller {
 | 
			
		||||
		controller: db_controller,
 | 
			
		||||
		relays,
 | 
			
		||||
	};
 | 
			
		||||
	let this = Controller::from_db_model(&mut conn, db_controller)
 | 
			
		||||
		.expect("Failed to convert database models");
 | 
			
		||||
 | 
			
		||||
	let url = format!(
 | 
			
		||||
		"ws://{}:{}/api/v1/ws/controllers",
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,12 +1,15 @@
 | 
			
		|||
use actix_cors::Cors;
 | 
			
		||||
use std::collections::HashMap;
 | 
			
		||||
use std::net::TcpListener;
 | 
			
		||||
use std::str::FromStr;
 | 
			
		||||
use std::sync::{Arc, Mutex};
 | 
			
		||||
 | 
			
		||||
use crate::utils::drop_privileges;
 | 
			
		||||
use actix_web::middleware::TrailingSlash;
 | 
			
		||||
use actix_web::{middleware, web, App, HttpServer};
 | 
			
		||||
use emgauwa_lib::db::DbController;
 | 
			
		||||
use emgauwa_lib::handlers;
 | 
			
		||||
use emgauwa_lib::types::ConnectedControllersType;
 | 
			
		||||
use log::{trace, LevelFilter};
 | 
			
		||||
use simple_logger::SimpleLogger;
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -41,6 +44,8 @@ async fn main() -> std::io::Result<()> {
 | 
			
		|||
			.expect("Error setting all controllers inactive");
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	let connected_controllers: ConnectedControllersType = Arc::new(Mutex::new(HashMap::new()));
 | 
			
		||||
 | 
			
		||||
	log::info!("Starting server on {}:{}", settings.host, settings.port);
 | 
			
		||||
	HttpServer::new(move || {
 | 
			
		||||
		let cors = Cors::default().allow_any_method().allow_any_header();
 | 
			
		||||
| 
						 | 
				
			
			@ -59,6 +64,8 @@ async fn main() -> std::io::Result<()> {
 | 
			
		|||
			.wrap(middleware::NormalizePath::new(TrailingSlash::Trim))
 | 
			
		||||
			.app_data(web::JsonConfig::default().error_handler(handlers::json_error_handler))
 | 
			
		||||
			.app_data(web::Data::new(pool.clone()))
 | 
			
		||||
			.app_data(web::Data::new(connected_controllers.clone()))
 | 
			
		||||
			.service(handlers::v1::controllers::index)
 | 
			
		||||
			.service(handlers::v1::relays::index)
 | 
			
		||||
			.service(handlers::v1::schedules::index)
 | 
			
		||||
			.service(handlers::v1::schedules::tagged)
 | 
			
		||||
| 
						 | 
				
			
			@ -67,7 +74,7 @@ async fn main() -> std::io::Result<()> {
 | 
			
		|||
			.service(handlers::v1::schedules::add_list)
 | 
			
		||||
			.service(handlers::v1::schedules::update)
 | 
			
		||||
			.service(handlers::v1::schedules::delete)
 | 
			
		||||
			.service(handlers::v1::ws::controllers::index)
 | 
			
		||||
			.service(handlers::v1::ws::ws_controllers)
 | 
			
		||||
	})
 | 
			
		||||
	.listen(listener)?
 | 
			
		||||
	.run()
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -18,7 +18,7 @@ config = "0.13"
 | 
			
		|||
 | 
			
		||||
chrono = { version = "0.4", features = ["serde"] }
 | 
			
		||||
 | 
			
		||||
sqlx = { version = "0.7", features = ["sqlite", "runtime-async-std", "macros", "chrono"] }
 | 
			
		||||
sqlx = { version = "0.7", features = ["sqlite", "runtime-tokio", "macros", "chrono"] }
 | 
			
		||||
libsqlite3-sys = { version = "*", features = ["bundled"] }
 | 
			
		||||
 | 
			
		||||
log = "0.4"
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1 +1,5 @@
 | 
			
		|||
use std::time::Duration;
 | 
			
		||||
 | 
			
		||||
pub const DEFAULT_PORT: u16 = 4419;
 | 
			
		||||
pub const HEARTBEAT_INTERVAL: Duration = Duration::from_secs(5);
 | 
			
		||||
pub const HEARTBEAT_TIMEOUT: Duration = Duration::from_secs(15);
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -5,7 +5,7 @@ use sqlx::pool::PoolConnection;
 | 
			
		|||
use sqlx::Sqlite;
 | 
			
		||||
 | 
			
		||||
use crate::db::errors::DatabaseError;
 | 
			
		||||
use crate::db::DbTag;
 | 
			
		||||
use crate::db::{DbRelay, DbTag};
 | 
			
		||||
use crate::types::ControllerUid;
 | 
			
		||||
 | 
			
		||||
#[derive(Debug, Clone, Serialize, Deserialize)]
 | 
			
		||||
| 
						 | 
				
			
			@ -144,6 +144,20 @@ impl DbController {
 | 
			
		|||
			.ok_or(DatabaseError::UpdateGetError)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	pub async fn get_relays(
 | 
			
		||||
		&self,
 | 
			
		||||
		conn: &mut PoolConnection<Sqlite>,
 | 
			
		||||
	) -> Result<Vec<DbRelay>, DatabaseError> {
 | 
			
		||||
		sqlx::query_as!(
 | 
			
		||||
			DbRelay,
 | 
			
		||||
			"SELECT * FROM relays WHERE controller_id = ?",
 | 
			
		||||
			self.id
 | 
			
		||||
		)
 | 
			
		||||
		.fetch_all(conn.deref_mut())
 | 
			
		||||
		.await
 | 
			
		||||
		.map_err(DatabaseError::from)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	pub async fn all_inactive(conn: &mut PoolConnection<Sqlite>) -> Result<(), DatabaseError> {
 | 
			
		||||
		sqlx::query!("UPDATE controllers SET active = 0")
 | 
			
		||||
			.execute(conn.deref_mut())
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -20,20 +20,20 @@ pub struct DbRelay {
 | 
			
		|||
 | 
			
		||||
impl DbRelay {
 | 
			
		||||
	pub async fn get_all(conn: &mut PoolConnection<Sqlite>) -> Result<Vec<DbRelay>, DatabaseError> {
 | 
			
		||||
		Ok(sqlx::query_as!(DbRelay, "SELECT * FROM relays")
 | 
			
		||||
		sqlx::query_as!(DbRelay, "SELECT * FROM relays")
 | 
			
		||||
			.fetch_all(conn.deref_mut())
 | 
			
		||||
			.await?)
 | 
			
		||||
			.await
 | 
			
		||||
			.map_err(DatabaseError::from)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	pub async fn get(
 | 
			
		||||
		conn: &mut PoolConnection<Sqlite>,
 | 
			
		||||
		id: i64,
 | 
			
		||||
	) -> Result<Option<DbRelay>, DatabaseError> {
 | 
			
		||||
		Ok(
 | 
			
		||||
		sqlx::query_as!(DbRelay, "SELECT * FROM relays WHERE id = ?", id)
 | 
			
		||||
			.fetch_optional(conn.deref_mut())
 | 
			
		||||
				.await?,
 | 
			
		||||
		)
 | 
			
		||||
			.await
 | 
			
		||||
			.map_err(DatabaseError::from)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	pub async fn get_by_controller_and_num(
 | 
			
		||||
| 
						 | 
				
			
			@ -41,14 +41,15 @@ impl DbRelay {
 | 
			
		|||
		controller: &DbController,
 | 
			
		||||
		number: i64,
 | 
			
		||||
	) -> Result<Option<DbRelay>, DatabaseError> {
 | 
			
		||||
		Ok(sqlx::query_as!(
 | 
			
		||||
		sqlx::query_as!(
 | 
			
		||||
			DbRelay,
 | 
			
		||||
			"SELECT * FROM relays WHERE controller_id = ? AND number = ?",
 | 
			
		||||
			controller.id,
 | 
			
		||||
			number
 | 
			
		||||
		)
 | 
			
		||||
		.fetch_optional(conn.deref_mut())
 | 
			
		||||
		.await?)
 | 
			
		||||
		.await
 | 
			
		||||
		.map_err(DatabaseError::from)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	pub async fn get_by_controller_and_num_or_create(
 | 
			
		||||
| 
						 | 
				
			
			@ -67,9 +68,10 @@ impl DbRelay {
 | 
			
		|||
		conn: &mut PoolConnection<Sqlite>,
 | 
			
		||||
		tag: &DbTag,
 | 
			
		||||
	) -> Result<Vec<DbRelay>, DatabaseError> {
 | 
			
		||||
		Ok(sqlx::query_as!(DbRelay, "SELECT schedule.* FROM relays AS schedule INNER JOIN junction_tag ON junction_tag.schedule_id = schedule.id WHERE junction_tag.tag_id = ?", tag.id)
 | 
			
		||||
		sqlx::query_as!(DbRelay, "SELECT schedule.* FROM relays AS schedule INNER JOIN junction_tag ON junction_tag.schedule_id = schedule.id WHERE junction_tag.tag_id = ?", tag.id)
 | 
			
		||||
			.fetch_all(conn.deref_mut())
 | 
			
		||||
			.await?)
 | 
			
		||||
			.await
 | 
			
		||||
			.map_err(DatabaseError::from)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	pub async fn create(
 | 
			
		||||
| 
						 | 
				
			
			@ -135,9 +137,10 @@ impl DbRelay {
 | 
			
		|||
		&self,
 | 
			
		||||
		conn: &mut PoolConnection<Sqlite>,
 | 
			
		||||
	) -> Result<Vec<String>, DatabaseError> {
 | 
			
		||||
		Ok(sqlx::query_scalar!("SELECT tag FROM tags INNER JOIN junction_tag ON junction_tag.tag_id = tags.id WHERE junction_tag.relay_id = ?", self.id)
 | 
			
		||||
		sqlx::query_scalar!("SELECT tag FROM tags INNER JOIN junction_tag ON junction_tag.tag_id = tags.id WHERE junction_tag.relay_id = ?", self.id)
 | 
			
		||||
			.fetch_all(conn.deref_mut())
 | 
			
		||||
			.await?)
 | 
			
		||||
			.await
 | 
			
		||||
			.map_err(DatabaseError::from)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	pub async fn set_tags(
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
							
								
								
									
										26
									
								
								emgauwa-lib/src/handlers/v1/controllers.rs
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										26
									
								
								emgauwa-lib/src/handlers/v1/controllers.rs
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,26 @@
 | 
			
		|||
use actix_web::{get, web, HttpResponse};
 | 
			
		||||
 | 
			
		||||
use sqlx::{Pool, Sqlite};
 | 
			
		||||
 | 
			
		||||
use crate::db::DbController;
 | 
			
		||||
 | 
			
		||||
use crate::handlers::errors::ApiError;
 | 
			
		||||
use crate::models::{convert_db_list, Controller};
 | 
			
		||||
use crate::types::ConnectedControllersType;
 | 
			
		||||
 | 
			
		||||
#[get("/api/v1/controllers")]
 | 
			
		||||
pub async fn index(
 | 
			
		||||
	pool: web::Data<Pool<Sqlite>>,
 | 
			
		||||
	connected_controllers: web::Data<ConnectedControllersType>,
 | 
			
		||||
) -> Result<HttpResponse, ApiError> {
 | 
			
		||||
	let mut pool_conn = pool.acquire().await?;
 | 
			
		||||
 | 
			
		||||
	let db_controllers = DbController::get_all(&mut pool_conn).await?;
 | 
			
		||||
 | 
			
		||||
	let controllers: Vec<Controller> = convert_db_list(&mut pool_conn, db_controllers)?;
 | 
			
		||||
 | 
			
		||||
	let data = connected_controllers.lock().unwrap();
 | 
			
		||||
	println!("{:?}", *data);
 | 
			
		||||
 | 
			
		||||
	Ok(HttpResponse::Ok().json(controllers))
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -1,3 +1,4 @@
 | 
			
		|||
pub mod controllers;
 | 
			
		||||
pub mod relays;
 | 
			
		||||
pub mod schedules;
 | 
			
		||||
pub mod ws;
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -24,138 +24,3 @@ pub async fn index(pool: web::Data<Pool<Sqlite>>) -> Result<HttpResponse, ApiErr
 | 
			
		|||
 | 
			
		||||
	Ok(HttpResponse::Ok().json(relays))
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
//#[get("/api/v1/tags/tag/{tag}")]
 | 
			
		||||
//pub async fn tagged(
 | 
			
		||||
//	pool: web::Data<Pool<Sqlite>>,
 | 
			
		||||
//	path: web::Path<(String,)>,
 | 
			
		||||
//) -> Result<HttpResponse, ApiError> {
 | 
			
		||||
//	let mut pool_conn = pool.acquire().await?;
 | 
			
		||||
//
 | 
			
		||||
//	let (tag,) = path.into_inner();
 | 
			
		||||
//	let tag_db = Tag::get_by_tag(&mut pool_conn, &tag).await?;
 | 
			
		||||
//
 | 
			
		||||
//	let relays = Relay::get_by_tag(&mut pool_conn, &tag_db).await?;
 | 
			
		||||
//
 | 
			
		||||
//	let return_relays: Vec<ReturnRelay> =
 | 
			
		||||
//		relays.into_iter().map(|s| ReturnRelay::from_relay(s, &mut pool_conn)).collect();
 | 
			
		||||
//
 | 
			
		||||
//	Ok(HttpResponse::Ok().json(return_relays))
 | 
			
		||||
//}
 | 
			
		||||
//
 | 
			
		||||
//#[get("/api/v1/tags/{relay_id}")]
 | 
			
		||||
//pub async fn show(
 | 
			
		||||
//	pool: web::Data<Pool<Sqlite>>,
 | 
			
		||||
//	path: web::Path<(String,)>,
 | 
			
		||||
//) -> Result<HttpResponse, ApiError> {
 | 
			
		||||
//	let mut pool_conn = pool.acquire().await?;
 | 
			
		||||
//
 | 
			
		||||
//	let (relay_uid,) = path.into_inner();
 | 
			
		||||
//	let uid = RelayUid::try_from(relay_uid.as_str()).or(Err(ApiError::BadUid))?;
 | 
			
		||||
//
 | 
			
		||||
//	let relay = Relay::get_by_uid(&mut pool_conn, &uid).await?;
 | 
			
		||||
//
 | 
			
		||||
//	let return_relay = ReturnRelay::from_relay(relay, &mut pool_conn);
 | 
			
		||||
//	Ok(HttpResponse::Ok().json(return_relay))
 | 
			
		||||
//}
 | 
			
		||||
//
 | 
			
		||||
//#[post("/api/v1/tags")]
 | 
			
		||||
//pub async fn add(
 | 
			
		||||
//	pool: web::Data<Pool<Sqlite>>,
 | 
			
		||||
//	data: web::Json<RequestRelay>,
 | 
			
		||||
//) -> Result<HttpResponse, ApiError> {
 | 
			
		||||
//	let mut pool_conn = pool.acquire().await?;
 | 
			
		||||
//
 | 
			
		||||
//	let new_relay = Relay::create(&mut pool_conn, &data.name, &data.periods).await?;
 | 
			
		||||
//
 | 
			
		||||
//	new_relay
 | 
			
		||||
//		.set_tags(&mut pool_conn, data.tags.as_slice())
 | 
			
		||||
//		.await?;
 | 
			
		||||
//
 | 
			
		||||
//	let return_relay = ReturnRelay::from_relay(new_relay, &mut pool_conn);
 | 
			
		||||
//	Ok(HttpResponse::Created().json(return_relay))
 | 
			
		||||
//}
 | 
			
		||||
//
 | 
			
		||||
//async fn add_list_single(
 | 
			
		||||
//	conn: &mut PoolConnection<Sqlite>,
 | 
			
		||||
//	request_relay: &RequestRelay,
 | 
			
		||||
//) -> Result<Relay, DatabaseError> {
 | 
			
		||||
//	let new_relay =
 | 
			
		||||
//		Relay::create(conn, &request_relay.name, &request_relay.periods).await?;
 | 
			
		||||
//
 | 
			
		||||
//	new_relay
 | 
			
		||||
//		.set_tags(conn, request_relay.tags.as_slice())
 | 
			
		||||
//		.await?;
 | 
			
		||||
//
 | 
			
		||||
//	Ok(new_relay)
 | 
			
		||||
//}
 | 
			
		||||
//
 | 
			
		||||
//#[post("/api/v1/tags/list")]
 | 
			
		||||
//pub async fn add_list(
 | 
			
		||||
//	pool: web::Data<Pool<Sqlite>>,
 | 
			
		||||
//	data: web::Json<Vec<RequestRelay>>,
 | 
			
		||||
//) -> Result<HttpResponse, ApiError> {
 | 
			
		||||
//	let mut pool_conn = pool.acquire().await?;
 | 
			
		||||
//
 | 
			
		||||
//	let result: Vec<Result<Relay, DatabaseError>> = data
 | 
			
		||||
//		.as_slice()
 | 
			
		||||
//		.iter()
 | 
			
		||||
//		.map(|request_relay| {
 | 
			
		||||
//			futures::executor::block_on(add_list_single(&mut pool_conn, request_relay))
 | 
			
		||||
//		})
 | 
			
		||||
//		.collect();
 | 
			
		||||
//
 | 
			
		||||
//	let mut return_relays: Vec<ReturnRelay> = Vec::new();
 | 
			
		||||
//	for relay in result {
 | 
			
		||||
//		match relay {
 | 
			
		||||
//			Ok(relay) => return_relays.push(ReturnRelay::from_relay(relay, &mut pool_conn)),
 | 
			
		||||
//			Err(e) => return Ok(HttpResponse::from(e)),
 | 
			
		||||
//		}
 | 
			
		||||
//	}
 | 
			
		||||
//	Ok(HttpResponse::Created().json(return_relays))
 | 
			
		||||
//}
 | 
			
		||||
//
 | 
			
		||||
//#[put("/api/v1/tags/{relay_id}")]
 | 
			
		||||
//pub async fn update(
 | 
			
		||||
//	pool: web::Data<Pool<Sqlite>>,
 | 
			
		||||
//	path: web::Path<(String,)>,
 | 
			
		||||
//	data: web::Json<RequestRelay>,
 | 
			
		||||
//) -> Result<HttpResponse, ApiError> {
 | 
			
		||||
//	let mut pool_conn = pool.acquire().await?;
 | 
			
		||||
//
 | 
			
		||||
//	let (relay_uid,) = path.into_inner();
 | 
			
		||||
//	let uid = RelayUid::try_from(relay_uid.as_str()).or(Err(ApiError::BadUid))?;
 | 
			
		||||
//
 | 
			
		||||
//	let relay = Relay::get_by_uid(&mut pool_conn, &uid).await?;
 | 
			
		||||
//
 | 
			
		||||
//	let relay = relay
 | 
			
		||||
//		.update(&mut pool_conn, data.name.as_str(), &data.periods)
 | 
			
		||||
//		.await?;
 | 
			
		||||
//
 | 
			
		||||
//	relay
 | 
			
		||||
//		.set_tags(&mut pool_conn, data.tags.as_slice())
 | 
			
		||||
//		.await?;
 | 
			
		||||
//
 | 
			
		||||
//	let return_relay = ReturnRelay::from_relay(relay, &mut pool_conn);
 | 
			
		||||
//	Ok(HttpResponse::Ok().json(return_relay))
 | 
			
		||||
//}
 | 
			
		||||
//
 | 
			
		||||
//#[delete("/api/v1/tags/{relay_id}")]
 | 
			
		||||
//pub async fn delete(
 | 
			
		||||
//	pool: web::Data<Pool<Sqlite>>,
 | 
			
		||||
//	path: web::Path<(String,)>,
 | 
			
		||||
//) -> Result<HttpResponse, ApiError> {
 | 
			
		||||
//	let mut pool_conn = pool.acquire().await?;
 | 
			
		||||
//
 | 
			
		||||
//	let (relay_uid,) = path.into_inner();
 | 
			
		||||
//	let uid = RelayUid::try_from(relay_uid.as_str()).or(Err(ApiError::BadUid))?;
 | 
			
		||||
//
 | 
			
		||||
//	match uid {
 | 
			
		||||
//		RelayUid::Off => Err(ApiError::ProtectedRelay),
 | 
			
		||||
//		RelayUid::On => Err(ApiError::ProtectedRelay),
 | 
			
		||||
//		RelayUid::Any(_) => {
 | 
			
		||||
//			Relay::delete_by_uid(&mut pool_conn, uid).await?;
 | 
			
		||||
//			Ok(HttpResponse::Ok().json("relay got deleted"))
 | 
			
		||||
//		}
 | 
			
		||||
//	}
 | 
			
		||||
//}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -104,16 +104,10 @@ pub async fn add_list(
 | 
			
		|||
	let mut pool_conn = pool.acquire().await?;
 | 
			
		||||
 | 
			
		||||
	let mut db_schedules: Vec<DbSchedule> = Vec::new();
 | 
			
		||||
	data.iter().try_for_each(|s| {
 | 
			
		||||
		let new_s = futures::executor::block_on(add_list_single(&mut pool_conn, s));
 | 
			
		||||
		match new_s {
 | 
			
		||||
			Ok(new_s) => {
 | 
			
		||||
	for s in data.iter() {
 | 
			
		||||
		let new_s = futures::executor::block_on(add_list_single(&mut pool_conn, s))?;
 | 
			
		||||
		db_schedules.push(new_s);
 | 
			
		||||
				Ok(())
 | 
			
		||||
	}
 | 
			
		||||
			Err(e) => Err(e),
 | 
			
		||||
		}
 | 
			
		||||
	})?;
 | 
			
		||||
 | 
			
		||||
	let schedules: Vec<Schedule> = convert_db_list(&mut pool_conn, db_schedules)?;
 | 
			
		||||
	Ok(HttpResponse::Created().json(schedules))
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,14 +1,15 @@
 | 
			
		|||
use crate::constants::{HEARTBEAT_INTERVAL, HEARTBEAT_TIMEOUT};
 | 
			
		||||
use crate::db::errors::DatabaseError;
 | 
			
		||||
use crate::db::{DbController, DbRelay};
 | 
			
		||||
use crate::handlers::errors::ApiError;
 | 
			
		||||
use crate::models::Controller;
 | 
			
		||||
use actix::{Actor, StreamHandler};
 | 
			
		||||
use actix_web::{get, web, HttpRequest, HttpResponse};
 | 
			
		||||
use crate::models::{Controller, FromDbModel};
 | 
			
		||||
use crate::types::{ConnectedControllersType, ControllerUid};
 | 
			
		||||
use actix::{Actor, ActorContext, AsyncContext, StreamHandler};
 | 
			
		||||
use actix_web_actors::ws;
 | 
			
		||||
use actix_web_actors::ws::ProtocolError;
 | 
			
		||||
use serde_derive::{Deserialize, Serialize};
 | 
			
		||||
use sqlx::pool::PoolConnection;
 | 
			
		||||
use sqlx::{Pool, Sqlite};
 | 
			
		||||
use std::time::Instant;
 | 
			
		||||
use ws::Message;
 | 
			
		||||
 | 
			
		||||
#[derive(Debug, Serialize, Deserialize)]
 | 
			
		||||
| 
						 | 
				
			
			@ -16,48 +17,35 @@ pub enum ControllerWsAction {
 | 
			
		|||
	Register(Controller),
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
struct ControllerWs {
 | 
			
		||||
pub struct ControllerWs {
 | 
			
		||||
	pub pool: Pool<Sqlite>,
 | 
			
		||||
	pub controller: Option<DbController>,
 | 
			
		||||
	pub controller_uid: Option<ControllerUid>,
 | 
			
		||||
	pub connected_controllers: ConnectedControllersType,
 | 
			
		||||
	pub hb: Instant,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl Actor for ControllerWs {
 | 
			
		||||
	type Context = ws::WebsocketContext<Self>;
 | 
			
		||||
 | 
			
		||||
	fn started(&mut self, ctx: &mut Self::Context) {
 | 
			
		||||
		self.hb(ctx);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	fn stopped(&mut self, _ctx: &mut Self::Context) {
 | 
			
		||||
		if let Some(controller) = &self.controller {
 | 
			
		||||
			let mut pool_conn = futures::executor::block_on(self.pool.acquire()).unwrap();
 | 
			
		||||
			futures::executor::block_on(controller.update_active(&mut pool_conn, false)).unwrap();
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl StreamHandler<Result<Message, ProtocolError>> for ControllerWs {
 | 
			
		||||
	fn handle(&mut self, msg: Result<Message, ProtocolError>, ctx: &mut Self::Context) {
 | 
			
		||||
		if let Some(controller_uid) = &self.controller_uid {
 | 
			
		||||
			let mut pool_conn = futures::executor::block_on(self.pool.acquire()).unwrap();
 | 
			
		||||
 | 
			
		||||
		match msg {
 | 
			
		||||
			Ok(Message::Ping(msg)) => ctx.pong(&msg),
 | 
			
		||||
			Ok(Message::Text(text)) => {
 | 
			
		||||
				let action: ControllerWsAction = serde_json::from_str(&text).unwrap();
 | 
			
		||||
				let action_res =
 | 
			
		||||
					futures::executor::block_on(self.handle_action(&mut pool_conn, action));
 | 
			
		||||
				if let Err(e) = action_res {
 | 
			
		||||
					log::error!("Error handling action: {:?}", e);
 | 
			
		||||
					ctx.text(serde_json::to_string(&e).unwrap());
 | 
			
		||||
			let mut data = self.connected_controllers.lock().unwrap();
 | 
			
		||||
			if let Some(controller) = data.remove(controller_uid) {
 | 
			
		||||
				futures::executor::block_on(controller.c.update_active(&mut pool_conn, false))
 | 
			
		||||
					.unwrap();
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
			_ => {}
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		//let schedules = futures::executor::block_on(DbSchedule::get_all(&mut pool_conn)).unwrap();
 | 
			
		||||
		//let schedules_json = serde_json::to_string(&schedules).unwrap();
 | 
			
		||||
		//ctx.text(schedules_json);
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl ControllerWs {
 | 
			
		||||
	pub async fn handle_action(
 | 
			
		||||
	pub fn handle_action(
 | 
			
		||||
		&mut self,
 | 
			
		||||
		conn: &mut PoolConnection<Sqlite>,
 | 
			
		||||
		action: ControllerWsAction,
 | 
			
		||||
| 
						 | 
				
			
			@ -65,49 +53,94 @@ impl ControllerWs {
 | 
			
		|||
		match action {
 | 
			
		||||
			ControllerWsAction::Register(controller) => {
 | 
			
		||||
				log::info!("Registering controller: {:?}", controller);
 | 
			
		||||
				let c = &controller.controller;
 | 
			
		||||
				let controller_db =
 | 
			
		||||
					DbController::get_by_uid_or_create(conn, &c.uid, &c.name, c.relay_count)
 | 
			
		||||
						.await?;
 | 
			
		||||
				controller_db.update_active(conn, true).await?;
 | 
			
		||||
 | 
			
		||||
				println!("Controller: {:?}", controller_db);
 | 
			
		||||
				let c = &controller.c;
 | 
			
		||||
				let controller_db = futures::executor::block_on(
 | 
			
		||||
					DbController::get_by_uid_or_create(conn, &c.uid, &c.name, c.relay_count),
 | 
			
		||||
				)?;
 | 
			
		||||
				futures::executor::block_on(controller_db.update_active(conn, true))?;
 | 
			
		||||
 | 
			
		||||
				for relay in &controller.relays {
 | 
			
		||||
					let r = &relay.relay;
 | 
			
		||||
					let relay_db = DbRelay::get_by_controller_and_num_or_create(
 | 
			
		||||
					let r = &relay.r;
 | 
			
		||||
					futures::executor::block_on(DbRelay::get_by_controller_and_num_or_create(
 | 
			
		||||
						conn,
 | 
			
		||||
						&controller_db,
 | 
			
		||||
						r.number,
 | 
			
		||||
						&r.name,
 | 
			
		||||
					)
 | 
			
		||||
					.await?;
 | 
			
		||||
					println!("Controller relay: {:?}", relay_db);
 | 
			
		||||
					))?;
 | 
			
		||||
				}
 | 
			
		||||
 | 
			
		||||
				self.controller = Some(controller_db);
 | 
			
		||||
				let controller = Controller::from_db_model(conn, controller_db)?;
 | 
			
		||||
 | 
			
		||||
				let controller_uid = &controller.c.uid;
 | 
			
		||||
				self.controller_uid = Some(controller_uid.clone());
 | 
			
		||||
 | 
			
		||||
				let mut data = self.connected_controllers.lock().unwrap();
 | 
			
		||||
				data.insert(controller_uid.clone(), controller);
 | 
			
		||||
 | 
			
		||||
				Ok(())
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	/// helper method that sends ping to client every 5 seconds (HEARTBEAT_INTERVAL).
 | 
			
		||||
	///
 | 
			
		||||
	/// also this method checks heartbeats from client
 | 
			
		||||
	fn hb(&self, ctx: &mut ws::WebsocketContext<Self>) {
 | 
			
		||||
		ctx.run_interval(HEARTBEAT_INTERVAL, |act, ctx| {
 | 
			
		||||
			// check client heartbeats
 | 
			
		||||
			if Instant::now().duration_since(act.hb) > HEARTBEAT_TIMEOUT {
 | 
			
		||||
				log::warn!("Websocket Controller heartbeat failed, disconnecting!");
 | 
			
		||||
				ctx.stop();
 | 
			
		||||
				// don't try to send a ping
 | 
			
		||||
				return;
 | 
			
		||||
			}
 | 
			
		||||
 | 
			
		||||
			ctx.ping(&[]);
 | 
			
		||||
		});
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#[get("/api/v1/ws/controllers")]
 | 
			
		||||
pub async fn index(
 | 
			
		||||
	pool: web::Data<Pool<Sqlite>>,
 | 
			
		||||
	req: HttpRequest,
 | 
			
		||||
	stream: web::Payload,
 | 
			
		||||
) -> Result<HttpResponse, ApiError> {
 | 
			
		||||
	let resp = ws::start(
 | 
			
		||||
		ControllerWs {
 | 
			
		||||
			pool: pool.get_ref().clone(),
 | 
			
		||||
			controller: None,
 | 
			
		||||
		},
 | 
			
		||||
		&req,
 | 
			
		||||
		stream,
 | 
			
		||||
	)
 | 
			
		||||
	.map_err(|_| ApiError::InternalError(String::from("error starting websocket")));
 | 
			
		||||
	println!("{:?}", resp);
 | 
			
		||||
	resp
 | 
			
		||||
impl StreamHandler<Result<Message, ProtocolError>> for ControllerWs {
 | 
			
		||||
	fn handle(&mut self, msg: Result<Message, ProtocolError>, ctx: &mut Self::Context) {
 | 
			
		||||
		let mut pool_conn = futures::executor::block_on(self.pool.acquire()).unwrap();
 | 
			
		||||
 | 
			
		||||
		let msg = match msg {
 | 
			
		||||
			Err(_) => {
 | 
			
		||||
				ctx.stop();
 | 
			
		||||
				return;
 | 
			
		||||
			}
 | 
			
		||||
			Ok(msg) => msg,
 | 
			
		||||
		};
 | 
			
		||||
 | 
			
		||||
		match msg {
 | 
			
		||||
			Message::Ping(msg) => {
 | 
			
		||||
				self.hb = Instant::now();
 | 
			
		||||
				ctx.pong(&msg)
 | 
			
		||||
			}
 | 
			
		||||
			Message::Pong(_) => {
 | 
			
		||||
				self.hb = Instant::now();
 | 
			
		||||
			}
 | 
			
		||||
			Message::Text(text) => {
 | 
			
		||||
				let action: ControllerWsAction = serde_json::from_str(&text).unwrap();
 | 
			
		||||
				let action_res = self.handle_action(&mut pool_conn, action);
 | 
			
		||||
				if let Err(e) = action_res {
 | 
			
		||||
					log::error!("Error handling action: {:?}", e);
 | 
			
		||||
					ctx.text(serde_json::to_string(&e).unwrap());
 | 
			
		||||
				}
 | 
			
		||||
			}
 | 
			
		||||
			Message::Binary(_) => log::warn!("Received unexpected binary in controller ws"),
 | 
			
		||||
			Message::Close(reason) => {
 | 
			
		||||
				ctx.close(reason);
 | 
			
		||||
				ctx.stop();
 | 
			
		||||
			}
 | 
			
		||||
			Message::Continuation(_) => {
 | 
			
		||||
				ctx.stop();
 | 
			
		||||
			}
 | 
			
		||||
			Message::Nop => (),
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		//let schedules = futures::executor::block_on(DbSchedule::get_all(&mut pool_conn)).unwrap();
 | 
			
		||||
		//let schedules_json = serde_json::to_string(&schedules).unwrap();
 | 
			
		||||
		//ctx.text(schedules_json);
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1 +1,31 @@
 | 
			
		|||
use crate::handlers::errors::ApiError;
 | 
			
		||||
use crate::handlers::v1::ws::controllers::ControllerWs;
 | 
			
		||||
use crate::types::ConnectedControllersType;
 | 
			
		||||
use actix_web::{get, web, HttpRequest, HttpResponse};
 | 
			
		||||
use actix_web_actors::ws;
 | 
			
		||||
use sqlx::{Pool, Sqlite};
 | 
			
		||||
use std::time::Instant;
 | 
			
		||||
 | 
			
		||||
pub mod controllers;
 | 
			
		||||
 | 
			
		||||
#[get("/api/v1/ws/controllers")]
 | 
			
		||||
pub async fn ws_controllers(
 | 
			
		||||
	pool: web::Data<Pool<Sqlite>>,
 | 
			
		||||
	connected_controllers: web::Data<ConnectedControllersType>,
 | 
			
		||||
	req: HttpRequest,
 | 
			
		||||
	stream: web::Payload,
 | 
			
		||||
) -> Result<HttpResponse, ApiError> {
 | 
			
		||||
	let resp = ws::start(
 | 
			
		||||
		ControllerWs {
 | 
			
		||||
			pool: pool.get_ref().clone(),
 | 
			
		||||
			controller_uid: None,
 | 
			
		||||
			connected_controllers: connected_controllers.get_ref().clone(),
 | 
			
		||||
			hb: Instant::now(),
 | 
			
		||||
		},
 | 
			
		||||
		&req,
 | 
			
		||||
		stream,
 | 
			
		||||
	)
 | 
			
		||||
	.map_err(|_| ApiError::InternalError(String::from("error starting websocket")));
 | 
			
		||||
	println!("{:?}", resp);
 | 
			
		||||
	resp
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,6 +1,5 @@
 | 
			
		|||
use crate::db;
 | 
			
		||||
use crate::db::errors::DatabaseError;
 | 
			
		||||
use crate::db::{DbRelay, DbSchedule};
 | 
			
		||||
use crate::db::{DbController, DbRelay, DbSchedule};
 | 
			
		||||
use crate::types::ControllerUid;
 | 
			
		||||
use futures::executor;
 | 
			
		||||
use serde_derive::{Deserialize, Serialize};
 | 
			
		||||
| 
						 | 
				
			
			@ -21,15 +20,15 @@ pub trait FromDbModel {
 | 
			
		|||
#[derive(Serialize, Deserialize, Debug)]
 | 
			
		||||
pub struct Schedule {
 | 
			
		||||
	#[serde(flatten)]
 | 
			
		||||
	pub schedule: DbSchedule,
 | 
			
		||||
	pub s: DbSchedule,
 | 
			
		||||
	pub tags: Vec<String>,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#[derive(Serialize, Deserialize, Debug)]
 | 
			
		||||
pub struct Relay {
 | 
			
		||||
	#[serde(flatten)]
 | 
			
		||||
	pub relay: DbRelay,
 | 
			
		||||
	pub controller: db::DbController,
 | 
			
		||||
	pub r: DbRelay,
 | 
			
		||||
	pub controller: DbController,
 | 
			
		||||
	pub controller_id: ControllerUid,
 | 
			
		||||
	pub tags: Vec<String>,
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -37,7 +36,7 @@ pub struct Relay {
 | 
			
		|||
#[derive(Serialize, Deserialize, Debug)]
 | 
			
		||||
pub struct Controller {
 | 
			
		||||
	#[serde(flatten)]
 | 
			
		||||
	pub controller: db::DbController,
 | 
			
		||||
	pub c: DbController,
 | 
			
		||||
	pub relays: Vec<Relay>,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -51,7 +50,7 @@ impl FromDbModel for Schedule {
 | 
			
		|||
		let schedule = db_model.clone();
 | 
			
		||||
		let tags = executor::block_on(schedule.get_tags(conn))?;
 | 
			
		||||
 | 
			
		||||
		Ok(Schedule { schedule, tags })
 | 
			
		||||
		Ok(Schedule { s: schedule, tags })
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -68,7 +67,7 @@ impl FromDbModel for Relay {
 | 
			
		|||
		let tags = executor::block_on(relay.get_tags(conn))?;
 | 
			
		||||
 | 
			
		||||
		Ok(Relay {
 | 
			
		||||
			relay,
 | 
			
		||||
			r: relay,
 | 
			
		||||
			controller,
 | 
			
		||||
			controller_id,
 | 
			
		||||
			tags,
 | 
			
		||||
| 
						 | 
				
			
			@ -76,6 +75,22 @@ impl FromDbModel for Relay {
 | 
			
		|||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl FromDbModel for Controller {
 | 
			
		||||
	type DbModel = DbController;
 | 
			
		||||
 | 
			
		||||
	fn from_db_model(
 | 
			
		||||
		conn: &mut PoolConnection<Sqlite>,
 | 
			
		||||
		db_model: Self::DbModel,
 | 
			
		||||
	) -> Result<Self, DatabaseError> {
 | 
			
		||||
		let relays_db = executor::block_on(db_model.get_relays(conn))?;
 | 
			
		||||
		let relays = convert_db_list(conn, relays_db)?;
 | 
			
		||||
		Ok(Controller {
 | 
			
		||||
			c: db_model,
 | 
			
		||||
			relays,
 | 
			
		||||
		})
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
pub fn convert_db_list<T: FromDbModel>(
 | 
			
		||||
	conn: &mut PoolConnection<Sqlite>,
 | 
			
		||||
	db_models: Vec<T::DbModel>,
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -7,7 +7,7 @@ use sqlx::{Decode, Encode, Sqlite, Type};
 | 
			
		|||
use std::str::FromStr;
 | 
			
		||||
use uuid::Uuid;
 | 
			
		||||
 | 
			
		||||
#[derive(Clone, Debug)]
 | 
			
		||||
#[derive(Clone, Debug, Eq, PartialEq, Hash)]
 | 
			
		||||
pub struct ControllerUid(Uuid);
 | 
			
		||||
 | 
			
		||||
impl Default for ControllerUid {
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,5 +1,12 @@
 | 
			
		|||
mod controller_uid;
 | 
			
		||||
mod schedule_uid;
 | 
			
		||||
 | 
			
		||||
use crate::models::Controller;
 | 
			
		||||
 | 
			
		||||
pub use controller_uid::ControllerUid;
 | 
			
		||||
pub use schedule_uid::ScheduleUid;
 | 
			
		||||
 | 
			
		||||
use std::collections::HashMap;
 | 
			
		||||
use std::sync::{Arc, Mutex};
 | 
			
		||||
 | 
			
		||||
pub type ConnectedControllersType = Arc<Mutex<HashMap<ControllerUid, Controller>>>;
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue