|
@@ -0,0 +1,172 @@
|
|
|
|
+package com.huaxu.service;
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
|
+import com.fasterxml.jackson.core.JsonParser;
|
|
|
|
+import com.fasterxml.jackson.databind.JsonNode;
|
|
|
|
+import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
|
+import com.fasterxml.jackson.databind.node.ObjectNode;
|
|
|
|
+import com.huaxu.dao.DeviceSetupMethodMapper;
|
|
|
|
+import com.huaxu.dto.SetupMethod;
|
|
|
|
+import com.huaxu.entity.DeviceEntity;
|
|
|
|
+import com.huaxu.entity.DeviceSetupMethodEntity;
|
|
|
|
+import com.huaxu.entity.DeviceSetupRecordEntity;
|
|
|
|
+import com.huaxu.model.LoginUser;
|
|
|
|
+import com.huaxu.util.UserUtil;
|
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
|
+import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
|
|
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
|
+
|
|
|
|
+import javax.annotation.Resource;
|
|
|
|
+
|
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
|
+
|
|
|
|
+import java.io.IOException;
|
|
|
|
+import java.util.Date;
|
|
|
|
+import java.util.Map;
|
|
|
|
+import java.util.List;
|
|
|
|
+import java.util.Arrays;
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * 参数设置指令Service接口
|
|
|
|
+ *
|
|
|
|
+ * @author: WYY
|
|
|
|
+ * @date 2021-05-10 14:39
|
|
|
|
+ */
|
|
|
|
+@Service
|
|
|
|
+public class DeviceSetupMethodService extends ServiceImpl<DeviceSetupMethodMapper, DeviceSetupMethodEntity> {
|
|
|
|
+
|
|
|
|
+ @Resource
|
|
|
|
+ private DeviceSetupMethodMapper deviceSetupMethodMapper;
|
|
|
|
+ @Autowired
|
|
|
|
+ private DeviceSetupRecordService deviceSetupRecordService;
|
|
|
|
+ @Autowired
|
|
|
|
+ private DeviceService deviceService;
|
|
|
|
+ @Value("${send.setsetup.url}")
|
|
|
|
+ private String setSetupUrl;
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 查列表
|
|
|
|
+ */
|
|
|
|
+ public List<DeviceSetupMethodEntity> findList(DeviceSetupMethodEntity deviceSetupMethodEntity) {
|
|
|
|
+ return deviceSetupMethodMapper.findList(deviceSetupMethodEntity);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 批量删除
|
|
|
|
+ */
|
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
|
+ public boolean delDeviceSetupMethodByIds(Long[] ids) {
|
|
|
|
+
|
|
|
|
+ return this.removeByIds(Arrays.asList(ids));
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 单个删除
|
|
|
|
+ */
|
|
|
|
+ public boolean delDeviceSetupMethodById(Long id) {
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ return this.removeById(id);
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 保存
|
|
|
|
+ */
|
|
|
|
+ public boolean addDeviceSetupMethod(DeviceSetupMethodEntity deviceSetupMethod) {
|
|
|
|
+
|
|
|
|
+ if (this.save(deviceSetupMethod)) {
|
|
|
|
+ return true;
|
|
|
|
+ }
|
|
|
|
+ return false;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 修改根居ID
|
|
|
|
+ */
|
|
|
|
+ public boolean updateDeviceSetupMethodById(DeviceSetupMethodEntity deviceSetupMethod) {
|
|
|
|
+
|
|
|
|
+ if (this.updateById(deviceSetupMethod)) {
|
|
|
|
+ return true;
|
|
|
|
+ }
|
|
|
|
+ return false;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 根居ID获取对象
|
|
|
|
+ */
|
|
|
|
+ public DeviceSetupMethodEntity findDeviceSetupMethodById(Long id) {
|
|
|
|
+ return deviceSetupMethodMapper.findDeviceSetupMethodById(id);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 添加设备指令
|
|
|
|
+ *
|
|
|
|
+ * @param type
|
|
|
|
+ * @param deviceId
|
|
|
|
+ * @param attributeId
|
|
|
|
+ * @param parms
|
|
|
|
+ * @return
|
|
|
|
+ */
|
|
|
|
+ public boolean setSetupForDevice(Integer type, Long sceneId,Long deviceId, Long attributeId, String parms) {
|
|
|
|
+ LoginUser currentUser = UserUtil.getCurrentUser();
|
|
|
|
+ String methodParms="";
|
|
|
|
+ //查询设备
|
|
|
|
+ DeviceEntity deviceEntity = deviceService.selectById(deviceId);
|
|
|
|
+ if (deviceEntity == null)
|
|
|
|
+ return false;
|
|
|
|
+ //查询操作方法
|
|
|
|
+ DeviceSetupMethodEntity info = new DeviceSetupMethodEntity();
|
|
|
|
+ info.setType(type);
|
|
|
|
+ info.setDeviceId(deviceId);
|
|
|
|
+ info.setAttributeId(attributeId);
|
|
|
|
+ List<DeviceSetupMethodEntity> deviceSetupMethodEntities = deviceSetupMethodMapper.findList(info);
|
|
|
|
+ //查询到操作方法后执行操作
|
|
|
|
+ if (deviceSetupMethodEntities.size() > 0) {
|
|
|
|
+ String method = deviceSetupMethodEntities.get(0).getMethod();
|
|
|
|
+ methodParms = deviceSetupMethodEntities.get(0).getParams();
|
|
|
|
+ SetupMethod setupMethod = new SetupMethod();
|
|
|
|
+ setupMethod.setMethod(method);
|
|
|
|
+ setupMethod.setUnitIdentifier(deviceEntity.getDeviceCode());
|
|
|
|
+ setupMethod.setReceiveTime(new Date());
|
|
|
|
+ //封装指令
|
|
|
|
+ String[] parm = parms.split(",");
|
|
|
|
+ ObjectMapper mapper = new ObjectMapper();
|
|
|
|
+ for (String item : parm) {
|
|
|
|
+ methodParms = methodParms.replaceFirst("%", item);
|
|
|
|
+ }
|
|
|
|
+ JsonNode node = null;
|
|
|
|
+ try {
|
|
|
|
+ node = mapper.readTree(methodParms);
|
|
|
|
+ } catch (IOException e) {
|
|
|
|
+ return false;
|
|
|
|
+ }
|
|
|
|
+ setupMethod.setParams(node);
|
|
|
|
+
|
|
|
|
+ System.out.println("指令:"+setSetupUrl);
|
|
|
|
+
|
|
|
|
+ //添加操作记录
|
|
|
|
+ DeviceSetupRecordEntity deviceSetupRecordEntity = new DeviceSetupRecordEntity();
|
|
|
|
+ deviceSetupRecordEntity.setType(type);
|
|
|
|
+ deviceSetupRecordEntity.setAttributeId(attributeId);
|
|
|
|
+ deviceSetupRecordEntity.setDeviceId(deviceId);
|
|
|
|
+ deviceSetupRecordEntity.setCreateBy(currentUser.getUsername());
|
|
|
|
+ deviceSetupRecordEntity.setUpdateBy(currentUser.getUsername());
|
|
|
|
+ deviceSetupRecordEntity.setStatus(1);
|
|
|
|
+ deviceSetupRecordEntity.setDateCreate(new Date());
|
|
|
|
+ deviceSetupRecordEntity.setDateUpdate(new Date());
|
|
|
|
+ deviceSetupRecordEntity.setSceneId(sceneId);
|
|
|
|
+ deviceSetupRecordEntity.setParams(methodParms);
|
|
|
|
+ deviceSetupRecordEntity.setTenantId(currentUser.getTenantId());
|
|
|
|
+ deviceSetupRecordEntity.setSetupMethodId(deviceSetupMethodEntities.get(0).getId());
|
|
|
|
+ deviceSetupRecordEntity.setState(1);
|
|
|
|
+ deviceSetupRecordService.addDeviceSetupRecord(deviceSetupRecordEntity);
|
|
|
|
+ }
|
|
|
|
+ return true;
|
|
|
|
+ }
|
|
|
|
+}
|