/******************************************
  * Giai ma ban tin tra ve tu cong COM
  * Tham so dau vao: Chuoi du lieu nhan tu cong COM
  * Kieu tra ve : void
  * Dinh dang ban tin tra ve:
  * #JN:NNNN MM  Ban tin join mang
  * #AD:NNNN MM D1D2D3D4 D5D6D7D8 E1E2E3E4 Ban tin du lieu dinh ky
  * #RD:NNNN MM D1D2D3D4 D5D6D7D8 E1E2E3E4 Ban tin du lieu theo yeu cau
  * #VL:MM Ban tin thong bao ngu cua mot sensor
  * #OK:NNNN MM SS Ban tin thong bao trang thai actor
  * #SN: NNNN MM SS  Ban tin thong bao trang thai sensor
  * ***************************************/
 public void Converter(string mesg)
 {
     try
     {
         Database myDatabase = new Database();
         switch (mesg[1])
         {
             case 'J':
                 JoinMang(mesg);
                 reload(pictureBox);
                 break;
             case 'A':
                 // VD #AD: NNNN MM D1D2D3D4 D5D6D7D8 EEEE
                 string mac = mesg.Substring(8,2);
                 string status = "";
                 if (mac[0] == '0')
                 {
                     status = myDatabase.getStatusSensor(mac);
                 }
                 AutoData(mesg);
                 if (status != "true" && status != "True")
                 {
                     reload(pictureBox);
                 }
                 break;
             case 'S':
                 // VD: #SN: NNNN MM SS
                 TrangThaiNode(mesg);
                 break;
             case 'V':
                 try
                 {
                     DisplayData("(" + DateTime.Now + "): Sensor " + mesg.Substring(4, 2) + " da vao che do ngu\r\n", tb);
                     myDatabase.setActiveSensor(mesg.Substring(4, 2), false);
                     reload(pictureBox);
                 }
                 catch
                 {
                     DisplayData("Error", tb);
                 }
                 break;
             case 'O':
                 // VD: #OK:NNNN MM SS
                 ThongTinActor(mesg);
                 reload(pictureBox);
                 break;
             case 'R':
                 // VD #RD: NNNN MM D1D2D3D4 D5D6D7D8 EEEE
                 RequestData(mesg);
                 break;
             case 'P':
                 PingNode(mesg);
                 break;
         }
     }
     catch
     {
         DisplayData("Khong the giai ma du lieu", tb);
     }
 }
예제 #2
0
        //Draw sensor
        public void DrawSensor(string mac)
        {
            try
            {
                Database myDatabase = new Database();
                Bitmap icon_true;
                Bitmap icon_false;
#if ACTOR_BAOCHAY
                int pixel_x = Convert.ToInt32(myDatabase.getSensorBCPixel_x(mac));
                int pixel_y = Convert.ToInt32(myDatabase.getSensorBCPixel_y(mac));
                string status = myDatabase.getStatusSensorBC(mac);
#else
                int pixel_x = Convert.ToInt32(myDatabase.getSensorPixel_x(mac));
                int pixel_y = Convert.ToInt32(myDatabase.getSensorPixel_y(mac));
                string status = myDatabase.getStatusSensor(mac);
#endif
                icon_true = new Bitmap(path_icon_sensor_true);
                icon_false = new Bitmap(path_icon_sensor_false);
                
                if (status == "true" || status == "True")
                {
                    gr.DrawImage(icon_true, pixel_x, pixel_y);
                }
                else
                {
                    gr.DrawImage(icon_false, pixel_x, pixel_y);
                }
                gr.DrawString(mac, draw_font, draw_brush, pixel_x, pixel_y);
            }
            catch
            { }
        }
        public void TuoiCayVal1(object sender, EventArgs e)
        {
            #if ACTOR_COOR
            for (int i = 1; i < 13; i++)
            {
                timePing[i]++;
                if (timePing[i] == 120)
                {
                    Database myDatabase = new Database();
                    byte[] bCommand = new byte[4];
                    string sHex = i.ToString("x").ToUpper();
                    sHex = "0" + sHex;
                    if (myDatabase.getStatusSensor(sHex) == "true" || myDatabase.getStatusSensor(sHex) == "True")
                    {
                        string sIp = myDatabase.getNetworkIpSensor(sHex);
                        int byte0 = int.Parse(sIp.Substring(0,2), System.Globalization.NumberStyles.HexNumber);
                        int byte1 = int.Parse(sIp.Substring(2,2), System.Globalization.NumberStyles.HexNumber);
                        bCommand[0] = (byte)byte0;
                        bCommand[1] = (byte)byte1;
                        bCommand[2] = (byte)255;
                        bCommand[3] = (byte)36;
                        DisplayData("Gui lenh ping node: "+ sHex,tb);
                        WriteData(bCommand);
                        checkPing[i] = true;
                    }
                    
                }

                if (checkPing[i] == true)
                {
                    countTime[i]++;
                    if (countTime[i] == 12)
                    {
                        Database myDatabase = new Database();
                        string sHex = i.ToString("x").ToUpper();
                        sHex = "0" + sHex;
                        myDatabase.setActiveSensor(sHex, false);
                        DisplayData("Sensor "+sHex+" khong con trong mang",tb);
                        countTime[i] = 0;
                        checkPing[i] = false;
                        reload(pictureBox);
                    }
                }
            }
            #endif
            count++;
            #if BC_COOR
            count_BC++;
            if (count_BC > Time_alarm * 12)
            {
                AutoSendCanhBao();
                count_BC = 0;
            }
            #endif
            #if ACTOR_COOR
            if (onofVal1 == true)
            {
                try
                {
                    time_count_val1++;
                    if(time_count_val1 > Time_control *12)
                    {
                        //gui lenh tat van 1
                        Database mydatabase = new Database();
                        string command = mydatabase.getNetworkIpActor("00");

                        command += "010$";
                        byte[] com = ConvertTobyte(command);
                        WriteData(com);

                        onofVal1 = false;
                    }
                }
                catch
                {
                    DisplayData("Khong gui duoc lenh tat van so 1 " + DateTime.Now, tb);
                }
            }
            if (onofVal2 == true)
            {
                try
                {
                    time_count_val2++;
                    if(time_count_val2 > Time_control *12)
                    {
                        //gui lenh tat van 2
                        Database mydatabase = new Database();
                        string command = mydatabase.getNetworkIpActor("00");

                        command += "020$";
                        byte[] com = ConvertTobyte(command);
                        WriteData(com);

                        onofVal2 = false;
                    }
                }
                catch
                {
                    DisplayData("Khong gui duoc lenh tat van so 2 " + DateTime.Now, tb);
                }
            }
            if (onofVal3 == true)
            {
                try
                {
                    time_count_val3++;
                    if(time_count_val3 > Time_control * 12)
                    {
                        //gui lenh tat van 3
                        Database mydatabase = new Database();
                        string command = mydatabase.getNetworkIpActor("00");

                        command += "030$";
                        byte[] com = ConvertTobyte(command);
                        WriteData(com);

                        onofVal3 = false;
                    }
                }
                catch
                {
                    DisplayData("Khong gui duoc lenh tat van so 3 " + DateTime.Now, tb);
                }
            }
            if (onofVal4 == true)
            {
                try
                {
                    time_count_val4++;
                    if(time_count_val4 > Time_control * 12)
                    {
                        //gui lenh tat van 4
                        Database mydatabase = new Database();
                        string command = mydatabase.getNetworkIpActor("00");

                        command += "040$";
                        byte[] com = ConvertTobyte(command);
                        WriteData(com);

                        onofVal4 = false;
                    }
                }
                catch
                {
                    DisplayData("Khong gui duoc lenh tat van so 4 " + DateTime.Now, tb);
                }
            }
            if (onofVal5 == true)
            {
                try
                {
                    time_count_val5++;
                    if(time_count_val5 > Time_control *12)
                    {
                        //gui lenh tat van 5
                        Database mydatabase = new Database();
                        string command = mydatabase.getNetworkIpActor("00");

                        command += "050$";
                        byte[] com = ConvertTobyte(command);
                        WriteData(com);

                        onofVal5 = false;
                    }
                }
                catch
                {
                    DisplayData("Khong gui duoc lenh tat van so 5 " + DateTime.Now, tb);
                }
            }
            if (onofVal6 == true)
            {
                try
                {
                    time_count_val6++;
                    if (time_count_val6 > Time_control * 12)
                    {
                        //gui lenh tat van 6
                        Database mydatabase = new Database();
                        string command = mydatabase.getNetworkIpActor("00");

                        command += "060$";
                        byte[] com = ConvertTobyte(command);
                        WriteData(com);

                        onofVal6 = false;
                    }
                }
                catch
                {
                    DisplayData("Khong gui duoc lenh tat van so 6 " + DateTime.Now, tb);
                }
            }
            #endif
        }