예제 #1
0
        private void BtnSendData_Click(object sender, EventArgs e)
        {
            string mqttstate = tSStatusLblMQTT.Text;

            if ((mqttstate == "subscribe") || (mqttstate == "connect") || (mqttstate == "unsubscribe"))
            {
                //온도와 습도 입력 Text값을 JSON 형태로 플랫폼 서버로 전송
                InSensors inSensors = new InSensors();
                inSensors.Temperature = tBoxDataTemp.Text;
                inSensors.Humidity    = tBoxDataHumi.Text;
                string text = JsonConvert.SerializeObject(inSensors);
                logPrintInTextBox(text, "");

                string base64 = Convert.ToBase64String(Encoding.UTF8.GetBytes(text));

                // Data send to SERVER (string original)
                this.sendDataOut(commands["mqttpub"] + tBoxMqttTopic.Text + "\"," + base64.Length);
                tBoxActionState.Text = states.mqttpub.ToString();

                MQTT_Msg = base64;

                timer1.Start();
            }
            else
            {
                logPrintInTextBox("MQTT 서버 연결 상태를 확인하시고 재시도하세요.", "");
            }
        }
예제 #2
0
        // 수신한 응답 값과 특정 값과 일치하는 경우
        // 응답을 받고 후 작업이 필요한지 확인한다.
        void parseReceiveData(string s, string str2)
        {
            if (s == "OK")
            {
                states state = (states)Enum.Parse(typeof(states), tBoxActionState.Text);
                switch (state)
                {
                case states.setservertype:
                    // LWM2M bootstrap 자동 요청 순서
                    // (servertype) - (endpointpame) - mbsps - bootstrap
                    // EndPointName 플랫폼 device ID 설정
                    //AT+QLWM2M="enps",0,<service code>
                    //this.sendDataOut(commands["setepns"] + "ASN-CSE-D-6399301537-FOTA" + "\"");
                    this.sendDataOut(commands["setepns"] + tBoxSVCIP.Text + "\"");
                    tBoxActionState.Text = states.setepns.ToString();

                    timer1.Start();
                    nextcommand = "skip";
                    break;

                case states.setepns:
                    // LWM2M bootstrap 자동 요청 순서
                    // servertype - (endpointpame) - (mbsps) - bootstrap
                    // PLMN 정보 확인 후 진행
                    string imsi = tBoxIMSI.Text;
                    if (imsi.StartsWith("45006"))
                    {
                        string ctn = "0" + imsi.Substring(5, imsi.Length - 5);

                        // Bootstrap Parameter 설정
                        //AT+QLWM2M="mbsps",<service code>,<sn>,<ctn>,<iccid>,<device model>
                        string command = commands["setmbsps"] + tBoxSVCIP.Text + "\",\"";
                        command = command + tBoxDeviceID.Text + "\",\"";
                        command = command + ctn + "\",\"";

                        string iccid = tBoxIccid.Text;
                        command = command + iccid.Substring(iccid.Length - 6, 6) + "\",\"";
                        command = command + tBoxSVCPort.Text + "\"";
                        this.sendDataOut(command);
                        tBoxActionState.Text = states.setmbsps.ToString();

                        timer1.Start();
                        nextcommand = "skip";
                    }
                    else
                    {
                        MessageBox.Show("USIM이 정상인지 확인해주세요.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        nextcommand = "";
                        timer1.Stop();
                    }
                    break;

                case states.setmbsps:
                    // LWM2M bootstrap 자동 요청 순서
                    // servertype - endpointpame - (mbsps) - (bootstrap) 마지막
                    // Bootstrap 요청
                    //AT+QLWM2M="bootstrap",1
                    nextcommand = states.bootstrap.ToString();
                    break;

                case states.setcereg:
                    // LTE network attach 요청하면 정상적으로 attach 성공했는지 확인 필요
                    nextcommand = states.getcereg.ToString();
                    break;

                case states.mqttopen:
                    // OK 수신한 다음에 +QMTOPEN을 기다렸다가 connect 시도해야 함.
                    nextcommand          = "skip";
                    tSStatusLblMQTT.Text = "open";
                    tSProgressMQTT.Value = 50;
                    break;

                case states.automqttopen:
                    // OK 수신한 다음에 +QMTOPEN을 기다렸다가 connect 시도해야 함.
                    nextcommand          = "skip";
                    tSStatusLblMQTT.Text = "open";
                    tSProgressMQTT.Value = 50;
                    break;

                case states.mqttconn:
                    // OK 수신한 다음에 +QMTCONN을 기다렸다가 subscribe 시도해야 함.
                    tSStatusLblMQTT.Text = "connect";
                    nextcommand          = "skip";
                    tSProgressMQTT.Value = 50;
                    break;

                case states.automqttconn:
                    // OK 수신한 다음에 +QMTCONN을 기다렸다가 subscribe 시도해야 함.
                    tSStatusLblMQTT.Text = "connect";
                    nextcommand          = "skip";
                    tSProgressMQTT.Value = 50;
                    break;

                case states.mqttsub:
                    // MQTT 서버 Subscribe 등록 성공
                    tSStatusLblMQTT.Text = "subscribe";
                    tSProgressMQTT.Value = 100;
                    logPrintInTextBox("MQTT 서버에 Subscribe가 성공하였습니다.", "");
                    break;

                case states.mqttunsub:
                    // MQTT 서버 Subscribe 해제
                    tSStatusLblMQTT.Text = "connect";           //서버 연결 상태로 pub는 가능하나 subscribe 불가 상태
                    nextcommand          = "skip";
                    tSProgressMQTT.Value = 50;
                    break;

                case states.automqttunsub:
                    // MQTT 서버 자동 연결 해제 요청시 +QMTUNS 기다렸다가 진행
                    nextcommand          = "skip";
                    tSStatusLblMQTT.Text = "connect";           //서버 연결 상태로 pub는 가능하나 subscribe 불가 상태
                    tSProgressMQTT.Value = 50;
                    break;

                case states.mqttdisconn:
                    // MQTT 서버 연결 해제
                    tSStatusLblMQTT.Text = "disconnect";
                    tSProgressMQTT.Value = 0;
                    logPrintInTextBox("MQTT 서버와 연결이 해제되었습니다.", "");
                    break;

                case states.mqttclose:
                    // MQTT 서버 Socket 해제
                    tSStatusLblMQTT.Text = "disconnect";
                    tSProgressMQTT.Value = 0;
                    break;

                default:
                    break;
                }

                // 마지막 응답(OK)을 받은 후에 후속 작업이 필요한지 확인한다.
                if (nextcommand != "skip")
                {
                    if (nextcommand != "")
                    {
                        this.sendDataOut(commands[nextcommand]);
                        tBoxActionState.Text = nextcommand;
                        nextcommand          = "";

                        timer1.Start();
                    }
                    else
                    {
                        tBoxActionState.Text = states.idle.ToString();
                        timer1.Stop();
                    }
                }
            }
            else if (s == "ERROR")
            {
                tBoxActionState.Text = states.idle.ToString();
                nextcommand          = "";
                timer1.Stop();
            }
            else if (s == "+ICCID:")
            {
                // AT+ICCID의 응답으로 ICCID 값 화면 표시/bootstrap 정보 생성를 위해 저장,
                // OK 응답이 따라온다
                tBoxIccid.Text = str2.Substring(0, 20);
                logPrintInTextBox("ICCID값이 저장되었습니다.", "");
            }
            else if (s == "+CEREG:")
            {
                // AT+CEREG의 응답으로 LTE attach 상태 확인하고 disable되어 있어면 attach 요청,
                // attach가 완료되지 않았으면 1초 후에 재확인, (timer2 사용)
                // OK 응답이 따라온다
                timer2.Stop();

                string ltestatus = str2.Substring(1, 1);
                if (ltestatus == "0")
                {
                    tSStatusLblLTE.Text = "disconnect";
                    tSProgressLTE.Value = 0;

                    network_chkcnt = 3;             // LTE attach disable을 경우 enable하고 getcereg 3회 확인
                    nextcommand    = states.setcereg.ToString();
                    logPrintInTextBox("LTE 연결을 요청이 필요합니다.", "");
                }
                else if (ltestatus == "1")
                {
                    string lteregi = str2.Substring(3, 1);

                    if ((lteregi == "1") || (lteregi == "5"))
                    {
                        tSStatusLblLTE.Text = "registered";
                        tSProgressLTE.Value = 100;
                        timer2.Stop();
                        logPrintInTextBox("LTE망에 연결 되었습니다.", "");
                    }
                    else
                    {
                        // LTE attach 시도 중
                        tSStatusLblLTE.Text = "registerring";
                        tSProgressLTE.Value = 50;

                        timer2.Start();     // 1초 후에 AT+CEREG 호출
                    }
                }
                else
                {
                    tSStatusLblLTE.Text = "enable";
                    tSProgressLTE.Value = 100;

                    timer2.Stop();
                }

                tBoxActionState.Text = states.idle.ToString();
                timer1.Stop();
            }
            else if (s == ">")
            {
                if (tBoxActionState.Text == "mqttpub")
                {
                    this.sendDataOut(MQTT_Msg);
                    tBoxActionState.Text = states.mqttpubtext.ToString();
                    timer1.Start();
                }
                else
                {
                    logPrintInTextBox("MQTT data 전송 상태가 아닙니다.", "");
                }
            }
            else if (s == "+QMTRECV:")
            {
                // 모듈이 MQTT서버에서 받은 데이터를 전달하는 이벤트,
                // OK 응답 발생하지 않음
                string[] words = str2.Split(','); // 수신한 데이터를 한 문장씩 나누어 array에 저장
                if (words[0] == " 0")             // MQTT 소켓 ID ("0"만 사용)
                {
                    string base64   = words[3].Substring(1, words[3].Length - 2);
                    byte[] orgBytes = Convert.FromBase64String(base64);
                    string orgStr   = Encoding.UTF8.GetString(orgBytes);
                    logPrintInTextBox("\"" + orgStr + "\"를 수신하였습니다.", "");

                    InSensors inSensors1 = JsonConvert.DeserializeObject <InSensors>(orgStr);
                    string    text       = "온도 : " + inSensors1.Temperature + ", 습도 : " + inSensors1.Humidity;

                    logPrintInTextBox("\"" + text + "\"를 수신하였습니다.", "");
                }
                else
                {
                    logPrintInTextBox("지원하지 않는 data object입니다.", "");
                }
            }
            else if (s == "+QMTOPEN:")
            {
                // 모듈이 MQTT서버 Socket Open이 완료되면 전달하는 이벤트,
                // OK 응답 이후에 발생
                if (tBoxActionState.Text == states.automqttopen.ToString())
                {
                    // MQTT 서버 자동 연결 요청시
                    // (automqttopen) -> (automqttconn) -> mqttsub
                    MqttServerConnect(states.automqttconn.ToString());
                }
            }
            else if (s == "+QMTCONN:")
            {
                // 모듈이 MQTT서버와 연결이 되면 전달하는 이벤트,
                // OK 응답 이후에 발생
                if (tBoxActionState.Text == states.automqttconn.ToString())
                {
                    // MQTT 서버 자동 연결 요청시
                    // automqttopen -> (automqttconn) -> (mqttsub)
                    MqttSubscribe(states.mqttsub.ToString());
                }
            }
            else if (s == "+QMTUNS:")
            {
                // 모듈이 MQTT서버와 Unsubscribe 되면 전달하는 이벤트,
                // OK 응답 이후에 발생
                if (tBoxActionState.Text == states.automqttunsub.ToString())
                {
                    // MQTT 서버 자동 연결 해제 요청시
                    // automqttunsub -> mqttdisconn
                    MqttServerDisconn(states.mqttdisconn.ToString());
                }
            }
        }