whyclj
2019-10-21 aecc5aa11fd2cbdea193db53adfc7c7920f43b26
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
package io.hybird.jsbridge;
 
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Handler;
import android.os.Looper;
import android.os.SystemClock;
import android.util.Log;
import android.webkit.JavascriptInterface;
import android.webkit.WebView;
import android.widget.Toast;
 
import com.google.gson.Gson;
import com.mode.ServiceModel;
import com.service.MyInteractionService;
 
import java.util.List;
 
public class JsInterface {
    public static final String TAG = "JsInterface";
 
    public static final String JS_INTERFACE_NAME = "JSInterface";//JS调用类名
    private Context mContext;
    private WebView webView;
    private Gson gson = new Gson();
    private MyInteractionService service = null;
    private Activity activity;
 
    public JsInterface(Context context, WebView webView, Activity activity) {
        this.mContext = context;
        this.webView = webView;
        this.activity = activity;
        String server_ip = readServerIp();
        //server_ip = "192.168.7.130";
        service = new MyInteractionService(server_ip,this);
 
 
    }
 
    @JavascriptInterface
    public void hello(String content) {
        Log.i("bqt", "JS 调用原生时是否发生在主线程:" + (Looper.myLooper() == Looper.getMainLooper()));//false
        new Handler(Looper.getMainLooper()).post(() -> //WebView等UI操作必须在主线程中进行
                Toast.makeText(mContext, "原生的hello方法被调用了:" + content, Toast.LENGTH_SHORT).show());
 
        SystemClock.sleep(3000);//模拟耗时操作
 
        String call = "javascript:javacalljs(" + System.currentTimeMillis() + ")";//格式很重要,大部分错误都是由于格式问题导致的
        new Handler(Looper.getMainLooper()).post(() -> webView.loadUrl(call));//WebView等UI操作必须在主线程中进行
    }
 
    //获取当前设备IP
    @JavascriptInterface
    public void obtainDeviceIp(){
        ServiceModel model = new ServiceModel();
        String server_ip = readServerIp();
        if(server_ip != null && server_ip.length()>0){
            model.code = 1;
            model.data = server_ip;
        }
        SendCallDataToJS("obtainDeviceIp",model);
    }
 
    //设置当前设备IP
    @JavascriptInterface
    public void setUpDeviceIp(String deviceIp){
        writeServerIp(deviceIp);
        ServiceModel model = new ServiceModel();
        boolean flag = service.setUpDeviceIp(deviceIp);
        if(flag){
            model.code = 1;
        }
        SendCallDataToJS("setUpDeviceIp",model);
    }
 
    //测试连接
    @JavascriptInterface
    public void testDeviceIp(String deviceIp){
        // Android 4.0 之后不能在主线程中请求HTTP请求
        new Thread(new Runnable(){
            @Override
            public void run() {
                ServiceModel model = new ServiceModel();
                boolean flag = service.testDeviceIp(deviceIp);
                if(flag){
                    model.code = 1;
                }
 
                SendCallDataToJS("testDeviceIp",model);
            }
        }).start();
 
    }
 
    //获取电池组列表
    @JavascriptInterface
    public void  getBattNames(){
        // Android 4.0 之后不能在主线程中请求HTTP请求
        new Thread(new Runnable(){
            @Override
            public void run() {
                Log.e(TAG, "run: ###########################" );
                ServiceModel model = new ServiceModel();
                List list = service.getBattNames();
                if(list != null && list.size()>0){
                    model.code =1;
                    model.data = list;
                }
                Log.e(TAG, "run: ###########################" );
                SendCallDataToJS("getBattNames",model);
            }
        }).start();
    }
 
 
    //获取测试参数
    @JavascriptInterface
    public boolean getTestParam(){
        return service.getTestParam();
    }
 
    //新建电池组
    @JavascriptInterface
    public boolean newBatt(String battName){
        return service.newBatt(battName);
    }
 
    //切换电池组
    @JavascriptInterface
    public boolean changeBatt(String battName){
        return service.changeBatt(battName);
    }
 
    //删除电池组
    @JavascriptInterface
    public boolean delectBatt(String battName){
        return service.delectBatt(battName);
    }
 
    //获取单体实时数据
    @JavascriptInterface
    public boolean getBattRTData(){
        return service.getBattRTData();
    }
 
    //设置电池测试参数
    @JavascriptInterface
    public boolean setTestParam(String param){
        return service.setTestParam(param);
    }
 
    //启动放电
    @JavascriptInterface
    public boolean startDischarge(){
        return service.startDischarge();
    }
 
    //暂停放电
    @JavascriptInterface
    public boolean pauseDischarge(){
        return service.pauseDischarge();
    }
 
    //停止放电
    @JavascriptInterface
    public boolean stopDischarge(){
        return service.stopDischarge();
    }
 
    //获取放电数据
    @JavascriptInterface
    public boolean getTestData(){
        return service.getTestData();
    }
 
    //启动充电测试
    @JavascriptInterface
    public boolean startCharge(){
        return service.startCharge();
    }
 
    //暂停充电测试
    @JavascriptInterface
    public boolean pauseCharge(){
        return service.pauseCharge();
    }
 
    //停止充电测试
    @JavascriptInterface
    public boolean stopCharge(){
        return service.stopCharge();
    }
 
    //获取充电数据
    @JavascriptInterface
    public boolean getChargeData(){
        return service.getChargeData();
    }
 
    //获取当前选中的电池组以及当前的状态
    @JavascriptInterface
    public boolean getNowWorkBatt(){
        return service.getNowWorkBatt();
    }
 
    //清除所有的告警
    @JavascriptInterface
    public boolean clearAllAlarm(){
        return service.clearAllAlarm();
    }
 
    //获取设备ip
    public String readServerIp(){
        String server_ip = "127.0.0.1";
        if(mContext != null){
            SharedPreferences pref    = mContext.getSharedPreferences("data",    Context.MODE_PRIVATE);
            server_ip = pref.getString("server_ip","");
        }
        return  server_ip;
    }
 
    //保存设备ip
    public boolean writeServerIp(String server_ip){
        SharedPreferences.Editor editor    = mContext.getSharedPreferences("data",Context.MODE_PRIVATE).edit();
        editor.putString("server_ip",server_ip);
        editor.apply();
        return true;
    }
 
 
 
    /**
     *  返回数据给前台js
     * @param funName
     * @param model
     */
    public void SendCallDataToJS(String funName,ServiceModel model){
        String call = "javascript:"+funName+"calljs(" + gson.toJson(model) + ")";
        new Handler(Looper.getMainLooper()).post(() -> webView.loadUrl(call));
    }
 
}