private void btn_login_Click(object sender, EventArgs e) { var input_username = username.Text; var input_password = password.Text; bool res = login_passwd(input_username, input_password); if (res) { SDK = new NLECloudAPI("http://api.nlecloud.com"); AccountLoginDTO submitData = new AccountLoginDTO() { Account = input_username, Password = input_password, IsRememberMe = false }; Login_obj = SDK.UserLogin(submitData); Login_res = Login_obj.IsSuccess(); if (Login_res) { accessToken = Login_obj.ResultObj.AccessToken; Form1 f1 = new Form1(SDK, Login_obj, Login_res); f1.Show(); this.Hide(); } } else { MessageBox.Show("账号或密码错误,请重新输入!!!", "提示"); } }
public void Button_Click_login(object sender, RoutedEventArgs e) { SDK = new NLECloudAPI("http://api.nlecloud.com"); SentencedToEmpty(); ProgressBar.IsIndeterminate = true; //string LocalLoginAuthentication = "" AccountLoginDTO data_account = new AccountLoginDTO() //实例化登录信息 { Account = getUsername.Text, //从xaml中的TextBox中的name中获取账号文本 Password = getPassword.Password //从xaml中的PasswordBox中的name中获取密码 }; /* * 自己写的账号密码判空提示 * 缺陷:代码冗余,且不符合使用逻辑 * 改进:使用this.getUsername.Focus聚焦文本框提示用户、使用return跳出方法 * while (LocalLoginAuthentication == "") * { * LocalLoginAuthentication = "账号密码判空通过"; * if (getUsername.Text == "") * {//账号判空提示 * MessageBox.Show("账号不能为空!", "登陆失败"); * LocalLoginAuthentication = ""; * break; * } * //密码判空提示 * if (getPassword.Password == "") * { * LocalLoginAuthentication = ""; * MessageBox.Show("密码不能为空!", "登陆失败"); * break; * } * } */ //Thread.Sleep(5000);//全界面Sleep 5秒 ResultMsg <AccountLoginResultDTO> data_account_verify = SDK.UserLogin(data_account); if (data_account_verify.IsSuccess()) {//问题:输入账号时,textbox内预留的提示文字应消失(×) //问题:password内没有预留的提示文字(√) //问题:记住密码(×) Schoolname = data_account_verify.ResultObj.CollegeName; //获取ResultObj.CollegeName学校名 accessToken = data_account_verify.ResultObj.AccessToken; //获取ResultObj.AccessToken,用于后续API调用 MessageBox.Show("登陆成功!,您位于" + Schoolname + "\n即将进入空调控制器!", "登录成功"); MainWindow mainWindow = new MainWindow(); mainWindow.Show(); this.Close(); } else { MessageBox.Show("登录失败!,请重试", "登录失败"); } }
/// <summary> /// 获取一个项目 /// </summary> /// <returns></returns> public string GetProject() { int projectID = 6571; ResultMsg <ProjectInfoDTO> opResult = SDK.GetProjectInfo(projectID, this.Token); if (opResult.IsSuccess()) { return(opResult.ResultObj.ProjectTag); } else { return(null); } }
/// <summary> /// 执行读取传感数据 /// </summary> private static void QuerySensorData() { //调用批量查询设备最新数据 ResultMsg <IEnumerable <DeviceSensorDataDTO> > qry = SDK.GetDevicesDatas(Cfg.deviceId.ToString(), Token); Console.WriteLine(lineNum + "、批量查询设备最新数据返回JSON:" + Environment.NewLine); Console.WriteLine(SerializeToJson(qry) + Environment.NewLine); lineNum++; if (qry.IsSuccess()) { var dev = qry.ResultObj.FirstOrDefault(); try { SensorDataDTO jsonSensor = dev.Datas.FirstOrDefault(item => { return(item.ApiTag == Cfg.jsonApiTag); }); if (jsonSensor != null && jsonSensor.Value != null && jsonSensor.Value.ToString() != "") { //将设备传输的JSON字符串,使用JsonConvert转为对象 Member user = new Member(); user = JsonConvert.DeserializeObject <Member>(jsonSensor.Value.ToString()); Console.WriteLine(lineNum + "、JSON传感器值:" + Environment.NewLine); Console.WriteLine(String.Format("{0}/{1}/{2}/{3}" , user.UserName , user.Age , user.Sex , user.IsMarry) + Environment.NewLine); lineNum++; } SensorDataDTO binarySensor = dev.Datas.FirstOrDefault(item => { return(item.ApiTag == Cfg.binaryApiTag); }); if (binarySensor != null && binarySensor.Value != null && binarySensor.Value.ToString() != "") { //将设备传输的Base64字符串,使用Base64解码为byte[] Byte[] ary = Convert.FromBase64String(binarySensor.Value.ToString()); Console.WriteLine(lineNum + "、二进制传感器值:" + Environment.NewLine); Console.WriteLine(String.Format("{0}/{1}/{2}/{3}" , ary[0] , ary[1] , ary[2] , ary[3]) + Environment.NewLine); lineNum++; } } catch { } } }
private void Login() { AccountLoginDTO dto = new AccountLoginDTO() { Account = "ytvciot", Password = "******" }; ResultMsg <AccountLoginResultDTO> result = api.UserLogin(dto); if (result.IsSuccess()) { token = result.ResultObj.AccessToken; } else { MessageBox.Show(result.ToString()); } }
public bool UserLoginTest() { AccountLoginDTO dto = new AccountLoginDTO() { Account = "18965562233", Password = "******" }; ResultMsg <AccountLoginResultDTO> opResult = SDK.UserLogin(dto); if (opResult.IsSuccess()) { this.Token = opResult.ResultObj.AccessToken; return(true); } else { return(false); } }
private void update_data(object sender, EventArgs e) { ResultMsg <SensorBaseInfoDTO> status = SDK.GetSensorInfo(devi_id, "nl_temperature", accessToken); if (status.IsSuccess()) { string resu = status.ResultObj.Value.ToString(); } ResultMsg <IEnumerable <OnlineDataDTO> > devi_status = SDK.GetDevicesStatus("" + devi_id, accessToken); bool is_online = false; foreach (var l in devi_status.ResultObj) { is_online = l.IsOnline; } if (is_online) { online_staus.Text = "在线"; } else { online_staus.Text = "离线"; } }
static void Main(string[] args) { TestClass tc = new TestClass(); #region ——登陆测试—— bool b = tc.UserLoginTest(); if (b) { Console.WriteLine("登陆成功"); } else { Console.WriteLine("登录失败"); } #endregion #region ——项目测试—— #region ——获取单个项目—— //string projectTag = tc.GetProject(); //if (!string.IsNullOrEmpty(projectTag)) //{ // Console.WriteLine("项目tag={0}", projectTag); //} //else //{ // Console.WriteLine("获取项目失败!" + // ""); //} #endregion #region ——模糊查询项目—— //ResultMsg<ListPagerSet<ProjectInfoDTO>> opResult = tc.GetProjects(); //if(opResult.IsSuccess()) // { // List<ProjectInfoDTO> listProjects = opResult.ResultObj.PageSet as List<ProjectInfoDTO> ; // if(listProjects.Count > 0) // { // foreach (var project in listProjects) // { // Console.WriteLine("ProjectId={0}",project.ProjectID); // } // } // else if(listProjects.Count == 0) // { // Console.WriteLine("项目个数为0"); // } // } //else // { // Console.WriteLine(opResult.Msg); // } #endregion #region ——获取传感器—— //ResultMsg<List<SensorBaseInfoDTO>> opResult = tc.GetSensorsByProjectId(); //if (opResult.IsSuccess()) //{ // List<SensorBaseInfoDTO> listSensors = opResult.ResultObj; // if (listSensors.Count > 0) // { // foreach (var sensor in listSensors) // { // Console.WriteLine(sensor.Name); // } // } // else if (listSensors.Count == 0) // { // Console.WriteLine("该项目下还未添加传感器"); // } //} //else //{ // Console.WriteLine(opResult.Msg); //} #endregion #endregion #region ——设备测试—— #region ——批量查询设备最新数据—— //ResultMsg<List<DeviceSensorDataDTO>> opResult = tc.GetSensorsData(); //if (opResult.IsSuccess()) //{ // List<DeviceSensorDataDTO> sensorsDataList = opResult.ResultObj; // if (sensorsDataList.Count > 0) // { // foreach (var sensorData in sensorsDataList) // { // Console.WriteLine(sensorData.Name); // } // } // else if (sensorsDataList.Count == 0) // { // Console.WriteLine("无传感数据"); // } //} //else //{ // Console.WriteLine(opResult.Msg); //} #endregion #region ——批量查询设备的在线状态—— //ResultMsg<List<OnlineDataDTO>> opResult = tc.GetDeviceStatus(); //if (opResult.IsSuccess()) //{ // List<OnlineDataDTO> onlineDataList = opResult.ResultObj; // if (onlineDataList.Count > 0) // { // foreach (var onlineData in onlineDataList) // { // Console.WriteLine("{0}在线状态:{1}",onlineData.Name,onlineData.IsOnline); // } // } // else if (onlineDataList.Count == 0) // { // Console.WriteLine("无传感设备"); // } //} //else //{ // Console.WriteLine(opResult.Msg); //} #endregion #region ——查询单个设备—— //ResultMsg<DeviceInfoDTO> opResult = tc.GetDeviceByDeviceId(); //if (opResult.IsSuccess()) //{ // DeviceInfoDTO device = opResult.ResultObj; // Console.WriteLine("设备名称:{0}", device.Name); //} //else //{ // Console.WriteLine(opResult.Msg); //} #endregion #region ——模糊查询设备—— //ResultMsg<ListPagerSet<DeviceBaseInfoDTO>> opResult = tc.GetDevices(); //if (opResult.IsSuccess()) //{ // List<DeviceBaseInfoDTO> listDevices = opResult.ResultObj.PageSet as List<DeviceBaseInfoDTO>; // if (listDevices.Count > 0) // { // foreach (var device in listDevices) // { // Console.WriteLine("DeviceName={0},在线状态:{1}", device.Name, device.IsOnline); // } // } // else if (listDevices.Count == 0) // { // Console.WriteLine("设备个数为0"); // } //} //else //{ // Console.WriteLine(opResult.Msg); //} #endregion #region ——添加设备—— //ResultMsg<int> opResult = tc.AddDevice(); //if (opResult.IsSuccess()) //{ // Console.WriteLine("添加设备成功,新添加的设备ID{0}", opResult.ResultObj); //} //else //{ // Console.WriteLine(opResult.Msg); //} #endregion #region ——更新设备—— //ResultMsg<Result> opResult = tc.UpdateDevice(); //if (opResult.IsSuccess()) //{ // Console.WriteLine("更新设备成功,设备状态{0}", opResult.ResultObj.Status); //} //else //{ // Console.WriteLine(opResult.Msg); //} #endregion #region ——删除设备—— //ResultMsg<Result> opResult = tc.DeleteDeviceByDeviceId(); //if (opResult.IsSuccess()) //{ // Console.WriteLine("删除设备成功{0}", opResult.ResultObj.Msg); //} //else //{ // Console.WriteLine(opResult.Msg); //} #endregion #endregion #region ——设备传感器API测试—— #region ——查询单个传感器—— //ResultMsg<SensorBaseInfoDTO> opResult = tc.GetSensorOfDevice(); //if (opResult.IsSuccess()) //{ // SensorBaseInfoDTO sensor = opResult.ResultObj; // Console.WriteLine("传感器名称:{0}", sensor.Name); //} //else //{ // Console.WriteLine(opResult.Msg); //} #endregion #region ——模糊查询传感器—— //ResultMsg<List<SensorBaseInfoDTO>> opResult = tc.GetSensorsOfDevice(); //if (opResult.IsSuccess()) //{ // List<SensorBaseInfoDTO> listSensors = opResult.ResultObj; // if (listSensors.Count > 0) // { // foreach (var sensor in listSensors) // { // Console.WriteLine("DeviceName={0}", sensor.Name); // } // } // else if (listSensors.Count == 0) // { // Console.WriteLine("传感器个数为0"); // } //} //else //{ // Console.WriteLine(opResult.Msg); //} #endregion #region ——添加新的传感器—— //ResultMsg<int> opResult = tc.AddSensorOfDevice(NLECloudSDK.Enum.SensorType.Camera); //if (opResult.IsSuccess()) //{ // Console.WriteLine("添加成功,新添加的ID:{0}", opResult.ResultObj); //} //else //{ // Console.WriteLine(opResult.Msg); //} #endregion #region ——更新传感器—— //ResultMsg<Result> opResult = tc.UpdateSensorOfDevice(NLECloudSDK.Enum.SensorType.Actuator); //if (opResult.IsSuccess()) //{ // Console.WriteLine("更新成功"); //} //else //{ // Console.WriteLine(opResult.Msg); //} #endregion #region ——删除某个传感器—— //ResultMsg<Result> opResult = tc.DeleteSensorOfDevice(); //if (opResult.IsSuccess()) //{ // Console.WriteLine("删除成功"); //} //else //{ // Console.WriteLine(opResult.Msg); //} #endregion #endregion #region ——传感数据API测试—— #region ——新增传感数据—— //ResultMsg<Result> opResult = tc.AddDatasOfSensors(); //if (opResult.IsSuccess()) //{ // Console.WriteLine("新增数据成功"); //} //else //{ // Console.WriteLine(opResult.Msg); //} #endregion #region ——查询传感数据—— //ResultMsg<SensorDataInfoDTO> opResult = tc.GetDatasOfSensors(); //if (opResult.IsSuccess()) //{ // Console.WriteLine("设备ID:{0}", opResult.ResultObj.DeviceId); //} //else //{ // Console.WriteLine(opResult.Msg); //} #endregion #endregion #region ——命令API测试—— ResultMsg <Result> opResult = tc.CmdDeviced(); if (opResult.IsSuccess()) { Console.WriteLine("命令发送,操作成功"); } else { Console.WriteLine(opResult.Msg); } #endregion Console.ReadKey(); }
/// <summary> /// 云平台登录 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnLogin_Click(object sender, RoutedEventArgs e) { if (txtUserName.Text.Trim() == "" || this.txtPasswd.Password.Trim() == "") { MessageBox.Show("请输入登录用户名和密码!"); return; } //1、根据该API接口 的请求参数中 得知需要创建个Body Parameters的参数对象,下面创建一个该类的对象 AccountLoginDTO DTO = new AccountLoginDTO(); DTO.Account = txtUserName.Text.Trim(); //帐号为云平台注册的手机号或用户名等 DTO.Password = txtPasswd.Password.Trim(); //密码为云平台注册的帐号密码 //3、定义该API接口返回的对象,初始化为空 ResultMsg <AccountLoginResultDTO> qry = SDK.UserLogin(DTO); Out(qry, txtLoginAPI.Text, DTO.DTOToJson()); //请求成功 if (qry.IsSuccess()) { txtToken.Text = qry.ResultObj.AccessToken; // HttpReqEntity req = new HttpReqEntity(); req.Method = HttpMethod.POST; req.Headers.Add("AccessToken", txtToken.Text); SensorDataListAddDTO data = new SensorDataListAddDTO() { DeviceId = 164, }; var aDatasDTO = new List <SensorDataAddDTO>(); SensorDataAddDTO bb = new SensorDataAddDTO() { Tag = "nl_displacement" }; aDatasDTO.Add(bb); List <SensorDataPointDTO> cc = new List <SensorDataPointDTO>(); cc.Add(new SensorDataPointDTO() { Value = 23123123 }); cc.Add(new SensorDataPointDTO() { Value = 2222 }); bb.PointDTO = cc; bb = new SensorDataAddDTO() { Tag = "nl_thermocouple" }; aDatasDTO.Add(bb); cc = new List <SensorDataPointDTO>(); cc.Add(new SensorDataPointDTO() { Value = true }); cc.Add(new SensorDataPointDTO() { Value = false }); bb.PointDTO = cc; data.DatasDTO = aDatasDTO; req.Datas = JsonFormatter.Serialize(data); var a = RequestAPIHelper.RequestServer <HttpReqEntity>("http://api0.nlecloud.com/v2/devices/164/Datas", req); string sdf = a.Msg; } }