package com.develop.common.utils; import static android.content.Context.MODE_PRIVATE; import android.content.Context; import android.content.SharedPreferences; import android.text.TextUtils; import android.view.View; import com.develop.base.util.MMkvUtils; import com.google.gson.Gson; import com.kuyuntech.cofarcooking.device.sdk.constant.core.DevStatus; import com.kuyuntech.cofarcooking.device.sdk.constant.core.MotorDirections; import com.kuyuntech.cofarcooking.device.sdk.eventbus.core.DevInfo; import com.kuyuntech.cofarcooking.device.sdk.util.core.CofarSDK; import com.tuya.smartai.iot_sdk.DPEvent; import com.tuya.smartai.iot_sdk.IoTSDKManager; import com.tuya.smartai.iot_sdk.Log; import com.tuya.smartai.iot_sdk.UpgradeEventCallback; import org.json.JSONObject; import java.util.Arrays; import java.util.Map; import java.util.Objects; import java.util.stream.Collectors; public class TuyaUtils { public static final String TAG = "TUYA"; public static final String mPid = "dquk8axtyy72c1ap"; public static final String mUid = "uuid50e9e0c707553116"; public static final String mAk = "MJhcxmQ6LeZDj1tNu6F5kqK5aQJfpdOb"; public static String qrcodeUrl = MMkvUtils.INSTANCE.getString("tuya_url"); public static IoTSDKManager ioTSDKManager = null; public static boolean init = false; public static void output(String text) { Log.d(TAG, text); } public static void uploadData(){ if(ioTSDKManager == null || !init){ return; } DevInfo info = CofarSDK.devInfo(); DPEvent[] dpEvents = new DPEvent[]{ new DPEvent(TuyaDps.SWITCH,(byte) DPEvent.Type.PROP_BOOL,true,0), new DPEvent(TuyaDps.START,(byte) DPEvent.Type.PROP_BOOL, info.getStatus() == DevStatus.RUNNING,0), new DPEvent(TuyaDps.MODE,(byte) DPEvent.Type.PROP_ENUM,1,0), new DPEvent(TuyaDps.COOK_TEMPERATURE,(byte) DPEvent.Type.PROP_VALUE,(int)info.getTargetTemp(),0), new DPEvent(TuyaDps.TEMP_CURRENT,(byte) DPEvent.Type.PROP_VALUE,(int)info.getTemp(),0), new DPEvent(TuyaDps.COOK_TIME,(byte) DPEvent.Type.PROP_VALUE,(int)info.getTargetTime(),0), new DPEvent(TuyaDps.REMAIN_TIME,(byte) DPEvent.Type.PROP_VALUE,(int)info.getRemainTime(),0), new DPEvent(TuyaDps.FAULT,(byte) DPEvent.Type.PROP_VALUE,(int)info.getErrCode(),0), new DPEvent(TuyaDps.WORK_STATE,(byte) DPEvent.Type.PROP_VALUE,(int)info.getStatus(),0), new DPEvent(TuyaDps.STOP,(byte) DPEvent.Type.PROP_VALUE, (int)info.getStatus(),0), new DPEvent(TuyaDps.ROTATE_SPEED,(byte) DPEvent.Type.PROP_VALUE,(int)info.getMotorGear(),0), new DPEvent(TuyaDps.SET_CW_CCW,(byte) DPEvent.Type.PROP_VALUE,(int)info.getMotorDirection() ,0), new DPEvent(TuyaDps.SPRAY_WATER,(byte) DPEvent.Type.PROP_VALUE,(int)info.getWaterSprayGear(),0), new DPEvent(TuyaDps.TEMER_UNIT,(byte) DPEvent.Type.PROP_VALUE,0,0), new DPEvent(TuyaDps.WIND_SPEED,(byte) DPEvent.Type.PROP_VALUE,0,0), new DPEvent(TuyaDps.COOK_TEMP_F,(byte) DPEvent.Type.PROP_VALUE,(int)info.getTargetTemp(),0), new DPEvent(TuyaDps.CORRENT_TEMP_F,(byte) DPEvent.Type.PROP_VALUE,(int)info.getTemp(),0), new DPEvent(TuyaDps.CUP_SW,(byte) DPEvent.Type.PROP_VALUE,(int)info.getPotCloverStatus(),0), new DPEvent(TuyaDps.WORK_MODE,(byte) DPEvent.Type.PROP_STR,info.getDevMode(),0) }; ioTSDKManager.sendDP(dpEvents); } public static void initSDK(Context ctx,String version) { Log.init(ctx, "/sdcard/tuya_log/iot_demo/", 3); ioTSDKManager = new IoTSDKManager(ctx) { @Override protected boolean isOffline() { //实现自定义网络监测 Log.d(TAG, "isOffline: " + super.isOffline()); return super.isOffline(); } }; output("固件版本:" + version); output("init sdk:" + mPid + "/" + mUid + "/" + mAk); //注意:这里的pid等配置读取自local.properties文件,不能直接使用。请填写你自己的配置! ioTSDKManager.initSDK("/sdcard/tuya_iot/", mPid , mUid, mAk, version, new IoTSDKManager.IoTCallback() { @Override public void onDpEvent(DPEvent event) { if (event != null) { output("收到 dp: " + event); } } @Override public void onReset() { ctx.getSharedPreferences("event_cache", MODE_PRIVATE).edit().clear().commit(); } @Override public void onShorturl(String urlJson) { output("shorturl: " + urlJson); Gson gson = new Gson(); Map d = gson.fromJson(urlJson, Map.class); String url = (String) d.get("shortUrl"); qrcodeUrl = url; MMkvUtils.INSTANCE.save("tuya_url",url); } @Override public void onActive() { output("onActive: devId-> " + ioTSDKManager.getDeviceId()); init = true; } @Override public void onFirstActive() { output("onFirstActive"); } @Override public void onMQTTStatusChanged(int status) { output("onMQTTStatusChanged: " + status); switch (status) { case IoTSDKManager.STATUS_OFFLINE: // 设备网络离线 break; case IoTSDKManager.STATUS_MQTT_OFFLINE: // 网络在线MQTT离线 break; case IoTSDKManager.STATUS_MQTT_ONLINE: // 网络在线MQTT在线 SharedPreferences sp = ctx.getSharedPreferences("event_cache", MODE_PRIVATE); DPEvent[] events = ioTSDKManager.getEvents(); if (events != null) { for (DPEvent event : events) { if (event != null) { output(event.toString()); } } } break; } } // @Override // public void onMqttMsg(int protocol, org.json.JSONObject msgObj) { // // } }); ioTSDKManager.setUpgradeCallback(new UpgradeEventCallback() { @Override public void onUpgradeInfo(String s) { Log.w(TAG, "onUpgradeInfo: " + s); output("收到升级信息: " + s); // runOnUiThread(() -> upgradeDialog.show()); ioTSDKManager.startUpgradeDownload(); } @Override public void onUpgradeDownloadStart() { Log.w(TAG, "onUpgradeDownloadStart"); output("开始升级下载"); } @Override public void onUpgradeDownloadUpdate(int i) { Log.w(TAG, "onUpgradeDownloadUpdate: " + i); } @Override public void upgradeFileDownloadFinished(int result, String file) { Log.w(TAG, "upgradeFileDownloadFinished: " + result); output("下载完成:" + result + " / " + file); } }); } }