严智鑫
2023-12-22 54fe125d1165783cc5bc401124fccf203dfe5814
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
package com.northglass.service.device;
 
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLDecoder;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.sql.*;
 
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
 
import com.northglass.entity.MachineType;
import com.northglass.entity.Machines;
import com.northglass.entity.PatternTask;
import com.northglass.GlassLayout.Algorithm.AlgorithmDBInterface;
import com.northglass.GlassLayout.Algorithm.AlgorithmDBInterface.ComputeResult;
import com.northglass.GlassLayout.Tempering.LayoutAlgorithm.AlgorithmParams;
import com.northglass.entity.GlassNumberColor;
import com.northglass.entity.LoadRack;
import com.northglass.entity.MachineDispose;
import com.northglass.entity.MachineFont;
import com.northglass.entity.MachineMessage;
import com.northglass.entity.RawFile;
import com.northglass.entity.ShelfTask;
import com.northglass.entity.Slicecage;
import com.northglass.repository.MachinesTypeDao;
import com.northglass.repository.GlassNumberColorDao;
import com.northglass.repository.LoadRackDao;
import com.northglass.repository.MachineDao;
import com.northglass.repository.MachineDisposeDao;
import com.northglass.repository.MachineFontDao;
import com.northglass.repository.MachineMessageDao;
import com.northglass.repository.RawFileDao;
import com.northglass.repository.ShelfTaskDao;
import com.northglass.repository.SlicecageDao;
import com.northglass.service.MachineService.MachineService;
import com.northglass.service.message.MessageProcessor;
import com.northglass.util.HexUtil;
 
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
 
 
@Component
@Transactional
public class DeviceService {
 
    private static final Logger LOGGER = LoggerFactory.getLogger(DeviceService.class);
    @Autowired
    private MachinesTypeDao machinesTypeDao;
    
    @Autowired
    private MessageProcessor MessageProcessor;
    
    @Autowired
    private MachineDao machinesDao;
    
    @Autowired
    private MachineFontDao machineFontDao;
    
    @Autowired
    private MachineDisposeDao machineDisposeDao;
    
    @Autowired
    private MachineMessageDao machineMessageDao;
    
    @Autowired
    private RawFileDao rawFileDao;
    @Autowired
    private LoadRackDao loadRackDao;
    @Autowired
    private ShelfTaskDao shelfTaskDao;
    
    @Autowired
    private MachineService machineService;
    
    
    @Autowired
    private MachineDao machineDao;
    
    @Autowired
    private GlassNumberColorDao glassNumberColorDao;
    
    @Autowired
    SlicecageDao slicecageDao;
 
 
    public void CeshiMainche() {
        
        Machines Machine=machineDao.findOne(38L);    
        String str="000000000000000000";
        for(int i=0;i<45;i++){            
            if(i==0){
                str+="0001";
            }else if(i==99){
                str+="02ed";
            }else if(i==99){
                str+="022f";
            }else{
                str+="0000";
            }            
        }
        MessageProcessor.generateReturnMessage(str,Machine);
    }
    public void connect() {
//        List<Machines> shelf67=machineDao.findAll();        
//        for (Machines machines : shelf67) {
//            machineService.connect(machines);
//        }
//        Machines Machine=machineDao.findOne(75L);
//        machineService.connect(Machine);
//        Machines Machine=machineDao.findOne(38L);
//        machineService.connect(Machine);
        //com.northglass.GlassLayout.Algorithm.AlgorithmDBInterface.f(2);
        
    }
    
    public void resetState() {
 
    }
    
    //得到所有的45个字
    public String[] ResultTexts(String Str) {
        String []InfoTex =new String[45];
        Str=Str.substring(18);
        for(int i=0;i<45;i++){
            InfoTex[i]=Str.substring(0,4);
            Str=Str.substring(4);
        }
        return InfoTex;
    }
    //保存TEXT文本
    public static void WriteFile(String URL,String FileName,String Content,boolean isappend){
        try {
            String FileUrl=URL+File.separator+FileName;
               File file=new File(FileUrl);
               if(file.exists()){
                   file.createNewFile();
               }
               OutputStream out =new FileOutputStream(file,isappend);
               Content="\r"+Content;
               byte[] b=Content.getBytes();
               out.write(b);
               out.close();
        } catch (Exception e) {
            // TODO: handle exception
        }    
    }
    
    public static void WriteFiles(String URL,String FileName,String Content){
        try {
            String FileUrl=URL+File.separator+FileName;
               File file=new File(FileUrl);
               if(file.exists()){
                   file.createNewFile();
               }
               OutputStream out =new FileOutputStream(file,true);
               Content="\r"+Content;
               byte[] b=Content.getBytes();
               out.write(b);
               out.close();
               
        } catch (Exception e) {
            System.out.println("写入文件错误!错误原因"+e);
        }    
    }
    
    
    
    public String findColorHtml() {
        StringBuffer html = new StringBuffer();
        List<GlassNumberColor> colors = glassNumberColorDao.findAll();
        for (int i = 0; i < colors.size(); i++) {
            GlassNumberColor color = colors.get(i);
            html.append("<tr><td>").append(color.getId()).append("</td><td>");
            html.append("<span class='label label-success'>").append(color.getGlassId());
            html.append("</span></td><td><a href='/gmms2/device/deletecolor/").append(color.getId())
                    .append("' class='btn btn-danger'>删除</a></td></tr>");
        }
        return html.toString();
    }
    
    public void addcolor(GlassNumberColor color) {
        color.setFlag("0");
        glassNumberColorDao.save(color);
    }
 
    public void deletecolor(String id) {
        GlassNumberColor color = glassNumberColorDao.findOne(Long.parseLong(id));
        glassNumberColorDao.delete(Long.parseLong(id));
        LOGGER.debug("删除膜系" + color.getGlassId());
    }
    
    public String getshelfstatu() {
        StringBuffer html = new StringBuffer();
        List<Machines> status = machinesDao.findAll();
        if (status.size() > 0) {
            for (Machines statu : status) {
                MachineMessage machineMessage=machineMessageDao.findmachineid(statu.getId());
                html.append("<tr><td>"+statu.getMachineName()+"</td>");
                html.append("<td>"+statu.getConnectState()+"</td>");
                html.append("<td>"+statu.getIpAddress()+"</td>");
                html.append("<td>"+machineMessage.getModTime()+"</td></tr>");
            }
        }
        return html.toString();
    }
        
    public String getshelfstatuall() {
        StringBuffer html = new StringBuffer();
        List<Machines> status = machinesDao.findAll();
        if (status.size() > 0) {
            for (Machines statu : status) {
                if(statu.getConnectState().equals("已连接")){
                    html.append("<button type='button'  class='btn btn-primary '>"+statu.getMachineName()+"</button>");
                }else{
                    html.append("<button style='background-color: red' type='button' class='btn btn-primary '>"+statu.getMachineName()+"</button>");
                }            
            }
        }
        return html.toString();
    }
    
    public static Connection getConnection() {
        //localhost 192.168.200.201 192.168.2.6   10.12.0.100
        try {
            return DriverManager.getConnection("jdbc:mariadb://localhost/gmms","root","");                     
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
 
}