关于RK芯片定时开关机周模式的梳理设定

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/fkgjdkblxckvbxbgb/article/details/83212923

这篇博客也是花费了好几天的工作量,从开始的毫无头绪到各种设定被打乱,直到现在的版本,主要是一种逻辑。

有时候我们程序员遇到问题会去死磕bug 。 这几天就完成了一个逻辑,一个比较复杂的业务逻辑。这个时候如果一步完成的话会比较复杂的话,其实有更多的选择,比如通过两个界面或者3个相同的界面,去处理不同的业务逻辑.

说远了,不扯了,先看一下后台设定的定时开关机效果图.

嗯,效果看上去比较简洁。但是获取的数据对于设备端来说是个噩梦. 系统的形式开关机需要我输入 年月日,时分秒,精确到秒,一眼看过去,全部都是以星期来定义的.,我是这样梳理的,

 1: 设备定时开关机是 年月日 时分秒

 2 :   服务器的数据的星期

 3:   获取当天的日期,星期,以今天为基准,吧数据库所有的数据增加年月日时分秒

 4:开机时间,关机时间分别分开存储

 5:获取目前的时间戳

 6:使用储存的数据比对,比现在大的,最小的一个就是下一个的关机时间

 7:比关机时间大的最小的一个就是开机时间.

 8:这样就可以获取下一次的开机时间和关机时间

 

详情请见代码,下面的就是筛选下一次的开关机时间

package com.etv.util.system;

import com.etv.entity.ScheduleRecord;
import com.etv.entity.TimeEntity;
import com.etv.util.MyLog;
import com.etv.util.SimpleDateUtil;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;


/***
 * 此类是用来获取定时开关机
 *的开机时间  和  关机时间
 *
 */
public class TSchedule {

    /****
     *  1:星期小于当天的星期
     *  2:算法   7-currentWeek+weekList
     *
     *  2:如果星期大于当天
     *  2:算法 weekList-current
     *
     *  给所有的定时开关机设定完整的日期,直接对完整的日期进行排序分组
     *
     *
     */
    public static List<ScheduleRecord> initTimerTask(List<ScheduleRecord> listSch, boolean isOnOff) {
        Calendar calendar = Calendar.getInstance();
        int currentDayWeek = calendar.get(Calendar.DAY_OF_WEEK);  //今天的工作星期
        Long currentTimeMillis = System.currentTimeMillis();
        long simpleTime = SimpleDateUtil.formatBig(currentTimeMillis);
        //以今天基准,遍历所有日期,补全所有的时间数据
        TimeEntity entity = SimpleDateUtil.getFormatLongTime(simpleTime);
        for (int i = 0; i < listSch.size(); i++) {

            ScheduleRecord scheduleRecord = listSch.get(i);
            int weekList = scheduleRecord.getDayOfWeek();
            int distanceDay = 0;
            int year = entity.getYear();
            int month = entity.getMonth();
            int day = entity.getDay();

            if (weekList < currentDayWeek) {
                distanceDay = 7 - currentDayWeek + weekList;
            } else {  //星期属大于今天
                distanceDay = weekList - currentDayWeek;
            }
            day = day + distanceDay;
            if (month == 12 && day > 31) {  //跨年了
                year = year + 1;
            }
            if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) {
                if (day > 31) {
                    day = day - 31;
                    month = month + 1;
                }
            } else if (month == 4 || month == 6 || month == 9 || month == 11) {
                if (day > 30) {
                    day = day - 30;
                    month = month + 1;
                }
            } else if (month == 2) {
                if (year % 4 == 0 && year % 100 != 0) {//闰年
                    if (day > 29) {
                        day = day - 29;
                        month = month + 1;
                    }
                } else if (year % 400 == 0) { //闰年
                    if (day > 29) {
                        day = day - 29;
                        month = month + 1;
                    }
                } else {  //平年
                    if (day > 28) {
                        day = day - 28;
                        month = month + 1;
                    }
                }
            }
            scheduleRecord.setYear(year);
            scheduleRecord.setMonth(month);
            scheduleRecord.setDay(day);
        }
        return matchPowerTime(listSch, isOnOff);
    }

    private static List<ScheduleRecord> matchPowerTime(List<ScheduleRecord> listSch, boolean isOnOff) {
        List<ScheduleRecord> listPowerOn = new ArrayList<ScheduleRecord>();
        List<ScheduleRecord> listPowerOff = new ArrayList<ScheduleRecord>();
        for (int k = 0; k < listSch.size(); k++) {
            ScheduleRecord schduleRecorder = listSch.get(k);
            int year = schduleRecorder.getYear();
            int month = schduleRecorder.getMonth();
            int day = schduleRecorder.getDay();
            int powerOnTHour = schduleRecorder.getPowerHour();
            int powerOnMin = schduleRecorder.getPowerMinute();

            int powerOffTHour = schduleRecorder.getCloseHour();
            int powerOffMin = schduleRecorder.getCloseMinute();
            String firstHour = "";
            String firstMin = "";
            if (powerOnTHour < 10) {
                firstHour = "0";
            }
            if (powerOnMin < 10) {
                firstMin = "0";
            }
            String endHour = "";
            String endMin = "";
            if (powerOffTHour < 10) {
                endHour = "0";
            }
            if (powerOffMin < 10) {
                endMin = "0";
            }
            String onTime = year + "" + month + "" + day + "" + firstHour + powerOnTHour + firstMin + powerOnMin + "00";
            String offTime = year + "" + month + "" + day + "" + endHour + powerOffTHour + endMin + powerOffMin + "00";
            listPowerOn.add(new ScheduleRecord(year, month, day, schduleRecorder.getDayOfWeek(), schduleRecorder.getPowerHour(), schduleRecorder.getPowerMinute(), 0, 0, ""));
            listPowerOff.add(new ScheduleRecord(year, month, day, schduleRecorder.getDayOfWeek(), 0, 0, schduleRecorder.getCloseHour(), schduleRecorder.getCloseMinute(), ""));
            MyLog.powerOnOff("===========封装的开机的时间===" + onTime);
            MyLog.powerOnOff("===========封装的关机的时间===" + offTime);
        }
        if (isOnOff) {
            return listPowerOn;
        } else {
            return listPowerOff;
        }
    }


    /***
     * 获取最早的开关机时间
     * @param lists
     */
    public static long getLastPowerOffTime(List<ScheduleRecord> lists) {
        long backTime = -1;
        List<ScheduleRecord> listPowerOff = initTimerTask(lists, false);
        if (listPowerOff.size() < 1) {
            return backTime;
        }
        long[] powerOffArr = new long[listPowerOff.size()];
        for (int i = 0; i < listPowerOff.size(); i++) {
            ScheduleRecord mScheduleRecord = listPowerOff.get(i);
            long timeToLong = formatTimePowerOn(mScheduleRecord, false);
            powerOffArr[i] = timeToLong;
        }
        powerOffArr = Bubblesort(powerOffArr);
        long currentTime = SimpleDateUtil.formatBig(System.currentTimeMillis());
        for (long number : powerOffArr) {
            MyLog.powerOnOff("====遍历纯属的时间==" + number + "   /   " + currentTime);
            if (number > currentTime) {
                backTime = number;
                MyLog.powerOnOff("====000遍历纯属的时间==" + number + "   /   " + currentTime);
                break;
            }
        }
        MyLog.powerOnOff("====排序后获取的关机==" + backTime);
        return backTime;
    }

    public static long getLastPowerOnTime(List<ScheduleRecord> lists, long powerOffTime) {
        long backTime = -1;
        List<ScheduleRecord> listPowerOn = initTimerTask(lists, true);
        if (listPowerOn.size() < 1) {
            return backTime;
        }
        long[] powerOnArr = new long[listPowerOn.size()];
        for (int i = 0; i < listPowerOn.size(); i++) {
            ScheduleRecord mScheduleRecord = listPowerOn.get(i);
            long timeToLong = formatTimePowerOn(mScheduleRecord, true);
            powerOnArr[i] = timeToLong;
        }
        powerOnArr = Bubblesort(powerOnArr);
        for (long number : powerOnArr) {
            if (number > powerOffTime) {
                backTime = number;
                break;
            }
        }
        MyLog.powerOnOff("====排序后获取的开机时间==" + backTime);
        return backTime;
    }

    /***
     * 冒泡算法排序
     * @param arr
     * @return
     */
    public static long[] Bubblesort(long[] arr) {
        for (int i = 0; i < arr.length - 1; i++) {
            for (int j = 0; j < arr.length - 1 - i; j++) {
                if (arr[j] > arr[j + 1]) {
                    long temp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = temp;
                }
            }
        }
        return arr;
    }


    /***
     *
     * @param schduleRecorder
     * @param on
     * true 解析开机列表
     * false 解析关机列表
     * @return
     */
    public static long formatTimePowerOn(ScheduleRecord schduleRecorder, boolean on) {
        int year = schduleRecorder.getYear();
        int month = schduleRecorder.getMonth();
        int day = schduleRecorder.getDay();
        String firstHour = "";
        String firstMin = "";
        String onTime = "";
        if (on) {  //开机时间
            int powerOnTHour = schduleRecorder.getPowerHour();
            int powerOnMin = schduleRecorder.getPowerMinute();
            if (powerOnTHour < 10) {
                firstHour = "0";
            }
            if (powerOnMin < 10) {
                firstMin = "0";
            }
            onTime = year + "" + month + "" + day + "" + firstHour + powerOnTHour + firstMin + powerOnMin + "00";
        } else {
            int powerOffTHour = schduleRecorder.getCloseHour();
            int powerOffMin = schduleRecorder.getCloseMinute();
            String endHour = "";
            String endMin = "";
            if (powerOffTHour < 10) {
                endHour = "0";
            }
            if (powerOffMin < 10) {
                endMin = "0";
            }
            onTime = year + "" + month + "" + day + "" + endHour + powerOffTHour + endMin + powerOffMin + "00";
        }
        long timeBack = Long.parseLong(onTime);
        return timeBack;
    }
}

 

这个类就是获取定时开关机时间,和设定开关机时间

package com.etv.util.system;

import android.content.Context;
import android.content.Intent;

import com.etv.entity.ScheduleRecord;
import com.etv.entity.TimeEntity;
import com.etv.entity.TimedTaskListEntity;
import com.etv.util.MyLog;
import com.etv.util.SimpleDateUtil;

import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.TimeZone;

import startest.ys.com.poweronoff.PowerOnOffManager;

/**
 * 定时开关机管理类
 */
public class PowerOnOffUtil {

    Context context;
    private Calendar c;
    PowerOnOffManager manager;

    public PowerOnOffUtil(Context context) {
        this.context = context;
        c = Calendar.getInstance();
        manager = PowerOnOffManager.getInstance(context);
        c.setTimeZone(TimeZone.getTimeZone("GMT+8:00"));
    }

    /**
     * 设置系统定时开关机
     * 亿晟定时开关机规律
     * 1:分组智能设定一组
     * 2:采取获取当前最近的一组,设定,
     * 3:下次开机重新获取离下次最近的一组
     *
     * @param lists
     */
    public void setPowerOnOffTime(List<TimedTaskListEntity> lists) {
        listPowerOnOff.clear();
        if (lists.size() < 1) {
            //===清理定时开关机====
            clearPowerOnOffTime0();
            MyLog.powerOnOff("获取的定时开关机 < 1 ,用户删除定时开关机任务,终止操作");
            return;
        }
        for (int i = 0; i < lists.size(); i++) {
            int dayOfWeek = -1;
            TimedTaskListEntity entity = lists.get(i);
            String powerOnTime = entity.getTtOnTime();
            String powerOffTime = entity.getTtOffTime();
            int onHour = Integer.parseInt(powerOnTime.substring(0, powerOnTime.indexOf(":")));
            int onMin = Integer.parseInt(powerOnTime.substring(powerOnTime.indexOf(":") + 1, powerOnTime.length()));
            int offHour = Integer.parseInt(powerOffTime.substring(0, powerOffTime.indexOf(":")));
            int offMin = Integer.parseInt(powerOffTime.substring(powerOffTime.indexOf(":") + 1, powerOffTime.length()));
//            MyLog.powerOnOff("=====获取的开关机机小时==" + onHour + " / " + onMin + "  /关机时间==" + offHour + " / " + offMin);
            boolean ttMon = Boolean.parseBoolean(entity.getTtMon());
            if (ttMon) {
                dayOfWeek = 2;
                saveTimerTask(dayOfWeek, onHour, onMin, offHour, offMin);
            }
            boolean ttTue = Boolean.parseBoolean(entity.getTtTue());
            if (ttTue) {
                dayOfWeek = 3;
                saveTimerTask(dayOfWeek, onHour, onMin, offHour, offMin);
            }
            boolean ttWed = Boolean.parseBoolean(entity.getTtWed());
            if (ttWed) {
                dayOfWeek = 4;
                saveTimerTask(dayOfWeek, onHour, onMin, offHour, offMin);
            }
            boolean ttThu = Boolean.parseBoolean(entity.getTtThu());
            if (ttThu) {
                dayOfWeek = 5;
                saveTimerTask(dayOfWeek, onHour, onMin, offHour, offMin);
            }
            boolean ttFri = Boolean.parseBoolean(entity.getTtFri());
            if (ttFri) {
                dayOfWeek = 6;
                saveTimerTask(dayOfWeek, onHour, onMin, offHour, offMin);
            }
            boolean ttSat = Boolean.parseBoolean(entity.getTtSat());
            if (ttSat) {
                dayOfWeek = 7;
                saveTimerTask(dayOfWeek, onHour, onMin, offHour, offMin);
            }
            boolean ttSun = Boolean.parseBoolean(entity.getTtSun());
            if (ttSun) {
                dayOfWeek = 1;
                saveTimerTask(dayOfWeek, onHour, onMin, offHour, offMin);
            }
        }
        long powerOffTime = TSchedule.getLastPowerOffTime(listPowerOnOff);
        MyLog.powerOnOff("===获取的关机时间===" + powerOffTime);
        long powerOnTime = TSchedule.getLastPowerOnTime(listPowerOnOff, powerOffTime);
        MyLog.powerOnOff("===获取的开机时间===" + powerOnTime);
        if (powerOffTime < 0 || powerOnTime < 0) {   //这里可能获取时间异常了 ,不设置定时开关机
            MyLog.powerOnOff("===获取的开关机时间异常了,这里清理定时开关机");
            clearPowerOnOffTime0();
            return;
        }
        TimeEntity entityOff = SimpleDateUtil.getFormatLongTime(powerOffTime);
        TimeEntity entityOn = SimpleDateUtil.getFormatLongTime(powerOnTime);
        MyLog.powerOnOff("===设定的开机时间===" + entityOn.toString());
        MyLog.powerOnOff("===设定的关机时间===" + entityOff.toString());
        setPowerOnOffTime(entityOn, entityOff);
    }

    /***
     * 设置定时开关机时间方法d
     * @param entityOn
     * @param entityOff
     */
    private void setPowerOnOffTime(TimeEntity entityOn, TimeEntity entityOff) {
        int[] timeoffArray = new int[]{entityOff.getYear(), entityOff.getMonth(), entityOff.getDay(), entityOff.getHour(), entityOff.getMinite()};
        int[] timeonArray = new int[]{entityOn.getYear(), entityOn.getMonth(), entityOn.getDay(), entityOn.getHour(), entityOn.getMinite()};
        PowerOnOffManager powerManager = PowerOnOffManager.getInstance(context);
        powerManager.clearPowerOnOffTime();
        powerManager.setPowerOnOff(timeonArray, timeoffArray);
    }

    List<ScheduleRecord> listPowerOnOff = new ArrayList<ScheduleRecord>();

    private void saveTimerTask(int dayOfWeek, int onHour, int onMin, int offHour, int offMin) {
        ScheduleRecord scheduleRecord = new ScheduleRecord(dayOfWeek, onHour, onMin, offHour, offMin);
//        MyLog.powerOnOff("===设定的定时开关机时间列表==" + scheduleRecord.toString());
        listPowerOnOff.add(scheduleRecord);
    }

    /***=========================================================================================================
     * 关机
     * @param context
     */
    public void setShowDownMethion(Context context) {
        try {
            PowerOnOffManager powerOnOffManager = PowerOnOffManager.getInstance(context);
            powerOnOffManager.shutdown();
        } catch (Exception e) {
        }
    }

    /***
     * 清理定时开关机任务
     */
    public void clearPowerOnOffTime0() {
        try {
            MyLog.powerOnOff("准备清理定时开关机");
            manager.clearPowerOnOffTime();
            MyLog.powerOnOff("清理定时开关机 success");
        } catch (Exception e) {
            MyLog.powerOnOff("清理定时开关机error =" + e.toString());
        }
    }

    /***
     * 获取定时开机时间
     * @return
     */
    public static String getPowerOnTime(Context context) {
        PowerOnOffManager powerOnOffManager = PowerOnOffManager.getInstance(context);
        String time = powerOnOffManager.getPowerOnTime();
        return time;
    }

    /***
     * 获取关机的时间
     * @param context
     * @return
     */
    public static String getPowerOffTime(Context context) {
        PowerOnOffManager powerOnOffManager = PowerOnOffManager.getInstance(context);
        String time = powerOnOffManager.getPowerOffTime();
        return time;
    }

    /***
     * 获取定时开关机版本号
     * @return
     */
    public String getVersion(Context context) {
        String version = manager.getVersion();
        return version;
    }

    //==================背光相关操作=================================================================

    /***
     * 判断背光是否打开
     * @return
     */
    public static String isOpenLight() {
        String str = "";
        try {
            File file = new File("/sys/devices/fb.11/graphics/fb0/pwr_bl");
            FileReader reader = new FileReader(file);
            char[] bb = new char[1024];
            int n;
            while ((n = reader.read(bb)) != -1) {
                str += new String(bb, 0, n);
            }
            reader.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return str;
    }

    /***
     * 开关背光操作
     * @param paramInt
     */
    public static void openOrClose(int paramInt) {
        try {
            writeFile(paramInt);
            return;
        } catch (Exception localException) {
            localException.printStackTrace();
        }
    }

    public static void writeFile(int paramInt) {
        try {
            File localFile = new File("/sys/devices/fb.11/graphics/fb0/pwr_bl");
            localFile.setExecutable(true);
            localFile.setReadable(true);
            localFile.setWritable(true);
            if (paramInt == 0) {
                do_exec("busybox echo 0 > /sys/devices/fb.11/graphics/fb0/pwr_bl");
            } else if (paramInt == 1) {
                do_exec("busybox echo 1 > /sys/devices/fb.11/graphics/fb0/pwr_bl");
            }
        } catch (Exception localException) {
            localException.printStackTrace();
        }
    }

    public static void do_exec(String paramString) {
        try {
            Process localProcess = Runtime.getRuntime().exec("su");
            String str = paramString + "\nexit\n";
            localProcess.getOutputStream().write(str.getBytes());
            if (localProcess.waitFor() != 0) {
                System.out.println("cmd=" + paramString + " error!");
                throw new SecurityException();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}

 

封装时间的实体类

package com.etv.entity;

/**
 * 用来封装详细的数据的事件
 */

public class TimeEntity {

    int year;
    int month;
    int day;
    int hour;
    int minite;

    public TimeEntity() {
    }

    public TimeEntity(int year, int month, int day, int hour, int minite) {
        this.year = year;
        this.month = month;
        this.day = day;
        this.hour = hour;
        this.minite = minite;
    }

    public int getYear() {
        return year;
    }

    public void setYear(int year) {
        this.year = year;
    }

    public int getMonth() {
        return month;
    }

    public void setMonth(int month) {
        this.month = month;
    }

    public int getDay() {
        return day;
    }

    public void setDay(int day) {
        this.day = day;
    }

    public int getHour() {
        return hour;
    }

    public void setHour(int hour) {
        this.hour = hour;
    }

    public int getMinite() {
        return minite;
    }

    public void setMinite(int minite) {
        this.minite = minite;
    }

    @Override
    public String toString() {
        return "TimeEntity{" +
                "year=" + year +
                ", month=" + month +
                ", day=" + day +
                ", hour=" + hour +
                ", minite=" + minite +
                '}';
    }
}

 

 

展开阅读全文

没有更多推荐了,返回首页