FlexBox.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Net.Http;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using FBoxClientDriver;
  7. using FBoxClientDriver.Contract;
  8. using FBoxClientDriver.Impl;
  9. using NLog;
  10. namespace Ropin.Environmentally.WebScada
  11. {
  12. public class FlexBox
  13. {
  14. }
  15. public class FBoxClientParameters
  16. {
  17. //Fbox线上地址,私有云的改成私有地址即可。
  18. public static string IdServer { get; set; } = "https://account.flexem.com/core";
  19. public static string MainServer { get; set; } = "http://fbox360.com";
  20. public static string HdataServer { get; set; } = "http://fbhs1.fbox360.com";
  21. //填写平台创建的开发者账号
  22. public static string ClientId { get; set; } = "b19d14eeacb74522bd29627b79c18ab8"; //"a1d6d4291a0e4caf8d1b486cdb12ba0f"
  23. public static string ClientSecret { get; set; } = "7b89e021586c43d3b79440ba6eea0b67"; //"412e1a83cd1e4e64a98ef9eb83f9aaf9"
  24. }
  25. public class demo : IDisposable //实现接口
  26. {
  27. public static Logger logger = LogManager.GetLogger("SimpleDemo"); //打印日志在文件夹中,文件夹命名SimpleDemo
  28. public readonly IFBoxClientManager _fbox;
  29. private readonly string guid = Guid.NewGuid().ToString("N");
  30. public demo()
  31. {
  32. HttpClient httpClient = new HttpClient();
  33. var provider = new DefaultCredentialProvider(FBoxClientParameters.ClientId, FBoxClientParameters.ClientSecret);
  34. _fbox = new FBoxClientManager(FBoxClientParameters.IdServer, FBoxClientParameters.MainServer,
  35. FBoxClientParameters.HdataServer, provider, guid, null, httpClient);
  36. //侦听盒子状态变更事件 (订阅事件是,只能订阅一次)
  37. _fbox.BoxConnectStateChanged += _fbox_BoxConnectStateChanged;
  38. // 侦听实时数据变更事件 ,和盒子状态变更事件同时用
  39. //_fbox.DataMonitorValueChanged += _fbox_DataMonitorValueChanged;
  40. //侦听报警触发推送,和盒子状态变更事件同时用
  41. // _fbox.AlarmTriggered += _fbox_AlarmTriggered;
  42. //监听时报警还原推送
  43. //_fbox.AlarmRecoverd += _fbox_AlarmRecoverd;
  44. Console.WriteLine(guid);
  45. Console.WriteLine();
  46. }
  47. public void Dispose()
  48. {
  49. _fbox?.Dispose();
  50. }
  51. //时实数据推送方法,处理有时间限制,尽量控制在100秒左右
  52. private void _fbox_DataMonitorValueChanged(object sender, IList<DataMonitorValueChangedArgs> e)
  53. {
  54. foreach (var dmon in e)
  55. {
  56. Console.WriteLine(
  57. $"dmv:{dmon.Uid},Value:{dmon.Value},Status:{dmon.Status},BoxNo:{dmon.BoxNo},Name:{dmon.Name},BoxId:{dmon.BoxId},GroupName:{dmon.GroupName},Timestamp:{dmon.Timestamp}");
  58. //logger.Error($"uid:{dmon.Uid},value:{dmon.Value},Status:{dmon.Status},BoxNo:{dmon.BoxNo},BoxId:{dmon.BoxId},GroupName:{dmon.GroupName},Timestamp:{dmon.Timestamp},Name:{dmon.Name}");
  59. System.DateTime currentTime = new System.DateTime();
  60. currentTime = System.DateTime.Now;
  61. Console.WriteLine(currentTime);
  62. logger.Info("时间" + currentTime + "Uid" + dmon.Uid + "名称:" + dmon.Name + "值:" + dmon.Value + "状态:" + dmon.Status + "盒子号:" + dmon.BoxNo + "分组名称:" + dmon.GroupName + "时间" + dmon.Timestamp);
  63. }
  64. //接收数据,先存数据,再把数据显示到页面上
  65. }
  66. /// <summary>
  67. /// 盒子状态变更函数监听一次事件也叫(订阅事件)
  68. /// 第一个连接上或者是重连signalr,推送的盒子的状态只会推送盒子的在线状态
  69. /// </summary>
  70. /// <param name="sender"></param>
  71. /// <param name="e"></param>
  72. ///
  73. ///
  74. private void _fbox_BoxConnectStateChanged(object sender, IList<BoxConnectionStateItem> e) //处理有时间限制,尽量控制在100秒左右
  75. {
  76. foreach (var stateItem in e)
  77. {
  78. // 每一个盒子断线之后再上线,需要重新调用这个方法判断盒子的一个状态 ,这个方法是不包含在这个监听事件里面的,只是写在这个地方了,如果客户自己写的话,需要客户调用这个方法。
  79. if (stateItem.NewState == BoxConnectionState.Connected ||
  80. stateItem.NewState == BoxConnectionState.TimedOut) //判断这个盒子的在线的一个状态
  81. {
  82. //监控所有的监控点,(这个方法里面会有报错误500,需要在task里面捕获异常,需要添加重试机制)
  83. _fbox.StartAllDataMonitorPointsOnBox(new BoxArgs(stateItem.BoxNo)).Wait();
  84. }
  85. Console.WriteLine($"{stateItem.BoxNo},{stateItem.NewState},{stateItem.NetworkType}");
  86. logger.Error($"{stateItem.BoxNo},{stateItem.NewState}"); //打印日志在文件夹中
  87. logger.Info("盒子号:" + stateItem.BoxNo + "状态:" + stateItem.NewState);
  88. logger.Info("信号:" + stateItem.Rssi);
  89. }
  90. }
  91. private int guard;
  92. // 报警触发推送
  93. private void _fbox_AlarmTriggered(object sender, IList<AlarmTriggerDefinitionArgs> e)
  94. {
  95. foreach (var triggered in e)
  96. {
  97. Console.WriteLine(
  98. $"dmv:{triggered.BoxNo},{triggered.BoxId}{triggered.Uid},{triggered.Value},{triggered.Name}{triggered.Message}");
  99. }
  100. System.DateTime currentTime = new System.DateTime();
  101. currentTime = System.DateTime.Now;
  102. Console.WriteLine(currentTime);
  103. }
  104. //报警还原推送
  105. private void _fbox_AlarmRecoverd(object sender, IList<AlarmRecoverDefinitonArgs> e)
  106. {
  107. foreach (var recover in e)
  108. {
  109. Console.WriteLine($"dmv:{recover.Uid},{recover.Value},{recover.Name},{recover.Message}");
  110. }
  111. }
  112. //<summary>
  113. //启用
  114. //</summary>
  115. public async Task Start()
  116. {
  117. try
  118. {
  119. //通过参数登录服务器,连接signalr
  120. await _fbox.Restart();
  121. //只会重启一次,是单例,盒子和服务器只能建立一条通道传输数据
  122. }
  123. catch (Exception e)
  124. {
  125. Console.WriteLine(e); //控制台输出
  126. throw e; //抛异常
  127. }
  128. }
  129. // <summary>
  130. //监控点写值
  131. // </summary>
  132. // public void WriteValue()
  133. // {
  134. //
  135. // try
  136. // {
  137. // _fbox.WriteValue(new DataMonitorWriteValueArgsV2()
  138. // {
  139. // BoxNo = boxNo,
  140. // // DataMonitorUid = 177968673027267519,
  141. // DataMonitorGroupName= "test",
  142. // DataMonitorName= "ceshi1",
  143. // Value = 1,
  144. // Type = 0
  145. // }).Wait();
  146. //
  147. // }
  148. // catch (Exception e)
  149. // {
  150. // Console.WriteLine(e);
  151. // throw;
  152. // }
  153. //
  154. // }
  155. //<summary>
  156. //获取历史记录条目 (历史记录接口)
  157. //</summary>
  158. // public void GetHdataItems()
  159. // {
  160. // var itemsss = _fbox.GetHdataItems(new BoxArgs()
  161. // {
  162. // BoxNo = boxNo
  163. // }).Result;
  164. // foreach (var items in itemsss)
  165. // {
  166. // Console.WriteLine(items.BoxId);
  167. // foreach (var channels in items.Channels)
  168. // {
  169. // Console.WriteLine(channels.Uid); //通道Id
  170. // Console.WriteLine(channels.HasSubAddress); //寄存器是否有子地址
  171. // Console.WriteLine(channels.HasSubIndex); //寄存器是否有DB块地址
  172. // Console.WriteLine(channels.DataType); //数据类型,详情见接口文档中附录二
  173. // Console.WriteLine(channels.MainAddress); //主地址
  174. // Console.WriteLine(channels.RegName); //寄存器名称
  175. // Console.WriteLine(channels.RegId); //寄存器Id
  176. // Console.WriteLine(channels.IoWidth); //寄存器位宽
  177. // Console.WriteLine(channels.StationNo); //站号
  178. // Console.WriteLine(channels.SubAddress); //子地址
  179. // Console.WriteLine(channels.DevAlias); //PLC别名
  180. // Console.WriteLine(channels.SubIndex); //DB块地址
  181. // }
  182. // Console.WriteLine(items.Name); //名称
  183. // Console.WriteLine(items.RecordingPeriod); //采集周期
  184. // Console.WriteLine(items.Uid); //主键
  185. // Console.WriteLine(items.IsControl); //是否使用使能
  186. // }
  187. // }
  188. //获取历史记录数据
  189. //获取历史的数据的sdk里方法的时间是正确时间,如果是调用接口的话需要加上8小时。
  190. // public IList<ByRowHistoryDataRow> GetByRowHistoryData()
  191. // {
  192. // try
  193. // {
  194. // var hdata = _fbox.GetByRowHistoryData(new GetHistoryDataArgs()
  195. // {
  196. // BoxNo = boxNo, //盒子号
  197. // StartTime = DateTime.Parse("2020-12-10 05:00:00"), //new DateTime(2020, 8, 11, 6, 0, 0) //起始时间
  198. // EndTime = DateTime.Parse("2020-12-11 06:00:00"), //结束时间
  199. // Limit = 500, //最大获取数据条数
  200. // TimeRange = TimeRangeTypes.BeginOpenEndOpen, //区间类型,BeginCloseEndClose:全毕区间,BeginCloseEndOpen:左闭右开,BeginOpenEndClose:左开右闭,BeginOpenEndOpen:全开区间
  201. // HdataItemName = "test2", //历史数据条目名称
  202. // HdataChannelNames = new List<string>() { "test2","test3" }//通道名称
  203. // }).Result;
  204. //
  205. // foreach (var d in hdata.Rows)
  206. // {
  207. // Console.WriteLine(d.Time); // 历史数据条目的采集时间
  208. // Console.WriteLine(d.Values[0]); // 对应通道名称集合中的第一个元素
  209. // Console.WriteLine(d.Values[1]); // 对应通道名称集合中的第二个元素
  210. // }
  211. // return hdata.Rows;
  212. // //Console.WriteLine(itemssData.Rows.Count); //不是数组集合,不用循环
  213. // //return itemssData.Rows;
  214. // }
  215. // catch (Exception e)
  216. // {
  217. // Console.WriteLine(e);
  218. // throw e;
  219. // }
  220. // }
  221. }
  222. }