package com.wsm.admin.api;

import com.wsm.admin.constant.MsgCode;
import com.wsm.admin.model.Device;
import com.wsm.admin.service.IDeviceService;
import com.wsm.common.api.BaseController;
import com.wsm.common.util.AjaxJson;
import com.wsm.common.util.RedisUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundGeoOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/api/device")
public class ApiDeviceController extends BaseController {

    private Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private IDeviceService deviceService;


    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 获取所有的设备信息
     * @return
     */
    @RequestMapping(value = {"/list"})
    public AjaxJson list() {
        List<Device> list = (List) redisTemplate.opsForValue().get(MsgCode.REDIS_CYCLE_DEVICE_LIST);
        if (list == null) {
            list = deviceService.findAll();
            logger.warn("list device size: " + list.size());
            redisTemplate.opsForValue().set(MsgCode.REDIS_CYCLE_DEVICE_LIST, list, 24, TimeUnit.HOURS);

        }
       return AjaxJson.success(list);
    }
}
