package com.moral.monitor.controller; import java.io.IOException; import java.io.InputStreamReader; import java.util.HashMap; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import javax.servlet.http.HttpServletRequest; import org.apache.commons.collections.CollectionUtils; import org.apache.commons.lang.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.core.io.Resource; import org.springframework.web.bind.annotation.CrossOrigin; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; import com.alibaba.fastjson.JSONReader; import com.alibaba.fastjson.TypeReference; import com.moral.monitor.entity.AccountEntity; import com.moral.monitor.service.ScreenService; import com.moral.monitor.util.BusinessException; import com.moral.monitor.util.Crypto; import com.moral.monitor.util.WebUtils; @RestController @RequestMapping(value = "screen") @CrossOrigin(origins = "*", maxAge = 3600) public class ScreenController { /** The screen service. */ @Autowired private ScreenService screenService; @Value(value="classpath:system/alarmLevels.json") private Resource resource; /** * Screen login. * * @param request the request * @return the map */ @RequestMapping(value = "/login", method = RequestMethod.GET) public Map screenLogin(HttpServletRequest request) { Map resultMap = new HashMap(); String msg = ""; Integer accountId = -1; String account = request.getParameter("account"); String password = request.getParameter("password"); if (StringUtils.isBlank(account) || StringUtils.isBlank(password)) { msg = "用户名及密码不允许为空!"; } else { try { password = Crypto.md5(password); List accountLists = screenService.getAccountLists(account, password); if (CollectionUtils.isEmpty(accountLists) || accountLists.size() != 1) { msg = "用户名及密码输入错误!"; } else { AccountEntity accountEntity = accountLists.get(0); if ("1".equals(accountEntity.getEnable())) { msg = "登录成功!"; accountId = accountEntity.getId(); } else { msg = "您的账号已禁用,请联系管理员!"; } } } catch (Exception e) { e.printStackTrace(); msg = "系统正忙,请稍后再试!"; } } resultMap.put("msg", msg); resultMap.put("accountId", accountId); return resultMap; } /** * Gets the month data by equipment. * * @param request the request * @return the month data by equipment */ @RequestMapping(value = "/month", method = RequestMethod.GET) public Map getMonthDataByEquipment(HttpServletRequest request) { Map result = new LinkedHashMap(); try { Map parameters = WebUtils.getParametersStartingWith(request, null); if (!(parameters.containsKey("mac") && parameters.containsKey("macKey"))) { result.put("msg", "参数不能为空!"); } else { result = screenService.getMonthDataByEquipment(parameters); } } catch (Exception e) { e.printStackTrace(); result.put("msg", "系统错误,请联系管理员!原因如下:"+e.getMessage()); } return result; } /** * Gets the average by all. * * @param request the request * @return the average by all */ @RequestMapping(value = "/all-average", method = RequestMethod.GET) public Map getAverageByAll(HttpServletRequest request) { Map result = new LinkedHashMap(); try { Map parameters = WebUtils.getParametersStartingWith(request, null); if (!(parameters.containsKey("areaCode") && parameters.containsKey("accountId"))) { result.put("msg", "参数不能为空!"); } else { result = screenService.getAverageByAll(parameters); } } catch (BusinessException be) { be.printStackTrace(); result.put("msg", be.getMessage()); } catch (Exception e) { e.printStackTrace(); result.put("msg", "系统错误,请联系管理员!原因如下:"+e.getMessage()); } return result; } /** * Gets the equipment states. * * @param request the request * @return the equipment states */ @RequestMapping(value = "/equipment-state", method = RequestMethod.GET) public Map getEquipmentStates(HttpServletRequest request) { Map result = new LinkedHashMap(); try { Map parameters = WebUtils.getParametersStartingWith(request, null); if (!parameters.containsKey("accountId")) { result.put("msg", "参数不能为空!"); } else { result = screenService.getEquipmentStates(parameters); } } catch (BusinessException be) { be.printStackTrace(); result.put("msg", be.getMessage()); } catch (Exception e) { e.printStackTrace(); result.put("msg", "系统错误,请联系管理员!原因如下:"+e.getMessage()); } return result; } @RequestMapping(value = "/sensor-average", method = RequestMethod.GET) public Map getAverageBySensor(HttpServletRequest request) { Map result = new HashMap(); try { Map parameters = WebUtils.getParametersStartingWith(request, null); if (!(parameters.containsKey("areaCode") && parameters.containsKey("accountId") && parameters.containsKey("macKey"))) { result.put("msg", "参数不能为空!"); } else { result = screenService.getAverageBySensor(parameters); } } catch (BusinessException be) { be.printStackTrace(); result.put("msg", be.getMessage()); } catch (Exception e) { e.printStackTrace(); result.put("msg", "系统错误,请联系管理员!原因如下:"+e.getMessage()); } return result; } @RequestMapping(value = "/alarm-levels", method = RequestMethod.GET) public Map getAlarmLevels(HttpServletRequest request){ Map result = new HashMap(); try { InputStreamReader reader = new InputStreamReader(resource.getInputStream()); result = new JSONReader(reader).readObject(new TypeReference>(){}); } catch (IOException e) { e.printStackTrace(); result.put("msg", "系统错误,请联系管理员!原因如下:"+e.getMessage()); } return result; } }