陈力
2023-06-13 eea903f2ddbc26f66b9d1b53219ecf69c9568a18
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
package com.lotaai.canguiayw;
 
import androidx.appcompat.app.AppCompatActivity;
import androidx.arch.core.executor.DefaultTaskExecutor;
 
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Build.VERSION;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
 
import com.alibaba.fastjson.JSONObject;
import com.blankj.utilcode.util.CacheDiskUtils;
import com.blankj.utilcode.util.DeviceUtils;
import com.blankj.utilcode.util.LogUtils;
import com.lotaai.canguiayw.common.HttpLoggerInterceptor;
import com.lotaai.canguiayw.common.HttpUrlDefine;
import com.lotaai.canguiayw.common.SettingConfig;
import com.lotaai.canguiayw.device.service.CanGuiService;
import com.lotaai.canguiayw.mqtt.MyMQTTService;
import com.pranavpandey.android.dynamic.toasts.DynamicToast;
 
import java.io.IOException;
import java.util.concurrent.TimeUnit;
 
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
 
public class MainActivity extends AppCompatActivity {
    private Context context;
    private ImageView logoView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        LogUtils.i("VERSION.SDK_INT:" +VERSION.SDK_INT);
        logoView = findViewById(R.id.logo);
        context = MainActivity.this;
        Intent startIntent = new Intent(context, CanGuiService.class);
        startService(startIntent);
        Intent startMqttIntent = new Intent(context, MyMQTTService.class);
        startService(startMqttIntent);
 
        login();
 
        logoView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {
                Intent intent = new Intent();
                intent.setClass(MainActivity.this, ManageActivity.class);
                startActivity(intent);
                return true;
            }
        });
//        testData();
    }
 
//    public void  testData(){
//        Random random = new Random();
//        int rt= random.nextInt(10000-1000+1)  + 1000;
//        Order order = new Order();
//        order.setOrderNo("GB"+ rt);
//        order.setGridNo("1");
//        order.setTakeCode("526895");
//        order.setState(0);
//        Date time = new Date();
//        order.setPutInDate(TimeUtils.date2String(time,"yyyy-MM-dd HH:mm:ss"));
//        SqlliteDbManage.getInstance().insertOrderInfo(order);
//        for (int p = 0; p < 3;p ++){
//            OrderDetail detail = new OrderDetail();
//            detail.setItemName("项目"+p);
//            detail.setNum(p);
//            detail.setOrderNo(order.getOrderNo());
//            SqlliteDbManage.getInstance().insertOrUpdateOrderDetailInfo(detail);
//        }
//        LogUtils.i("插入订单数据:" + order.getOrderNo());
//    }
 
    public void gotoSetting(View view){
//        Intent intent = new Intent();
//        intent.setClass(MainActivity.this, AdminLoginActivity.class);
//        startActivity(intent);
    }
 
    public void take(View view){
        Intent intent = new Intent();
        intent.setClass(MainActivity.this, TakeActivity.class);
        startActivity(intent);
    }
 
    public void putIn(View view){
        Intent intent = new Intent();
        intent.setClass(MainActivity.this, ManageActivity.class);
        startActivity(intent);
    }
 
    public void login() {
        HttpLoggerInterceptor loggingInterceptor = new HttpLoggerInterceptor(true);
        OkHttpClient client = new OkHttpClient.Builder()
                .connectTimeout(20, TimeUnit.SECONDS)//响应时间,读取时间
                .readTimeout(20, TimeUnit.SECONDS)
                .callTimeout(20, TimeUnit.SECONDS)
                .addInterceptor(loggingInterceptor)//添加日志拦截器
                .build();
 
        RequestBody body = new FormBody.Builder()
                .add("equipmentUid", DeviceUtils.getUniqueDeviceId()).build();
        final Request request = new Request.Builder()
                .url(HttpUrlDefine.equipmentLogin)
                .addHeader("language", "zh-CN")
                .post(body)
                .build();
        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Message msg = new Message();
                msg.what = 0;
                msg.obj = "登录失败";
                showHandler.sendMessage(msg);
                LogUtils.e(e);
            }
 
            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String result = response.body().string();
                LogUtils.i("获取到的数据:"+result);
                JSONObject obj = JSONObject.parseObject(result);
                if ("0".equals(obj.getString("code"))) {
                    JSONObject dataObj = obj.getJSONObject("data");
                    String name = dataObj.getString("name");
                    Message msg = new Message();
                    msg.what = 1;
                    msg.obj = "登录成功!" + name;
                    showHandler.sendMessage(msg);
                    //equipmentCode
                    String code = dataObj.getString("equipmentCode");
                    CacheDiskUtils.getInstance().put(SettingConfig.getInstance().Cache_Device_Code, code);
                } else {
                    Message msg = new Message();
                    msg.what = 0;
                    msg.obj = "登录失败:请重新激活!" ;
                    showHandler.sendMessage(msg);
                }
            }
        });
    }
 
    private Handler showHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            if (msg.what == 0) {
                DynamicToast.makeError(context, msg.obj.toString(), 3).show();
            } else if (msg.what == 1) {
                DynamicToast.makeSuccess(context, msg.obj.toString(), 3).show();
            } else {
                DynamicToast.makeWarning(context, msg.obj.toString(), 3).show();
            }
        }
    };
 
}