1
0

DeviceControl.java 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. package com.emato.ich.device;
  2. import android.util.Log;
  3. import com.cherry.sdk.controller.InitDevice;
  4. import com.cherry.sdk.controller.MoternDeviceController;
  5. import com.cherry.sdk.controller.callback.CmdCallback;
  6. import com.emato.ich.entity.Cabinet;
  7. import com.emato.ich.utils.JacksonUtils;
  8. import com.emato.ich.utils.LoggingUtils;
  9. import com.fasterxml.jackson.core.JsonProcessingException;
  10. import com.fasterxml.jackson.databind.JavaType;
  11. import com.fasterxml.jackson.databind.ObjectMapper;
  12. import java.util.ArrayList;
  13. import java.util.Arrays;
  14. import java.util.Date;
  15. import java.util.List;
  16. import java.util.concurrent.CountDownLatch;
  17. import java.util.concurrent.atomic.AtomicBoolean;
  18. import java.util.stream.Collectors;
  19. /**
  20. * 0 : 操作成功
  21. * 1 : 操作失败
  22. * 1701:串口通讯超时
  23. * 1702:串口占用
  24. * 1703:串口发送数据错误
  25. * 1704:串口数据校验失败
  26. * 1705:开门时返回锁状态错误
  27. * 1801:不支持该操作
  28. * 单查柜门返回: unlocked(柜门关闭)locked(柜门打开)
  29. * 全查柜门返回 {“section”:1,”port”: 1, “unlocked”: true /false (true柜门打开,false柜门关闭)}
  30. * 其他指令无message返回
  31. */
  32. public class DeviceControl {
  33. public static final Object lock = new Object();
  34. private static final String TAG = "DeviceControl";
  35. private static final MoternDeviceController initedInstance;
  36. static {
  37. int boardType = 7;
  38. initedInstance = MoternDeviceController.getInstance();
  39. initedInstance.init(new InitDevice(boardType));
  40. }
  41. /**
  42. * 函数描述
  43. * 函数用途:查询锁版版本
  44. */
  45. public static void queryVersion(int section, CmdCallback cmdCallback) {
  46. Log.d(TAG, new Date() + "查询锁版本开始");
  47. DeviceResult result = new DeviceResult();
  48. initedInstance.queryVersion(section, cmdCallback);
  49. Log.d(TAG, new Date() + "查询锁版本结束");
  50. }
  51. /**
  52. * 函数用途:控制面板灯(仅3号类型设备支持)
  53. * 参数 :model, CmdCallback(model灯控指令如下)
  54. * 0; //开补光灯
  55. * 1; //关所有灯
  56. * 2; //打开白灯
  57. * 3; //关闭白灯
  58. * 4; //打开红外补光灯
  59. * 5; //关闭红外补光灯
  60. * 6; //打开红灯
  61. * 7; //打开绿灯
  62. */
  63. @Deprecated
  64. public static boolean setFaceLight(int mode, CmdCallback cmdCallback) {
  65. initedInstance.setFaceLight(mode, cmdCallback);
  66. return false;
  67. }
  68. /**
  69. * 函数用途:打开柜门
  70. * 参数 :section, port, CmdCallback
  71. */
  72. public static void unlockLocker(int section, int port, CmdCallback cmdCallback) {
  73. Log.i(TAG, new Date() + "unlockLocker: 打开柜门开始");
  74. initedInstance.unlockLocker(section, port, cmdCallback);
  75. Log.i(TAG, new Date() + "unlockLocker: 打开柜门结束");
  76. }
  77. /**
  78. * 函数用途:查询柜门状态
  79. * 参数 :section, port, CmdCallback
  80. */
  81. @Deprecated
  82. public static void queryLocker(int section, int port, CmdCallback cmdCallback) {
  83. initedInstance.queryLocker(section, port, cmdCallback);
  84. }
  85. // public static Cabinet aa(CountDownLatch countDownLatch, int startSection){
  86. // AtomicReference<AtomicInteger> section = new AtomicReference<>(new AtomicInteger(startSection));
  87. // AtomicBoolean ext = new AtomicBoolean(true);
  88. // Cabinet cabinet = new Cabinet();
  89. // List<Cabinet.CabinetPlat> lockPlateInfos = new ArrayList<>();
  90. //
  91. // while(ext.get()) {
  92. // querySection(section.get().get(), (code, data) -> {
  93. // if (code != 0) {
  94. // section.set(new AtomicInteger(section.get().incrementAndGet()));
  95. // countDownLatch.countDown();
  96. // ext.set(false);
  97. // return;
  98. // }
  99. // Cabinet.CabinetPlat lockPlateInfo = new Cabinet.CabinetPlat();
  100. // lockPlateInfo.setSection(section.get().get());
  101. //
  102. // AtomicReference<AtomicInteger> port = new AtomicReference<>(new AtomicInteger(1));
  103. // List<Cabinet.CabinetLocker> lockInfos = new ArrayList<>();
  104. //
  105. // AtomicBoolean inner = new AtomicBoolean(true);
  106. // CountDownLatch downLatch = new CountDownLatch(32);
  107. // while(inner.get()){
  108. // queryLocker(section.get().get(), port.get().get(), (code2, data2) -> {
  109. // if (code2 != 0) {
  110. // port.set(new AtomicInteger(1));
  111. // downLatch.countDown();
  112. // inner.set(false);
  113. // return;
  114. // }
  115. // Cabinet.CabinetLocker lockInfo = new Cabinet.CabinetLocker();
  116. // lockInfo.setPort(port.get().get());
  117. // lockInfo.setLocked(Arrays.asList(data2).contains("unlocked")+"");
  118. // lockInfos.add(lockInfo);
  119. //
  120. // lockPlateInfo.setCabinetLockers(lockInfos);
  121. // port.set(new AtomicInteger(port.get().incrementAndGet()));
  122. // downLatch.countDown();
  123. // });
  124. // }
  125. //
  126. // try {
  127. // downLatch.await();
  128. // } catch (InterruptedException e) {
  129. // Log.e(TAG, new Date() + "aa: 线程中断错误!", e);
  130. // }
  131. //
  132. // countDownLatch.countDown();
  133. // section.set(new AtomicInteger(section.get().incrementAndGet()));
  134. // lockPlateInfos.add(lockPlateInfo);
  135. // });
  136. // }
  137. // cabinet.setCabinetPlats(lockPlateInfos);
  138. // return cabinet;
  139. // }
  140. // public void sss(){
  141. // Cabinet cabinet = new Cabinet();
  142. // List<Cabinet.CabinetPlat> lockPlateInfos = new ArrayList<>();
  143. //
  144. // for (int i = 1; i <= 10; i++) {
  145. //
  146. // AtomicBoolean atomicBoolean = new AtomicBoolean(true);
  147. // if (!atomicBoolean.get()) {
  148. // break;
  149. // }
  150. // int finalI = i;
  151. // querySection(i, (code, data) -> {
  152. // if (code == 0) {
  153. //
  154. // Cabinet.CabinetPlat lockPlateInfo = new Cabinet.CabinetPlat();
  155. // lockPlateInfo.setSection(finalI);
  156. //
  157. // List<Cabinet.CabinetLocker> lockInfos = new ArrayList<>();
  158. // for (int j = 1; j <= 50; j++) {
  159. //
  160. // int finalJ = j;
  161. // queryLocker(finalI, j, (code2, data2) -> {
  162. //
  163. // if (code2 == 0) {
  164. // Cabinet.CabinetLocker lockInfo = new Cabinet.CabinetLocker();
  165. // lockInfo.setPort(finalJ);
  166. // lockInfo.setUnlocked(Arrays.asList(data2).contains("unlocked"));
  167. // lockInfos.add(lockInfo);
  168. //
  169. // lockPlateInfo.setCabinetLockers(lockInfos);
  170. // }
  171. //
  172. // });
  173. //
  174. // }
  175. // }
  176. // });
  177. // }
  178. //
  179. // cabinet.setCabinetPlats(lockPlateInfos);
  180. // }
  181. /**
  182. * 查询柜子信息
  183. * @param countDownLatch 计数器
  184. * @return 整个柜子信息
  185. */
  186. public static Cabinet queryCabinetInfo(CountDownLatch countDownLatch) {
  187. Cabinet cabinet = new Cabinet();
  188. List<Cabinet.CabinetPlat> lockPlateInfos = new ArrayList<>();
  189. for (int i = 1; i <= 10; i++) {
  190. queryLockListInfo(i, lockPlateInfos, countDownLatch);
  191. }
  192. cabinet.setCabinetPlats(lockPlateInfos);
  193. return cabinet;
  194. }
  195. /**
  196. * 查询柜子信息
  197. * @param finalI 锁板编号
  198. * @param lockPlateInfos 锁板信息集合
  199. * @param countDownLatch 计数器
  200. */
  201. private static void queryLockListInfo(int finalI, List<Cabinet.CabinetPlat> lockPlateInfos, CountDownLatch countDownLatch){
  202. querySection(finalI, (code, data) -> {
  203. if (code == 0) {
  204. String s = Arrays.asList(data).get(0);
  205. ObjectMapper objectMapper = JacksonUtils.objectmapper;
  206. JavaType javaType = objectMapper.getTypeFactory().constructParametricType(List.class, Cabinet.CabinetLocker.class);
  207. try {
  208. List<Cabinet.CabinetLocker> lockInfos = objectMapper.readValue(s, javaType);
  209. lockInfos = lockInfos.stream().filter(lockInfo -> lockInfo.getLocked().equals("false")).collect(Collectors.toList());
  210. // if (finalI <= 3) {
  211. // lockInfos = lockInfos.stream().filter(lockInfo -> lockInfo.getLocked().equals("false")).collect(Collectors.toList()).subList(0, 12);
  212. // } else if (finalI > 3) {
  213. // lockInfos = lockInfos.stream().filter(lockInfo -> lockInfo.getLocked().equals("false")).collect(Collectors.toList()).subList(0, 8);
  214. // }
  215. Cabinet.CabinetPlat lockPlateInfo = new Cabinet.CabinetPlat();
  216. lockPlateInfo.setCabinetLockers(lockInfos);
  217. lockPlateInfo.setSection(finalI);
  218. // TODO 查询锁版本
  219. // lockPlateInfo.setType("类型");
  220. lockPlateInfos.add(lockPlateInfo);
  221. } catch (JsonProcessingException e) {
  222. Log.e(TAG, new Date() + "queryCabinetInfo: JSON转换对象失败!", e);
  223. LoggingUtils.sendErrorLog("业务异常: 查询柜子信息时JSON转换对象失败! ", e);
  224. }
  225. }
  226. countDownLatch.countDown();
  227. });
  228. }
  229. /**
  230. * 函数用途:全开柜门
  231. * 参数 :section, CmdCallback
  232. */
  233. public static boolean unlockSection(int section, CmdCallback cmdCallback) {
  234. AtomicBoolean result = new AtomicBoolean(false);
  235. initedInstance.unlockSection(section, cmdCallback);
  236. return result.get();
  237. }
  238. /**
  239. * 函数用途:查询柜门状态
  240. * 参数 :section, CmdCallback
  241. */
  242. public static void querySection(int section, CmdCallback cmdCallback) {
  243. initedInstance.querySection(section, cmdCallback);
  244. }
  245. /**
  246. * 函数用途:控制柜门加热
  247. * 参数 :section, port, opt, CmdCallback
  248. * opt: 1 打开加热
  249. * opt: 2 关闭加热
  250. */
  251. @Deprecated
  252. public static boolean hotLocker() {
  253. initedInstance.hotLocker(1, 1, 1, new CmdCallback() {
  254. @Override
  255. public void onMessage(int i, String... strings) {
  256. }
  257. });
  258. return false;
  259. }
  260. /**
  261. * 函数用途:全控柜门加热
  262. * 参数 :section, opt, CmdCallback
  263. * opt: 1 打开加热
  264. * opt: 2 关闭加热
  265. */
  266. @Deprecated
  267. public static boolean hotSection() {
  268. initedInstance.hotSection(2, 1, new CmdCallback() {
  269. @Override
  270. public void onMessage(int i, String... strings) {
  271. Log.d(TAG, new Date() + "onMessage: " + i);
  272. Log.d(TAG, new Date() + "onMessage: " + Arrays.asList(strings));
  273. }
  274. });
  275. return false;
  276. }
  277. /**
  278. * 函数用途:控制柜门消毒 S905不支持
  279. * 参数 :section, port, opt, CmdCallback
  280. * opt: 1 打开消毒
  281. * opt: 2 关闭消毒
  282. */
  283. @Deprecated
  284. public static boolean degasLocker() {
  285. initedInstance.degasLocker(2, 1, 1, new CmdCallback() {
  286. @Override
  287. public void onMessage(int i, String... strings) {
  288. Log.d(TAG, "onMessage: " + i);
  289. }
  290. });
  291. return false;
  292. }
  293. /**
  294. * 函数用途:全控柜门消毒 S905不支持
  295. * 参数 :section, opt, CmdCallback
  296. * opt: 1 打开消毒
  297. * opt: 2 关闭消毒
  298. */
  299. @Deprecated
  300. public static boolean degasSection() {
  301. // initedInstance.degasSection(1, new CmdCallback() {
  302. // @Override
  303. // public void onMessage(int i, String... strings) {
  304. //
  305. // }
  306. // });
  307. return false;
  308. }
  309. /**
  310. * 函数用途:继电器控制 S905不支持
  311. * 参数 :section, port, opt, CmdCallback
  312. * opt: 1 开启继电器
  313. * opt: 0 关闭继电器
  314. */
  315. @Deprecated
  316. public static String relayOpt(int section, int port, int opt){
  317. initedInstance.relayOpt(section, port, opt, (i, s) -> {
  318. Log.d(TAG, "relayOpt: " + i);
  319. Log.d(TAG, "relayOpt: " + Arrays.asList(s));
  320. });
  321. return "";
  322. }
  323. private static class DefaultCmdCallbackHandler implements CmdCallback {
  324. private DeviceResult result;
  325. public DefaultCmdCallbackHandler() {
  326. this.result = new DeviceResult();
  327. }
  328. public DeviceResult getResult() {
  329. return result;
  330. }
  331. @Override
  332. public void onMessage(int i, String... strings) {
  333. result.setCode(i);
  334. result.setData(Arrays.asList(strings));
  335. }
  336. }
  337. }