Blame view

src/com/ectrip/cyt/service/FtpParamerService.java 5.39 KB
07670a44   黄灿宏   畅游通核销app: 1.增加日志上...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
  package com.ectrip.cyt.service;
  
  import android.annotation.SuppressLint;
  import android.app.Service;
  import android.content.Intent;
  import android.os.Handler;
  import android.os.IBinder;
  import android.os.Message;
  //import android.support.annotation.Nullable;
  import android.text.TextUtils;
  
  import com.ectrip.cyt.bean.GetFtp;
  import com.ectrip.cyt.bean.ResultBean;
  import com.ectrip.cyt.config.DevicTool;
  import com.ectrip.cyt.constant.CallBackTypeConstant;
  import com.ectrip.cyt.constant.CheckTicketConstant;
  import com.ectrip.cyt.constant.ParamContants;
  import com.ectrip.cyt.utils.LogUtil;
  import com.ectrip.cyt.utils.PreferenceUtils;
  import com.ectrip.trips.net.BasePostProtocol;
  import com.ectrip.trips.net.INetCallBack;
  import com.ectrip.trips.net.ModelTools;
  
  
  public class FtpParamerService extends Service {
  
      private String TAG = "FtpParamerService";
      private String myId;
      private String accid;
      private String mac;
      private String url ;
  
  
  //    private final int MSG_FTP_TIME = 1;
      private final int MSG_FTP_3721 = 2;
  //    private final int MSG_FTP_8650 = 3;
  //    private final int MSG_FTP_8612 = 4;
  //    private final int MSG_FTP_SLCK = 5;
  //    private final int MSG_FTP_ONE = 6;
  
      @SuppressLint("HandlerLeak")
      private Handler mHandle = new Handler() {
          @Override
          public void handleMessage(Message msg) {
              super.handleMessage(msg);
              switch (msg.what) {
                  case MSG_FTP_3721:
                      initFtp3721();
                      break;
              }
          }
      };
  
      @Override
      public IBinder onBind(Intent intent) {
  
          return null;
      }
  
      @Override
      public void onCreate() {
          super.onCreate();
      }
  
      @Override
      public int onStartCommand(Intent intent, int flags, int startId) {
          initData();
          mHandle.sendEmptyMessageDelayed(MSG_FTP_3721, 2500);
  
  
  //        AlarmManager manager = (AlarmManager) getSystemService(ALARM_SERVICE);
  //        int anHour = 5 * 60 * 1000;
  //        long triggerAtTime = SystemClock.elapsedRealtime() + anHour;
  //        Intent intent1 = new Intent(this, AlarmReceiver.class);
  //        intent1.setAction(AlarmReceiver.ACTIION_CHECKDEV);
  //        PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent1, PendingIntent.FLAG_UPDATE_CURRENT);
  //        manager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, triggerAtTime, pendingIntent);
  
          return super.onStartCommand(intent, flags, startId);
      }
  
  
      private void initData() {
          mac = PreferenceUtils.getString(this, ParamContants.SET_mac, "");
          if(TextUtils.isEmpty(mac)){
              mac = DevicTool.getInstance().getDevId();
              PreferenceUtils.putString(this, ParamContants.SET_mac, mac);
          }
          String ip = PreferenceUtils.getString(this, ParamContants.SET_ip, "");
          if (TextUtils.isEmpty(ip)) {
              return;
          }
          url = CheckTicketConstant.httphead + ip + CheckTicketConstant.action;
      }
  
  
  
      /**
       * 获取服务器参数,用于日志上传
       */
      private void initFtp3721() {
          GetFtp getFtp = new GetFtp();
          getFtp.setAccid("3721");  //闸机ID
          getFtp.setMac(mac);
          new BasePostProtocol().loadMoreData(url, ModelTools.getParam(getFtp, CheckTicketConstant.GETFTP), new MainNetCallBack(CallBackTypeConstant.GETFEPPRARMETER));
      }
  
  
  
  
      /**
       * 网络请求的回调
       */
      class MainNetCallBack implements INetCallBack {
          int type;
  
          public MainNetCallBack(int type) {
              this.type = type;
          }
  
          @Override
          public void onFailure(String error) {
  
              switch (type) {
                  case CallBackTypeConstant.GETFEPPRARMETER:
                      LogUtil.d(TAG, "【获取ftp参数失败 == 3721】");
                      break;
                  default:
                      LogUtil.d(TAG, error);
                      break;
              }
          }
  
          @Override
          public void onSuccessed(String result) {
              LogUtil.d("HTTP", "【返回结果】" + result);
              switch (type) {
                  case CallBackTypeConstant.GETFEPPRARMETER:
                      getFtpParamer(result);
                      break;
                  default:
                      break;
              }
          }
  
  
          @Override
          public void onPrepare() {
          }
      }
  
  
  
      private void getFtpParamer(String result) {
          try {
              ResultBean bean = ModelTools.parseRequest(result, ResultBean.class);
              String host = bean.getResult(0, 0);
              String port = bean.getResult(0, 1);
              String username = bean.getResult(0, 2);
              String password = bean.getResult(0, 3);
              if (!TextUtils.isEmpty(host) && !TextUtils.isEmpty(username) && !TextUtils.isEmpty(password) && host.length() > 1 && username.length() > 1 && password.length() > 1) {
                  LogUtil.i(TAG, "【获取ftp参数正确】");
                  PreferenceUtils.putString(this,ParamContants.SET_FPT_HOST, host);
                  PreferenceUtils.putString(this,ParamContants.SET_FPT_USETR, username);
                  PreferenceUtils.putString(this,ParamContants.SET_FPT_PASSWORD, password);
                  PreferenceUtils.putString(this,ParamContants.SET_FPT_PORT, port);
              } else {
                  LogUtil.e(TAG, "【获取ftp参数失败!】");
              }
          } catch (Exception e) {
              LogUtil.e(TAG, LogUtil.getExMsg(e));
          }
  
      }
  
  
  }