b8adeb7d5fc32ee5b18f860f52eb371555f5d027.svn-base 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. package com.iamberry.app.service;
  2. import java.io.IOException;
  3. import java.text.MessageFormat;
  4. import java.util.Date;
  5. import java.util.HashMap;
  6. import java.util.List;
  7. import java.util.Map;
  8. import java.util.Map.Entry;
  9. import org.apache.http.ParseException;
  10. import org.json.JSONException;
  11. import org.json.JSONObject;
  12. import org.slf4j.Logger;
  13. import org.slf4j.LoggerFactory;
  14. import org.springframework.stereotype.Service;
  15. import org.springframework.transaction.annotation.Transactional;
  16. import com.iamberry.app.config.ImberryConfig;
  17. import com.iamberry.app.core.entity.Machine;
  18. import com.iamberry.app.core.entity.User;
  19. import com.iamberry.app.face.MachineService;
  20. import com.iamberry.app.tool.util.HttpUtility;
  21. /**
  22. * MachineService
  23. *
  24. * @author Moon Cheng
  25. * @date 2016年3月23日 下午12:27:32
  26. */
  27. @Service
  28. public class MachineServiceImpl extends BaseService implements MachineService {
  29. private Logger logger = LoggerFactory.getLogger(UserServiceImpl.class);
  30. /**
  31. * setMachine
  32. * @author Moon Cheng
  33. * @param machine
  34. * @param token
  35. * @return Machine Information
  36. */
  37. @Transactional
  38. public Machine setMachine(Machine machine, String token, String ip) {
  39. User userInfo = validateUserToken(token);
  40. if (userInfo == null) {
  41. return null;
  42. }
  43. if (ip.indexOf(",") != -1) {
  44. ip = ip.split(",")[0];
  45. }
  46. logger.info("IP为:" + ip);
  47. String location = null;
  48. try {
  49. location = new JSONObject(
  50. HttpUtility.httpsGet(MessageFormat.format(ImberryConfig.SINA_IP_URL,ip)))
  51. .getString("city");
  52. } catch (ParseException | IOException | JSONException e) {
  53. e.printStackTrace();
  54. }
  55. machine.setLocation(location);
  56. machine.setOwner(userInfo.getId());
  57. Long machineId = machineMapper.isDevUserPairExist(machine.getMachine_mac(), userInfo.getId());
  58. machine.setCreated_on(new Date());
  59. if (machineId == null) {
  60. machineMapper.insertMachine(machine);
  61. } else {
  62. machine.setId(machineId);
  63. machineMapper.updateMachine(machine);
  64. }
  65. return machine;
  66. }
  67. /**
  68. * updateMachineStatus
  69. *
  70. * @author Moon Cheng
  71. * @param machineId
  72. * @param status
  73. * @param token
  74. * @return
  75. */
  76. public int updateMachineStatus(Long machineId, String status, String token) {
  77. User userInfo = validateUserToken(token);
  78. if (userInfo == null) {
  79. return -1;
  80. }
  81. return machineMapper.updateMachineStatus(status, machineId);
  82. }
  83. /**
  84. * updateMachineLocation
  85. *
  86. * @author Moon Cheng
  87. * @param machineId
  88. * @param location
  89. * @param token
  90. * @return
  91. */
  92. public int updateMachineLocation(Long machineId, String location, String token) {
  93. User userInfo = validateUserToken(token);
  94. if (userInfo == null) {
  95. return -1;
  96. }
  97. return machineMapper.updateMachineLocation(location, machineId);
  98. }
  99. /**
  100. * 按用户,查询所有机器
  101. * @param owner
  102. * @param token
  103. * @return
  104. */
  105. public List<Machine> searchMachineByOwner(Long owner, String token) {
  106. User userInfo = validateUserToken(token);
  107. if (userInfo == null) {
  108. return null;
  109. }
  110. List<Machine> machines = machineMapper.selectMachineByOwner(owner);
  111. return machines;
  112. }
  113. /**
  114. * searchMachineById
  115. *
  116. * @author Moon Cheng
  117. * @param machineId
  118. * @param token
  119. * @return
  120. */
  121. public Machine searchMachineById(Long machineId, String token) {
  122. User userInfo = validateUserToken(token);
  123. if (userInfo == null) {
  124. return null;
  125. }
  126. Machine machine = machineMapper.selectMachineById(machineId);
  127. return machine;
  128. }
  129. /**
  130. * updateStatus
  131. *
  132. * @param status
  133. */
  134. @SuppressWarnings("unchecked")
  135. @Transactional
  136. public void updateStatus(String status) {
  137. // the method is expensive!
  138. Map<String, Object> map = new HashMap<>();
  139. try {
  140. map = mapper.readValue(status,new HashMap<String, Object>().getClass());
  141. } catch (IOException e) {
  142. logger.error(e.getMessage());
  143. e.printStackTrace();
  144. }
  145. for (Entry<String, Object> entry : map.entrySet()) {
  146. if (entry.getValue() instanceof String) {
  147. machineMapper.updateStatusByMac(entry.getKey(), entry.getValue().equals("0") ? "online" : "offline");
  148. } else {
  149. machineMapper.updateStatusByMac(entry.getKey(), (Integer)entry.getValue() == 0 ? "online" : "offline");
  150. }
  151. }
  152. }
  153. @Override
  154. public Long selectUserIDByDevId(String devId) {
  155. // TODO Auto-generated method stub
  156. return machineMapper.selectUserIDByDevId(devId);
  157. }
  158. }