/// <summary> /// 新增传感数据 /// </summary> /// <param name="deviceId">设备ID</param> /// <param name="data">传感数据</param> /// <param name="token"></param> /// <returns></returns> public Result AddSensorDatas(Int32 deviceId, SensorDataListAddDTO data, String token = null) { var result = new Result(); //验证 var vry = PrimaryKeyVerify(deviceId, ref token); if (!vry.IsSuccess()) { vry.CopyTo(result); return(result); } //1、先定义该API接口路径,可以从http://api.nlecloud.com/页面的得知 String apiPath = String.Format("{0}/{1}", mApiHost, NLECloudAPIUrl.DatasOfSensorUrl); //2、根据该API接口的请求参数中 得知需要创建个URI Parameters String类型参数,所以该参数直接跟在apiPath中 apiPath = apiPath.Replace("{deviceid}", deviceId.ToString());//将API地址中的{projectid}替换成真实项目ID //3、由于调用该API需要Token,所以我们定义了一个通用的对象HttpReqEntity,在AccessToken当成头部请求信息提交过去 HttpReqEntity req = new HttpReqEntity(); req.Method = HttpMethod.POST; req.Datas = JsonFormatter.Serialize(data); req.Headers.Add("AccessToken", token); //4、定义该API接口返回的对象 result = RequestAPIHelper.RequestServer <HttpReqEntity>(apiPath, req); return(result); }
/// <summary> /// 执行API测试调用 /// </summary> private static void ExeccuteApiTestCall() { #region -- 项目测试 -- //查询单个项目。===================请修改为自己的项目ID dynamic qry = SDK.GetProjectInfo(projectId, Token); Console.WriteLine("查询单个项目返回JSON:" + Environment.NewLine); Console.WriteLine(SerializeToJson(qry) + Environment.NewLine); //模糊查询项目。===================请修改为自己的查询条件 var query = new ProjectFuzzyQryPagingParas() { PageIndex = 1, PageSize = 20, StartDate = "2009-01-01", EndDate = "2018-06-14", }; qry = SDK.GetProjects(query, Token); Console.WriteLine("模糊查询项目返回JSON:" + Environment.NewLine); Console.WriteLine(SerializeToJson(qry) + Environment.NewLine); //查询项目所有设备的传感器。===================请修改为自己的项目ID qry = SDK.GetProjectSensors(projectId, Token); Console.WriteLine("查询项目所有设备的传感器返回JSON:" + Environment.NewLine); Console.WriteLine(SerializeToJson(qry) + Environment.NewLine); #endregion #region -- 设备测试 -- //批量查询设备最新数据。===================请修改为自己的设备ID串 qry = SDK.GetDevicesDatas(devIds, Token); Console.WriteLine("批量查询设备最新数据返回JSON:" + Environment.NewLine); Console.WriteLine(SerializeToJson(qry) + Environment.NewLine); //批量查询设备的在线状态。===================请修改为自己的设备ID串 qry = SDK.GetDevicesStatus(devIds, Token); Console.WriteLine("批量查询设备的在线状态返回JSON:" + Environment.NewLine); Console.WriteLine(SerializeToJson(qry) + Environment.NewLine); //查询单个设备。===================请修改为自己的设备ID qry = SDK.GetDeviceInfo(deviceId, Token); Console.WriteLine("查询单个设备返回JSON:" + Environment.NewLine); Console.WriteLine(SerializeToJson(qry) + Environment.NewLine); //模糊查询设备 var query1 = new DeviceFuzzyQryPagingParas() { DeviceIds = deviceId.ToString(), PageIndex = 1, PageSize = 20, StartDate = "2009-01-01", EndDate = "2018-06-14", }; qry = SDK.GetDevices(query1, Token); Console.WriteLine("模糊查询设备返回JSON:" + Environment.NewLine); Console.WriteLine(SerializeToJson(qry) + Environment.NewLine); //添加个新设备 var device = new DeviceAddUpdateDTO() { Protocol = 1, IsTrans = true, ProjectIdOrTag = projectId.ToString(), Name = "新添加的设备", Tag = "newDevice2018" }; qry = SDK.AddDevice(device, Token); Console.WriteLine("添加个新设备返回JSON:" + Environment.NewLine); Console.WriteLine(SerializeToJson(qry) + Environment.NewLine); if (qry.IsSuccess() && qry.ResultObj > 0) { var newDeviceId = qry.ResultObj; //更新某个设备 device = new DeviceAddUpdateDTO() { Protocol = 1, IsTrans = true, ProjectIdOrTag = projectId.ToString(), Name = "新添加的设备(更新后)", Tag = "newUpdateDevice" }; qry = SDK.UpdateDevice(newDeviceId, device, Token); Console.WriteLine("更新某个设备返回JSON:" + Environment.NewLine); Console.WriteLine(SerializeToJson(qry) + Environment.NewLine); //删除某个设备 qry = SDK.DeleteDevice(newDeviceId, Token); Console.WriteLine("删除某个设备返回JSON:" + Environment.NewLine); Console.WriteLine(SerializeToJson(qry) + Environment.NewLine); } #endregion #region -- 设备传感器API测试 -- //查询单个传感器。===================请修改为自己的设备ID,传感标识名ApiTag qry = SDK.GetSensorInfo(deviceId, apiTag, Token); Console.WriteLine("查询单个传感器返回JSON:" + Environment.NewLine); Console.WriteLine(SerializeToJson(qry) + Environment.NewLine); //模糊查询传感器。===================请修改为自己的设备ID串 qry = SDK.GetSensors(deviceId, "", Token); Console.WriteLine("模糊查询传感器返回JSON:" + Environment.NewLine); Console.WriteLine(SerializeToJson(qry) + Environment.NewLine); //添加一个新的传感器。===================请修改为自己的设备ID,传感标识名ApiTag var newApiTag = "newsensor"; SensorAddUpdate sensor = new SensorAddUpdate() { ApiTag = newApiTag, DataType = 1, Name = "新的传感器", Unit = "C", }; //注意:创建对象是遵循以下类别创建对象 //传感器:为SensorAddUpdate对象 //执行器:为ActuatorAddUpdate对象 //摄像头:为CameraAddUpdate对象 //这里模拟创建传感SensorAddUpdate对象 qry = SDK.AddSensor <SensorAddUpdate>(deviceId, sensor, Token); Console.WriteLine("添加一个新的传感器返回JSON:" + Environment.NewLine); Console.WriteLine(SerializeToJson(qry) + Environment.NewLine); if (qry.IsSuccess() && qry.ResultObj > 0) { //更新某个传感器。===================请修改为自己的设备ID,传感标识名ApiTag sensor = new SensorAddUpdate() { Name = "新的传感器(更新后)" }; qry = SDK.UpdateSensor <SensorAddUpdate>(deviceId, newApiTag, sensor, Token); Console.WriteLine("更新某个传感器返回JSON:" + Environment.NewLine); Console.WriteLine(SerializeToJson(qry) + Environment.NewLine); //删除某个传感器。===================请修改为自己的设备ID,传感标识名ApiTag qry = SDK.DeleteSensor(deviceId, newApiTag, Token); Console.WriteLine("删除某个传感器返回JSON:" + Environment.NewLine); Console.WriteLine(SerializeToJson(qry) + Environment.NewLine); } #endregion #region -- 传感数据API测试 -- //新增传感数据。===================请修改为自己的设备ID,传感标识名ApiTag var sensorData1 = new SensorDataAddDTO() { ApiTag = apiTag }; sensorData1.PointDTO = new List <SensorDataPointDTO>() { new SensorDataPointDTO() { Value = 3000 }, new SensorDataPointDTO() { Value = 5500 } }; var apiTag2 = "apiTagDemo"; SensorDataAddDTO sensorData2 = new SensorDataAddDTO() { ApiTag = apiTag2 }; sensorData2.PointDTO = new List <SensorDataPointDTO>() { new SensorDataPointDTO() { Value = "dataDemo" } }; var data = new SensorDataListAddDTO(); data.DatasDTO = new List <SensorDataAddDTO>() { sensorData1, sensorData2 }; qry = SDK.AddSensorDatas(deviceId, data, Token); Console.WriteLine("新增传感数据返回JSON:" + Environment.NewLine); Console.WriteLine(SerializeToJson(qry) + Environment.NewLine); //模糊查询传感数据。===================请修改为自己的设备ID,传感标识名ApiTag var query2 = new SensorDataFuzzyQryPagingParas() { DeviceID = deviceId, Method = 6, //TimeAgo = 30, //ApiTags = "m_waterPH,m_waterNTU,m_waterConduct", StartDate = "2018-09-13 12:06:09 ", Sort = "DESC", PageSize = 30, PageIndex = 1 }; System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch(); sw.Start(); qry = SDK.GetSensorDatas(query2, Token); sw.Stop(); var tmp = ((ResultMsg <SensorDataInfoDTO>)qry); if (tmp.IsSuccess() && tmp.ResultObj != null) { } Console.WriteLine("查询传感数据返回JSON:" + Environment.NewLine); Console.WriteLine(SerializeToJson(qry) + Environment.NewLine); //聚合查询传感数据。===================请修改为自己的设备ID,传感标识名ApiTag var query3 = new SensorDataJuHeQryPagingParas() { DeviceID = deviceId, //ApiTags = "nl_temperature,nl_fan", GroupBy = 2, Func = "MAX", StartDate = "2018-01-02 12:06:09" }; sw.Restart(); qry = SDK.GroupingSensorDatas(query3, Token); sw.Stop(); Console.WriteLine("聚合查询传感数据返回JSON:" + Environment.NewLine); Console.WriteLine(SerializeToJson(qry) + Environment.NewLine); #endregion #region -- 命令API测试 -- //发送命令。===================请修改为自己的设备ID,标识名ApiTag,控制值 qry = SDK.Cmds(deviceId, actuatorApiTag, 1, Token); Console.WriteLine("发送命令返回JSON:" + Environment.NewLine); Console.WriteLine(SerializeToJson(qry) + Environment.NewLine); #endregion }
/// <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; } }