public void FunDeviceShadowSample(string serverUri, int port, string deviceId) { // 创建设备 string deviceCertPath = IotUtil.GetRootDirectory() + @"\certificate\deviceCert.pfx"; if (!File.Exists(deviceCertPath)) { Log.Error("请将设备证书放到根目录!"); return; } X509Certificate2 deviceCert = new X509Certificate2(deviceCertPath, "123456"); // 使用证书创建设备,X509证书接入 device = new IoTDevice(serverUri, port, deviceId, deviceCert); if (device.Init() != 0) { return; } device.GetClient().deviceShadowListener = this; string guid = Guid.NewGuid().ToString(); Console.WriteLine(guid); string topic = CommonTopic.TOPIC_SYS_SHADOW_GET + "=" + guid; device.GetClient().Report(new PubMessage(topic, string.Empty)); }
public void FunPropertySample(string serverUri, int port, string deviceId, string deviceSecret) { // 创建设备 IoTDevice device = new IoTDevice(serverUri, port, deviceId, deviceSecret); if (device.Init() != 0) { return; } Dictionary <string, object> json = new Dictionary <string, object>(); // 按照物模型设置属性 json["alarm"] = 1; json["temperature"] = 23.45812; json["humidity"] = 56.89013; json["smokeConcentration"] = 89.56724; ServiceProperty serviceProperty = new ServiceProperty(); serviceProperty.properties = json; serviceProperty.serviceId = "smokeDetector"; // serviceId要和物模型一致 List <ServiceProperty> properties = new List <ServiceProperty>(); properties.Add(serviceProperty); device.GetClient().messagePublishListener = this; device.GetClient().Report(new PubMessage(properties)); }
public void OnPropertiesSet(string requestId, List <ServiceProperty> services) { Console.WriteLine("requestId Set:" + requestId); Console.WriteLine("services Set:" + JsonUtil.ConvertObjectToJsonString(services)); device.GetClient().Report(new PubMessage(CommonTopic.TOPIC_SYS_PROPERTIES_SET_RESPONSE + "=" + requestId, "{\"result_code\": 0,\"result_desc\": \"success\"}")); }
public void FunMessageSample(string serverUri, int port, string deviceId, string deviceSecret) { // 创建设备 IoTDevice device = new IoTDevice(serverUri, port, deviceId, deviceSecret); if (device.Init() != 0) { return; } device.GetClient().Report(new PubMessage(new DeviceMessage("hello11"))); device.GetClient().deviceCustomMessageListener = this; device.GetClient().messagePublishListener = this; // 上报自定义topic消息,注意需要先在平台配置自定义topic,并且topic的前缀已经规定好,固定为:$oc/devices/{device_id}/user/,通过Postman模拟应用侧使用自定义Topic进行命令下发。 string suf_topic = "wpy"; device.GetClient().SubscribeTopic(suf_topic); device.GetClient().Report(new PubMessage(CommonTopic.PRE_TOPIC + suf_topic, "hello raw message ")); ////while (true) ////{ //// Thread.Sleep(5000); ////} }
/// <summary> /// 通过Postman查询和设置平台属性 /// </summary> /// <param name="serverUri"></param> /// <param name="port"></param> /// <param name="deviceId"></param> /// <param name="deviceSecret"></param> public void FunPropertiesSample(string serverUri, int port, string deviceId, string deviceSecret) { // 创建设备 device = new IoTDevice(serverUri, port, deviceId, deviceSecret); if (device.Init() != 0) { return; } device.GetClient().propertyListener = this; }
public void OnCommand(string requestId, string serviceId, string commandName, Dictionary <string, object> paras) { Console.WriteLine("onCommand, serviceId = " + serviceId); Console.WriteLine("onCommand, name = " + commandName); Console.WriteLine("onCommand, paras = " + JsonUtil.ConvertObjectToJsonString(paras)); ////处理命令 Dictionary <string, string> dic = new Dictionary <string, string>(); dic.Add("result", "success"); // 发送命令响应 device.GetClient().Report(new PubMessage(requestId, new CommandRsp(0, dic))); }
public void FunCertificateSample(string serverUri, int port, string deviceId) { string deviceCertPath = IotUtil.GetRootDirectory() + @"\certificate\deviceCert.pfx"; if (!File.Exists(deviceCertPath)) { Log.Error("请将设备证书放到根目录!"); return; } X509Certificate2 deviceCert = new X509Certificate2(deviceCertPath, "123456"); // 使用证书创建设备 IoTDevice device = new IoTDevice(serverUri, port, deviceId, deviceCert); if (device.Init() != 0) { return; } Dictionary <string, object> json = new Dictionary <string, object>(); // 按照物模型设置属性 json["alarm"] = 1; json["temperature"] = 23.45811; json["humidity"] = 56.89012; json["smokeConcentration"] = 89.56723; ServiceProperty serviceProperty = new ServiceProperty(); serviceProperty.properties = json; serviceProperty.serviceId = "smokeDetector"; // serviceId要和物模型一致 List <ServiceProperty> properties = new List <ServiceProperty>(); properties.Add(serviceProperty); device.GetClient().Report(new PubMessage(properties)); }