| | |
| | | |
| | | |
| | | public class Server { |
| | | static String host="tcp://39.99.43.227:1883"; |
| | | static String clientId="1231@qq.com"; |
| | | static String host="tcp://39.99.43.227:1883"; |
| | | static String clientId="1231@qq.com"; |
| | | |
| | | /** |
| | | * 获取当前系统时间 |
| | | * @return 格式化的时间字符串,格式为 "yyyy-MM-dd HH:mm:ss" |
| | | */ |
| | | public static String getCurrentSystemTime() { |
| | | LocalDateTime now = LocalDateTime.now(); |
| | | DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); |
| | | return now.format(formatter); |
| | | } |
| | | /** |
| | | * 获取当前系统时间 |
| | | * @return 格式化的时间字符串,格式为 "yyyy-MM-dd HH:mm:ss" |
| | | */ |
| | | public static String getCurrentSystemTime() { |
| | | LocalDateTime now = LocalDateTime.now(); |
| | | DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); |
| | | return now.format(formatter); |
| | | } |
| | | |
| | | /** |
| | | * 获取当前系统时间(自定义格式) |
| | | * @param pattern 时间格式模式,例如 "yyyy-MM-dd HH:mm:ss"、"yyyy/MM/dd HH:mm:ss" 等 |
| | | * @return 格式化的时间字符串 |
| | | */ |
| | | public static String getCurrentSystemTime(String pattern) { |
| | | LocalDateTime now = LocalDateTime.now(); |
| | | DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern); |
| | | return now.format(formatter); |
| | | } |
| | | /** |
| | | * 获取当前系统时间(自定义格式) |
| | | * @param pattern 时间格式模式,例如 "yyyy-MM-dd HH:mm:ss"、"yyyy/MM/dd HH:mm:ss" 等 |
| | | * @return 格式化的时间字符串 |
| | | */ |
| | | public static String getCurrentSystemTime(String pattern) { |
| | | LocalDateTime now = LocalDateTime.now(); |
| | | DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern); |
| | | return now.format(formatter); |
| | | } |
| | | |
| | | |
| | | |
| | | private static void testCode() { |
| | | String mes_id="hxzkcontrol_20151104";//消息ID |
| | | String userId=Usrdell.getUserEmail();//用户ID |
| | | String deviceID=Setsys.getMowerIdValue();//设备ID |
| | | String protiesFilePath="./dikuai.properties";//地块信息配置文件 |
| | | String json = PropertiesFileUploader.movePathSend(mes_id,userId, deviceID, protiesFilePath);//生成给设备下发的JSON |
| | | sendMQTT(json, host, clientId,deviceID); |
| | | } |
| | | /**发送割草路径*/ |
| | | public static void sendMowingPath() { |
| | | String mes_id="hxzkcontrol_20151104";//消息ID |
| | | String userId=Usrdell.getUserEmail();//用户ID |
| | | String deviceID=Setsys.getMowerIdValue();//设备ID |
| | | String protiesFilePath="./dikuai.properties";//地块信息配置文件 |
| | | String json = PropertiesFileUploader.movePathSend(mes_id,userId, deviceID, protiesFilePath);//生成给设备下发的JSON |
| | | sendMQTT(json, host, clientId,deviceID); |
| | | } |
| | | |
| | | private static void sendMQTT(String messageJson, String host, String clientId,String deviceID) { |
| | | try { |
| | | String topic="app/p/mower/"+deviceID+"/path"; |
| | | MqttConnectOptions options = new MqttConnectOptions(); |
| | | options.setCleanSession(true); |
| | | MqttClient client = new MqttClient(host, clientId); |
| | | client.connect(options); |
| | | // 添加关闭钩子,程序退出时关闭连接 |
| | | Runtime.getRuntime().addShutdownHook(new Thread(() -> { |
| | | try { |
| | | if (client.isConnected()) { |
| | | client.disconnect(); |
| | | client.close(); |
| | | } |
| | | } catch (Exception e) { |
| | | } |
| | | })); |
| | | MqttMessage message = new MqttMessage(); |
| | | message.setPayload(messageJson.getBytes("UTF-8")); |
| | | client.publish(topic, message); |
| | | } catch (MqttException e) { |
| | | throw new RuntimeException(e); |
| | | } catch (UnsupportedEncodingException e) { |
| | | throw new RuntimeException(e); |
| | | } |
| | | } |
| | | private static void sendMQTT(String messageJson, String host, String clientId,String deviceID) { |
| | | try { |
| | | String topic="app/p/mower/"+deviceID+"/path"; |
| | | MqttConnectOptions options = new MqttConnectOptions(); |
| | | options.setCleanSession(true); |
| | | MqttClient client = new MqttClient(host, clientId); |
| | | client.connect(options); |
| | | // 添加关闭钩子,程序退出时关闭连接 |
| | | Runtime.getRuntime().addShutdownHook(new Thread(() -> { |
| | | try { |
| | | if (client.isConnected()) { |
| | | client.disconnect(); |
| | | client.close(); |
| | | } |
| | | } catch (Exception e) { |
| | | } |
| | | })); |
| | | MqttMessage message = new MqttMessage(); |
| | | message.setPayload(messageJson.getBytes("UTF-8")); |
| | | client.publish(topic, message); |
| | | } catch (MqttException e) { |
| | | throw new RuntimeException(e); |
| | | } catch (UnsupportedEncodingException e) { |
| | | throw new RuntimeException(e); |
| | | } |
| | | } |
| | | |
| | | } |