DeviceService.cs 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848
  1. using IoTIntegrationPlatform.Common;
  2. using IoTIntegrationPlatform.Interface;
  3. using IoTIntegrationPlatform.Model.common;
  4. using IoTIntegrationPlatform.Model.Dto;
  5. using IoTIntegrationPlatform.Model.Enum;
  6. using IoTIntegrationPlatform.Model.Model;
  7. using MySqlX.XDevAPI.Relational;
  8. using SqlSugar;
  9. using System;
  10. using System.Collections.Generic;
  11. using System.Linq;
  12. using System.Text;
  13. using System.Threading.Tasks;
  14. using static IoTIntegrationPlatform.Model.Enum.EnumDevice;
  15. namespace IoTIntegrationPlatform.Services
  16. {
  17. /// <summary>
  18. /// 设备服务
  19. /// </summary>
  20. public class DeviceService : IDeviceService
  21. {
  22. private ISqlSugarClient db { get; }
  23. public DeviceService(ISqlSugarClient _db)
  24. {
  25. db = _db;
  26. }
  27. #region 类句柄操作
  28. private IntPtr handle;
  29. private bool disposed = false;
  30. //关闭句柄
  31. [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1060:MovePInvokesToNativeMethodsClass"), System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Interoperability", "CA1401:PInvokesShouldNotBeVisible"), System.Runtime.InteropServices.DllImport("Kernel32")]
  32. public extern static Boolean CloseHandle(IntPtr handle);
  33. /// <summary>
  34. /// 释放对象资源
  35. /// </summary>
  36. public void Dispose()
  37. {
  38. Dispose(true);
  39. GC.SuppressFinalize(this);
  40. }
  41. /// <summary>
  42. /// 释放对象资源
  43. /// </summary>
  44. /// <param name="disposing"></param>
  45. protected virtual void Dispose(bool disposing)
  46. {
  47. if (!this.disposed)
  48. {
  49. if (handle != IntPtr.Zero)
  50. {
  51. CloseHandle(handle);
  52. handle = IntPtr.Zero;
  53. }
  54. }
  55. disposed = true;
  56. }
  57. ~DeviceService()
  58. {
  59. Dispose(false);
  60. }
  61. #endregion
  62. #region 平台型号
  63. /// <summary>
  64. /// 获取平台型号
  65. /// </summary>
  66. /// <param name="platformModelId"></param>
  67. /// <param name="code"></param>
  68. /// <returns></returns>
  69. public PlatformModel GetPlatformModelInfo(int platformModelId, string code)
  70. {
  71. PlatformModel model = new PlatformModel();
  72. try
  73. {
  74. model = db.Queryable<PlatformModel>()
  75. .Where(it => it.IsValid == true)
  76. .WhereIF(platformModelId > 0, it => it.PlatformModelId == platformModelId)
  77. .WhereIF(!string.IsNullOrEmpty(code), it => it.PlatformModelCode.Equals(code))
  78. .First();
  79. }
  80. catch (Exception ex)
  81. {
  82. Logging.Error(ex, ex.Message);
  83. }
  84. return model;
  85. }
  86. /// <summary>
  87. /// 获取平台型号列表
  88. /// </summary>
  89. /// <param name="parm"></param>
  90. /// <param name="page"></param>
  91. /// <returns></returns>
  92. public List<PlatformModel> GetPlatformModelList(BaseParm parm, ref int page)
  93. {
  94. List<PlatformModel> list = new List<PlatformModel>();
  95. try
  96. {
  97. list = db.Queryable<PlatformModel>()
  98. .Where(it => it.IsValid == true)
  99. .WhereIF(!string.IsNullOrEmpty(parm.Code), it => it.PlatformModelCode.Equals(parm.Code))
  100. .ToPageList(parm.PageIndex, parm.PageSize, ref page);
  101. }
  102. catch (Exception ex)
  103. {
  104. Logging.Error(ex, ex.Message);
  105. }
  106. return list;
  107. }
  108. /// <summary>
  109. /// 添加平台型号
  110. /// </summary>
  111. /// <param name="model"></param>
  112. /// <returns></returns>
  113. public bool AddPlatformModel(PlatformModel model)
  114. {
  115. bool result = false;
  116. try
  117. {
  118. model.CreateTime = DateTime.Now;
  119. model.UpdateTime = DateTime.Now;
  120. model.IsValid = true;
  121. int row = db.Insertable(model).ExecuteCommand();
  122. if (row > 0)
  123. {
  124. result = true;
  125. }
  126. }
  127. catch (Exception ex)
  128. {
  129. Logging.Error(ex, "添加平台设备型号");
  130. }
  131. return result;
  132. }
  133. /// <summary>
  134. /// 编辑平台设备型号
  135. /// </summary>
  136. /// <param name="model"></param>
  137. /// <returns></returns>
  138. public bool UpdatedPlatformModel(PlatformModel model)
  139. {
  140. bool result = false;
  141. try
  142. {
  143. var row = db.Updateable(model).IgnoreColumns(it => new { it.PlatformModelId, it.CreateTime, it.IsValid })
  144. .Where(it => it.PlatformModelId == model.PlatformModelId)
  145. .ExecuteCommand();
  146. if (row > 0)
  147. {
  148. result = true;
  149. }
  150. }
  151. catch (Exception ex)
  152. {
  153. Logging.Error(ex, ex.Message);
  154. }
  155. return result;
  156. }
  157. /// <summary>
  158. /// 删除平台设备型号
  159. /// </summary>
  160. /// <param name="id"></param>
  161. /// <returns></returns>
  162. public bool DeletePlatformModel(int id)
  163. {
  164. bool result = false;
  165. try
  166. {
  167. int row = db.Updateable<PlatformModel>()
  168. .SetColumns(it => new PlatformModel { UpdateTime = DateTime.Now, IsValid = false })
  169. .Where(it => it.PlatformModelId == id)
  170. .ExecuteCommand();
  171. if (row > 0)
  172. {
  173. result = true;
  174. }
  175. }
  176. catch (Exception ex)
  177. {
  178. Logging.Error(ex, ex.Message);
  179. }
  180. return result;
  181. }
  182. #endregion
  183. #region 平台型号明细
  184. /// <summary>
  185. /// 获取设备明细信息
  186. /// </summary>
  187. /// <param name="detailId"></param>
  188. /// <param name="deviceName"></param>
  189. /// <param name="platformModelId"></param>
  190. /// <param name="deviceType"></param>
  191. /// <param name="deviceCommunicationType"></param>
  192. /// <returns></returns>
  193. public PlatformModelDetail GetPlatformModelDetailInfo(int detailId, string deviceName, int platformModelId, int deviceType, int deviceCommunicationType)
  194. {
  195. PlatformModelDetail model = new PlatformModelDetail();
  196. try
  197. {
  198. model = db.Queryable<PlatformModelDetail>()
  199. .Where(it => it.IsValid == true)
  200. .WhereIF(detailId > 0, it => it.PlatformModelDetailId == detailId)
  201. .WhereIF(!string.IsNullOrEmpty(deviceName), it => it.DeviceName.Contains(deviceName))
  202. .WhereIF(platformModelId > 0, it => it.PlatformModelId == platformModelId)
  203. .WhereIF(deviceType > 0, it => it.DeviceType == (EnumDeviceType)deviceType)
  204. .WhereIF(deviceType > 0, it => it.DeviceCommunicationType == (EnumDeviceCommunicationType)deviceCommunicationType)
  205. .First();
  206. }
  207. catch (Exception ex)
  208. {
  209. Logging.Error(ex, ex.Message);
  210. }
  211. return model;
  212. }
  213. /// <summary>
  214. /// 获取平台型号明细列表
  215. /// </summary>
  216. /// <param name="parm"></param>
  217. /// <param name="page"></param>
  218. /// <returns></returns>
  219. public List<PlatformModelDetail> GetPlatformModelDetailList(BaseParm parm, ref int page)
  220. {
  221. List<PlatformModelDetail> list = new List<PlatformModelDetail>();
  222. try
  223. {
  224. list = db.Queryable<PlatformModelDetail>()
  225. .Where(it => it.IsValid == true)
  226. .WhereIF(!string.IsNullOrEmpty(parm.Name), it => it.DeviceName.Contains(parm.Name))
  227. .WhereIF(parm.Int1 > 0, it => it.DeviceType == (EnumDeviceType)parm.Int1)
  228. .WhereIF(parm.Int2 > 0, it => it.DeviceCommunicationType == (EnumDeviceCommunicationType)parm.Int2)
  229. .WhereIF(parm.Type > 0, it => it.ModbusRegisterType == (EnumModbusRegisterType)parm.Type)
  230. .OrderBy(it => it.CreateTime)
  231. .ToPageList(parm.PageIndex, parm.PageSize, ref page);
  232. }
  233. catch (Exception ex)
  234. {
  235. Logging.Error(ex, ex.Message);
  236. }
  237. return list;
  238. }
  239. /// <summary>
  240. /// 添加平台型号明细
  241. /// </summary>
  242. /// <param name="model"></param>
  243. /// <returns></returns>
  244. public bool AddPlatformModelDetail(PlatformModelDetail model)
  245. {
  246. bool result = false;
  247. try
  248. {
  249. model.CreateTime = DateTime.Now;
  250. model.UpdateTime = DateTime.Now;
  251. model.IsValid = true;
  252. int row = db.Insertable(model).ExecuteCommand();
  253. if (row > 0)
  254. {
  255. result = true;
  256. }
  257. }
  258. catch (Exception ex)
  259. {
  260. Logging.Error(ex, "添加平台设备型号");
  261. }
  262. return result;
  263. }
  264. /// <summary>
  265. /// 编辑平台设备型号
  266. /// </summary>
  267. /// <param name="model"></param>
  268. /// <returns></returns>
  269. public bool UpdatedPlatformModelDetail(PlatformModelDetail model)
  270. {
  271. bool result = false;
  272. try
  273. {
  274. var row = db.Updateable(model).IgnoreColumns(it => new { it.PlatformModelDetailId, it.CreateTime, it.IsValid })
  275. .Where(it => it.PlatformModelDetailId == model.PlatformModelDetailId)
  276. .ExecuteCommand();
  277. if (row > 0)
  278. {
  279. result = true;
  280. }
  281. }
  282. catch (Exception ex)
  283. {
  284. Logging.Error(ex, ex.Message);
  285. }
  286. return result;
  287. }
  288. /// <summary>
  289. /// 删除平台设备型号
  290. /// </summary>
  291. /// <param name="id"></param>
  292. /// <returns></returns>
  293. public bool DeletePlatformModelDetail(int id)
  294. {
  295. bool result = false;
  296. try
  297. {
  298. int row = db.Updateable<PlatformModelDetail>()
  299. .SetColumns(it => new PlatformModelDetail { UpdateTime = DateTime.Now, IsValid = false })
  300. .Where(it => it.PlatformModelDetailId == id)
  301. .ExecuteCommand();
  302. if (row > 0)
  303. {
  304. result = true;
  305. }
  306. }
  307. catch (Exception ex)
  308. {
  309. Logging.Error(ex, ex.Message);
  310. }
  311. return result;
  312. }
  313. #endregion
  314. #region 平台设备
  315. /// <summary>
  316. /// 获取平台设备信息
  317. /// </summary>
  318. /// <param name="platformId"></param>
  319. /// <param name="code"></param>
  320. /// <param name="customerId"></param>
  321. /// <returns></returns>
  322. public PlatformDeviceInfo GetPlatformDeviceInfoInfo(int platformId, string code, int customerId)
  323. {
  324. PlatformDeviceInfo model = new PlatformDeviceInfo();
  325. try
  326. {
  327. model = db.Queryable<PlatformDeviceInfo>()
  328. .Where(it => it.IsValid == true)
  329. .WhereIF(platformId > 0, it => it.PlatformId == platformId)
  330. .WhereIF(!string.IsNullOrEmpty(code), it => it.PlatformCode.Equals(code))
  331. .WhereIF(customerId > 0, it => it.CustomerId == customerId)
  332. .First();
  333. }
  334. catch (Exception ex)
  335. {
  336. Logging.Error(ex, ex.Message);
  337. }
  338. return model;
  339. }
  340. /// <summary>
  341. /// 获取平台设备列表
  342. /// </summary>
  343. /// <param name="parm"></param>
  344. /// <param name="page"></param>
  345. /// <returns></returns>
  346. public List<PlatformDeviceInfoDto> GetPlatformDeviceList(BaseParm parm, ref int page)
  347. {
  348. List<PlatformDeviceInfoDto> list = new List<PlatformDeviceInfoDto>();
  349. try
  350. {
  351. list = db.Queryable<PlatformDeviceInfo>()
  352. .Where(it => it.IsValid == true)
  353. .WhereIF(!string.IsNullOrEmpty(parm.Param), it => it.PlatformCode.Contains(parm.Param))
  354. .WhereIF(!string.IsNullOrEmpty(parm.Code), it => it.PlatformCode.Equals(parm.Code))//平台编号
  355. .WhereIF(parm.Int1 > 0, it => it.PlatformModelId == parm.Int1)//平台型号id
  356. .WhereIF(parm.CustomerId > 0, it => it.CustomerId == parm.CustomerId)//客户id
  357. .WhereIF(parm.Status > 0, it => it.PlatformStatus == (EnumPlatformStatus)parm.Status)//平台状态
  358. .Select(it => new PlatformDeviceInfoDto()
  359. {
  360. PlatformId = it.PlatformId,
  361. PlatformCode = it.PlatformCode,
  362. PlatformModelId = it.PlatformModelId,
  363. CustomerId = it.CustomerId,
  364. EffectiveTime = it.EffectiveTime,
  365. ExpireTime = it.ExpireTime,
  366. DeviceMac = it.DeviceMac,
  367. HeartbeatTime = it.HeartbeatTime,
  368. DeviceState = it.DeviceState,
  369. PlatformStatus = it.PlatformStatus,
  370. Remark = it.Remark,
  371. UpdateTime = it.UpdateTime,
  372. OperationId = it.OperationId,
  373. OperationName = it.OperationName,
  374. CustomerCode = SqlFunc.Subqueryable<CustomerInfo>().Where(t => t.IsValid && t.CustomerId == it.CustomerId).Select(t => t.CustomerCode),
  375. CustomerName = SqlFunc.Subqueryable<CustomerInfo>().Where(t => t.IsValid && t.CustomerId == it.CustomerId).Select(t => t.CustomerName),
  376. PlatformModelCode = SqlFunc.Subqueryable<PlatformModel>().Where(t => t.IsValid && t.PlatformModelId == it.PlatformModelId).Select(t => t.PlatformModelCode),
  377. PlatformModelName = SqlFunc.Subqueryable<PlatformModel>().Where(t => t.IsValid && t.PlatformModelId == it.PlatformModelId).Select(t => t.PlatformModelName)
  378. })
  379. .ToPageList(parm.PageIndex, parm.PageSize, ref page);
  380. }
  381. catch (Exception ex)
  382. {
  383. Logging.Error(ex, ex.Message);
  384. }
  385. return list;
  386. }
  387. /// <summary>
  388. /// 添加平台设备
  389. /// </summary>
  390. /// <param name="model"></param>
  391. /// <returns></returns>
  392. public bool AddPlatformDeviceInfo(PlatformDeviceInfo model)
  393. {
  394. bool result = false;
  395. db.Ado.BeginTran();
  396. try
  397. {
  398. //序列号
  399. string serialNumber = RandomHelper.GenerateRandomStr(16);
  400. model.CreateTime = DateTime.Now;
  401. model.UpdateTime = DateTime.Now;
  402. model.DeviceMac = DeviceHelper.GenerateDeviceMacAddress(serialNumber);
  403. model.HeartbeatTime = DateTime.Now;
  404. model.DeviceState = EnumDeviceStatus.Close;
  405. model.IsValid = true;
  406. model.PlatformStatus = EnumPlatformStatus.NotActivated;
  407. model.PlatformId = db.Insertable(model).ExecuteReturnIdentity();
  408. //根据物联网平台型号id获取平台型号明细设备
  409. //物联网平台型号id
  410. int platformModelId = model.PlatformModelId;
  411. //获取当前平台型号下的明细设备
  412. List<PlatformModelDetail> platformModelDeviceList = db.Queryable<PlatformModelDetail>().Where(it => it.IsValid && it.PlatformModelId == platformModelId).OrderBy(it => it.PlatformModelDetailId).ToList();
  413. //平台子设备
  414. List<PlatformDeviceDetail> subDevicelist = new List<PlatformDeviceDetail>();
  415. foreach (var item in platformModelDeviceList)
  416. {
  417. PlatformDeviceDetail subDevicdModel = new PlatformDeviceDetail();
  418. subDevicdModel = Mapping.EntityMapping<PlatformDeviceDetail, PlatformModelDetail>(item);
  419. subDevicdModel.DeviceId = Guid.NewGuid().ToString();
  420. subDevicdModel.PlatformId = model.PlatformId;
  421. subDevicdModel.DeviceName = item.DeviceName;
  422. subDevicdModel.DeviceType = item.DeviceType;
  423. subDevicdModel.DeviceTypeName = EnumExtension.GetDescription(subDevicdModel.DeviceType);
  424. subDevicdModel.DeviceState = EnumDeviceStatus.Close;
  425. subDevicdModel.DeviceValue = item.DeviceValue;
  426. subDevicdModel.CreateTime = DateTime.Now;
  427. subDevicdModel.UpdateTime = DateTime.Now;
  428. subDevicdModel.IsValid = true;
  429. subDevicelist.Add(subDevicdModel);
  430. }
  431. int row = db.Insertable(subDevicelist).ExecuteCommand();
  432. if (row > 0)
  433. {
  434. db.Ado.CommitTran();
  435. result = true;
  436. }
  437. }
  438. catch (Exception ex)
  439. {
  440. Logging.Error(ex, ex.Message);
  441. db.Ado.RollbackTran();
  442. }
  443. return result;
  444. }
  445. /// <summary>
  446. /// 编辑平台设备
  447. /// </summary>
  448. /// <param name="model"></param>
  449. /// <returns></returns>
  450. public bool UpdatedPlatformDeviceInfo(PlatformDeviceInfo model)
  451. {
  452. bool result = false;
  453. try
  454. {
  455. model.UpdateTime = DateTime.Now;
  456. var row = db.Updateable(model).IgnoreColumns(it => new { it.PlatformId, it.PlatformCode, it.UpdateTime, it.IsValid })
  457. .Where(it => it.PlatformId == model.PlatformId)
  458. .ExecuteCommand();
  459. if (row > 0)
  460. {
  461. result = true;
  462. }
  463. }
  464. catch (Exception ex)
  465. {
  466. Logging.Error(ex, ex.Message);
  467. }
  468. return result;
  469. }
  470. /// <summary>
  471. /// 批量编辑平台设备状态
  472. /// </summary>
  473. /// <param name="dataList"></param>
  474. /// <returns></returns>
  475. public bool BatchUpdatePlatformDeviceStatus(List<PlatformDeviceInfoDto> dataList)
  476. {
  477. bool result = false;
  478. db.Ado.BeginTran();
  479. try
  480. {
  481. foreach (var item in dataList)
  482. {
  483. db.Updateable<PlatformDeviceInfo>()
  484. .SetColumns(it => new PlatformDeviceInfo() { UpdateTime = DateTime.Now, DeviceState = item.DeviceState })
  485. .Where(it => it.IsValid && it.PlatformId == item.PlatformId)
  486. .ExecuteCommand();
  487. }
  488. db.Ado.CommitTran();
  489. }
  490. catch (Exception ex)
  491. {
  492. db.Ado.RollbackTran();
  493. Logging.Error(ex, "BatchUpdateDeviceStatus");
  494. }
  495. return result;
  496. }
  497. /// <summary>
  498. /// 编辑平台状态
  499. /// </summary>
  500. /// <param name="model"></param>
  501. /// <returns></returns>
  502. public bool UpdatedPlatformState(PlatformDeviceInfo model)
  503. {
  504. bool result = false;
  505. try
  506. {
  507. model.UpdateTime = DateTime.Now;
  508. var row = 0;
  509. if (model.PlatformStatus == EnumPlatformStatus.NotActivated)
  510. {
  511. model.ExpireTime = DateTime.Now;
  512. row = db.Updateable(model).UpdateColumns(it => new { it.PlatformStatus, it.UpdateTime }).Where(it => it.PlatformId == model.PlatformId).ExecuteCommand();
  513. }
  514. else if (model.PlatformStatus == EnumPlatformStatus.Normal)
  515. {
  516. model.EffectiveTime = DateTime.Now;
  517. row = db.Updateable(model).UpdateColumns(it => new { it.PlatformStatus, it.UpdateTime, it.EffectiveTime, it.ExpireTime }).Where(it => it.PlatformId == model.PlatformId).ExecuteCommand();
  518. }
  519. else if (model.PlatformStatus == EnumPlatformStatus.HaveExpired)
  520. {
  521. model.ExpireTime = DateTime.Now;
  522. row = db.Updateable(model).UpdateColumns(it => new { it.PlatformStatus, it.UpdateTime, it.ExpireTime }).Where(it => it.PlatformId == model.PlatformId).ExecuteCommand();
  523. }
  524. if (row > 0)
  525. {
  526. result = true;
  527. }
  528. }
  529. catch (Exception ex)
  530. {
  531. Logging.Error(ex, ex.Message);
  532. }
  533. return result;
  534. }
  535. /// <summary>
  536. /// 删除平台设备
  537. /// </summary>
  538. /// <param name="platformId"></param>
  539. /// <returns></returns>
  540. public bool DeletePlatformDeviceInfo(int platformId)
  541. {
  542. bool result = false;
  543. try
  544. {
  545. int row = db.Updateable<PlatformDeviceInfo>()
  546. .SetColumns(it => new PlatformDeviceInfo { UpdateTime = DateTime.Now, IsValid = false })
  547. .Where(it => it.PlatformId == platformId)
  548. .ExecuteCommand();
  549. if (row > 0)
  550. {
  551. result = true;
  552. }
  553. }
  554. catch (Exception ex)
  555. {
  556. Logging.Error(ex, ex.Message);
  557. }
  558. return result;
  559. }
  560. /// <summary>
  561. /// 根据mac地址修改平台设备心跳时间
  562. /// </summary>
  563. /// <param name="deviceMac"></param>
  564. /// <returns></returns>
  565. public bool UpdateGatewayDeviceHeartTime(string deviceMac)
  566. {
  567. bool result = false;
  568. try
  569. {
  570. int row = db.Updateable<PlatformDeviceInfo>()
  571. .SetColumns(it => new PlatformDeviceInfo { UpdateTime = DateTime.Now, HeartbeatTime = DateTime.Now, DeviceState = EnumDeviceStatus.Open })
  572. .Where(it => it.IsValid && it.DeviceMac == deviceMac)
  573. .ExecuteCommand();
  574. if (row > 0)
  575. {
  576. result = true;
  577. }
  578. }
  579. catch (Exception ex)
  580. {
  581. Logging.Error(ex, ex.Message);
  582. }
  583. return result;
  584. }
  585. #endregion
  586. #region 平台设备明细
  587. /// <summary>
  588. /// 获取平台设备明细列表
  589. /// </summary>
  590. /// <param name="parm"></param>
  591. /// <param name="page"></param>
  592. /// <returns></returns>
  593. public List<PlatformDeviceDetailDto> GetPlatformDeviceDetailList(BaseParm parm, ref int page)
  594. {
  595. List<PlatformDeviceDetailDto> list = new List<PlatformDeviceDetailDto>();
  596. try
  597. {
  598. list = db.Queryable<PlatformDeviceDetail>()
  599. .Where(it => it.IsValid)
  600. .WhereIF(parm.Id > 0, it => it.PlatformId == parm.Id)//平台id
  601. .WhereIF(!string.IsNullOrEmpty(parm.Name), it => it.DeviceName.Contains(parm.Name))//设备名称
  602. .WhereIF(parm.Int1 > 0, it => it.DeviceType == (EnumDeviceType)parm.Int1)//设备类型
  603. .WhereIF(parm.Int2 > 0, it => it.DeviceCommunicationType == (EnumDeviceCommunicationType)parm.Int2)//设备通讯方式
  604. .OrderBy(it => it.PlatformModelDetailId)
  605. .Select(it => new PlatformDeviceDetailDto()
  606. {
  607. DeviceCommunicationTypeName = "Rs485",
  608. ModbusRegisterTypeName = "保持寄存器"
  609. },
  610. true)//开启自动映射
  611. .ToPageList(parm.PageIndex, parm.PageSize, ref page);
  612. }
  613. catch (Exception ex)
  614. {
  615. Logging.Error(ex, ex.Message);
  616. }
  617. return list;
  618. }
  619. /// <summary>
  620. /// 编辑平台设备
  621. /// </summary>
  622. /// <param name="model"></param>
  623. /// <returns></returns>
  624. public bool UpdatedPlatformSubDeviceInfo(PlatformDeviceDetail model)
  625. {
  626. bool result = false;
  627. try
  628. {
  629. var row = db.Updateable(model).IgnoreColumns(it => new { it.DeviceId, it.PlatformId, it.CreateTime, it.IsValid })
  630. .Where(it => it.DeviceId == model.DeviceId)
  631. .ExecuteCommand();
  632. if (row > 0)
  633. {
  634. result = true;
  635. }
  636. }
  637. catch (Exception ex)
  638. {
  639. Logging.Error(ex, ex.Message);
  640. }
  641. return result;
  642. }
  643. /// <summary>
  644. /// 编辑平台明细设备值
  645. /// </summary>
  646. /// <param name="model"></param>
  647. /// <returns></returns>
  648. public bool UpdatedPlatformSubDeviceValue(PlatformDeviceDetail model)
  649. {
  650. bool result = false;
  651. try
  652. {
  653. model.UpdateTime = DateTime.Now;
  654. var row = db.Updateable(model).UpdateColumns(it => new { it.DeviceValue, it.UpdateTime }).Where(it => it.DeviceId == model.DeviceId).ExecuteCommand();
  655. if (row > 0)
  656. {
  657. result = true;
  658. }
  659. }
  660. catch (Exception ex)
  661. {
  662. Logging.Error(ex, ex.Message);
  663. }
  664. return result;
  665. }
  666. /// <summary>
  667. /// 删除平台子设备
  668. /// </summary>
  669. /// <param name="deviceId"></param>
  670. /// <returns></returns>
  671. public bool DeletePlatformSubDeviceInfo(string deviceId)
  672. {
  673. bool result = false;
  674. try
  675. {
  676. int row = db.Updateable<PlatformDeviceDetail>()
  677. .SetColumns(it => new PlatformDeviceDetail { UpdateTime = DateTime.Now, IsValid = false })
  678. .Where(it => it.DeviceId == deviceId)
  679. .ExecuteCommand();
  680. if (row > 0)
  681. {
  682. result = true;
  683. }
  684. }
  685. catch (Exception ex)
  686. {
  687. Logging.Error(ex, ex.Message);
  688. }
  689. return result;
  690. }
  691. /// <summary>
  692. /// 编辑设备状态或值(根据设备ID)
  693. /// </summary>
  694. /// <param name="deviceId"></param>
  695. /// <param name="deviceStatus"></param>
  696. /// <param name="deviceValue"></param>
  697. /// <returns></returns>
  698. public bool UpdateDeviceStateOrValue(string deviceId, EnumDeviceStatus deviceStatus, string deviceValue)
  699. {
  700. bool result = false;
  701. try
  702. {
  703. //int row = db.Updateable<PlatformDeviceDetail>()
  704. // .SetColumns(it => new PlatformDeviceDetail { UpdateTime = DateTime.Now, DeviceState = deviceStatus, DeviceValue = deviceValue })
  705. // .Where(it => it.DeviceId == deviceId)
  706. // .ExecuteCommand();
  707. if (1 > 0)
  708. {
  709. result = true;
  710. }
  711. }
  712. catch (Exception ex)
  713. {
  714. Logging.Error(ex, ex.Message);
  715. }
  716. return result;
  717. }
  718. /// <summary>
  719. /// 获取设备信息
  720. /// </summary>
  721. /// <param name="device_id"></param>
  722. /// <param name="deviceMac"></param>
  723. /// <param name="type"></param>
  724. /// <returns></returns>
  725. public PlatformDeviceDetail GetDevideInfo(string device_id)
  726. {
  727. PlatformDeviceDetail model = new PlatformDeviceDetail();
  728. try
  729. {
  730. model = db.Queryable<PlatformDeviceDetail>()
  731. .Where(it => it.IsValid)
  732. .WhereIF(!string.IsNullOrEmpty(device_id), it => it.DeviceId.Equals(device_id))
  733. .First();
  734. }
  735. catch (Exception ex)
  736. {
  737. Logging.Error(ex, ex.Message);
  738. }
  739. return model;
  740. }
  741. /// <summary>
  742. /// 编辑设备读取数据状态
  743. /// </summary>
  744. /// <param name="model"></param>
  745. /// <returns></returns>
  746. public bool UpdateDeviceReadState(PlatformDeviceDetail model)
  747. {
  748. bool result = false;
  749. try
  750. {
  751. model.UpdateTime = DateTime.Now;
  752. var row = db.Updateable(model).UpdateColumns(it => new { it.UpdateTime, it.IsRead }).Where(it => it.DeviceId == model.DeviceId).ExecuteCommand();
  753. if (row > 0)
  754. {
  755. result = true;
  756. }
  757. }
  758. catch (Exception ex)
  759. {
  760. Logging.Error(ex, ex.Message);
  761. }
  762. return result;
  763. }
  764. #endregion
  765. }
  766. }