Beispiel #1
0
        public void subscriptionHandler(object sender, MqttMsgPublishEventArgs e)
        {
            try{
                string          result     = System.Text.Encoding.UTF8.GetString(e.Message);
                var             serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
                DeviceActionReq fwData;
                int             rc;
                string          msg;

                switch (e.Topic)
                {
                case DM_RESPONSE_TOPIC:
                    var response = serializer.Deserialize <DMResponse>(result);
                    var itm      = collection.Find(x => x.reqID == response.reqId);
                    if (itm is DMRequest)
                    {
                        log.Info("[" + response.rc + "]  " + itm.topic + " of ReqId  " + itm.reqID);
                        if (this.mgmtCallback != null)
                        {
                            this.mgmtCallback(itm.reqID, response.rc);
                        }
                        collection.Remove(itm);
                    }
                    if (this.isSync)
                    {
                        oSignalEvent.Set();
                        oSignalEvent.Dispose();
                        oSignalEvent = new ManualResetEvent(false);
                    }
                    break;

                case DM_REBOOT_TOPIC:
                    var res = serializer.Deserialize <DMResponse>(result);
                    log.Info("Device Reboot action called with ReqId : " + res.reqId);
                    if (this.actionCallback != null)
                    {
                        this.actionCallback(res.reqId, "reboot");
                    }
                    break;

                case DM_FACTORY_RESET:
                    var resetResponse = serializer.Deserialize <DMResponse>(result);
                    log.Info("Device Factory rest action called with ReqId : " + resetResponse.reqId);
                    if (this.actionCallback != null)
                    {
                        this.actionCallback(resetResponse.reqId, "reset");
                    }
                    break;

                case DM_UPDATE_TOPIC:
                    fwData = serializer.Deserialize <DeviceActionReq>(result);
                    var fields = fwData.d.fields;
                    for (int i = 0; i < fields.Length; i++)
                    {
                        if (fields[i].field == "mgmt.firmware")
                        {
                            this.fw = fields[i].value;
                            break;
                        }
                    }
                    if (this.fw != null)
                    {
                        sendResponse(fwData.reqId, 204, "");
                    }

                    break;

                case DM_OBSERVE_TOPIC:
                    fwData = serializer.Deserialize <DeviceActionReq>(result);
                    sendResponse(fwData.reqId, 200, "");
                    break;

                case DM_FIRMWARE_DOWNLOAD_TOPIC:
                    fwData = serializer.Deserialize <DeviceActionReq>(result);
                    rc     = RESPONSECODE_ACCEPTED;
                    msg    = "";
                    if (this.fw.state != UPDATESTATE_IDLE)
                    {
                        rc  = RESPONSECODE_BAD_REQUEST;
                        msg = "Cannot download as the device is not in idle state";
                        sendResponse(fwData.reqId, rc, msg);
                        break;
                    }
                    if (this.fwCallback != null)
                    {
                        this.fwCallback("download", this.fw);
                    }

                    break;

                case DM_FIRMWARE_UPDATE_TOPIC:
                    fwData = serializer.Deserialize <DeviceActionReq>(result);
                    rc     = RESPONSECODE_ACCEPTED;
                    msg    = "";
                    if (this.fw.state != UPDATESTATE_DOWNLOADED)
                    {
                        rc  = RESPONSECODE_BAD_REQUEST;
                        msg = "Firmware is still not successfully downloaded.";
                        sendResponse(fwData.reqId, rc, msg);
                        break;
                    }
                    if (this.fwCallback != null)
                    {
                        this.fwCallback("update", this.fw);
                    }

                    break;

                case DM_CANCEL_OBSERVE_TOPIC:
                    fwData = serializer.Deserialize <DeviceActionReq>(result);
                    sendResponse(fwData.reqId, 200, "");
                    break;

                default:

                    break;
                }
            }
            catch (Exception ex)
            {
                log.Error("Exception has occurred in subscriptionHandler ", ex);
            }
        }
        public void subscriptionHandler(object sender, MqttMsgPublishEventArgs e)
        {
            try{
                string result = System.Text.Encoding.UTF8.GetString(e.Message);
                log.Info(e.Topic + "  with  " + result);

                var             serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
                DeviceActionReq fwData;
                int             rc;
                string          msg;
                //string pat = @"(^iotdm-1/type/)(.*)(/id/)(.*)(/response)";
                Regex regx;
                Match match;
                // Instantiate the regular expression object.
                regx = new Regex(DM_RESPONSE_TOPIC_REGX, RegexOptions.IgnoreCase);
                //if(e.Topic == string.Format(DM_RESPONSE_TOPIC,this.gatewayDeviceType,this.gatewayDeviceID) ){
                if (regx.IsMatch(e.Topic))
                {
                    var response = serializer.Deserialize <DMResponse>(result);
                    var itm      = collection.Find(x => x.reqID == response.reqId);
                    if (itm is DMRequest)
                    {
                        log.Info("[" + response.rc + "]  " + itm.topic + " of ReqId  " + itm.reqID);
                        if (this.mgmtCallback != null)
                        {
                            this.mgmtCallback(itm.reqID, response.rc);
                        }
                        collection.Remove(itm);
                    }
                    if (this.isSync)
                    {
                        oSignalEvent.Set();
                        oSignalEvent.Dispose();
                        oSignalEvent = new ManualResetEvent(false);
                    }
                    return;
                }
                regx  = new Regex(DM_REBOOT_TOPIC_REGX, RegexOptions.IgnoreCase);
                match = regx.Match(e.Topic);
                if (match.Success)
                {
                    var res = serializer.Deserialize <DMResponse>(result);
                    if (match.Groups[2].Value == this.gatewayDeviceType && match.Groups[4].Value == this.gatewayDeviceID)
                    {
                        log.Info("Gateway Reboot action called with ReqId : " + res.reqId);
                        if (this.actionCallback != null)
                        {
                            this.actionCallback(res.reqId, "reboot");
                        }
                    }
                    else
                    {
                        log.Info("Gateway connected device Reboot action called with ReqId : " + res.reqId);
                        if (this.connectedDeviceActionCallback != null)
                        {
                            this.connectedDeviceActionCallback(match.Groups[2].Value, match.Groups[4].Value, res.reqId, "reboot");
                        }
                    }
                    return;
                }


                regx  = new Regex(DM_FACTORY_RESET_REGX, RegexOptions.IgnoreCase);
                match = regx.Match(e.Topic);
                if (match.Success)
                {
                    var resetResponse = serializer.Deserialize <DMResponse>(result);
                    if (match.Groups[2].Value == this.gatewayDeviceType && match.Groups[4].Value == this.gatewayDeviceID)
                    {
                        log.Info("Gateway Factory rest action called with ReqId : " + resetResponse.reqId);
                        if (this.actionCallback != null)
                        {
                            this.actionCallback(resetResponse.reqId, "reset");
                        }
                    }
                    else
                    {
                        log.Info("Gateway connected device Factory rest action called with ReqId : " + resetResponse.reqId);
                        if (this.connectedDeviceActionCallback != null)
                        {
                            this.connectedDeviceActionCallback(match.Groups[2].Value, match.Groups[4].Value, resetResponse.reqId, "reset");
                        }
                    }
                    return;
                }
                regx  = new Regex(DM_UPDATE_TOPIC_REGX, RegexOptions.IgnoreCase);
                match = regx.Match(e.Topic);
                if (match.Success)
                {
                    fwData = serializer.Deserialize <DeviceActionReq>(result);
                    if (match.Groups[2].Value == this.gatewayDeviceType && match.Groups[4].Value == this.gatewayDeviceID)
                    {
                        var fields = fwData.d.fields;
                        for (int i = 0; i < fields.Length; i++)
                        {
                            if (fields[i].field == "mgmt.firmware")
                            {
                                this.fw = fields[i].value;
                                break;
                            }
                        }
                        if (this.fw != null)
                        {
                            sendResponse(fwData.reqId, 204, "");
                        }
                    }
                    else
                    {
                        if (this.connectedDevicefwActionCallback != null)
                        {
                            this.connectedDevicefwActionCallback(match.Groups[2].Value, match.Groups[4].Value, "info", fwData);
                        }
                    }
                    return;
                }

                regx  = new Regex(DM_OBSERVE_TOPIC_REGX, RegexOptions.IgnoreCase);
                match = regx.Match(e.Topic);
                if (match.Success)
                {
                    //if(e.Topic ==  string.Format(DM_OBSERVE_TOPIC,this.gatewayDeviceType,this.gatewayDeviceID) ){
                    fwData = serializer.Deserialize <DeviceActionReq>(result);
                    sendResponse(fwData.reqId, 200, "", match.Groups[2].Value, match.Groups[4].Value);
                    //	}
                    return;
                }
                regx  = new Regex(DM_FIRMWARE_DOWNLOAD_TOPIC_REGX, RegexOptions.IgnoreCase);
                match = regx.Match(e.Topic);
                if (match.Success)
                {
                    fwData = serializer.Deserialize <DeviceActionReq>(result);
                    if (match.Groups[2].Value == this.gatewayDeviceType && match.Groups[4].Value == this.gatewayDeviceID)
                    {
                        rc  = RESPONSECODE_ACCEPTED;
                        msg = "";
                        if (this.fw.state != UPDATESTATE_IDLE)
                        {
                            rc  = RESPONSECODE_BAD_REQUEST;
                            msg = "Cannot download as the device is not in idle state";
                            sendResponse(fwData.reqId, rc, msg);
                            return;
                        }
                        if (this.fwCallback != null)
                        {
                            this.fwCallback("download", this.fw);
                        }
                    }
                    else
                    {
                        if (this.connectedDevicefwActionCallback != null)
                        {
                            this.connectedDevicefwActionCallback(match.Groups[2].Value, match.Groups[4].Value, "download", fwData);
                        }
                    }
                    return;
                }
                regx  = new Regex(DM_FIRMWARE_UPDATE_TOPIC_REGX, RegexOptions.IgnoreCase);
                match = regx.Match(e.Topic);
                if (match.Success)
                {
                    fwData = serializer.Deserialize <DeviceActionReq>(result);

                    if (match.Groups[2].Value == this.gatewayDeviceType && match.Groups[4].Value == this.gatewayDeviceID)
                    {
                        rc  = RESPONSECODE_ACCEPTED;
                        msg = "";
                        if (this.fw.state != UPDATESTATE_DOWNLOADED)
                        {
                            rc  = RESPONSECODE_BAD_REQUEST;
                            msg = "Firmware is still not successfully downloaded.";
                            sendResponse(fwData.reqId, rc, msg);
                            return;
                        }
                        if (this.fwCallback != null)
                        {
                            this.fwCallback("update", this.fw);
                        }
                    }
                    else
                    {
                        if (this.connectedDevicefwActionCallback != null)
                        {
                            this.connectedDevicefwActionCallback(match.Groups[2].Value, match.Groups[4].Value, "update", fwData);
                        }
                    }
                    return;
                }

                regx  = new Regex(DM_CANCEL_OBSERVE_TOPIC_REGX, RegexOptions.IgnoreCase);
                match = regx.Match(e.Topic);
                if (match.Success)
                {
                    //if(e.Topic ==  string.Format(DM_CANCEL_OBSERVE_TOPIC,this.gatewayDeviceType,this.gatewayDeviceID) ){
                    fwData = serializer.Deserialize <DeviceActionReq>(result);
                    sendResponse(fwData.reqId, 200, "", match.Groups[2].Value, match.Groups[4].Value);
                    //	}
                    return;
                }
            }
            catch (Exception ex)
            {
                log.Error("Exception has occurred in subscriptionHandler ", ex);
            }
        }