No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

RSTP_DSLink.cs 21KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  1. using DSLink;
  2. using DSLink.Nodes;
  3. using DSLink.Nodes.Actions;
  4. using DSLink.Request;
  5. using Newtonsoft.Json.Linq;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Threading;
  9. using System.Threading.Tasks;
  10. using System.Net.NetworkInformation;
  11. using System.Net;
  12. using System.Text.RegularExpressions;
  13. using System.Diagnostics;
  14. using Iot.Device.Pigpio;
  15. using Serilog;
  16. using CommandLine;
  17. using System.IO;
  18. using RTSP_DSLink;
  19. using System.Device.Gpio;
  20. namespace RTSP_DSLink
  21. {
  22. public class RSTP_DSLink : DSLinkContainer
  23. {
  24. private readonly Dictionary<string, System.Diagnostics.Process> _processes;
  25. private readonly Dictionary<string, CancellationTokenSource> _alarmCTs;
  26. public static bool PortInUse(int port)
  27. {
  28. bool inUse = false;
  29. IPGlobalProperties ipProperties = IPGlobalProperties.GetIPGlobalProperties();
  30. IPEndPoint[] ipEndPoints = ipProperties.GetActiveTcpListeners();
  31. foreach (IPEndPoint endPoint in ipEndPoints)
  32. {
  33. if (endPoint.Port == port)
  34. {
  35. inUse = true;
  36. break;
  37. }
  38. }
  39. return inUse;
  40. }
  41. private static void Main(string[] args)
  42. {
  43. Parser.Default.ParseArguments<CommandLineArguments>(args)
  44. .WithParsed(cmdLineOptions =>
  45. {
  46. cmdLineOptions = ProcessDSLinkJson(cmdLineOptions);
  47. //Init the logging engine
  48. InitializeLogging(cmdLineOptions);
  49. //Construct a link Configuration
  50. var config = new Configuration(cmdLineOptions.LinkName, true, true);
  51. //Construct our custom link
  52. var dslink = new RSTP_DSLink(config, cmdLineOptions);
  53. InitializeLink(dslink).Wait();
  54. })
  55. .WithNotParsed(errors => { Environment.Exit(-1); });
  56. while (true)
  57. {
  58. Thread.Sleep(1000);
  59. }
  60. }
  61. public static async Task InitializeLink(RSTP_DSLink dsLink)
  62. {
  63. await dsLink.Connect();
  64. //dsLink.ClearNodes();
  65. }
  66. public RSTP_DSLink(Configuration config, CommandLineArguments cmdLineOptions) : base(config)
  67. {
  68. _processes = new Dictionary<string, Process>();
  69. _alarmCTs = new Dictionary<string, CancellationTokenSource>();
  70. //Perform any configuration overrides from command line options
  71. if (cmdLineOptions.BrokerUrl != null)
  72. {
  73. config.BrokerUrl = cmdLineOptions.BrokerUrl;
  74. }
  75. if (cmdLineOptions.Token != null)
  76. {
  77. config.Token = cmdLineOptions.Token;
  78. }
  79. if (cmdLineOptions.NodesFileName != null)
  80. {
  81. config.NodesFilename = cmdLineOptions.NodesFileName;
  82. }
  83. if (cmdLineOptions.KeysFolder != null)
  84. {
  85. config.KeysFolder = cmdLineOptions.KeysFolder;
  86. }
  87. Responder.AddNodeClass("streamAdd", delegate(Node node)
  88. {
  89. node.Configs.Set(ConfigType.DisplayName, new Value("Add Stream"));
  90. node.AddParameter(new Parameter
  91. {
  92. Name = "RSTP URL",
  93. ValueType = DSLink.Nodes.ValueType.String
  94. }
  95. );
  96. node.AddParameter(new Parameter
  97. {
  98. Name = "Stream name",
  99. ValueType = DSLink.Nodes.ValueType.String
  100. }
  101. );
  102. node.SetAction(new ActionHandler(Permission.Config, _createStreamAction));
  103. });
  104. Responder.AddNodeClass("alarmAdd", delegate (Node node)
  105. {
  106. node.Configs.Set(ConfigType.DisplayName, new Value("Add Alarm"));
  107. node.AddParameter(new Parameter
  108. {
  109. Name = "Alarm name",
  110. ValueType = DSLink.Nodes.ValueType.String
  111. }
  112. );
  113. /*node.AddParameter(new Parameter
  114. {
  115. Name = "Camera name",
  116. ValueType = DSLink.Nodes.ValueType.String
  117. }
  118. ); */
  119. node.AddParameter(new Parameter
  120. {
  121. Name = "Raspberry Pi IP Address",
  122. ValueType = DSLink.Nodes.ValueType.String
  123. }
  124. );
  125. node.AddParameter(new Parameter
  126. {
  127. Name = "Port Number",
  128. ValueType = DSLink.Nodes.ValueType.Number
  129. }
  130. );
  131. node.AddParameter(new Parameter
  132. {
  133. Name = "GPIO Pin",
  134. ValueType = DSLink.Nodes.ValueType.Number
  135. }
  136. );
  137. node.SetAction(new ActionHandler(Permission.Config, _createAlarmAction));
  138. });
  139. Responder.AddNodeClass("removeNode", delegate (Node node)
  140. {
  141. node.Configs.Set(ConfigType.DisplayName, new Value("Remove"));
  142. node.SetAction(new ActionHandler(Permission.Config, removeNodeAction));
  143. });
  144. Responder.AddNodeClass("stream", delegate (Node node)
  145. {
  146. node.Configs.Set(ConfigType.Writable, new Value(Permission.Read.Permit));
  147. node.Configs.Set(ConfigType.ValueType, DSLink.Nodes.ValueType.Number.TypeValue);
  148. node.Value.Set(-1);
  149. });
  150. Responder.AddNodeClass("alarm", delegate (Node node)
  151. {
  152. node.Configs.Set(ConfigType.Writable, new Value(Permission.Read.Permit));
  153. node.Configs.Set(ConfigType.ValueType, DSLink.Nodes.ValueType.Boolean.TypeValue);
  154. node.Value.Set(false);
  155. });
  156. /*Responder.AddNodeClass("rng", delegate(Node node)
  157. {
  158. node.Configs.Set(ConfigType.Writable, new Value(Permission.Read.Permit));
  159. node.Configs.Set(ConfigType.ValueType, DSLink.Nodes.ValueType.Number.TypeValue);
  160. node.Value.Set(0);
  161. });*/
  162. }
  163. public void ClearNodes()
  164. {
  165. foreach (var key in Responder.SuperRoot.Children.Keys)
  166. {
  167. Responder.SuperRoot.Children.TryGetValue(key, out Node oldStream);
  168. if (oldStream.ClassName == "stream")
  169. {
  170. //Console.WriteLine(key);
  171. Responder.SuperRoot.RemoveChild(key);
  172. }
  173. }
  174. }
  175. public override void InitializeDefaultNodes()
  176. {
  177. Responder.SuperRoot.CreateChild("createStream", "streamAdd").BuildNode();
  178. Responder.SuperRoot.CreateChild("createAlarm", "alarmAdd").BuildNode();
  179. SaveNodes();
  180. //Responder.SuperRoot.CreateChild("removeStream", "remove").BuildNode();
  181. }
  182. private async void _createStreamAction(InvokeRequest request)
  183. {
  184. try
  185. {
  186. var address = request.Parameters["RSTP URL"].Value<string>();
  187. var streamName = request.Parameters["Stream name"].Value<string>();
  188. int port = 8081;
  189. if (string.IsNullOrEmpty(address)) return;
  190. while (PortInUse(port))
  191. port++;
  192. /*foreach (var oldStreamName in Responder.SuperRoot.Children.Keys)
  193. {
  194. Responder.SuperRoot.Children.TryGetValue(oldStreamName, out Node oldStream);
  195. if (oldStream.ClassName == "stream")
  196. {
  197. //Console.WriteLine(oldStreamName);
  198. if (oldStream.Value.Int == port)
  199. {
  200. Responder.SuperRoot.RemoveChild(oldStreamName);
  201. }
  202. }
  203. }*/
  204. if (string.IsNullOrEmpty(streamName)) return;
  205. if (Responder.SuperRoot.Children.ContainsKey(streamName)) return;
  206. var stream = Responder.SuperRoot.CreateChild(streamName, "stream").BuildNode();
  207. stream.Value.Set(port);
  208. stream.CreateChild("remove", "removeNode").BuildNode();
  209. /*System.Diagnostics.Process.Start("CMD.exe", "/C \"C:\\Program Files\\VideoLAN\\VLC\\VLC.exe\"" +
  210. " -vvv -Idummy " + address + " --sout #transcode{vcodec=MJPG,venc=ffmpeg{strict=1}}" +
  211. ":standard{access=http{mime=multipart/x-mixed-replace;boundary=--7b3cc56e5f51db803f790dad720ed50a}," +
  212. "mux=mpjpeg,dst=:" + port + "/} vlc://quit;");*/
  213. var proc = new Process {
  214. StartInfo = new ProcessStartInfo
  215. {
  216. FileName = "C:\\Program Files\\VideoLAN\\VLC\\VLC.exe",
  217. Arguments = " -vvv -Idummy " + address + " --sout #transcode{vcodec=MJPG,venc=ffmpeg{strict=1}}" +
  218. ":standard{access=http{mime=multipart/x-mixed-replace;boundary=--7b3cc56e5f51db803f790dad720ed50a}," +
  219. "mux=mpjpeg,dst=:" + port + "/} vlc://quit;",
  220. UseShellExecute = false,
  221. RedirectStandardOutput = false,
  222. CreateNoWindow = true
  223. }};
  224. proc.Start();
  225. _processes.Add(streamName, proc);
  226. await request.Close();
  227. await SaveNodes();
  228. }
  229. catch(Exception e)
  230. {
  231. Console.WriteLine(e.GetType() + ":" + e.Message);
  232. }
  233. }
  234. private async void _createAlarmAction(InvokeRequest request)
  235. {
  236. try
  237. {
  238. var alarmName = request.Parameters["Alarm name"].Value<string>();
  239. //var cameraName = request.Parameters["Camera name"].Value<string>();
  240. var address = request.Parameters["Raspberry Pi IP Address"].Value<string>();
  241. var port = request.Parameters["Port Number"].Value<int>();
  242. var pin = request.Parameters["GPIO Pin"].Value<int>();
  243. if (string.IsNullOrEmpty(alarmName)) return;
  244. //if (string.IsNullOrEmpty(cameraName)) return;
  245. if (Responder.SuperRoot.Children.ContainsKey(alarmName)) return;
  246. //if (!Responder.SuperRoot.Children.ContainsKey(cameraName)) return;
  247. var alarm = Responder.SuperRoot.CreateChild(alarmName, "alarm").BuildNode();
  248. alarm.CreateChild("remove", "removeNode").BuildNode();
  249. var ts = new CancellationTokenSource();
  250. CancellationToken ct = ts.Token;
  251. Task.Run(() => _updateAlarm(alarm, address, port, pin, ct));
  252. _alarmCTs.Add(alarmName, ts);
  253. await request.Close();
  254. await SaveNodes();
  255. }
  256. catch (Exception e)
  257. {
  258. Console.WriteLine(e.GetType() + ":" + e.Message);
  259. }
  260. }
  261. private async void removeNodeAction(InvokeRequest request)
  262. {
  263. try
  264. {
  265. //Console.WriteLine(request.Path);
  266. //on récupère le nom du noeud ayant envoyé la requête
  267. //dans le chemin de la requête en utilisant une regex (le chemin se terminant par /noeud/remove)
  268. var nodeName = Regex.Replace(request.Path, "^.*\\/([^\\/]*)\\/remove$", "$1");
  269. //Console.WriteLine(streamName);
  270. Responder.SuperRoot.Children.TryGetValue(nodeName, out Node node);
  271. if (node != null)
  272. {
  273. if (node.ClassName == "stream")
  274. {
  275. var port = node.Value.Int;
  276. _processes.TryGetValue(nodeName, out Process proc);
  277. if (proc != null)
  278. proc.Kill();
  279. _processes.Remove(nodeName);
  280. /*System.Diagnostics.Process.Start("CMD.exe", "/C FOR /F \"tokens=5 delims= \" %P IN ('netstat -a -n -o ^|" +
  281. "findstr :" + stream.Value.Int + "') DO @ECHO taskkill /F /PID %P");*/
  282. //FOR /F "tokens=5 delims= " %P IN('netstat -a -n -o | findstr :8081') DO @ECHO TaskKill.exe /PID %P
  283. }
  284. else if (node.ClassName == "alarm")
  285. {
  286. _alarmCTs.TryGetValue(nodeName, out CancellationTokenSource ts);
  287. if(ts != null)
  288. ts.Cancel();
  289. }
  290. Responder.SuperRoot.RemoveChild(nodeName);
  291. }
  292. await request.Close();
  293. await SaveNodes();
  294. }
  295. catch (Exception e)
  296. {
  297. Console.WriteLine(e.GetType() + ":" + e.Message);
  298. }
  299. }
  300. private async void _updateAlarm(Node alarm, string addr, int port, int pin, CancellationToken ct)
  301. {
  302. using (var driver = new Driver(new IPEndPoint(IPAddress.Parse(addr), port)))
  303. {
  304. await driver.ConnectAsync();
  305. await Task.Delay(TimeSpan.FromSeconds(1)); //Give the socket time to get connected
  306. Console.WriteLine("Connected");
  307. using (var controller = new GpioController(PinNumberingScheme.Logical, driver))
  308. {
  309. controller.OpenPin(pin);
  310. controller.SetPinMode(pin, PinMode.InputPullUp);
  311. while (!ct.IsCancellationRequested)
  312. {
  313. controller.WaitForEvent(pin, PinEventTypes.Falling, ct);
  314. if(!ct.IsCancellationRequested)
  315. {
  316. Console.WriteLine("Beep boop");
  317. alarm.Value.Set(true);
  318. }
  319. }
  320. Console.WriteLine("Task cancelled");
  321. controller.ClosePin(pin);
  322. }
  323. }
  324. }
  325. #region Initialize Logging
  326. /// <summary>
  327. /// This method initializes the logging engine. In this case Serilog is used, but you
  328. /// may use a variety of logging engines so long as they are compatible with
  329. /// Liblog (the interface used by the DSLink SDK)
  330. /// </summary>
  331. /// <param name="cmdLineOptions"></param>
  332. private static void InitializeLogging(CommandLineArguments cmdLineOptions)
  333. {
  334. if (cmdLineOptions.LogFileFolder != null &&
  335. !cmdLineOptions.LogFileFolder.EndsWith(Path.DirectorySeparatorChar))
  336. {
  337. throw new ArgumentException($"Specified LogFileFolder must end with '{Path.DirectorySeparatorChar}'");
  338. }
  339. var logConfig = new LoggerConfiguration();
  340. switch (cmdLineOptions.LogLevel)
  341. {
  342. case LogLevel.Debug:
  343. logConfig.MinimumLevel.Debug();
  344. break;
  345. case LogLevel.Unspecified:
  346. case LogLevel.Info:
  347. logConfig.MinimumLevel.Information();
  348. break;
  349. case LogLevel.Warning:
  350. logConfig.MinimumLevel.Warning();
  351. break;
  352. case LogLevel.Error:
  353. logConfig.MinimumLevel.Error();
  354. break;
  355. }
  356. logConfig.WriteTo.Console(
  357. outputTemplate:
  358. "{Timestamp:MM/dd/yyyy HH:mm:ss} {SourceContext} [{Level}] {Message}{NewLine}{Exception}");
  359. logConfig.WriteTo.Logger(lc =>
  360. {
  361. lc.WriteTo.RollingFile(cmdLineOptions.LogFileFolder + "log-{Date}.txt", retainedFileCountLimit: 3);
  362. });
  363. Log.Logger = logConfig.CreateLogger();
  364. }
  365. #endregion
  366. #region dslink-json file processing
  367. /// <summary>
  368. /// This method will return an instance of CommandLineArguments build with the following logic rules.
  369. /// The file dslink.json can be utilized to specifiy command line arguments. These live within the config block
  370. /// of the file. Here is an example:
  371. /// ...
  372. /// "configs" : {
  373. /// "broker" : {
  374. /// "type": "url",
  375. /// "value": "mybroker",
  376. /// "default": "http:localhost:8080\conn"
  377. /// },
  378. /// }
  379. ///
  380. /// The code in this method considers only the attribute's name ("broker") and value ("mybroker") in this example).
  381. /// "type" and "default" are not used.
  382. ///
  383. /// The receives an instance of CommandLineArguments previously built from the parser. If the dslink-json paramater
  384. /// is not null the code will use the value specified rather than the default value of "dslink.json" for the file
  385. /// to read containing the information.
  386. ///
  387. /// Options specified on the command line wins out over those specified in the file.
  388. ///
  389. /// </summary>
  390. /// <param name="cmdLineOptions"></param>
  391. /// <returns></returns>
  392. private static CommandLineArguments ProcessDSLinkJson(CommandLineArguments cmdLineOptions)
  393. {
  394. bool errorIfNotFound = false;
  395. string fileName = "dslink.json";
  396. //If filename is specified then error if it is not found
  397. if (!String.IsNullOrEmpty(cmdLineOptions.DSLinkJsonFilename))
  398. {
  399. errorIfNotFound = true;
  400. fileName = cmdLineOptions.DSLinkJsonFilename;
  401. }
  402. string fileData = "";
  403. if (File.Exists(fileName))
  404. {
  405. fileData = File.ReadAllText(fileName);
  406. Console.WriteLine(
  407. $"Will use a combination of options specified from the command line and those specified in {fileName}");
  408. }
  409. else
  410. {
  411. if (errorIfNotFound == true)
  412. {
  413. throw new ArgumentException($"Specified dslink-json file <{fileName}> was not found");
  414. }
  415. else
  416. {
  417. return cmdLineOptions;
  418. }
  419. }
  420. JObject dslinkJson = JObject.Parse(fileData);
  421. var dsLinkJsonConfig = dslinkJson["configs"];
  422. var cmdLineOptionsDslinkJson = new CommandLineArguments();
  423. cmdLineOptionsDslinkJson.BrokerUrl =
  424. GetDsLinkStringValueForAttributeName(dsLinkJsonConfig, "broker", cmdLineOptions.BrokerUrl);
  425. cmdLineOptionsDslinkJson.LinkName =
  426. GetDsLinkStringValueForAttributeName(dsLinkJsonConfig, "name", cmdLineOptions.LinkName);
  427. cmdLineOptionsDslinkJson.LogFileFolder =
  428. GetDsLinkStringValueForAttributeName(dsLinkJsonConfig, "log-file", cmdLineOptions.LogFileFolder);
  429. cmdLineOptionsDslinkJson.KeysFolder =
  430. GetDsLinkStringValueForAttributeName(dsLinkJsonConfig, "key", cmdLineOptions.KeysFolder);
  431. cmdLineOptionsDslinkJson.NodesFileName =
  432. GetDsLinkStringValueForAttributeName(dsLinkJsonConfig, "nodes", cmdLineOptions.NodesFileName);
  433. cmdLineOptionsDslinkJson.Token =
  434. GetDsLinkStringValueForAttributeName(dsLinkJsonConfig, "token", cmdLineOptions.Token);
  435. cmdLineOptionsDslinkJson.LogLevel = GetDsLinkLogLevel(dsLinkJsonConfig, cmdLineOptions.LogLevel);
  436. return cmdLineOptionsDslinkJson;
  437. }
  438. private static LogLevel GetDsLinkLogLevel(JToken configObj, LogLevel logLevel)
  439. {
  440. if (logLevel != LogLevel.Unspecified)
  441. {
  442. return logLevel;
  443. }
  444. string testString = "";
  445. try
  446. {
  447. testString = configObj["log"]["value"].ToString();
  448. }
  449. catch
  450. {
  451. }
  452. ;
  453. LogLevel useLogLevel = LogLevel.Info;
  454. if (!Enum.TryParse(testString, out useLogLevel))
  455. {
  456. throw new ArgumentException("Invalid 'value' specified for 'log' value in dslink-json file");
  457. }
  458. return useLogLevel;
  459. }
  460. private static string GetDsLinkStringValueForAttributeName(JToken configObj, string attributeName,
  461. string cmdLineValue)
  462. {
  463. //use cmdLineValue if specified else attempt to use the one from the dslink-json
  464. if (cmdLineValue != null)
  465. {
  466. return cmdLineValue;
  467. }
  468. try
  469. {
  470. return configObj[attributeName]["value"].ToString();
  471. }
  472. catch
  473. {
  474. return null;
  475. }
  476. }
  477. #endregion processingq
  478. }
  479. }