|
|
@@ -119,13 +119,13 @@ pub async fn auth(
|
|
|
} else{
|
|
|
if let Some(mq) = state.mq_detail{
|
|
|
if let Err(e)=mq.db_mq_map.execute(
|
|
|
- "insert into pub (clientid,topic,deadline)values(?,?,9223372036854775807),(?,?,9223372036854775807)",
|
|
|
+ "insert into pub (clientid,topic)values(?,?),(?,?)",
|
|
|
(mqid.clone(),format!("{}/cmd",sn),mqid.clone(),format!("/wf/Iot/device/{sn}"))).await{
|
|
|
log(Warning, format!("on register with device: {e}"));
|
|
|
};
|
|
|
|
|
|
if let Err(e)=mq.db_mq_map.execute(
|
|
|
- "insert into sub (clientid,topic,deadline)values(?,?,9223372036854775807),(?,?,9223372036854775807)",
|
|
|
+ "insert into sub (clientid,topic)values(?,?),(?,?)",
|
|
|
(mqid.clone(),format!("{}/state",sn),mqid.clone(),format!("/wf/Iot/client/{sn}"))).await{
|
|
|
log(Warning, format!("on register with device: {e}"));
|
|
|
};
|
|
|
@@ -180,27 +180,35 @@ pub async fn auth(
|
|
|
// 未注册时绑定设备
|
|
|
if let Some(sn)= q.state.strip_prefix("did="){
|
|
|
log(Debuging, format!("got did: {}",sn));
|
|
|
- if let Err(e) = state.db_lite.execute("update device set belongto=? where sn=? and belongto=0", (uid,sn)).await{
|
|
|
- println!("{e}");
|
|
|
- }else if let Ok(did) = state.db_lite.query("select id from device where sn=?", [sn], |r|{r.get::<usize,i64>(0)}).await{
|
|
|
- // if let Some(did)=state.db_lite.last_insert_rowid().await.ok(){ // 错误位置:update不会给last_insert_rowid
|
|
|
- if let Err(e) = state.db_lite.execute("insert into map_user_device (uid, did)values(?,?)", (uid,did)).await{
|
|
|
- println!("{e}");
|
|
|
+ match state.db_lite.execute("update device set belongto=? where sn=? and belongto=0", (uid,sn)).await{
|
|
|
+ Err(e)=>{println!("{e}")},
|
|
|
+ Ok(n ) => {
|
|
|
+ if n!=0{ // 设备绑定成功,update至少一条则记录并订阅
|
|
|
+ if let Ok(did) = state.db_lite.query(
|
|
|
+ "select id from device where sn=? and belongto=?",
|
|
|
+ (sn,uid), |r|{r.get::<usize,i64>(0)}).await{
|
|
|
+ if let Err(e) = state.db_lite.execute(
|
|
|
+ "insert into map_user_device (uid, did)values(?,?)",
|
|
|
+ (uid,did)).await{
|
|
|
+ println!("{e}");
|
|
|
+ }
|
|
|
+ if let Some(mq) = state.mq_detail{
|
|
|
+ if let Err(e)=mq.db_mq_map.execute(
|
|
|
+ "insert into pub (clientid,topic)values(?,?),(?,?)",
|
|
|
+ (mqid.clone(),format!("{}/cmd",sn),mqid.clone(),format!("/wf/Iot/device/{sn}"))).await{
|
|
|
+ log(Warning, format!("on register with device: {e}"));
|
|
|
+ };
|
|
|
+
|
|
|
+ if let Err(e)=mq.db_mq_map.execute(
|
|
|
+ "insert into sub (clientid,topic)values(?,?),(?,?)",
|
|
|
+ (mqid.clone(),format!("{}/state",sn),mqid.clone(),format!("/wf/Iot/client/{sn}"))).await{
|
|
|
+ log(Warning, format!("on register with device: {e}"));
|
|
|
+ };
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
};
|
|
|
- if let Some(mq) = state.mq_detail{
|
|
|
- if let Err(e)=mq.db_mq_map.execute(
|
|
|
- "insert into pub (clientid,topic,deadline)values(?,?,9223372036854775807),(?,?,9223372036854775807)",
|
|
|
- (mqid.clone(),format!("{}/cmd",sn),mqid.clone(),format!("/wf/Iot/device/{sn}"))).await{
|
|
|
- log(Warning, format!("on register with device: {e}"));
|
|
|
- };
|
|
|
-
|
|
|
- if let Err(e)=mq.db_mq_map.execute(
|
|
|
- "insert into sub (clientid,topic,deadline)values(?,?,9223372036854775807),(?,?,9223372036854775807)",
|
|
|
- (mqid.clone(),format!("{}/state",sn),mqid.clone(),format!("/wf/Iot/client/{sn}"))).await{
|
|
|
- log(Warning, format!("on register with device: {e}"));
|
|
|
- };
|
|
|
- }
|
|
|
}
|
|
|
log(Debug, "register success".to_string());
|
|
|
// 注册成功 函数末尾发生跳转到主页
|