廖井涛
2024-03-04 eae17d27ec56a6b7887f5597335e38ca40273ef4
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package ng.devices;
 
import java.sql.CallableStatement;
import java.sql.Connection;
 
import builder.Manager;
import ng.db.DBHelper;
import ng.db.DBSession;
import ng.devices.ModbusService.ModbusDataPackage;
 
public class MysqlService extends ModbusService {
 
    //Êý¾Ý¿â
    DBHelper db;
    //É豸ID
    int id;
    //ʹÓõĴ洢¹ý³ÌÃû,¹Ì¶¨Ãû³Æ
    String proc="Total_method";
    //¼Ç¼ͨѶ´ÎÊý
    public int ioCount;
 
    //×îºóÒ»´Î¶ÁµÄÊý¾Ý
    public String lastGet;
    //×îºóÒ»´ÎдµÄÊý¾Ý
    public String lastSend;
    //·¢Ëͱê־λ¶¨Î»
    public int fa_song_Pos=-1;
    //ÇëÇó±ê־λ¶¨Î»
    public int qing_qiu_Pos=-1;
    //״̬±ê־λ¶¨Î»
    public int zhuang_tai_Pos=-1;
    //»ã±¨±ê־λ¶¨Î»
    public int hui_bao_Pos=-1;
    //È·Èϱê־λ¶¨Î»
    public int que_ren_Pos=-1;
    
    
    public int zhangtai;
    
    //¹¹Ô캯Êý³õʼ»¯
    public MysqlService(DBHelper db,int id) {
        super();
        this.db=db;
        this.id=id;
        
        // TODO Auto-generated constructor stub
    }
    
    int readWord(byte[] buff,int offset){
        if(offset<0 || offset>=buff.length-1)
            return 0;
        int k=buff[offset];
        k=(k<<8)+buff[offset+1];
        return k;
    }
    
    //ÅжÏÊÇ·ñ±ê־λȫ0
    boolean PassInvoke(byte[] buff){
    
        int a=0,b=0,c=0,d=0;
            a=readWord(buff,fa_song_Pos);
            b=readWord(buff,qing_qiu_Pos);
            c=readWord(buff,hui_bao_Pos);
            d=readWord(buff,que_ren_Pos);
            this.zhangtai=readWord(buff,this.zhuang_tai_Pos);
        if(a==0 && b==0 && c==0 && d==0)
            return true;
        return false;
    }
    
    
    
    //ÕâÊǽ«¶ÁÓ¦´ð°üת»»ÎªMsyqlʶ±ðµÄÐÎʽ£¨Ö»ÄÜת»¯¶ÁÓ¦´ðÊý¾Ý°ü£©
    String packToString(ModbusDataPackage pack){
        String s=HexUtil.intTo2ByteHex(1);   
        s+=HexUtil.intTo2ByteHex(0);            
        s+=HexUtil.intTo2ByteHex((3+pack.ByteSize));
        s+="0103";
        s+=HexUtil.intTo1ByteHex(pack.ByteSize);
        s+=HexUtil.byteToHexString(pack.ByteSize,pack.Content);
        return s;
    }
    //ÕâÊǽ«mysql·µ»ØµÄ×Ö·û´®×ª»»ÎªÊý¾Ý°ü£¨Ö»ÄÜת»¯Ð´ÇëÇó°ü£©
    boolean fillPack(ModbusDataPackage pack,String message){
        if(message==null)
            return false;
        int a= HexUtil.hexToInt(message.substring(8*2,10*2));
        int b= HexUtil.hexToInt(message.substring(10*2,12*2));
        String s=message.substring(13*2,message.length());
        byte[] bb= HexUtil.stringToInt(s);
        pack.Address=a*2;
        pack.ByteSize=b*2;
        pack.Content=bb;
        return true;
    }
    
     //·þÎñʵÏÖº¯Êý
    @Override
    protected boolean Service(ModbusDataPackage arg0, ModbusDataPackage arg1) {
        // TODO Auto-generated method stub
        //¶Á´ÎÊý+1
        ioCount++;
        //ÅжÏÊÇ·ñ²»µ÷ÓÃ
    //     if(this.PassInvoke(arg0.Content))
        // return false;
        //ת»¯ÎªmysqlÊäÈë²ÎÊý
        String s=this.packToString(arg0);
        //¸üÐÂ×îºóÒ»Ìõ¶Á±¨ÎÄ
        this.lastGet=s;
        DBSession sn=null;
        String result=null;
        String flag=null;
        
        try{
        //´´½¨Á¬½Ó
            sn=this.db.createSession(false);
            Connection con= sn.getConnection();
            //µôŶÄǸö´æ´¢¹ý³Ì
             CallableStatement sql=con.prepareCall("{call Total_method(?,?,?,?)}");
             sql.registerOutParameter(3, java.sql.Types.VARCHAR);
               sql.registerOutParameter(4, java.sql.Types.VARCHAR);
               sql.setString(1, s);
               sql.setLong(2, id);
               sql.execute();
               //¶ÁÈ¡·µ»Ø²ÎÊý
               result= sql.getString(3);
              flag= sql.getString(4);
        }
        catch(Exception e){
            e.printStackTrace();
        }
        finally{
            sn.close();
            
        }
    
        if(result!=null){
            if(result.length()>0){
            char c=result.charAt(0);
            //# @ ¿ªÍ·´ú±íÌØÊâÏûÏ¢×Ö·û£¬·ñÔò´ú±íͨѶ
            if(c=='#' || c=='@'){
                //µ÷ÓÃÏûÏ¢´¦Àíº¯Êý£¨ÕâÊÇÒ»¸öÍⲿ½Ó¿Ú£©
                Manager.SendMessage(result);
            }
            else{
                //¸üÐÂ×îºóÒ»´Î·¢ËÍ
           this.lastSend=result;
           //·µ»ØÒª·¢Ë͵ı¨ÎÄ
           return this.fillPack(arg1,result);
            }
            }
        }
        return false;
    }
 
}