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 22KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577
  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("onoffNode", delegate (Node node)
  157. {
  158. node.Configs.Set(ConfigType.Writable, new Value(Permission.Read.Permit));
  159. node.Configs.Set(ConfigType.ValueType, DSLink.Nodes.ValueType.Boolean.TypeValue);
  160. node.Value.Set(true);
  161. });
  162. /*Responder.AddNodeClass("rng", delegate(Node node)
  163. {
  164. node.Configs.Set(ConfigType.Writable, new Value(Permission.Read.Permit));
  165. node.Configs.Set(ConfigType.ValueType, DSLink.Nodes.ValueType.Number.TypeValue);
  166. node.Value.Set(0);
  167. });*/
  168. }
  169. public void ClearNodes()
  170. {
  171. foreach (var key in Responder.SuperRoot.Children.Keys)
  172. {
  173. Responder.SuperRoot.Children.TryGetValue(key, out Node oldStream);
  174. if (oldStream.ClassName == "stream")
  175. {
  176. //Console.WriteLine(key);
  177. Responder.SuperRoot.RemoveChild(key);
  178. }
  179. }
  180. }
  181. public override void InitializeDefaultNodes()
  182. {
  183. Responder.SuperRoot.CreateChild("createStream", "streamAdd").BuildNode();
  184. Responder.SuperRoot.CreateChild("createAlarm", "alarmAdd").BuildNode();
  185. Responder.SuperRoot.CreateChild("alarms").BuildNode();
  186. Responder.SuperRoot.CreateChild("streams").BuildNode();
  187. SaveNodes();
  188. //Responder.SuperRoot.CreateChild("removeStream", "remove").BuildNode();
  189. }
  190. private async void _createStreamAction(InvokeRequest request)
  191. {
  192. try
  193. {
  194. var address = request.Parameters["RSTP URL"].Value<string>();
  195. var streamName = request.Parameters["Stream name"].Value<string>();
  196. int port = 8081;
  197. Responder.SuperRoot.Children.TryGetValue("streams", out Node streamRootNode);
  198. if (string.IsNullOrEmpty(address)) return;
  199. if (string.IsNullOrEmpty(streamName)) return;
  200. if (streamRootNode == null) return;
  201. if (streamRootNode.Children.ContainsKey(streamName)) return;
  202. //on part du port 8081 et on scanne les ports jusqu'à en trouver un inutilisé
  203. while (PortInUse(port))
  204. port++;
  205. /*foreach (var oldStreamName in Responder.SuperRoot.Children.Keys)
  206. {
  207. Responder.SuperRoot.Children.TryGetValue(oldStreamName, out Node oldStream);
  208. if (oldStream.ClassName == "stream")
  209. {
  210. //Console.WriteLine(oldStreamName);
  211. if (oldStream.Value.Int == port)
  212. {
  213. Responder.SuperRoot.RemoveChild(oldStreamName);
  214. }
  215. }
  216. }*/
  217. var stream = streamRootNode.CreateChild(streamName, "stream").BuildNode();
  218. stream.Value.Set(port);
  219. stream.CreateChild("remove", "removeNode").BuildNode();
  220. var proc = new Process {
  221. StartInfo = new ProcessStartInfo
  222. {
  223. FileName = "C:\\Program Files\\VideoLAN\\VLC\\VLC.exe",
  224. Arguments = " -vvv -Idummy " + address + " --sout #transcode{vcodec=MJPG,venc=ffmpeg{strict=1}}" +
  225. ":standard{access=http{mime=multipart/x-mixed-replace;boundary=--7b3cc56e5f51db803f790dad720ed50a}," +
  226. "mux=mpjpeg,dst=:" + port + "/} vlc://quit;",
  227. UseShellExecute = false,
  228. RedirectStandardOutput = false,
  229. CreateNoWindow = true
  230. }};
  231. proc.Start();//on lance la conversion du stream avec VLC
  232. _processes.Add(streamName, proc);
  233. await request.Close();
  234. await SaveNodes();
  235. }
  236. catch(Exception e)
  237. {
  238. Console.WriteLine(e.GetType() + ":" + e.Message);
  239. }
  240. }
  241. private async void _createAlarmAction(InvokeRequest request)
  242. {
  243. try
  244. {
  245. var alarmName = request.Parameters["Alarm name"].Value<string>();
  246. var address = request.Parameters["Raspberry Pi IP Address"].Value<string>();
  247. var port = request.Parameters["Port Number"].Value<int>();
  248. var pin = request.Parameters["GPIO Pin"].Value<int>();
  249. Responder.SuperRoot.Children.TryGetValue("alarms", out Node alarmRootNode);
  250. if (string.IsNullOrEmpty(alarmName)) return;
  251. if (alarmRootNode.Children.ContainsKey(alarmName)) return;
  252. var alarm = alarmRootNode.CreateChild(alarmName, "alarm").BuildNode();
  253. alarm.CreateChild("remove", "removeNode").BuildNode();
  254. alarm.CreateChild("armed", "onoffNode").BuildNode();
  255. var ts = new CancellationTokenSource();
  256. CancellationToken ct = ts.Token;
  257. //on lance une tâche en parallèle, qui observe la pin et met à jour la valeur du noeud
  258. Task.Run(() => _updateAlarm(alarm, address, port, pin, ct));
  259. _alarmCTs.Add(alarmName, ts);
  260. await request.Close();
  261. await SaveNodes();
  262. }
  263. catch (Exception e)
  264. {
  265. Console.WriteLine(e.GetType() + ":" + e.Message);
  266. }
  267. }
  268. private async void removeNodeAction(InvokeRequest request)
  269. {
  270. try
  271. {
  272. //Console.WriteLine(request.Path);
  273. //on récupère le nom et le type du noeud ayant envoyé la requête
  274. //dans le chemin de la requête en utilisant une regex (le chemin se terminant par /type/noeud/remove)
  275. var parentName = Regex.Replace(request.Path, "^.*\\/([^\\/]*)\\/([^\\/]*)\\/remove$", "$1");
  276. var nodeName = Regex.Replace(request.Path, "^.*\\/([^\\/]*)\\/([^\\/]*)\\/remove$", "$2");
  277. /*Console.WriteLine(nodeName);
  278. Console.WriteLine(parentName);*/
  279. Responder.SuperRoot.Children.TryGetValue(parentName, out Node parent);
  280. if(parent != null)
  281. {
  282. parent.Children.TryGetValue(nodeName, out Node node);
  283. if (node != null)
  284. {
  285. if (node.ClassName == "stream")
  286. {
  287. //si le noeud est un stream, on tue le processus VLC associé
  288. var port = node.Value.Int;
  289. _processes.TryGetValue(nodeName, out Process proc);
  290. if (proc != null)
  291. proc.Kill();
  292. _processes.Remove(nodeName);
  293. /*System.Diagnostics.Process.Start("CMD.exe", "/C FOR /F \"tokens=5 delims= \" %P IN ('netstat -a -n -o ^|" +
  294. "findstr :" + stream.Value.Int + "') DO @ECHO taskkill /F /PID %P");*/
  295. //FOR /F "tokens=5 delims= " %P IN('netstat -a -n -o | findstr :8081') DO @ECHO TaskKill.exe /PID %P
  296. }
  297. else if (node.ClassName == "alarm")
  298. {
  299. //si le noeud est une alarme, on arrête la tâche associée
  300. _alarmCTs.TryGetValue(nodeName, out CancellationTokenSource ts);
  301. if (ts != null)
  302. ts.Cancel();
  303. }
  304. parent.RemoveChild(nodeName);
  305. }
  306. }
  307. await request.Close();
  308. await SaveNodes();
  309. }
  310. catch (Exception e)
  311. {
  312. Console.WriteLine(e.GetType() + ":" + e.Message);
  313. }
  314. }
  315. private async void _updateAlarm(Node alarm, string addr, int port, int pin, CancellationToken ct)
  316. {
  317. try
  318. {
  319. using (var driver = new Driver(new IPEndPoint(IPAddress.Parse(addr), port)))
  320. {
  321. await driver.ConnectAsync();
  322. await Task.Delay(TimeSpan.FromSeconds(1)); //Give the socket time to get connected
  323. Console.WriteLine("Connected");
  324. using (var controller = new GpioController(PinNumberingScheme.Logical, driver))
  325. {
  326. controller.OpenPin(pin);
  327. controller.SetPinMode(pin, PinMode.InputPullUp);
  328. while (!ct.IsCancellationRequested)
  329. //boucle infinie, qui s'arrête uniquement lorsque l'on annule la tâche
  330. {
  331. controller.WaitForEvent(pin, PinEventTypes.Falling, ct);
  332. if (!ct.IsCancellationRequested)
  333. {
  334. // Console.WriteLine("Beep boop");
  335. alarm.Children.TryGetValue("armed", out Node ArmedNode);
  336. if(ArmedNode.Value.Boolean == true)
  337. {
  338. alarm.Value.Set(true);
  339. }
  340. }
  341. }
  342. //Console.WriteLine("Task cancelled");
  343. controller.ClosePin(pin);
  344. }
  345. }
  346. }
  347. catch (Exception e)
  348. {
  349. Console.WriteLine(e.GetType() + ":" + e.Message);
  350. }
  351. }
  352. #region Initialize Logging
  353. /// <summary>
  354. /// This method initializes the logging engine. In this case Serilog is used, but you
  355. /// may use a variety of logging engines so long as they are compatible with
  356. /// Liblog (the interface used by the DSLink SDK)
  357. /// </summary>
  358. /// <param name="cmdLineOptions"></param>
  359. private static void InitializeLogging(CommandLineArguments cmdLineOptions)
  360. {
  361. if (cmdLineOptions.LogFileFolder != null &&
  362. !cmdLineOptions.LogFileFolder.EndsWith(Path.DirectorySeparatorChar))
  363. {
  364. throw new ArgumentException($"Specified LogFileFolder must end with '{Path.DirectorySeparatorChar}'");
  365. }
  366. var logConfig = new LoggerConfiguration();
  367. switch (cmdLineOptions.LogLevel)
  368. {
  369. case LogLevel.Debug:
  370. logConfig.MinimumLevel.Debug();
  371. break;
  372. case LogLevel.Unspecified:
  373. case LogLevel.Info:
  374. logConfig.MinimumLevel.Information();
  375. break;
  376. case LogLevel.Warning:
  377. logConfig.MinimumLevel.Warning();
  378. break;
  379. case LogLevel.Error:
  380. logConfig.MinimumLevel.Error();
  381. break;
  382. }
  383. logConfig.WriteTo.Console(
  384. outputTemplate:
  385. "{Timestamp:MM/dd/yyyy HH:mm:ss} {SourceContext} [{Level}] {Message}{NewLine}{Exception}");
  386. logConfig.WriteTo.Logger(lc =>
  387. {
  388. lc.WriteTo.RollingFile(cmdLineOptions.LogFileFolder + "log-{Date}.txt", retainedFileCountLimit: 3);
  389. });
  390. Log.Logger = logConfig.CreateLogger();
  391. }
  392. #endregion
  393. #region dslink-json file processing
  394. /// <summary>
  395. /// This method will return an instance of CommandLineArguments build with the following logic rules.
  396. /// The file dslink.json can be utilized to specifiy command line arguments. These live within the config block
  397. /// of the file. Here is an example:
  398. /// ...
  399. /// "configs" : {
  400. /// "broker" : {
  401. /// "type": "url",
  402. /// "value": "mybroker",
  403. /// "default": "http:localhost:8080\conn"
  404. /// },
  405. /// }
  406. ///
  407. /// The code in this method considers only the attribute's name ("broker") and value ("mybroker") in this example).
  408. /// "type" and "default" are not used.
  409. ///
  410. /// The receives an instance of CommandLineArguments previously built from the parser. If the dslink-json paramater
  411. /// is not null the code will use the value specified rather than the default value of "dslink.json" for the file
  412. /// to read containing the information.
  413. ///
  414. /// Options specified on the command line wins out over those specified in the file.
  415. ///
  416. /// </summary>
  417. /// <param name="cmdLineOptions"></param>
  418. /// <returns></returns>
  419. private static CommandLineArguments ProcessDSLinkJson(CommandLineArguments cmdLineOptions)
  420. {
  421. bool errorIfNotFound = false;
  422. string fileName = "dslink.json";
  423. //If filename is specified then error if it is not found
  424. if (!String.IsNullOrEmpty(cmdLineOptions.DSLinkJsonFilename))
  425. {
  426. errorIfNotFound = true;
  427. fileName = cmdLineOptions.DSLinkJsonFilename;
  428. }
  429. string fileData = "";
  430. if (File.Exists(fileName))
  431. {
  432. fileData = File.ReadAllText(fileName);
  433. Console.WriteLine(
  434. $"Will use a combination of options specified from the command line and those specified in {fileName}");
  435. }
  436. else
  437. {
  438. if (errorIfNotFound == true)
  439. {
  440. throw new ArgumentException($"Specified dslink-json file <{fileName}> was not found");
  441. }
  442. else
  443. {
  444. return cmdLineOptions;
  445. }
  446. }
  447. JObject dslinkJson = JObject.Parse(fileData);
  448. var dsLinkJsonConfig = dslinkJson["configs"];
  449. var cmdLineOptionsDslinkJson = new CommandLineArguments();
  450. cmdLineOptionsDslinkJson.BrokerUrl =
  451. GetDsLinkStringValueForAttributeName(dsLinkJsonConfig, "broker", cmdLineOptions.BrokerUrl);
  452. cmdLineOptionsDslinkJson.LinkName =
  453. GetDsLinkStringValueForAttributeName(dsLinkJsonConfig, "name", cmdLineOptions.LinkName);
  454. cmdLineOptionsDslinkJson.LogFileFolder =
  455. GetDsLinkStringValueForAttributeName(dsLinkJsonConfig, "log-file", cmdLineOptions.LogFileFolder);
  456. cmdLineOptionsDslinkJson.KeysFolder =
  457. GetDsLinkStringValueForAttributeName(dsLinkJsonConfig, "key", cmdLineOptions.KeysFolder);
  458. cmdLineOptionsDslinkJson.NodesFileName =
  459. GetDsLinkStringValueForAttributeName(dsLinkJsonConfig, "nodes", cmdLineOptions.NodesFileName);
  460. cmdLineOptionsDslinkJson.Token =
  461. GetDsLinkStringValueForAttributeName(dsLinkJsonConfig, "token", cmdLineOptions.Token);
  462. cmdLineOptionsDslinkJson.LogLevel = GetDsLinkLogLevel(dsLinkJsonConfig, cmdLineOptions.LogLevel);
  463. return cmdLineOptionsDslinkJson;
  464. }
  465. private static LogLevel GetDsLinkLogLevel(JToken configObj, LogLevel logLevel)
  466. {
  467. if (logLevel != LogLevel.Unspecified)
  468. {
  469. return logLevel;
  470. }
  471. string testString = "";
  472. try
  473. {
  474. testString = configObj["log"]["value"].ToString();
  475. }
  476. catch
  477. {
  478. }
  479. ;
  480. LogLevel useLogLevel = LogLevel.Info;
  481. if (!Enum.TryParse(testString, out useLogLevel))
  482. {
  483. throw new ArgumentException("Invalid 'value' specified for 'log' value in dslink-json file");
  484. }
  485. return useLogLevel;
  486. }
  487. private static string GetDsLinkStringValueForAttributeName(JToken configObj, string attributeName,
  488. string cmdLineValue)
  489. {
  490. //use cmdLineValue if specified else attempt to use the one from the dslink-json
  491. if (cmdLineValue != null)
  492. {
  493. return cmdLineValue;
  494. }
  495. try
  496. {
  497. return configObj[attributeName]["value"].ToString();
  498. }
  499. catch
  500. {
  501. return null;
  502. }
  503. }
  504. #endregion processingq
  505. }
  506. }