Exemple #1
0
        public override void OnSubdevCommand(string requestId, Command command)
        {
            if (command.deviceId == null)
            {
                return;
            }

            string nodeId = IotUtil.GetNodeIdFromDeviceId(command.deviceId);

            if (nodeId == null)
            {
                return;
            }

            Session session = nodeIdToSesseionDic[nodeId];

            if (session == null)
            {
                Log.Error("session is null ,nodeId is " + nodeId);

                return;
            }

            // 这里我们直接把command对象转成string发给子设备,实际场景中可能需要进行一定的编解码转换
            session.channel.WriteAndFlushAsync(JsonUtil.ConvertObjectToJsonString(command));

            // 为了简化处理,我们在这里直接回命令响应。更合理做法是在子设备处理完后再回响应
            GetClient().RespondCommand(requestId, new CommandRsp(0));
            Log.Info("WriteAndFlushAsync " + command);
        }
Exemple #2
0
        public override void OnSubdevPropertiesSet(string requestId, PropsSet propsSet)
        {
            if (propsSet.deviceId == null)
            {
                return;
            }

            string nodeId = IotUtil.GetNodeIdFromDeviceId(propsSet.deviceId);

            if (nodeId == null)
            {
                return;
            }

            Session session = nodeIdToSesseionDic[nodeId];

            if (session == null)
            {
                Log.Error("session is null ,nodeId:" + nodeId);

                return;
            }

            // 这里我们直接把对象转成string发给子设备,实际场景中可能需要进行一定的编解码转换
            session.channel.WriteAndFlushAsync(JsonUtil.ConvertObjectToJsonString(propsSet));

            // 为了简化处理,我们在这里直接回响应。更合理做法是在子设备处理完后再回响应
            GetClient().RespondPropsSet(requestId, IotResult.SUCCESS);

            Log.Info("WriteAndFlushAsync " + propsSet);
        }
        private int RmvSubDeviceToFile(SubDevicesInfo subDevicesInfo)
        {
            try
            {
                string content = ReadJsonFile();

                SubDevInfo subDevInfo = JsonUtil.ConvertJsonStringToObject <SubDevInfo>(content);

                if (subDevInfo.subdevices == null)
                {
                    return(0);
                }

                foreach (DeviceInfo dev in subDevicesInfo.devices)
                {
                    subDevInfo.subdevices.Remove(dev.nodeId);
                    subDevInfo.version = subDevicesInfo.version;
                }

                File.WriteAllText(path, JsonUtil.ConvertObjectToJsonString(subDevInfo));
            }
            catch (Exception ex)
            {
                Log.Error("remove sub device fail in json file");

                return(-1);
            }

            return(0);
        }
Exemple #4
0
        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\"}"));
        }
Exemple #5
0
        public override string ToString()
        {
            StringBuilder sb = new StringBuilder();

            sb.Append("SubDevInfo{");
            sb.Append("version=" + version);
            sb.Append(", subdevices=" + JsonUtil.ConvertObjectToJsonString(subdevices));
            sb.Append("}");
            return(sb.ToString());
        }
Exemple #6
0
        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 CommandRsp OnCommand(Command command)
        {
            Dictionary <string, string> dic = new Dictionary <string, string>();

            if (!commands.ContainsKey(command.commandName))
            {
                dic.Clear();
                dic.Add("result", "command not found in commands");
                Log.Error("command not found in commands" + command.commandName);
                return(new CommandRsp(CommandRsp.FAIL, dic));
            }

            MethodInfo methodInfo = commands[command.commandName];

            if (methodInfo == null)
            {
                dic.Clear();
                dic.Add("result", "command not found");
                Log.Error("command not found " + command.commandName);
                return(new CommandRsp(CommandRsp.FAIL, dic));
            }

            try
            {
                dic.Clear();
                dic.Add("result", "success");

                // 反射调用方法
                MethodInfo nonstaticMethod = deviceServiceType.GetMethod(methodInfo.Name);

                // 非静态方法调用需要类实例
                object obj = nonstaticMethod.Invoke(deviceService, new string[] { JsonUtil.ConvertObjectToJsonString(command.paras) });

                return((CommandRsp)obj);
            }
            catch (Exception e)
            {
                Log.Error(e.Message);
                dic.Clear();
                dic.Add("result", e.Message);
                return(new CommandRsp(CommandRsp.FAIL, dic));
            }
        }
 /// <summary>
 /// 上报设备消息
 /// 如果需要上报子设备消息,需要调用DeviceMessage的setDeviceId接口设置为子设备的设备id
 /// </summary>
 /// <param name="deviceMessage">设备消息</param>
 public void ReportDeviceMessage(DeviceMessage deviceMessage)
 {
     Report(new PubMessage(string.Format(CommonTopic.TOPIC_MESSAGES_UP, deviceId), JsonUtil.ConvertObjectToJsonString(deviceMessage)));
 }
 /// <summary>
 /// 上报写属性响应
 /// </summary>
 /// <param name="requestId">请求id,响应的请求id必须和请求的一致</param>
 /// <param name="iotResult">写属性结果</param>
 public void RespondPropsSet(string requestId, IotResult iotResult)
 {
     Report(new PubMessage(CommonTopic.TOPIC_SYS_PROPERTIES_SET_RESPONSE + "=" + requestId, JsonUtil.ConvertObjectToJsonString(iotResult)));
 }
 /// <summary>
 /// 上报命令响应
 /// </summary>
 /// <param name="requestId">请求id,响应的请求id必须和请求的一致</param>
 /// <param name="commandRsp">命令响应</param>
 public void RespondCommand(string requestId, CommandRsp commandRsp)
 {
     Report(new PubMessage(CommonTopic.TOPIC_COMMANDS_RESPONSE + "=" + requestId, JsonUtil.ConvertObjectToJsonString(commandRsp)));
 }
 public override string ToString()
 {
     return(JsonUtil.ConvertObjectToJsonString(this));
 }
Exemple #12
0
        /// <summary>
        /// 批量上报子设备属性
        /// </summary>
        /// <param name="deviceProperties">子设备属性列表</param>
        public void ReportSubDeviceProperties(List <DeviceProperty> deviceProperties)
        {
            string msg = "{\"devices\":" + JsonUtil.ConvertObjectToJsonString(deviceProperties) + "}";

            GetClient().Report(new PubMessage(CommonTopic.TOPIC_SYS_GATEWAY_SUB_DEVICES, msg));
        }
 public PubMessage(string topic, DeviceEvents events)
 {
     this.Topic   = topic;
     this.Message = JsonUtil.ConvertObjectToJsonString(events);
 }
 /// <summary>
 /// 上报命令响应(respondCommand)
 /// </summary>
 /// <param name="requestId">请求id,响应的请求id必须和请求的一致</param>
 /// <param name="commandRsp">命令响应</param>
 public PubMessage(string requestId, CommandRsp commandRsp)
 {
     this.Topic   = CommonTopic.TOPIC_COMMANDS_RESPONSE + "=" + requestId;
     this.Message = JsonUtil.ConvertObjectToJsonString(commandRsp);
 }
 public PubMessage(string topic, DeviceProperties deviceProperties)
 {
     this.Topic   = topic;
     this.Message = "{\"services\":" + JsonUtil.ConvertObjectToJsonString(deviceProperties) + "}";
 }
 /// <summary>
 /// 上报设备属性(reportProperties)
 /// </summary>
 /// <param name="properties">设备属性列表</param>
 public PubMessage(List <ServiceProperty> properties)
 {
     this.Topic   = CommonTopic.TOPIC_PROPERTIES_REPORT;
     this.Message = "{\"services\":" + JsonUtil.ConvertObjectToJsonString(properties) + "}";
 }