用户定时任务示例

定时任务支持定时调用UDS的接口

示例场景

每天19:00查询当地空气质量,如果空气质量差,则打开空气净化器

UDS程序示例

package com.ablecloud.demo;

import com.ablecloud.common.*;
import com.ablecloud.service.AC;
import com.ablecloud.service.ACService;
import com.ablecloud.service.ACWeatherMgr;

import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Created by chenpeng on 15-1-17.
 */
public class DemoService extends ACService {
    private static final Logger logger = LoggerFactory.getLogger(DemoService.class);

    /**
     * 处理来自APP或其它service发来消息的入口函数
     *
     * @param req  请求消息
     * @param resp 响应消息
     * @throws Exception
     */
    public void handleMsg(ACMsg req, ACMsg resp) throws Exception {
        ACContext ctx = req.getContext();
        String name = req.getName();
        try {
            switch (name) {
                case "openAir":
                    handleOpenAirMsg(req, resp);
                    logger.info(String.format("[%s][%d][%s] success", ctx.getTraceId(), ctx.getUserId(), name));
                    resp.setAck();
                    break;
                default:
                    logger.warn("got an invalid request, method[" + name + "] is not implemented.");
                    resp.setErr(Errors.ERR_MSG_NOT_SUPPORTED.code, Errors.ERR_MSG_NOT_SUPPORTED.error);
                    break;
            }
        } catch(ACServiceException e) {
            logger.info(String.format("[%s][%d][%s] error: %d, %s", ctx.getTraceId(), ctx.getUserId(), name, e.getErrorCode(), e.getErrorMsg()));
            resp.setErr(e.getErrorCode(), e.getErrorMsg());
        }
    }

    private void handleOpenAirMsg(ACMsg req, ACMsg resp) throws Exception {
        ACContext ctx = req.getContext();
        ACObject params = req.get("params"); //获取用户自定义参数
        String area = params.get("area"); //获取区域
        long deviceId = params.get("deviceId"); //获取设备Id
        logger.info(String.format("handle open air msg: area[%s], deviceId[%d]", area, deviceId));
        ...
    }

    private void openAirDevice(long deviceId, int pm25) throws Exception {
        logger.info(String.format("open air device: deviceId[%d], pm25[%d]", deviceId, pm25));
    }

    public void handleDeviceMsg(ACDeviceReportInfo reportInfo, ACDeviceMsg req) throws Exception {
    }
}

APP端SDK接口调用示例

ACUserTask task = new ACUserTask();
//设置任务时间周期
task.setTimeCycle("day");
//设置任务执行的时间点,由于时间周期为天,所以只有19点精确到小时以后为有效参数,日期设置无效,即在每天19点执行任务
task.setTimePoint("2000-01-01 19:00:00");
//设置任务名称
task.setName("open-air-device");
//设置任务描述,选填
task.setDescription("open air device");

//具体发送到uds的消息指令,与sendToService参数类似,接口名及参数由UDS实际提供为主
ACMsg req = new ACMsg();
req.setName("openAir");
req.put("area", "北京");
req.put("deviceId", 1L);
//ACUserCommand参数分别为子域名,服务名,请求参数,默认发送到UDS最新版本号
ACUserCommand command = new ACUserCommand(subDomain, serviceName, req);
task.setUserCommand(command);
timerMgr.addTask(task, new PayloadCallback<ACUserTask>() {
     @Override
     public void success(ACUserTask task) {
         //成功添加定时任务,创建后默认为开启状态
     }

     @Override
     public void error(ACException e) {
         //网络错误或其他,根据e.getErrorCode()做不同的提示或处理,此处一般为参数类型错误,请仔细阅读注意事项
     }
});