|
1
|
1
|
package com.qnbar.smc.service;
|
|
2
|
2
|
|
|
|
3
|
+import android.content.ComponentName;
|
|
3
|
4
|
import android.content.Context;
|
|
4
|
5
|
import android.content.Intent;
|
|
|
6
|
+import android.content.ServiceConnection;
|
|
|
7
|
+import android.media.AudioManager;
|
|
5
|
8
|
import android.os.Handler;
|
|
6
|
9
|
import android.os.IBinder;
|
|
|
10
|
+import android.os.RemoteException;
|
|
7
|
11
|
import android.text.TextUtils;
|
|
8
|
12
|
import android.util.Log;
|
|
9
|
13
|
import android.widget.Toast;
|
|
10
|
14
|
import com.gimi.common.cinema.model.MessageEvent;
|
|
11
|
15
|
import com.gimi.common.cinema.model.RoomInfo;
|
|
12
|
16
|
import com.gimi.common.cinema.utils.ActivityCollector;
|
|
|
17
|
+import com.gimi.common.cinema.utils.ShellUtils;
|
|
13
|
18
|
import com.gimi.common.cinema.utils.SystemUtils;
|
|
14
|
19
|
import com.gimi.common.cinema.utils.Utils;
|
|
15
|
20
|
import com.google.gson.Gson;
|
|
16
|
21
|
import com.google.gson.JsonSyntaxException;
|
|
|
22
|
+import com.qnbar.smc.model.Lights;
|
|
|
23
|
+import com.qnbar.smc.soketProtocol.fromServer.EquipmentControl;
|
|
|
24
|
+import com.qnbar.smc.soketProtocol.toServer.EquipmentStatus;
|
|
|
25
|
+import com.qnbar.smc.soketProtocol.MainRequest;
|
|
|
26
|
+import com.qnbar.smc.soketProtocol.MainResponse;
|
|
|
27
|
+import com.qnbar.smc.soketProtocol.fromServer.OpenDoor;
|
|
|
28
|
+import com.qnbar.smc.soketProtocol.fromServer.ReportEquStatus;
|
|
17
|
29
|
import com.qnbar.smc.utils.LightOperationUtils;
|
|
|
30
|
+import com.telink.bluetooth.light.ConnectionStatus;
|
|
|
31
|
+import com.xgimi.gimicinema.ICinemaControl;
|
|
18
|
32
|
import com.xgimi.gimicinema.activity.MainActivity;
|
|
19
|
33
|
import com.xgimi.gimicinema.activity.QrCodeShowActivity;
|
|
20
|
34
|
import com.xgimi.gimicinema.application.FangTangApplication;
|
|
|
35
|
+import com.xgimi.gimicinema.service.CinemaControlService;
|
|
21
|
36
|
import com.xgimi.smartscreen.encrypt.AuthCode;
|
|
22
|
37
|
import org.greenrobot.eventbus.EventBus;
|
|
23
|
38
|
|
|
...
|
...
|
@@ -65,11 +80,15 @@ public class SocketService1 extends BaseService { |
|
65
|
80
|
private static final int ROOM_SN_CONNECTED = 10010;//room_sn 已连接,不能再连接
|
|
66
|
81
|
private static final int OPEN_DOOR = 50001;//开门命令
|
|
67
|
82
|
private static final int CLEAN_OVER = 50002;//
|
|
|
83
|
+ private static final int REPORT_EQU_STATUS = 50003;//返回设备状态信息
|
|
|
84
|
+ private static final int SET_EQU_STATUS = 50004;//返回设备状态信息
|
|
68
|
85
|
|
|
69
|
86
|
private static final int ROOM_HAS_REGISTERED = 1001;
|
|
70
|
87
|
private static final int ROOM_NOT_EXIST_M = 1002;
|
|
71
|
88
|
private static final int HEAT_BEAT_RTN = 1003;
|
|
72
|
89
|
|
|
|
90
|
+ private static final int CMD_REPORT_EQU_STATUS = 20030;//上报设备状态信息
|
|
|
91
|
+
|
|
73
|
92
|
// online
|
|
74
|
93
|
public static final String SERVER_HOST_ONLINE = "conn.ft.qnbar.com";// "192.168.1.21";//
|
|
75
|
94
|
public static final int SERVER_PORT_ONLINE = 8899;
|
|
...
|
...
|
@@ -91,6 +110,7 @@ public class SocketService1 extends BaseService { |
|
91
|
110
|
|
|
92
|
111
|
private ReadThread mReadThread;
|
|
93
|
112
|
private Gson gson;
|
|
|
113
|
+ ICinemaControl iPlayer;
|
|
94
|
114
|
|
|
95
|
115
|
private WeakReference<Socket> mSocket;
|
|
96
|
116
|
// For heart Beat
|
|
...
|
...
|
@@ -141,6 +161,9 @@ public class SocketService1 extends BaseService { |
|
141
|
161
|
gson = new Gson();
|
|
142
|
162
|
context = this;
|
|
143
|
163
|
Log.d(TAG, "onCreate");
|
|
|
164
|
+
|
|
|
165
|
+ bindService(new Intent(SocketService1.this, CinemaControlService.class), conn, Context.BIND_AUTO_CREATE);
|
|
|
166
|
+ startService(new Intent(SocketService1.this, CinemaControlService.class));
|
|
144
|
167
|
}
|
|
145
|
168
|
|
|
146
|
169
|
@Override
|
|
...
|
...
|
@@ -295,7 +318,8 @@ public class SocketService1 extends BaseService { |
|
295
|
318
|
if (message.endsWith(END_SYMBOL)) {
|
|
296
|
319
|
message = message.replace(END_SYMBOL, "");
|
|
297
|
320
|
}
|
|
298
|
|
- SocketResponse socketResponse = gson.fromJson(message, SocketResponse.class);
|
|
|
321
|
+// SocketResponse socketResponse = gson.fromJson(message, SocketResponse.class);
|
|
|
322
|
+ MainResponse socketResponse = gson.fromJson(message, MainResponse.class);
|
|
299
|
323
|
switch (socketResponse.getCode()) {
|
|
300
|
324
|
case SUCCESS_MESSAGE:
|
|
301
|
325
|
Log.d(TAG, "SUCCESS_MESSAGE");
|
|
...
|
...
|
@@ -336,55 +360,20 @@ public class SocketService1 extends BaseService { |
|
336
|
360
|
break;
|
|
337
|
361
|
case RETURN_VERIFY_CODE:
|
|
338
|
362
|
Log.d(TAG, "RETURN_VERIFY_CODE");
|
|
339
|
|
- if (!TextUtils.isEmpty(socketResponse.getData().getVerify())) {
|
|
340
|
|
- String verifyMsg = AuthCode.getDecodeStr(socketResponse.getData().getVerify());
|
|
341
|
|
- SocketSendMsg ssm = new SocketSendMsg().contractVerifyMsg(testRoomSn, verifyMsg);
|
|
342
|
|
- String msg = gson.toJson(ssm) + END_SYMBOL;
|
|
343
|
|
- sendMsg(msg);
|
|
|
363
|
+ if (!TextUtils.isEmpty(socketResponse.getData())) {
|
|
|
364
|
+ OpenDoor openDoorResponse = gson.fromJson(socketResponse.getData(), OpenDoor.class);
|
|
|
365
|
+
|
|
|
366
|
+ if (!TextUtils.isEmpty(openDoorResponse.getVerify())) {
|
|
|
367
|
+ String verifyMsg = AuthCode.getDecodeStr(openDoorResponse.getVerify());
|
|
|
368
|
+ SocketSendMsg ssm = new SocketSendMsg().contractVerifyMsg(testRoomSn, verifyMsg);
|
|
|
369
|
+ String msg = gson.toJson(ssm) + END_SYMBOL;
|
|
|
370
|
+ sendMsg(msg);
|
|
|
371
|
+ }
|
|
344
|
372
|
}
|
|
345
|
373
|
break;
|
|
346
|
374
|
case CONTAIN_MESSAGE:
|
|
347
|
375
|
Log.d(TAG, "CONTAIN_MESSAGE");
|
|
348
|
|
- if (socketResponse.getData() != null) {
|
|
349
|
|
- if (socketResponse.getCmd() == OPEN_DOOR) {
|
|
350
|
|
- switch (socketResponse.getData().getUser()) {
|
|
351
|
|
- //10用户,20管理员,默认值为0
|
|
352
|
|
- case 10:
|
|
353
|
|
- new SystemUtils().openFtLed(context.getApplicationContext());
|
|
354
|
|
- if (TextUtils.isEmpty(((FangTangApplication) getApplication()).getCurrentPlayUrl())) {
|
|
355
|
|
- LightOperationUtils.open();
|
|
356
|
|
- LightOperationUtils.setLightValue(Utils.getInt(context, "brightness", 50));
|
|
357
|
|
- }
|
|
358
|
|
- sendMessage(QrCodeShowActivity.KILL_SELF, "finish the QR CODE activity when new user come in");
|
|
359
|
|
- if (socketResponse.getData().getFirst() == 1) {
|
|
360
|
|
- sendMessage(USER_OPEN_DOOR_AND_GET_MOVIE, "user first open the door");
|
|
361
|
|
- } else {
|
|
362
|
|
- sendMessage(USER_OPEN_DOOR, "user open the door");
|
|
363
|
|
- }
|
|
364
|
|
- break;
|
|
365
|
|
- case 20:
|
|
366
|
|
- if (TextUtils.isEmpty(((FangTangApplication) getApplication()).getCurrentPlayUrl())) {
|
|
367
|
|
- Log.d("LightOperationUtils", "admin open light");
|
|
368
|
|
- LightOperationUtils.open();
|
|
369
|
|
- LightOperationUtils.setLightValue(Utils.getInt(context, "brightness", 50));
|
|
370
|
|
- }
|
|
371
|
|
- sendMessage(JUST_OPEN_DOOR, "administrator open the door");
|
|
372
|
|
- break;
|
|
373
|
|
- case 0:
|
|
374
|
|
- sendMessage(JUST_OPEN_DOOR, "get zero none user or administrator open the door");
|
|
375
|
|
- break;
|
|
376
|
|
- default:
|
|
377
|
|
- sendMessage(JUST_OPEN_DOOR, "none user or administrator open the door");
|
|
378
|
|
- break;
|
|
379
|
|
- }
|
|
380
|
|
- } else if (socketResponse.getCmd() == CLEAN_OVER) {
|
|
381
|
|
- Log.d("LightOperationUtils", "admin clean over close light");
|
|
382
|
|
- LightOperationUtils.setLightValue(5);
|
|
383
|
|
- LightOperationUtils.close();
|
|
384
|
|
- new SystemUtils().closeFtLed(context.getApplicationContext());
|
|
385
|
|
- sendMessage(QrCodeShowActivity.KILL_SELF, "finish the QR CODE activity when clean over");
|
|
386
|
|
- }
|
|
387
|
|
- }
|
|
|
376
|
+ serverCommandDeal(socketResponse);
|
|
388
|
377
|
break;
|
|
389
|
378
|
default:
|
|
390
|
379
|
Log.d(TAG, "default msg:" + socketResponse.toString());
|
|
...
|
...
|
@@ -418,4 +407,231 @@ public class SocketService1 extends BaseService { |
|
418
|
407
|
Log.d(TAG, "socket service destroy");
|
|
419
|
408
|
super.onDestroy();
|
|
420
|
409
|
}
|
|
|
410
|
+
|
|
|
411
|
+ /**
|
|
|
412
|
+ * 处理服务器发送的命令
|
|
|
413
|
+ * @param socketResponse
|
|
|
414
|
+ */
|
|
|
415
|
+ private void serverCommandDeal(MainResponse socketResponse)
|
|
|
416
|
+ {
|
|
|
417
|
+ switch (socketResponse.getCmd())
|
|
|
418
|
+ {
|
|
|
419
|
+ case OPEN_DOOR://开门
|
|
|
420
|
+ if (socketResponse.getData() != null) {
|
|
|
421
|
+ OpenDoor openDoorResponse = gson.fromJson(socketResponse.getData(), OpenDoor.class);
|
|
|
422
|
+ openDoor(openDoorResponse);
|
|
|
423
|
+ }
|
|
|
424
|
+ break;
|
|
|
425
|
+ case CLEAN_OVER://清洁完成处理
|
|
|
426
|
+ Log.d("LightOperationUtils", "admin clean over close light");
|
|
|
427
|
+ LightOperationUtils.setLightValue(5);
|
|
|
428
|
+ LightOperationUtils.close();
|
|
|
429
|
+ new SystemUtils().closeFtLed(context.getApplicationContext());
|
|
|
430
|
+ sendMessage(QrCodeShowActivity.KILL_SELF, "finish the QR CODE activity when clean over");
|
|
|
431
|
+ break;
|
|
|
432
|
+
|
|
|
433
|
+ case REPORT_EQU_STATUS://获取声音,灯光,播放暂停状态
|
|
|
434
|
+ Log.d("ReportInfo","收到上报命令");
|
|
|
435
|
+ //获取serverId
|
|
|
436
|
+ ReportEquStatus rps = gson.fromJson(socketResponse.getData(), ReportEquStatus.class);
|
|
|
437
|
+// String serverId = rps.getServerId();
|
|
|
438
|
+ reportEquStatus(rps.getServerId());
|
|
|
439
|
+ break;
|
|
|
440
|
+
|
|
|
441
|
+ case SET_EQU_STATUS://设置声音,灯光,播放暂停
|
|
|
442
|
+ Log.d("ReportInfo","收到控制命令:"+socketResponse.getData());
|
|
|
443
|
+ EquipmentControl equc = gson.fromJson(socketResponse.getData(), EquipmentControl.class);
|
|
|
444
|
+ setEquipmentStatus(equc);
|
|
|
445
|
+ break;
|
|
|
446
|
+ }
|
|
|
447
|
+ }
|
|
|
448
|
+
|
|
|
449
|
+/*--命令处理-start-----------------------------------*/
|
|
|
450
|
+
|
|
|
451
|
+ private void openDoor(OpenDoor openDoorResponse)
|
|
|
452
|
+ {
|
|
|
453
|
+ switch (openDoorResponse.getUser()) {
|
|
|
454
|
+ //10用户,20管理员,默认值为0
|
|
|
455
|
+ case 10:
|
|
|
456
|
+ new SystemUtils().openFtLed(context.getApplicationContext());
|
|
|
457
|
+ if (TextUtils.isEmpty(((FangTangApplication) getApplication()).getCurrentPlayUrl())) {
|
|
|
458
|
+ LightOperationUtils.open();
|
|
|
459
|
+ LightOperationUtils.setLightValue(Utils.getInt(context, "brightness", 50));
|
|
|
460
|
+ }
|
|
|
461
|
+ sendMessage(QrCodeShowActivity.KILL_SELF, "finish the QR CODE activity when new user come in");
|
|
|
462
|
+ if (openDoorResponse.getFirst() == 1) {
|
|
|
463
|
+ sendMessage(USER_OPEN_DOOR_AND_GET_MOVIE, "user first open the door");
|
|
|
464
|
+ } else {
|
|
|
465
|
+ sendMessage(USER_OPEN_DOOR, "user open the door");
|
|
|
466
|
+ }
|
|
|
467
|
+ break;
|
|
|
468
|
+ case 20:
|
|
|
469
|
+ if (TextUtils.isEmpty(((FangTangApplication) getApplication()).getCurrentPlayUrl())) {
|
|
|
470
|
+ Log.d("LightOperationUtils", "admin open light");
|
|
|
471
|
+ LightOperationUtils.open();
|
|
|
472
|
+ LightOperationUtils.setLightValue(Utils.getInt(context, "brightness", 50));
|
|
|
473
|
+ }
|
|
|
474
|
+ sendMessage(JUST_OPEN_DOOR, "administrator open the door");
|
|
|
475
|
+ break;
|
|
|
476
|
+ case 0:
|
|
|
477
|
+ sendMessage(JUST_OPEN_DOOR, "get zero none user or administrator open the door");
|
|
|
478
|
+ break;
|
|
|
479
|
+ default:
|
|
|
480
|
+ sendMessage(JUST_OPEN_DOOR, "none user or administrator open the door");
|
|
|
481
|
+ break;
|
|
|
482
|
+ }
|
|
|
483
|
+ }
|
|
|
484
|
+
|
|
|
485
|
+ private void reportEquStatus(String serverId)
|
|
|
486
|
+ {
|
|
|
487
|
+ //填充返回数据
|
|
|
488
|
+ int volume = getAudioVolume()/6 + 1;
|
|
|
489
|
+ int lightStatus = 2;
|
|
|
490
|
+
|
|
|
491
|
+ int playStatus = 0;
|
|
|
492
|
+ try {
|
|
|
493
|
+ playStatus = iPlayer.getCurrentStatus();
|
|
|
494
|
+ Log.d("ReportInfo","playStatus:"+playStatus);
|
|
|
495
|
+ } catch (RemoteException e) {
|
|
|
496
|
+ e.printStackTrace();
|
|
|
497
|
+ }
|
|
|
498
|
+
|
|
|
499
|
+ if( Lights.getInstance().getByMeshAddress(
|
|
|
500
|
+ ((FangTangApplication) getApplication()).getConnectDevice().meshAddress).status == ConnectionStatus.ON )
|
|
|
501
|
+ {
|
|
|
502
|
+ lightStatus = 1;
|
|
|
503
|
+ }
|
|
|
504
|
+
|
|
|
505
|
+ EquipmentStatus equStatus = new EquipmentStatus();
|
|
|
506
|
+ equStatus.setLightStatus(lightStatus);
|
|
|
507
|
+ equStatus.setPlayStatus(playStatus);
|
|
|
508
|
+ equStatus.setVolume(volume);
|
|
|
509
|
+ equStatus.setServerId(serverId);
|
|
|
510
|
+ equStatus.setRoom_sn(testRoomSn);
|
|
|
511
|
+
|
|
|
512
|
+ MainRequest rep = new MainRequest();
|
|
|
513
|
+ rep.setCmd(CMD_REPORT_EQU_STATUS);
|
|
|
514
|
+ rep.setData(equStatus);
|
|
|
515
|
+ String msg = gson.toJson(rep) + END_SYMBOL;
|
|
|
516
|
+ sendMsg(msg);
|
|
|
517
|
+ Log.d("ReportInfo",msg);
|
|
|
518
|
+ }
|
|
|
519
|
+
|
|
|
520
|
+ private void setLight(int op)
|
|
|
521
|
+ {
|
|
|
522
|
+ if (op == 1)//打开灯光
|
|
|
523
|
+ {
|
|
|
524
|
+ Log.d("setEqu","打开灯光");
|
|
|
525
|
+ LightOperationUtils.open();
|
|
|
526
|
+ LightOperationUtils.setLightValue(Utils.getInt(context, "brightness", 50));
|
|
|
527
|
+ }
|
|
|
528
|
+ else if(op == 2)//关闭灯光
|
|
|
529
|
+ {
|
|
|
530
|
+ Log.d("setEqu","关闭灯光");
|
|
|
531
|
+ LightOperationUtils.setLightValue(5);
|
|
|
532
|
+ LightOperationUtils.close();
|
|
|
533
|
+ }
|
|
|
534
|
+ }
|
|
|
535
|
+
|
|
|
536
|
+ private void playCtl(int op)
|
|
|
537
|
+ {
|
|
|
538
|
+ int status = -1;
|
|
|
539
|
+ try {
|
|
|
540
|
+ status = iPlayer.getCurrentStatus();
|
|
|
541
|
+ } catch (RemoteException e) {
|
|
|
542
|
+ e.printStackTrace();
|
|
|
543
|
+ }
|
|
|
544
|
+
|
|
|
545
|
+ if (op == 1 && status == 2)
|
|
|
546
|
+ {
|
|
|
547
|
+ Log.d("setEqu","开始播放");
|
|
|
548
|
+ ShellUtils.execCommand("input keyevent 23", false);
|
|
|
549
|
+ }
|
|
|
550
|
+ else if (op == 2 && status == 1)
|
|
|
551
|
+ {
|
|
|
552
|
+ Log.d("setEqu","暂停播放");
|
|
|
553
|
+ ShellUtils.execCommand("input keyevent 23", false);
|
|
|
554
|
+ }
|
|
|
555
|
+
|
|
|
556
|
+ try {
|
|
|
557
|
+ status = iPlayer.getCurrentStatus();
|
|
|
558
|
+ } catch (RemoteException e) {
|
|
|
559
|
+ e.printStackTrace();
|
|
|
560
|
+ }
|
|
|
561
|
+ }
|
|
|
562
|
+
|
|
|
563
|
+ private void setEquipmentStatus(EquipmentControl equc)
|
|
|
564
|
+ {
|
|
|
565
|
+ setLight(equc.getLightCtl());
|
|
|
566
|
+
|
|
|
567
|
+ playCtl(equc.getPlayCtl());
|
|
|
568
|
+
|
|
|
569
|
+ setAudioVolume(equc.getAudioCtl());
|
|
|
570
|
+
|
|
|
571
|
+ reportEquStatus(equc.getServerId());
|
|
|
572
|
+
|
|
|
573
|
+ }
|
|
|
574
|
+
|
|
|
575
|
+/*--命令处理-end-----------------------------------*/
|
|
|
576
|
+
|
|
|
577
|
+ private int getAudioVolume()
|
|
|
578
|
+ {
|
|
|
579
|
+ AudioManager mAudioManager;
|
|
|
580
|
+ mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
|
|
|
581
|
+ int current = mAudioManager.getStreamVolume( AudioManager.STREAM_MUSIC );
|
|
|
582
|
+ return current;
|
|
|
583
|
+ }
|
|
|
584
|
+
|
|
|
585
|
+ private int setAudioVolume(int volume)
|
|
|
586
|
+ {
|
|
|
587
|
+ AudioManager mAudioManager;
|
|
|
588
|
+ mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
|
|
|
589
|
+ int now = getAudioVolume()/6;
|
|
|
590
|
+
|
|
|
591
|
+ if (volume == 0)
|
|
|
592
|
+ {
|
|
|
593
|
+ return now;
|
|
|
594
|
+ }
|
|
|
595
|
+ else
|
|
|
596
|
+ {
|
|
|
597
|
+ volume--;
|
|
|
598
|
+ }
|
|
|
599
|
+
|
|
|
600
|
+ if (volume > now)//加音量
|
|
|
601
|
+ {
|
|
|
602
|
+ Log.d("setEqu","加音量"+now+":"+volume);
|
|
|
603
|
+ while(now < volume)
|
|
|
604
|
+ {
|
|
|
605
|
+ mAudioManager.adjustStreamVolume(
|
|
|
606
|
+ AudioManager.STREAM_MUSIC,
|
|
|
607
|
+ AudioManager.ADJUST_RAISE,
|
|
|
608
|
+ AudioManager.FLAG_PLAY_SOUND | AudioManager.FLAG_SHOW_UI);
|
|
|
609
|
+ now ++;
|
|
|
610
|
+ }
|
|
|
611
|
+ }
|
|
|
612
|
+ else if(volume < now)//减音量
|
|
|
613
|
+ {
|
|
|
614
|
+ Log.d("setEqu","减音量"+now+":"+volume);
|
|
|
615
|
+ while(now > volume)
|
|
|
616
|
+ {
|
|
|
617
|
+ mAudioManager.adjustStreamVolume(
|
|
|
618
|
+ AudioManager.STREAM_MUSIC,
|
|
|
619
|
+ AudioManager.ADJUST_LOWER,
|
|
|
620
|
+ AudioManager.FLAG_PLAY_SOUND | AudioManager.FLAG_SHOW_UI);
|
|
|
621
|
+ now --;
|
|
|
622
|
+ }
|
|
|
623
|
+ }
|
|
|
624
|
+
|
|
|
625
|
+ return now;
|
|
|
626
|
+ }
|
|
|
627
|
+
|
|
|
628
|
+ private ServiceConnection conn = new ServiceConnection() {
|
|
|
629
|
+ public void onServiceConnected(ComponentName className, IBinder service) {
|
|
|
630
|
+ Log.i("yao", "ServiceConnection -> onServiceConnected");
|
|
|
631
|
+ iPlayer = ICinemaControl.Stub.asInterface(service);
|
|
|
632
|
+ }
|
|
|
633
|
+
|
|
|
634
|
+ public void onServiceDisconnected(ComponentName className) {
|
|
|
635
|
+ };
|
|
|
636
|
+ };
|
|
421
|
637
|
} |
|
|
\ No newline at end of file |
...
|
...
|
|