/// <summary> /// 从用户接口收到数据 /// </summary> /// <param name="Ptype"></param> /// <param name="topic"></param> /// <param name="message"></param> private void _userPorts_UserPortMsgRvSetEvent(IotTopic topic, JObject message) { nlogger.Trace("the message topic is " + topic.ToString() + ", and the message is: " + message); switch (topic) { case IotTopic.ParamT: nlogger.Trace("Receive message from mqtt with topic Topic.ParamT"); break; case IotTopic.Relay: nlogger.Trace("Receive message from mqtt with topic Topic.Relay"); break; case IotTopic.DeviceCmd: iotPublishMessage(IotTopic.Relay); iotPublishMessage(IotTopic.DeviceState); iotPublishMessage(IotTopic.Error); iotPublishMessage(IotTopic.SampleState); iotPublishMessage(IotTopic.InstState); break; default: Console.WriteLine("Unknown message receieved."); break; } }
/// receive message from the mqtt cloud private void _mqttCloud_MessageReceievedEvent(IotTopic topic, string message) { // 初步解析 message,并根据 d_s 标志位判断是显示类型的消息,还是设置类型的消息 // 分别调用各自的事件函数 JObject jMsg = (JObject)JsonConvert.DeserializeObject(message); IotPortReceiveMessageEvent?.Invoke(topic, jMsg); nlogger.Debug("收到来自 mqttCloud 的数据: SubTopic - " + topic.ToString() + " message - " + message); }
/// <summary> /// 向特定接口发布消息 /// </summary> /// <param name="topic"></param> /// <param name="Message"></param> /// <param name="isWait"></param> /// <param name="ports"></param> /// <returns></returns> public bool PublishMessage(IotTopic topic, JObject message, bool isWait = false) { if (!Enable) { return(true); } _mqttCloud.Publish(topic, message.ToString(), isWait); return(true); }
bool InitIotPort(JObject child) { IotTopic[] tpSub = new IotTopic[] { IotTopic.DeviceCmd }; bool confOK = _userPorts.configIotPorts(child, tpSub); _userPorts.IotPortReceiveMessageEvent += _userPorts_UserPortMsgRvSetEvent; TimerTickEndEvent += DeviceStateM_TimerTickEndEvent; StateChangedEvent += DeviceStateM_StateChangedEvent; RelayDeviceMStatusUpdatedEvent += DeviceStateM_RelayDeviceStatusUpdatedEvent; RelayDeviceSStatusUpdatedEvent += DeviceStateM_RelayDeviceStatusUpdatedEvent; ErrorStatusChangedEvent += DeviceStateM_ErrorStatusChangedEvent; DeviceClosedEvent += DeviceStateM_DeviceClosedEvent; SampleStateChangedEvent += DeviceStateM_SampleStateChangedEvent; return(confOK); }
/// <summary> /// 发布主题 /// </summary> /// <param name="Message"></param> public void Publish(IotTopic topic, string Message, bool isWait = false) { if (!Enabled) { return; } try { if (mqttClient == null) { return; } if (mqttClient.IsConnected == false) { mqttClient.ConnectAsync(options); } if (mqttClient.IsConnected == false) { Console.WriteLine("Publish >>Connected Failed! "); return; } string tp = topicDeviceId + "/" + Enum.GetName(typeof(IotTopic), topic); Console.WriteLine("Publish >>Message: " + Message); MqttApplicationMessageBuilder mamb = new MqttApplicationMessageBuilder() .WithTopic(tp) .WithPayload(Message) .WithExactlyOnceQoS() .WithRetainFlag(false); if (isWait == true) { mqttClient.PublishAsync(mamb.Build()).Wait(); } else { mqttClient.PublishAsync(mamb.Build()); } } catch (Exception exp) { Console.WriteLine("Publish exception >>" + exp.Message); } }
public IotMessageBase(IotTopic tp) { Topic = tp; }
public bool iotPublishMessage(IotTopic tp) { switch (tp) { case IotTopic.ParamT: IotParamTMessage jPt = new IotParamTMessage(); jPt.DorS = IotDorS.Display; jPt.paramM = new ParamT(); jPt.paramS = new ParamT(); jPt.Topic = IotTopic.ParamT; float[] pM = new float[10]; tpDeviceM.tpParam.CopyTo(pM, 0); jPt.paramM.setValue(pM); jPt.paramM.TempShow = tpDeviceM.temperatures.Count > 0 ? tpDeviceM.temperatures.Last() : 0.0f; jPt.paramM.PowerShow = tpDeviceM.tpPowerShow; // 波动度 float fluc = 0.0f; tpDeviceM.GetFlucDurCountOrLess(_runningParameters.steadyTimeSec / _runningParameters.readTempIntervalSec, out fluc); jPt.paramM.Fluc = fluc; float[] pS = new float[10]; tpDeviceS.tpParam.CopyTo(pS, 0); jPt.paramS.setValue(pS); jPt.paramS.TempShow = tpDeviceS.temperatures.Count > 0 ? tpDeviceS.temperatures.Last() : 0.0f; jPt.paramS.PowerShow = tpDeviceS.tpPowerShow; // 波动度 tpDeviceS.GetFlucDurCountOrLess(_runningParameters.steadyTimeSec / _runningParameters.readTempIntervalSec, out fluc); jPt.paramS.Fluc = fluc; _userPorts.PublishMessage(IotTopic.ParamT, JObject.FromObject(jPt)); break; case IotTopic.Relay: IotRelay88Message jR = new IotRelay88Message(); jR.DorS = IotDorS.Display; jR.relayM = new Relay8(); jR.relayS = new Relay8(); jR.Topic = IotTopic.Relay; jR.relayM.setValue(ryDeviceM.ryStatus); jR.relayS.setValue(ryDeviceS.ryStatus); _userPorts.PublishMessage(IotTopic.Relay, JObject.FromObject(jR)); break; case IotTopic.DeviceState: IotDeviceStateMessage jSt = new IotDeviceStateMessage(); jSt.DorS = IotDorS.Display; jSt.Topic = IotTopic.DeviceState; jSt.state = _machine.State; _userPorts.PublishMessage(IotTopic.DeviceState, JObject.FromObject(jSt)); break; case IotTopic.Error: IotErrorMessage jEr = new IotErrorMessage(); jEr.DorS = IotDorS.Display; jEr.Topic = IotTopic.Error; jEr.errCnt = _deviceCurrentError; _userPorts.PublishMessage(IotTopic.Error, JObject.FromObject(jEr)); break; case IotTopic.SampleState: IotSampleStateMessage jSample = new IotSampleStateMessage(); jSample.DorS = IotDorS.Display; jSample.Topic = IotTopic.SampleState; jSample.state = _sampleMachine.State; _userPorts.PublishMessage(IotTopic.SampleState, JObject.FromObject(jSample)); break; case IotTopic.InstState: IotInstStateMessage jInstState = new IotInstStateMessage(); jInstState.DorS = IotDorS.Display; jInstState.Topic = IotTopic.InstState; jInstState.InstInfos = new List <InstInfoBase>(); foreach (var itm in _instDevices) { jInstState.InstInfos.Add(itm.GetBasicInfo()); } _userPorts.PublishMessage(IotTopic.InstState, JObject.FromObject(jInstState)); break; case IotTopic.InstValue: // todo: add the sensor value nlogger.Error("code error"); break; default: break; } return(true); }