package com.luxsin.app.controller; import cn.hutool.core.date.DateUtil; import cn.hutool.core.util.StrUtil; import cn.hutool.json.JSONObject; import cn.hutool.json.JSONUtil; import com.luxsin.app.annotation.Base64Response; import com.luxsin.app.bean.OTA; import com.luxsin.app.service.BrandService; import com.luxsin.app.service.ModelService; import com.luxsin.app.service.OTAService; import lombok.extern.slf4j.Slf4j; import org.eafon.data.EResponseTool; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.web.bind.annotation.*; import java.time.LocalDate; import java.util.Map; @Slf4j @RestController @CrossOrigin(origins = "*") public class EqualizeController { private final ModelService modelService; private final OTAService otaService; private final RedisTemplate redisTemplate; private final BrandService brandService; public EqualizeController(ModelService modelService, OTAService otaService, RedisTemplate redisTemplate, BrandService brandService) { this.modelService = modelService; this.otaService = otaService; this.redisTemplate = redisTemplate; this.brandService = brandService; } /** * 耳机列表 * @param key * @param count * @return */ @GetMapping("modelList") @Base64Response public String getModelList(String key, @RequestParam(value = "count", defaultValue = "100") int count){ return modelService.modelList(key, count); } /** * 目标曲线 * @param brand * @param name * @param target * @return */ @GetMapping("getCurve") @Base64Response public String getCurve(String brand, String name, String target, @RequestParam(value = "includeRaw", defaultValue = "false") boolean includeRaw){ String result = modelService.getCurvePoint(brand, name, target); if(!includeRaw){ JSONObject resp = JSONUtil.parseObj(result); if(resp.containsKey("code") && resp.getInt("code") != 0){ resp.remove("fr"); } return resp.toString(); } return result; } @GetMapping("modelCurve") @Base64Response public String modelCurve(String brand, String name){ return modelService.getModelCurve(brand, name); } @GetMapping("test/modelCurve") public String testModelCurve(String brand, String name){ return modelService.getModelCurve(brand, name); } @GetMapping("test/getCurve") public String testGetCurve(String brand, String name, String target, @RequestParam(value = "includeRaw", defaultValue = "false") boolean includeRaw){ String result = modelService.getCurvePoint(brand, name, target); if(!includeRaw){ JSONObject resp = JSONUtil.parseObj(result); if(resp.containsKey("code") && resp.getInt("code") != 0){ resp.remove("fr"); } return resp.toString(); } return result; } /** * 升级 * @param model * @param hw * @param mac * @param beta * @return */ @GetMapping("ota") public EResponseTool OTA(String model, String hw, @RequestParam(value = "mac", defaultValue = "null") String mac, @RequestParam(value = "beta", defaultValue = "0") int beta){ return otaService.getOTA(model, hw, mac, beta); } /** * 上报用户设备信息 * @param mac * @param model * @return */ @GetMapping("reportDevInfo") public EResponseTool reportDevInfo(String mac, String model,String ver, @RequestHeader(value = "X-Forwarded-For", required = false) String xForwardedFor){ if(StrUtil.isEmpty(mac) || StrUtil.isEmpty(model)){ return EResponseTool.error(EResponseTool.ResultCode.VALIDATE_FAILED); } log.info("report dev info remote ip = {}, now time = {}", xForwardedFor, DateUtil.now()); ver = null == ver ? "" : ver; xForwardedFor = null == xForwardedFor ? "" : xForwardedFor; redisTemplate.opsForHash().put("devices", mac, JSONUtil.toJsonStr(Map.of("mac_addr", mac, "model", model, "active_date", LocalDate.now().toString(), "ip_addr", xForwardedFor, "ver", ver))); return EResponseTool.success(); } /** * 获取品牌列表 * @param brandName * @return */ @GetMapping("getBrand") @Base64Response public String getBrand(String brandName){ return brandService.brandList(brandName); } /** * 获取型号列表 * @param brandName * @param modelName * @return */ @GetMapping("getModel") @Base64Response public String getModel(String brandName, String modelName){ return modelService.getModels(brandName, modelName); } }