package com.moral.monitor.service; import com.moral.monitor.dao.WeChatDao; import com.moral.monitor.entity.AlarmSensor; import com.moral.monitor.entity.Equipment; import com.moral.monitor.listener.message.WeChat; import net.sf.json.JSONObject; import org.springframework.stereotype.Service; import javax.annotation.Resource; import java.math.BigDecimal; import java.text.SimpleDateFormat; import java.util.*; /** * Created by a on 2017/5/26. */ @Service public class WeChatService { @Resource WeChatDao weChatDao; public Equipment weiXindetail(String mac) { Equipment equipment = weChatDao.selectFromequipment(mac); return equipment; } public Map wxMap(String mac, long time){ Date d = new Date(time); SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String date = formatter.format(d); String receiveMsg = weChatDao.selectFromloggerbymacandtime(mac,date); Map receiveMsgMap = toMap(receiveMsg); String rmac = receiveMsgMap.remove("mac"); String rver = receiveMsgMap.remove("ver"); String rtime = receiveMsgMap.remove("time"); List sensors = weChatDao.findAllsensorBymac(mac); Map sensor_descvalue=new TreeMap(); for (AlarmSensor sensor : sensors) { if (!(receiveMsgMap.containsKey(sensor.getMac_key()))) { continue; } String mac_key = sensor.getMac_key(); double value = Double.parseDouble(receiveMsgMap.get(sensor.getMac_key())); String displayvalue = digit(value, sensor.getDigit()); String units=sensor.getUnits(); sensor_descvalue.put(sensor.getSensor(),displayvalue+units); } return sensor_descvalue; } public String digit(double num, int digit) { BigDecimal y = new BigDecimal(num); double b = y.setScale(digit, BigDecimal.ROUND_HALF_UP).doubleValue(); double c = b - (int) b; if (c == 0) { return String.valueOf((int) b); } else { return String.valueOf(b); } } public Map toMap(Object object) { Map data = new LinkedHashMap(); JSONObject jsonObject = JSONObject.fromObject(object); Iterator ite = jsonObject.keys(); while (ite.hasNext()) { String key = ite.next().toString(); String value = jsonObject.get(key).toString(); data.put(key, value); } return data; } }