1.1.32,说有更新的话,但也只是那些代码级别的事情。可是,这是我们等了几个月的结果。
这些东西其实5月份就开始写了,那会是ProjectX,我们一直在研究怎么让主线程减少负担让网络线程,更好的使用和调用。
所以,你们等了几天,我们等了三个月。
终于让"com.haohanyh.hamos.huawei"独立出包,并且新添了不少代码~
/* 受Haohanyh Computer Software Products Open Source LICENSE保护 https://git.haohanyh.top:3001/Haohanyh/LICENSE */
package com.haohanyh.hamos.huawei;
...
public class Huawei {
//获取得到的HUAWEI华为云Token,不许填写!!!
protected String HUAWEITOKEN = "";
//需要添加的IAM账号名、账号密码。想要获取IAM信息?点它即可→https://support.huaweicloud.com/api-iam/iam_17_0002.html
protected final String JsonDomainName = "";
protected final String JsonName = "";
protected final String JsonPassword = "";
//下面老三样,不要动!!!
protected Huawei() { }
public static Huawei GetHuawei() { return huawei.network; }
protected static class huawei { private static final Huawei network = new Huawei(); }
/*
* 创建JSON转String,方便post函数进行使用和理解。
*/
public String CreateJsonToPost() {
JSONObject First = new JSONObject();
try {
JSONObject Auth = new JSONObject();
First.put("auth",Auth);
JSONObject Identity = new JSONObject();
Auth.put("identity",Identity);
JSONObject Password = new JSONObject();
Identity.put("password",Password);
JSONArray Methods = new JSONArray();
Methods.put("password");
Identity.put("methods",Methods);
JSONObject User = new JSONObject();
JSONObject UserDomain = new JSONObject();
User.put("domain",UserDomain);
UserDomain.put("name",JsonDomainName);
Password.put("user",User);
User.put("name",JsonName);
User.put("password",JsonPassword);
JSONObject Scope = new JSONObject();
Auth.put("scope",Scope);
JSONObject Domain = new JSONObject();
Scope.put("domain",Domain);
Domain.put("name",JsonDomainName);
} catch (JSONException e) {
e.printStackTrace();
}
return String.valueOf(First);
}
/*
* 创建JSON转String,方便ControlSenderneedPost函数进行使用和理解。
*/
public void CreateJsonToControlSenderneedPost(String project_id,String device_id,String service_id,String command_name,String command_param,String command_value) {
String jsonParas = "";
JSONObject object = new JSONObject();
try {
JSONObject jsonObject = new JSONObject();
jsonObject.put("service_id", service_id);
jsonObject.put("command_name", command_name);
object.put(command_param, command_value);
jsonObject.put("paras", object);
jsonParas = jsonObject.toString();
ControlSenderneedpost(jsonParas,project_id,device_id);
} catch (JSONException e) {
e.printStackTrace();
}
}
/*
* CreateCommand 下发设备命令
* 华为云API调试地址:https://apiexplorer.developer.huaweicloud.com/apiexplorer/doc?product=IoTDA&api=CreateCommand
* 护花使者:采用E53-IA1,MotorStatus
* 小熊派:(E53-IA1 MotorStatus、LightStatus)(E53-ST1 BeepStatus)(E53_SC1 LightStatus)
*/
public void ControlSenderneedpost(String json,String project_id,String device_id) {
RequestBody body = RequestBody.create(MediaType.parse("application/json;charset=utf-8"), json);
Request request = new Request.Builder()
.url("https://iotda.cn-north-4.myhuaweicloud.com/v5/iot/" + project_id + "/devices/" + device_id + "/commands")
.addHeader("X-Auth-Token",HUAWEITOKEN)
.post(body)
.build();
OkHttpClient mOkHttpClient = new OkHttpClient();
mOkHttpClient.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
System.out.println(e.getLocalizedMessage() + ",失败");
}
@Override
public void onResponse(Call call, Response response) throws IOException {
System.out.println("函数链接: " + response.body().string());
}
});
}
/*
* ShowDeviceShadow 查询设备影子数据
* 华为云API调试地址:https://apiexplorer.developer.huaweicloud.com/apiexplorer/doc?product=IoTDA&api=ShowDeviceShadow
*/
public String Get(String url) {
String content = "";
URLConnection urlConnection = null;
try {
urlConnection = new URL(url).openConnection();
HttpURLConnection connection = (HttpURLConnection) urlConnection;
connection.setRequestMethod("GET");
connection.setRequestProperty("Content-Type", "application/json");
connection.setRequestProperty("X-Auth-Token", HUAWEITOKEN);
connection.connect();
int responseCode = connection.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8));
StringBuilder bs = new StringBuilder();
String l;
while ((l = bufferedReader.readLine()) != null) {
bs.append(l).append("\n");
}
content = bs.toString();
} else if (responseCode == 401) {
System.out.println("Get函数:failed");
}
return content;
} catch (IOException e) {
e.printStackTrace();
}
return content;
}
/*
* KeystoneCreateUserTokenByPassword 获取IAM用户Token(使用密码)
* 华为云API调试地址:https://apiexplorer.developer.huaweicloud.com/apiexplorer/doc?product=IAM&api=KeystoneCreateUserTokenByPassword
*/
public void Post() {
String jsonwenben = CreateJsonToPost();
RequestBody body = RequestBody.create(MediaType.parse("application/json;charset=utf-8"), jsonwenben);
Request request = new Request.Builder()
.url("https://iam.cn-north-4.myhuaweicloud.com/v3/auth/tokens")
.post(body)
.build();
OkHttpClient mOkHttpClient = new OkHttpClient();
mOkHttpClient.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
System.out.println(e.getLocalizedMessage() + ",失败");
}
@Override
public void onResponse(Call call, Response response) throws IOException {
HUAWEITOKEN = response.header("x-subject-token");
}
});
}
}
基本上就5个函数名字:
CreateJsonToPost()————让Post函数可以有账号密码提交上去,从而得到Token
CreateJsonToControlSenderneedPost(...)————让ControlSenderneedPost函数可以提交信息控制传感器
剩下的老三样,就不用说了。代码里面有注释。
什么?你说看不懂?点我立马去复习~
然后我们最重要的MainActivity,就有了以下函数:
/*
* WaterStart()为护花进程开启后,启动的智能判断临界值情况,里面的算法其实很简单,就是触发到时设备浇花。
*/
private void WaterStart(){
Log.i( "Action!" , "浇花进程,开启!" );
water = new WaterControlThread();
water.start();
}
/*
* Water进程类
* 触发时,浇花3s后,暂停浇花。
*/
private class WaterControlThread extends Thread {
@Override
public void run() {
Btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
command_value = "ON";
Toast.makeText(MainActivity.this, "浇花中", Toast.LENGTH_SHORT).show();
WaterSomeFlower();
Toast.makeText(MainActivity.this, "浇花完成", Toast.LENGTH_SHORT).show();
}
});
}
/*
* 定制函数,浇花函数。
*/
private void WaterSomeFlower() {
//请保证6个东西你都填写了再执行该函数~
GetHuawei().CreateJsonToControlSenderneedPost(project_id,device_id,service_id,command_name,command_param,command_value);
}
}
我们继续包装成一个继承Thread父类的进程类,然后由于我们的硬件特殊性,只要执行一次ON,基本上3s-5s后就能停止浇花。
所以呢,我们就只要执行一下ON即可。
别小看CreateJsonToControlSenderneedPost可以一行解决你的控制需求,但在以前,我们得写100+,现在优化到一个package里60+行了。
所以,这一次的更新,挺伟大的,它会让我们接下来的鸿蒙开发板对华为云的开发,基本上奠定了代码基础和代码理论。(起码1-2年不会动)