diff --git a/iot-common/iot-base/src/main/java/com/qiuguo/iot/base/date/DateEnum.java b/iot-common/iot-base/src/main/java/com/qiuguo/iot/base/date/DateEnum.java index 6a3643e..38547a1 100644 --- a/iot-common/iot-base/src/main/java/com/qiuguo/iot/base/date/DateEnum.java +++ b/iot-common/iot-base/src/main/java/com/qiuguo/iot/base/date/DateEnum.java @@ -1,5 +1,7 @@ package com.qiuguo.iot.base.date; +import com.qiuguo.iot.base.utils.ChineseToAlaboUtils; + import java.time.LocalDateTime; import java.util.HashMap; import java.util.Map; @@ -32,6 +34,16 @@ public enum DateEnum implements IDate{ return MONRING.getDateTime(localDateTime); } + }, + /** + * 清晨 + */ + QING_CHEN("清晨"){ + @Override + public LocalDateTime getDateTime(LocalDateTime localDateTime){ + return MONRING.getDateTime(localDateTime); + } + }, /** * 拂晓 @@ -56,7 +68,7 @@ public enum DateEnum implements IDate{ /** * 中午 */ - NOON("中午"){ + NOON("中午", 1){ @Override public LocalDateTime getDateTime(LocalDateTime localDateTime){ return localDateTime.withHour(12); @@ -66,27 +78,27 @@ public enum DateEnum implements IDate{ /** * 下午 */ - PM("下午"){ + PM("下午", 1){ @Override public LocalDateTime getDateTime(LocalDateTime localDateTime){ - return localDateTime.withMonth(15); + return localDateTime.withHour(15); } }, /** * 傍晚 */ - EVENING("傍晚"){ + EVENING("傍晚", 1){ @Override public LocalDateTime getDateTime(LocalDateTime localDateTime){ - return localDateTime.withMonth(18); + return localDateTime.withHour(18); } }, /** * 晚上 */ - NIGHT("晚上"){ + NIGHT("晚上", 2){ @Override public LocalDateTime getDateTime(LocalDateTime localDateTime){ return localDateTime.withHour(21); @@ -96,7 +108,7 @@ public enum DateEnum implements IDate{ /** * 深夜 */ - DEEP_NIGHT("深夜"){ + DEEP_NIGHT("深夜", 2){ @Override public LocalDateTime getDateTime(LocalDateTime localDateTime){ return localDateTime.withHour(23); @@ -106,7 +118,7 @@ public enum DateEnum implements IDate{ /** * 凌晨 */ - BEFORE_DAWN("凌晨"){ + BEFORE_DAWN("凌晨", 2){ @Override public LocalDateTime getDateTime(LocalDateTime localDateTime){ return localDateTime.withHour(1); @@ -124,7 +136,47 @@ public enum DateEnum implements IDate{ }, /** - * 今天 + * 今晚 + */ + TONIGHT("今晚", 2){ + @Override + public LocalDateTime getDateTime(LocalDateTime localDateTime){ + return localDateTime.withHour(18); + } + + }, + /** + * 明晚 + */ + TOMORROW_EVENING("明晚", 2){ + @Override + public LocalDateTime getDateTime(LocalDateTime localDateTime){ + return localDateTime.plusDays(1).withHour(18); + } + + }, + /** + * 昨晚 + */ + LAST_NIGHT("昨晚", 2){ + @Override + public LocalDateTime getDateTime(LocalDateTime localDateTime){ + return localDateTime.minusDays(1).withHour(18); + } + + }, + /** + * 前晚 + */ + LAST_L_NIGHT("前晚", 2){ + @Override + public LocalDateTime getDateTime(LocalDateTime localDateTime){ + return localDateTime.minusDays(2).withHour(18); + } + + }, + /** + * 现在 */ NOW("现在"){ @Override @@ -406,7 +458,7 @@ public enum DateEnum implements IDate{ }, - DEFAULT("默认"){ + DEFAULT("默认error"){ @Override public LocalDateTime getDateTime(LocalDateTime localDateTime){ return localDateTime; @@ -414,14 +466,365 @@ public enum DateEnum implements IDate{ }, ; - Date date; String code; + /** + * 0:上午、1:下午、2:晚上 + */ + int ampm = 0; - String time; DateEnum(String c) { code = c; } + DateEnum(String c, int b) { + code = c; + ampm = b; + } + + public String getCode() {return code;} + + public int getAmPm() {return ampm;} + + public static LocalDateTime getDateWithCode(String c){ + return getDateWithCodeAndLocalDateTime(c, LocalDateTime.now(), 0); + } + + public static LocalDateTime getDateWithCodeAndLocalDateTime(String c, LocalDateTime localDateTime, int ampm){ + DateEnum dateEnu = null; + for(DateEnum enum1 : values()){ + if(c.startsWith(enum1.getCode())){ + dateEnu = enum1; + localDateTime = dateEnu.getDateTime(localDateTime); + c = c.substring(enum1.getCode().length()); + localDateTime = getDateWithCodeAndLocalDateTime(c, localDateTime, dateEnu.getAmPm()); + break; + } + } + if(dateEnu == null){ + Integer i = -1; + if(c.contains("天后")){ + c = c.replace("天后", ""); + i *= ChineseToAlaboUtils.getLong(c).intValue(); + return localDateTime.minusDays(i); + }else if(c.contains("天前")){ + c = c.replace("天前", ""); + i = 1; + i *= ChineseToAlaboUtils.getLong(c).intValue(); + return localDateTime.minusDays(i); + }else if(c.contains("天")){ + c = c.replace("天", ""); + i *= ChineseToAlaboUtils.getLong(c).intValue(); + return localDateTime.minusDays(i); + }else if(c.contains("个月后")){ + c = c.replace("个月后", ""); + i *= ChineseToAlaboUtils.getLong(c).intValue(); + return localDateTime.minusMonths(i); + }else if(c.contains("个月前")){ + c = c.replace("个月前", ""); + i = 1; + i *= ChineseToAlaboUtils.getLong(c).intValue(); + return localDateTime.minusMonths(i); + }else if(c.contains("月后")){ + c = c.replace("月后", ""); + i *= ChineseToAlaboUtils.getLong(c).intValue(); + return localDateTime.minusMonths(i); + }else if(c.contains("月前")){ + c = c.replace("月前", ""); + i = 1; + i *= ChineseToAlaboUtils.getLong(c).intValue(); + return localDateTime.minusMonths(i); + }else if(c.contains("月份")){ + c = c.replace("月份", ""); + i = ChineseToAlaboUtils.getLong(c).intValue(); + if(i > 12){ + i = 12; + }else if(i < 1){ + i = 1; + } + return localDateTime.withMonth(i); + }else if(c.contains("月")){ + //c = c.replace("月", ""); + int index = c.indexOf("月"); + i = ChineseToAlaboUtils.getLong(c.substring(0, index)).intValue(); + if(i > 12){ + i = 12; + }else if(i < 1){ + i = 1; + } + return getDateWithCodeAndLocalDateTime( + c.substring(index + 1), + localDateTime.withMonth(i), + ampm); + }else if(c.contains("年后")){ + c = c.replace("年后", ""); + i *= ChineseToAlaboUtils.getLong(c).intValue(); + return localDateTime.minusYears(i); + }else if(c.contains("年前")){ + c = c.replace("年前", ""); + i = 1; + i *= ChineseToAlaboUtils.getLong(c).intValue(); + return localDateTime.minusYears(i); + }else if(c.contains("年")){ + c = c.replace("年", ""); + i *= ChineseToAlaboUtils.getLong(c).intValue(); + return localDateTime.minusYears(i); + }else if(c.contains("小时后")){ + c = c.replace("小时后", ""); + i *= ChineseToAlaboUtils.getLong(c).intValue(); + return localDateTime.minusHours(i); + }else if(c.contains("小时前")){ + c = c.replace("小时前", ""); + i = 1; + i *= ChineseToAlaboUtils.getLong(c).intValue(); + return localDateTime.minusHours(i); + }else if(c.contains("小时")){ + c = c.replace("小时", ""); + i *= ChineseToAlaboUtils.getLong(c).intValue(); + return localDateTime.minusHours(i); + }else if(c.contains("点钟后")){ + c = c.replace("点钟后", ""); + i *= ChineseToAlaboUtils.getLong(c).intValue(); + return localDateTime.minusHours(i); + }else if(c.contains("点钟前")){ + c = c.replace("点钟前", ""); + i = 1; + i *= ChineseToAlaboUtils.getLong(c).intValue(); + return localDateTime.minusHours(i); + }else if(c.contains("点钟")){ + //c = c.replace("点钟", ""); + i = ChineseToAlaboUtils.getLong(c).intValue(); + if(i > 23){ + i = 23; + } + return getDateWithCodeAndLocalDateTime( + c.substring(c.indexOf("点钟") + 1), + localDateTime.withHour(i).withMinute(0).withSecond(0), + ampm); + }else if(c.contains("点前")){ + c = c.replace("点前", ""); + i = 1; + i *= ChineseToAlaboUtils.getLong(c).intValue(); + return localDateTime.minusHours(i); + }else if(c.contains("点后")){ + c = c.replace("点后", ""); + i *= ChineseToAlaboUtils.getLong(c).intValue(); + return localDateTime.minusHours(i); + }else if(c.contains("点")){ + int index = c.indexOf("点"); + i = ChineseToAlaboUtils.getLong(c.substring(0, index)).intValue(); + if(i > 23){ + i = 23; + }else if(i < 0){ + i = 0; + } + if(ampm == 1){ + //下午 + if(i < 12){ + i += 12; + } + }else if(ampm == 2){ + //晚上 + if(i < 12 && i > 5){ + i += 12; + }else{ + localDateTime = localDateTime.plusDays(1); + } + } + return getDateWithCodeAndLocalDateTime( + c.substring(index + 1), + localDateTime.withHour(i).withMinute(0).withSecond(0), + ampm); + }else if(c.contains("号")){ + c = c.replace("号", ""); + i = ChineseToAlaboUtils.getLong(c).intValue(); + //判断闰月大小月 + return localDateTime.withDayOfMonth(i); + }else if(c.contains("时后")){ + c = c.replace("时后", ""); + i *= ChineseToAlaboUtils.getLong(c).intValue(); + return localDateTime.minusHours(i); + }else if(c.contains("时前")){ + c = c.replace("时前", ""); + i = 1; + i *= ChineseToAlaboUtils.getLong(c).intValue(); + return localDateTime.minusHours(i); + }else if(c.contains("时")){ + int index = c.indexOf("时"); + i = ChineseToAlaboUtils.getLong(c.substring(0, index)).intValue(); + if(i > 23){ + i = 23; + }else if(i < 0){ + i = 0; + } + if(ampm == 1){ + //下午 + if(i < 12){ + i += 12; + } + }else if(ampm == 2){ + //晚上 + if(i < 12 && i > 5){ + i += 12; + }else{ + localDateTime = localDateTime.plusDays(1); + } + } + return getDateWithCodeAndLocalDateTime( + c.substring(index + 1), + localDateTime.withHour(i).withMinute(0).withSecond(0), + ampm); + }else if(c.contains("分钟后")){ + c = c.replace("分钟后", ""); + i *= ChineseToAlaboUtils.getLong(c).intValue(); + return localDateTime.minusMinutes(i); + }else if(c.contains("分钟前")){ + c = c.replace("分钟前", ""); + i = 1; + i *= ChineseToAlaboUtils.getLong(c).intValue(); + return localDateTime.minusMinutes(i); + }else if(c.contains("分钟")){ + int index = c.indexOf("分钟"); + i = ChineseToAlaboUtils.getLong(c.substring(0, index)).intValue(); + if(i > 59){ + i = 59; + }else if(i < 0){ + i = 0; + } + return getDateWithCodeAndLocalDateTime( + c.substring(index + 1), + localDateTime.withMinute(i), + ampm); + }else if(c.contains("分")){ + int index = c.indexOf("分"); + i = ChineseToAlaboUtils.getLong(c.substring(0, index)).intValue(); + if(i > 59){ + i = 59; + }else if(i < 0){ + i = 0; + } + return getDateWithCodeAndLocalDateTime( + c.substring(index + 1), + localDateTime.withMinute(i), + ampm); + }else if(c.contains("秒后")){ + c = c.replace("秒后", ""); + i *= ChineseToAlaboUtils.getLong(c).intValue(); + return localDateTime.minusSeconds(i); + }else if(c.contains("秒前")){ + c = c.replace("秒前", ""); + i = 1; + i *= ChineseToAlaboUtils.getLong(c).intValue(); + return localDateTime.minusSeconds(i); + }else if(c.contains("秒")){ + c = c.replace("秒", ""); + i = ChineseToAlaboUtils.getLong(c).intValue(); + return localDateTime.withSecond(i); + }else if(c.contains("上周")){// + c = c.replace("上周", ""); + i = ChineseToAlaboUtils.getLong(c).intValue(); + if(i > 7){ + i = 7; + }else if(i < 1){ + i = 1; + } + int now = localDateTime.getDayOfWeek().getValue(); + now = 7 - now; + i += now; + return localDateTime.minusDays(i); + }else if(c.contains("下周")){// + c = c.replace("下周", ""); + i = 1; + i = ChineseToAlaboUtils.getLong(c).intValue(); + if(i > 7){ + i = 7; + }else if(i < 1){ + i = 1; + } + int now = localDateTime.getDayOfWeek().getValue(); + i = 7 - i + now; + return localDateTime.plusDays(i); + }else if(c.contains("上星期")){// + c = c.replace("上星期", ""); + i = ChineseToAlaboUtils.getLong(c).intValue(); + if(i > 7){ + i = 7; + }else if(i < 1){ + i = 1; + } + int now = localDateTime.getDayOfWeek().getValue(); + + i = 7 - i + now; + return localDateTime.minusDays(i); + }else if(c.contains("下星期")){// + c = c.replace("下星期", ""); + i = 1; + i = ChineseToAlaboUtils.getLong(c).intValue(); + if(i > 7){ + i = 7; + }else if(i < 1){ + i = 1; + } + int now = localDateTime.getDayOfWeek().getValue(); + now = 7 - now; + i += now; + return localDateTime.plusDays(i); + }else if(c.contains("周后")){// + c = c.replace("周后", ""); + i *= ChineseToAlaboUtils.getLong(c).intValue(); + return localDateTime.minusWeeks(i); + }else if(c.contains("周前")){// + c = c.replace("周前", ""); + i = 1; + i *= ChineseToAlaboUtils.getLong(c).intValue(); + return localDateTime.minusWeeks(i); + }else if(c.contains("星期后")){// + c = c.replace("星期后", ""); + i *= ChineseToAlaboUtils.getLong(c).intValue(); + return localDateTime.minusWeeks(i); + }else if(c.contains("星期前")){// + c = c.replace("星期前", ""); + i = 1; + i *= ChineseToAlaboUtils.getLong(c).intValue(); + return localDateTime.minusWeeks(i); + }else if(c.indexOf("周") == 0){//周几 + c = c.replace("周", ""); + i = ChineseToAlaboUtils.getLong(c).intValue(); + if(i > 7){ + i = 7; + }else if(i < 1){ + i = 1; + } + int now = localDateTime.getDayOfWeek().getValue(); + i = now - i; + + return localDateTime.minusDays(i); + }else if(c.indexOf("周") > 0){//几周 + c = c.replace("周", ""); + i *= ChineseToAlaboUtils.getLong(c).intValue(); + return localDateTime.minusWeeks(i); + }else if(c.indexOf("星期") == 0){//星期几 + c = c.replace("星期", ""); + i = ChineseToAlaboUtils.getLong(c).intValue(); + if(i > 7){ + i = 7; + }else if(i < 1){ + i = 1; + } + int now = localDateTime.getDayOfWeek().getValue(); + i = now - i; + return localDateTime.minusDays(i); + }else if(c.indexOf("星期") > 0){//几星期 + c = c.replace("星期", ""); + i *= ChineseToAlaboUtils.getLong(c).intValue(); + return localDateTime.minusWeeks(i); + }else{ + //24节气,调用网络接口 + } + + } + return localDateTime; + } + public static DateEnum getWithCode(String c) { for (DateEnum dateEnum:values() ) { @@ -429,101 +832,6 @@ public enum DateEnum implements IDate{ return dateEnum; } } - if(c.contains("天后")){ - c = c.replace("天后", ""); - }else if(c.contains("天前")){ - c = c.replace("天前", ""); - }else if(c.contains("天")){ - c = c.replace("天", ""); - }else if(c.contains("个月后")){ - c = c.replace("个月后", ""); - }else if(c.contains("个月前")){ - c = c.replace("个月前", ""); - }else if(c.contains("个月")){ - c = c.replace("个月", ""); - }else if(c.contains("月后")){ - c = c.replace("月后", ""); - }else if(c.contains("月前")){ - c = c.replace("月前", ""); - }else if(c.contains("月份")){ - c = c.replace("月份", ""); - }else if(c.contains("月")){ - c = c.replace("月", ""); - }else if(c.contains("年后")){ - c = c.replace("年后", ""); - }else if(c.contains("年前")){ - c = c.replace("年前", ""); - }else if(c.contains("年")){ - c = c.replace("年", ""); - }else if(c.contains("小时后")){ - c = c.replace("小时后", ""); - }else if(c.contains("小时前")){ - c = c.replace("小时前", ""); - }else if(c.contains("小时")){ - c = c.replace("小时", ""); - }else if(c.contains("点钟后")){ - c = c.replace("点钟后", ""); - }else if(c.contains("点钟前")){ - c = c.replace("点钟前", ""); - }else if(c.contains("点钟")){ - c = c.replace("点钟", ""); - }else if(c.contains("点前")){ - c = c.replace("点前", ""); - }else if(c.contains("点后")){ - c = c.replace("点后", ""); - }else if(c.contains("点")){ - c = c.replace("点", ""); - }else if(c.contains("时后")){ - c = c.replace("时后", ""); - }else if(c.contains("时前")){ - c = c.replace("时前", ""); - }else if(c.contains("时")){ - c = c.replace("时", ""); - }else if(c.contains("分钟后")){ - c = c.replace("分钟后", ""); - }else if(c.contains("分钟前")){ - c = c.replace("分钟前", ""); - }else if(c.contains("分钟")){ - c = c.replace("分钟", ""); - }else if(c.contains("秒后")){ - c = c.replace("秒后", ""); - }else if(c.contains("秒前")){ - c = c.replace("秒前", ""); - }else if(c.contains("秒")){ - c = c.replace("秒", ""); - }else if(c.contains("周")){//一周,周一 - c = c.replace("周", ""); - }else if(c.contains("星期")){//一星期 星期一 - c = c.replace("星期", ""); - }else{ - //24节气,调用网络接口 - } - Integer n = 1; - try{ - n = Integer.parseInt(c); - }catch(Exception e){ - //含有中文 - Map chineseToArib = new HashMap<>(); - chineseToArib.put("个", 1); - chineseToArib.put("十", 10); - chineseToArib.put("百", 100); - chineseToArib.put("千", 1000); - chineseToArib.put("万", 10000); - chineseToArib.put("亿", 100000000); - chineseToArib.put("一", 1); - chineseToArib.put("二", 2); - chineseToArib.put("三", 3); - chineseToArib.put("四", 4); - chineseToArib.put("五", 5); - chineseToArib.put("六", 6); - chineseToArib.put("七", 7); - chineseToArib.put("八", 8); - chineseToArib.put("九", 9); - char[] ch = new char[c.length()]; - c.getChars(0, c.length(), ch, 0); - - } - //中英文数字 return DEFAULT; } diff --git a/iot-common/iot-base/src/main/java/com/qiuguo/iot/base/utils/ChineseToAlaboUtils.java b/iot-common/iot-base/src/main/java/com/qiuguo/iot/base/utils/ChineseToAlaboUtils.java new file mode 100644 index 0000000..b0cd8d9 --- /dev/null +++ b/iot-common/iot-base/src/main/java/com/qiuguo/iot/base/utils/ChineseToAlaboUtils.java @@ -0,0 +1,93 @@ +package com.qiuguo.iot.base.utils; + +import java.util.HashMap; +import java.util.Map; + +/** + * 中文字符串转阿拉伯数字 + */ +public class ChineseToAlaboUtils { + static Map chineseToArib1 = new HashMap(){{ + put("个", 1L); + put("十", 10L); + put("拾", 10L); + put("百", 100L); + put("佰", 100L); + put("千", 1000L); + put("仟", 1000L); + put("万", 10000L); + put("萬", 10000L); + put("亿", 100000000L); + }}; + + static Map chineseToArib = new HashMap(){{ + put("一", 1L); + put("二", 2L); + put("三", 3L); + put("四", 4L); + put("五", 5L); + put("六", 6L); + put("七", 7L); + put("八", 8L); + put("九", 9L); + put("零", 0L); + put("1", 1L); + put("2", 2L); + put("3", 3L); + put("4", 4L); + put("5", 5L); + put("6", 6L); + put("7", 7L); + put("8", 8L); + put("9", 9L); + put("0", 0L); + put("壹", 1L); + put("贰", 2L); + put("叁", 3L); + put("肆", 4L); + put("伍", 5L); + put("陆", 6L); + put("柒", 7L); + put("捌", 8L); + put("玖", 9L); + put("整", 9L); + }}; + public static Long getLong(String c){ + char[] ch = new char[c.length()]; + c.getChars(0, c.length(), ch, 0); + Long p = 1L; + Long n = 0L; + String key; + int i = ch.length - 1; + Long m ; + boolean isHi = false; + Long b = 1L; + while (i >= 0){ + char h = ch[i--]; + key = String.valueOf(h); + if(chineseToArib1.containsKey(key)){ + if(isHi){ + p *= chineseToArib1.get(key); + }else{ + p = chineseToArib1.get(key); + if(b < p && (p == 10000L || p == 100000000L)){ + b = p; + continue; + } + p *= b; + } + isHi = true; + }else if(chineseToArib.containsKey(key)){ + m = chineseToArib.get(key) * p; + n += m; + p *= 10; + isHi = false; + } + } + if(isHi){ + n += p; + } + System.out.println("传入数字:" + c + ",转换后数字:" + n); + return n; + } +} diff --git a/iot-common/iot-third/src/main/java/com/qiuguo/iot/third/nlp/action/ActionTime.java b/iot-common/iot-third/src/main/java/com/qiuguo/iot/third/nlp/action/ActionTime.java index 71e89bb..ea3d503 100644 --- a/iot-common/iot-third/src/main/java/com/qiuguo/iot/third/nlp/action/ActionTime.java +++ b/iot-common/iot-third/src/main/java/com/qiuguo/iot/third/nlp/action/ActionTime.java @@ -1,9 +1,7 @@ package com.qiuguo.iot.third.nlp.action; -import com.alibaba.fastjson.JSON; -import com.alibaba.fastjson.JSONArray; -import com.alibaba.fastjson.JSONObject; import com.qiuguo.iot.base.date.DateEnum; +import com.qiuguo.iot.base.utils.ChineseToAlaboUtils; import com.qiuguo.iot.base.utils.StringUtils; import lombok.Data; @@ -60,98 +58,18 @@ public class ActionTime { } public static void main(String[] args) { - JSONObject json = JSON.parseObject("{\"data\":[\"Success\",{\"name\":\"/data/wzg/vits_results/089135a743d4d438ecda93263174ab0cfe5dff0a/audio.wav\",\"data\":null,\"is_file\":true}],\"is_generating\":false,\"duration\":0.3392457962036133,\"average_duration\":0.5043019453684489}"); - JSONArray array = json.getJSONArray("data"); - json = array.getJSONObject(1); - String m = json.getString("name"); - DateTimeFormatter df1 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); + + DateTimeFormatter df1 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss E"); LocalDateTime localDateTime = LocalDateTime.now(); - localDateTime = DateEnum.getWithCode("国庆").getDateTime(DateEnum.getWithCode("拂晓").getDateTime(localDateTime)); + //localDateTime = DateEnum.getWithCode("国庆").getDateTime(DateEnum.getWithCode("拂晓").getDateTime(localDateTime)); System.out.println(localDateTime.format(df1)); + System.out.println(DateEnum.getDateWithCodeAndLocalDateTime("今晚三点50分4秒", localDateTime, 0).format(df1)); + //System.out.println(DateEnum.getDateWithCodeAndLocalDateTime("下周一", localDateTime, 0).format(df1)); + + - int n = 1; - Map chineseToArib1 = new HashMap<>(); - chineseToArib1.put("个", 1); - chineseToArib1.put("十", 10); - chineseToArib1.put("百", 100); - chineseToArib1.put("千", 1000); - chineseToArib1.put("万", 10000); - chineseToArib1.put("亿", 100000000); - Map chineseToArib = new HashMap<>(); - chineseToArib.put("一", 1); - chineseToArib.put("二", 2); - chineseToArib.put("三", 3); - chineseToArib.put("四", 4); - chineseToArib.put("五", 5); - chineseToArib.put("六", 6); - chineseToArib.put("七", 7); - chineseToArib.put("八", 8); - chineseToArib.put("九", 9); - chineseToArib.put("零", 0); - chineseToArib.put("1", 1); - chineseToArib.put("2", 2); - chineseToArib.put("3", 3); - chineseToArib.put("4", 4); - chineseToArib.put("5", 5); - chineseToArib.put("6", 6); - chineseToArib.put("7", 7); - chineseToArib.put("8", 8); - chineseToArib.put("9", 9); - chineseToArib.put("0", 0); - String c = "一"; - char[] ch = new char[c.length()]; - c.getChars(0, c.length(), ch, 0); - Integer p = 1; - String v = String.valueOf(n); - String key; - for(char h : ch){ - key = String.valueOf(h); - if(chineseToArib1.containsKey(key)){ - p = chineseToArib.get(key); - }else if(chineseToArib.containsKey(key)){ - n *= p; - } - } - c = "一十二"; - ch = new char[c.length()]; - c.getChars(0, c.length(), ch, 0); - p = 1; - n = 1; - v = String.valueOf(n); - for(char h : ch){ - if(chineseToArib1.containsKey(String.valueOf(h))){ - p = chineseToArib.get(String.valueOf(h)); - }else if(chineseToArib.containsKey(String.valueOf(h))){ - n *= p; - } - } - c = "十一"; - ch = new char[c.length()]; - c.getChars(0, c.length(), ch, 0); - p = 1; - n = 1; - v = String.valueOf(n); - for(char h : ch){ - if(chineseToArib1.containsKey(h)){ - p = chineseToArib.get(h); - }else if(chineseToArib.containsKey(h)){ - n *= p; - } - } - c = "一百零三"; - ch = new char[c.length()]; - c.getChars(0, c.length(), ch, 0); - p = 1; - n = 1; - v = String.valueOf(n); - for(char h : ch){ - if(chineseToArib1.containsKey(h)){ - p = chineseToArib.get(h); - }else if(chineseToArib.containsKey(h)){ - n *= p; - } - } - System.out.println(v); } + + } diff --git a/iot-modules/iot-box-websocket-api/src/main/java/com/qiuguo/iot/box/websocket/api/handler/BaseWebSocketProcess.java b/iot-modules/iot-box-websocket-api/src/main/java/com/qiuguo/iot/box/websocket/api/handler/BaseWebSocketProcess.java index 3a0a6a6..bf9e95b 100644 --- a/iot-modules/iot-box-websocket-api/src/main/java/com/qiuguo/iot/box/websocket/api/handler/BaseWebSocketProcess.java +++ b/iot-modules/iot-box-websocket-api/src/main/java/com/qiuguo/iot/box/websocket/api/handler/BaseWebSocketProcess.java @@ -100,44 +100,7 @@ public class BaseWebSocketProcess { protected static String apiType = "api-type"; protected static String apiToken = "api-token"; - @RabbitListener(queues = YunxiRabbitConst.QUEUE_LISTENER, containerFactory = "simpleRabbitListenerContainerFactory") - private void processRabbitMq(Channel channel, Message message){ - try { - String messageContent = new String(message.getBody(), "UTF-8"); - log.info("来自RabbitMQ的消息{}", messageContent); - U3dMsg u3dMsg = JSONObject.parseObject(messageContent, U3dMsg.class); - if(U3dMsgTypeEnum.IOT.getCode().equals(u3dMsg.getMsgType())){ - //物联网操作 - DeviceUserBindRequest deviceBindRequest = new DeviceUserBindRequest(); - deviceBindRequest.setU3dId(Long.parseLong(u3dMsg.getMetaId())); - if(StringUtils.isNotEmpty(u3dMsg.getScenceId())){ - deviceBindRequest.setScenceId(Long.parseLong(u3dMsg.getScenceId())); - } - deviceUserBindService.selectDeviceUserBindByRequest(deviceBindRequest).map(db ->{ - SystemTalkBindDeviceRequest systemTalkBindDeviceRequest = new SystemTalkBindDeviceRequest(); - systemTalkBindDeviceRequest.setU3dStatusId(Long.parseLong(u3dMsg.getStatusId())); - systemTalkBindDeviceRequest.setCategoryCode(db.getCategoryCode()); - systemTalkBindDeviceService.selectSystemTalkBindDeviceByRequest(systemTalkBindDeviceRequest).map(sd -> { - TuyaQuery query = new TuyaQuery(); - query.setDeviceId(db.getOtherDeviceId()); - query.setValue(u3dMsg.getExParam()); - query.setUserHandlingDeviceId(sd.getUserHandlingId()); - tuyaDeviceService.controlDevice(query).map(isOk ->{ - log.info("U3D打开设备调用情况{}", isOk); - return isOk; - }).subscribe(); - return sd; - }).subscribe(); - return db; - }).subscribe(); - } - //处理完毕 手动消息确认 配置需开启 acknowledge-mode: manual - channel.basicAck(message.getMessageProperties().getDeliveryTag(), false); - }catch(Exception e){ - log.info("RabbitMQ 消息处理异常{}", e); - } - } private Mono toQianWen(Action action, BaseSession baseSession, Integer type){ baseSession.setRequestId(baseSession.getRequestId() + 1); diff --git a/iot-modules/iot-box-websocket-api/src/main/java/com/qiuguo/iot/box/websocket/api/handler/BoxWebSocketHandler.java b/iot-modules/iot-box-websocket-api/src/main/java/com/qiuguo/iot/box/websocket/api/handler/BoxWebSocketHandler.java index 8f73c93..210a6fe 100644 --- a/iot-modules/iot-box-websocket-api/src/main/java/com/qiuguo/iot/box/websocket/api/handler/BoxWebSocketHandler.java +++ b/iot-modules/iot-box-websocket-api/src/main/java/com/qiuguo/iot/box/websocket/api/handler/BoxWebSocketHandler.java @@ -68,6 +68,7 @@ public class BoxWebSocketHandler extends BaseWebSocketProcess implements WebSock String ip = headers.get(LogWebFilter.HEAD_IP).get(0); String signature = headers.get("signature").get(0); Long userId = Long.parseLong(headers.get("userId").get(0)); + Integer isBind = Integer.parseInt(headers.get("isBind").get(0)); // BoxSession boxSession = new BoxSession(); @@ -90,7 +91,7 @@ public class BoxWebSocketHandler extends BaseWebSocketProcess implements WebSock //校验 - checkToken(boxSession, sn, linkTime, signature, userId).contextWrite(context -> { + checkToken(boxSession, sn, linkTime, signature, userId, isBind).contextWrite(context -> { Context contextTmp = context.put(LogMdcConfiguration.PRINT_LOG_ID, boxSession.getLogId()); return contextTmp; @@ -171,7 +172,7 @@ public class BoxWebSocketHandler extends BaseWebSocketProcess implements WebSock } - private Mono checkToken(BoxSession boxSession, String sn, Long linkTime, String signature, Long userId){ + private Mono checkToken(BoxSession boxSession, String sn, Long linkTime, String signature, Long userId, Integer isBind){ return reactiveStringRedisTemplate.opsForValue().get(RedisConstans.DEVICE_INFO + sn).defaultIfEmpty("").flatMap(s -> { if(com.qiuguo.iot.base.utils.StringUtils.isNotBlank(s)){ try{ @@ -220,7 +221,7 @@ public class BoxWebSocketHandler extends BaseWebSocketProcess implements WebSock DeviceInfoEntity dv = (DeviceInfoEntity)d; boxSession.setDeviceId(dv.getId()); - return bindBox(dv, userId).flatMap(db ->{ + return bindBox(dv, userId, isBind).flatMap(db ->{ BoxSession oldBoxSession = getBoxSessionWithSn(sn); boxGroup.put(sn, boxSession); if(oldBoxSession != null){ @@ -232,7 +233,7 @@ public class BoxWebSocketHandler extends BaseWebSocketProcess implements WebSock } - private Mono bindBox(DeviceInfoEntity dv, Long userId){ + private Mono bindBox(DeviceInfoEntity dv, Long userId, Integer isBind){ log.info("开始绑定设备userId:{}, SN:{}", userId, dv); DeviceUserBindRequest request = new DeviceUserBindRequest(); @@ -254,19 +255,7 @@ public class BoxWebSocketHandler extends BaseWebSocketProcess implements WebSock return deviceUserBindService.setNoMain(userId, DeviceTypeEnum.GUO_BOX.getCode()).defaultIfEmpty(0).flatMap(m ->{ log.info("解除历史isMain标注个数{}", m); return deviceUserBindService.insertDeviceUserBind(entity).flatMap(l ->{ - log.info("绑定成功SN:{} userId:{}", dv, userId); - //下面所有的以前未主设备改成非主设备 - //通知用户端设备绑定成功 - sendNoticeToUser(userId, "设备绑定成功,设备序列号:" + dv.getSn(), AskTypeEnum.DEVICE_BIND.getCode()); - UserDeviceInfoModel userDeviceInfoModel = new UserDeviceInfoModel(); - userDeviceInfoModel.setStatus(YesNo.YES.getCode()); - userDeviceInfoModel.setUserId(userId); - userDeviceInfoModel.setSn(entity.getOtherDeviceId()); - return reactiveStringRedisTemplate.opsForValue().set(RedisConstans.USER_BOX_INFO + userId, - JSONObject.toJSONString(userDeviceInfoModel), - Duration.ofDays(RedisConstans.TEN_YEAR)).flatMap(b -> { - return Mono.just(entity); - }); + return deviceBindSucess(userId, dv, entity); }); }); @@ -276,14 +265,28 @@ public class BoxWebSocketHandler extends BaseWebSocketProcess implements WebSock sendNoticeToUser(userId, "设备联网成功,设备序列号:" + dv.getSn(), AskTypeEnum.BOX_ON_LINE.getCode()); }else{ //通知用户端设备绑定成功 - BaseSession boxSession = getBoxSessionWithSn(dv.getSn()); - if(boxSession != null){ - return closeSendMsg(boxSession, "设备已解绑无法继续使用", AskTypeEnum.DEVICE_UNBIND.getCode()).flatMap( - v -> { - return Mono.just(entity); - } - ); + if(isBind.equals(YesNo.YES.getCode())){ + //重新绑定操作 + log.info("重新绑定:{}, userId:{}", dv.getSn(), userId); + entity.setIsMain(YesNo.YES.getCode()); + entity.setIsBind(YesNo.YES.getCode()); + return deviceUserBindService.setNoMain(userId, DeviceTypeEnum.GUO_BOX.getCode()).defaultIfEmpty(0).flatMap(m -> { + log.info("解除历史isMain标注个数{}", m); + return deviceUserBindService.updateDeviceUserBind(entity).flatMap(i -> { + return deviceBindSucess(userId, dv, entity); + }); + }); + }else{ + BaseSession boxSession = getBoxSessionWithSn(dv.getSn()); + if(boxSession != null){ + return closeSendMsg(boxSession, "设备已解绑无法继续使用", AskTypeEnum.DEVICE_UNBIND.getCode()).flatMap( + v -> { + return Mono.just(entity); + } + ); + } } + } } @@ -292,4 +295,20 @@ public class BoxWebSocketHandler extends BaseWebSocketProcess implements WebSock }); } + + private Mono deviceBindSucess(Long userId, DeviceInfoEntity dv, DeviceUserBindEntity entity) { + log.info("重新绑定成功绑定成功SN:{} userId:{}", dv, userId); + //下面所有的以前未主设备改成非主设备 + //通知用户端设备绑定成功 + sendNoticeToUser(userId, "设备绑定成功,设备序列号:" + dv.getSn(), AskTypeEnum.DEVICE_BIND.getCode()); + UserDeviceInfoModel userDeviceInfoModel = new UserDeviceInfoModel(); + userDeviceInfoModel.setStatus(YesNo.YES.getCode()); + userDeviceInfoModel.setUserId(userId); + userDeviceInfoModel.setSn(entity.getOtherDeviceId()); + return reactiveStringRedisTemplate.opsForValue().set(RedisConstans.USER_BOX_INFO + userId, + JSONObject.toJSONString(userDeviceInfoModel), + Duration.ofDays(RedisConstans.TEN_YEAR)).flatMap(b -> { + return Mono.just(entity); + }); + } } diff --git a/iot-modules/iot-box-websocket-api/src/main/java/com/qiuguo/iot/box/websocket/api/service/U3dToIotService.java b/iot-modules/iot-box-websocket-api/src/main/java/com/qiuguo/iot/box/websocket/api/service/U3dToIotService.java new file mode 100644 index 0000000..a3d3f86 --- /dev/null +++ b/iot-modules/iot-box-websocket-api/src/main/java/com/qiuguo/iot/box/websocket/api/service/U3dToIotService.java @@ -0,0 +1,70 @@ +package com.qiuguo.iot.box.websocket.api.service; + +import com.alibaba.fastjson.JSONObject; +import com.qiuguo.iot.base.enums.U3dMsgTypeEnum; +import com.qiuguo.iot.base.utils.StringUtils; +import com.qiuguo.iot.data.constants.YunxiRabbitConst; +import com.qiuguo.iot.data.request.device.DeviceUserBindRequest; +import com.qiuguo.iot.data.request.system.SystemTalkBindDeviceRequest; +import com.qiuguo.iot.data.resp.u3d.U3dMsg; +import com.qiuguo.iot.data.service.device.DeviceUserBindService; +import com.qiuguo.iot.data.service.system.SystemTalkBindDeviceService; +import com.qiuguo.iot.third.query.TuyaQuery; +import com.qiuguo.iot.third.service.TuyaDeviceService; +import com.rabbitmq.client.Channel; +import lombok.extern.slf4j.Slf4j; +import org.springframework.amqp.core.Message; +import org.springframework.amqp.rabbit.annotation.RabbitListener; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import javax.annotation.Resource; + +@Slf4j +@Service +public class U3dToIotService { + @Autowired + protected TuyaDeviceService tuyaDeviceService; + @Resource + protected DeviceUserBindService deviceUserBindService; + @Resource + protected SystemTalkBindDeviceService systemTalkBindDeviceService; + @RabbitListener(queues = YunxiRabbitConst.QUEUE_LISTENER, containerFactory = "simpleRabbitListenerContainerFactory") + private void processRabbitMq(Channel channel, Message message){ + try { + String messageContent = new String(message.getBody(), "UTF-8"); + log.info("来自RabbitMQ的消息{}", messageContent); + U3dMsg u3dMsg = JSONObject.parseObject(messageContent, U3dMsg.class); + if(U3dMsgTypeEnum.IOT.getCode().equals(u3dMsg.getMsgType())){ + //物联网操作 + DeviceUserBindRequest deviceBindRequest = new DeviceUserBindRequest(); + deviceBindRequest.setU3dId(Long.parseLong(u3dMsg.getMetaId())); + if(StringUtils.isNotEmpty(u3dMsg.getScenceId())){ + deviceBindRequest.setScenceId(Long.parseLong(u3dMsg.getScenceId())); + } + + deviceUserBindService.selectDeviceUserBindByRequest(deviceBindRequest).map(db ->{ + SystemTalkBindDeviceRequest systemTalkBindDeviceRequest = new SystemTalkBindDeviceRequest(); + systemTalkBindDeviceRequest.setU3dStatusId(Long.parseLong(u3dMsg.getStatusId())); + systemTalkBindDeviceRequest.setCategoryCode(db.getCategoryCode()); + systemTalkBindDeviceService.selectSystemTalkBindDeviceByRequest(systemTalkBindDeviceRequest).map(sd -> { + TuyaQuery query = new TuyaQuery(); + query.setDeviceId(db.getOtherDeviceId()); + query.setValue(u3dMsg.getExParam()); + query.setUserHandlingDeviceId(sd.getUserHandlingId()); + tuyaDeviceService.controlDevice(query).map(isOk ->{ + log.info("U3D打开设备调用情况{}", isOk); + return isOk; + }).subscribe(); + return sd; + }).subscribe(); + return db; + }).subscribe(); + } + //处理完毕 手动消息确认 配置需开启 acknowledge-mode: manual + channel.basicAck(message.getMessageProperties().getDeliveryTag(), false); + }catch(Exception e){ + log.info("RabbitMQ 消息处理异常{}", e); + } + } +}