2018-03-30

package com.jokey.wisdomaldetection.service;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;

import com.jokey.wisdomaldetection.utils.IConstant;
import com.jokey.wisdomaldetection.utils.LogUtil;
import com.jokey.wisdomaldetection.utils.TimeUtil;
import com.jokey.wisdomaldetection.utils.ToastUtil;

import android_serialport_api.SerialPortManager;


/**
 * Created by Administrator on 2018/1/16.
 */

public class MyService extends Service {

    private MyTimeThread myTimeThread;
    private ReceiveDataThread receiveDataThread;
    private byte[] buffer;
    private static final String open = "D&C0004010D";
    private static final String close = "D&C0004010E";


    @Override
    public void onCreate() {
        showTime();
        if (!SerialPortManager.getInstance().openSerialPort()) {
            ToastUtil.showToast(this, "OpenSerialPort--Failed!");
        } else {
            ToastUtil.showToast(this, "OpenSerialPort--Success!");
            startReceive();
        }
        super.onCreate();
    }


    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        return super.onStartCommand(intent, flags, startId);
    }


    @Override
    public void onDestroy() {
        stopTime();
        stopReceive();
        SerialPortManager.getInstance().write(close.getBytes());
        SerialPortManager.getInstance().closeSerialPort();
        super.onDestroy();
    }


    private void showTime() {
        if (myTimeThread == null) {
            myTimeThread = new MyTimeThread();
            myTimeThread.setContinue(true);
            myTimeThread.start();
        }
    }

    private void stopTime() {
        if (myTimeThread != null) {
            myTimeThread.setContinue(false);
            myTimeThread = null;
        }
    }

    private class MyTimeThread extends Thread {
        private boolean isContinue;

        public void setContinue(boolean aContinue) {
            isContinue = aContinue;
        }

        @Override
        public void run() {
            while (isContinue) {
                try {
                    Thread.sleep(999);
                    Intent intent = new Intent();
                    intent.setAction(IConstant.ACTION_TIME);
                    String time = TimeUtil.getFormatted(IConstant.TIME_FORMAT, System.currentTimeMillis());
                    intent.putExtra(IConstant.TIME_NOW, time);
                    sendBroadcast(intent);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

        }
    }


    private void startReceive() {
        if (receiveDataThread == null) {
            receiveDataThread = new ReceiveDataThread();
            receiveDataThread.setContinue(true);
            receiveDataThread.start();
        }
    }

    private void stopReceive() {
        if (receiveDataThread != null) {
            receiveDataThread.setContinue(false);
            receiveDataThread = null;
        }
    }

    private class ReceiveDataThread extends Thread {
        private boolean Continue;

        public void setContinue(boolean aContinue) {
            Continue = aContinue;
        }

        @Override
        public void run() {

            SerialPortManager.getInstance().write(open.getBytes());

            while (Continue) {
                //Log.i("jokeyservices","读取数据!!!!!!!!!");

                buffer = new byte[256];

                int length = SerialPortManager.getInstance().read(buffer, 100, 50);


                Log.i("jokeyservices", "length=" + length + "----buffer[1]=" + buffer[1]);

                if (length == 3 && buffer[1] != 0x4f) {

                    /*synchronized (this){
                        isReceiveData=true;
                    }*/

                    Log.i("jokeyservices", "串口数据读取成功,处理中....");

                    float vol = ((buffer[1] & 0xff) * 256 + (buffer[2] & 0xff)) / 10f;

                    float voltage = (Math.round((vol) * 10f)) / 10f;

                    LogUtil.d("jokeyservices", "vol: " + vol + "----voltage: " + voltage);


                    if (vol == 0) {
                        Intent intent = new Intent();
                        intent.setAction(IConstant.ACTION_VOLTAGE_DATA_ISZERO);
                        intent.putExtra(IConstant.VOLTAGE_VALUE_ISZERO, voltage);
                        sendBroadcast(intent);
                    }

                    Intent intent = new Intent();
                    intent.setAction(IConstant.ACTION_VOLTAGE_DATA);
                    intent.putExtra(IConstant.VOLTAGE_VALUE, voltage);
                    sendBroadcast(intent);


                } else {

                /*    synchronized (this){
                        isReceiveData=false;

                    }*/
                }
                SerialPortManager.getInstance().clearBuffer();
            }

        }
    }


    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
}

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 夫妻间的事,请自己解决 不要让你的孩子被迫加入仲裁 无意中 成为“迦南的父亲含” 不要一边要求他必须原谅 一边希望...
    苏小花_阅读 124评论 1 0
  • 羡慕那些与全人类为敌的独居者或者是流浪汉,死掉了以后周围人会一边拍手叫好一边唾弃你生前做过的各类没出息的事情。周围...
    吃错药的小黑狗阅读 151评论 0 0
  • 《唤醒内在天才的秘密》 作者:李胜杰、林青贤 营养早餐(20180224) ——《唤醒内在天才的秘密》摘录: (4...
    觉之灯阅读 99评论 0 0
  • 学校永驻生流水, 师生三载意未尽, 尚有太多未学...
    秋波送给你阅读 622评论 0 2
  • 1.那时候,我一点也不善解人意!我本应该根据行为,而不是根据话语来评价花儿。花儿给我的生活带来了芳香,带来了光彩。...
    说谎的猫阅读 414评论 0 0