예제 #1
0
        private void MqttClient_MqttMsgPublishReceived(object sender, uPLibrary.Networking.M2Mqtt.Messages.MqttMsgPublishEventArgs e)
        {
            try
            {
                string topic     = e.Topic;
                string msgString = System.Text.Encoding.UTF8.GetString(e.Message);
                string device    = "";
                string command   = "";
                Match  m         = exDevice.Match(topic);
                if (m.Success)
                {
                    device = m.Groups[1].Value;
                }
                else
                {
                    throw new ApplicationException($"Failed to extract device from topic '{topic}'");
                }
                m = exCmd.Match(topic);
                if (m.Success)
                {
                    command = m.Groups[1].Value;
                }
                else
                {
                    throw new ApplicationException($"Failed to extract command from topic '{topic}'");
                }

                Log($"device={device} cmd={command} topic={topic} msg={msgString}");

                UInt64 address = 0;
                if (UInt64.TryParse(device, System.Globalization.NumberStyles.HexNumber, CultureInfo.CurrentCulture, out address))
                {
                }

                ITransmitPacket pkt = null;
                switch (command.ToLower())
                {
                case "setvalve":
                    pkt = new STankWatererValve();
                    break;

                case "water":
                    pkt = new STankWatererWater();
                    break;

                case "setparameters":
                    pkt = new STankWatererParameters();
                    break;

                case "query":
                    pkt = new STankWatererQueryStats();
                    break;

                case "queryparameters":
                    pkt = new STankWatererQueryParameters();
                    break;

                case "resetcounters":
                    pkt = new STankWatererResetCounters();
                    break;

                case "settemperature":
                    pkt = new STankWatererSetTemperature();
                    break;
                }
                if (pkt != null)
                {
                    dynamic data = JsonConvert.DeserializeObject(msgString);
                    pkt.Init(data);
                    byte[] body = pkt.GetBody();
                    SendXBeeMessage(address, body);
                }
            }
            catch (Exception ex)
            {
                Log(ex.ToString());
            }
        }
예제 #2
0
        private void ProcessMessage(XBeeResponse msg)
        {
            try
            {
                if (msg.getApiId() == XBee.ZB_RX_RESPONSE)
                {
                    ZBRxResponse response = new ZBRxResponse();
                    msg.getZBRxResponse(response);
                    byte[] body = response.getData();

                    string         topic   = "";
                    string         payload = "";
                    SReceivePacket pkt     = null;
                    switch ((EMessageType)body[0])
                    {
                    case EMessageType.TankWatererStats:
                        pkt   = new STankWatererStats(body);
                        topic = $"/tele/TankWaterer/{response.getRemoteAddress64().get().ToString("X16")}/info";
                        break;

                    case EMessageType.TankWatererManual:
                        pkt   = new STankWatererValve(body);
                        topic = $"/tele/TankWaterer/{response.getRemoteAddress64().get().ToString("X16")}/manual";
                        break;

                    case EMessageType.TankWatererParameters:
                        pkt   = new STankWatererParameters(body);
                        topic = $"/tele/TankWaterer/{response.getRemoteAddress64().get().ToString("X16")}/parameters";
                        break;

                    case EMessageType.PotPlantStats:
                        pkt   = new SPotPlantStats(body);
                        topic = $"/tele/PotPlant/{response.getRemoteAddress64().get().ToString("X16")}/info";
                        break;

                    case EMessageType.RainGaugeStats:
                        pkt   = new SRainGaugeStats(body);
                        topic = $"/tele/RainGauge/{response.getRemoteAddress64().get().ToString("X16")}/info";
                        break;
                    }
                    payload = JsonConvert.SerializeObject(pkt);
                    Log(topic + " " + payload);
                    mqttClient.Publish(topic, Encoding.ASCII.GetBytes(payload));
                }
                else if (msg.getApiId() == XBee.ZB_TX_STATUS_RESPONSE)
                {
                    ZBTxStatusResponse response = new ZBTxStatusResponse();
                    msg.getZBTxStatusResponse(response);

                    Log("TxResponse " + (response.isSuccess() ? "OK" : $"Error: {response.getErrorCode()}"));
                }
                else
                {
                    Log($"Got Packet ApiId={msg.getApiId()}");
                }
            }
            catch (Exception ex)
            {
                Log("Unhandled exception while processing message: " + ex.ToString());
            }
        }