Skip to content

Commit

Permalink
🎉 complete gateway modbus feature
Browse files Browse the repository at this point in the history
  • Loading branch information
sanshengshui committed Jun 21, 2020
1 parent b82e546 commit 0a35e8b
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 12 deletions.
10 changes: 10 additions & 0 deletions IOT-Guide-Gateway-Modbus/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,16 @@
<artifactId>modbus</artifactId>

<dependencies>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.71</version>
</dependency>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>4.7.2</version>
</dependency>
<dependency>
<groupId>com.infiniteautomation</groupId>
<artifactId>modbus4j</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
package iot.technology.gateway.modbus;

import com.alibaba.fastjson.JSONObject;
import com.serotonin.modbus4j.BatchRead;
import com.serotonin.modbus4j.BatchResults;
import com.serotonin.modbus4j.ModbusMaster;
import com.serotonin.modbus4j.code.DataType;
import com.serotonin.modbus4j.exception.ErrorResponseException;
import com.serotonin.modbus4j.exception.ModbusTransportException;
import com.serotonin.modbus4j.locator.BaseLocator;
import iot.technology.gateway.modbus.conveter.OkHttpUtil;
import lombok.extern.slf4j.Slf4j;

import java.util.HashMap;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;

Expand All @@ -35,6 +39,9 @@ public void run() {
try {
BatchResults<String> results = ModbusUtil.batchRead(modbusMaster, batch);
log.info("slave id 1 读取结果为: " + results);
Map<String, String> map = new HashMap<>();
map.put("json", String.valueOf(results));
OkHttpUtil.post("http://localhost:8080/api/v1/telemetry", map);
} catch (Exception e) {
e.printStackTrace();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package iot.technology.gateway.modbus.conveter;

import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

import java.util.HashMap;
import java.util.Map;

/**
* @author james mu
* @date 2020/6/21 21:13
*/
@Slf4j
public class OkHttpUtil {

/**
* Get请求
* @param url URL地址
* @return 返回结果
*/
public static String get(String url){
String result=null;
try {
OkHttpClient okHttpClient=new OkHttpClient();
Request request = new Request.Builder().url(url).build();
Response response = okHttpClient.newCall(request).execute();
result=response.body().string();
log.info("Get请求返回:{}",result);
return result;
}catch (Exception e){
log.error("OkHttp[Get]请求异常",e);
return result;
}
}

/**
* Post请求
* @param url URL地址
* @param params 参数
* @return 返回结果
*/
public static String post(String url, Map<String,String> params){
String result=null;
if (params==null){
params=new HashMap<String, String>();
}
try {
OkHttpClient okHttpClient=new OkHttpClient();
FormBody.Builder formBodyBuilder = new FormBody.Builder();
//添加参数
log.info("params:{}", JSON.toJSONString(params));
for (Map.Entry<String,String> map:params.entrySet()){
String key=map.getKey();
String value;
if (map.getValue()==null){
value="";
}else{
value=map.getValue();
}
formBodyBuilder.add(key,value);
}
FormBody formBody =formBodyBuilder.build();
Request request = new Request.Builder().url(url).post(formBody).build();
Response response = okHttpClient.newCall(request).execute();
result=response.body().string();
log.info("Post请求返回:{}",result);
return result;
}catch (Exception e){
log.error("OkHttp[Post]请求异常",e);
return result;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import org.springframework.web.context.request.async.DeferredResult;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.util.*;


Expand Down Expand Up @@ -68,20 +69,27 @@ public DeferredResult<ResponseEntity> getDeviceAttributes(@RequestParam(value =
}

@RequestMapping(value = "/telemetry",method = RequestMethod.POST)
public DeferredResult<ResponseEntity> postTelemetry(@RequestBody String json, HttpServletRequest request){
public DeferredResult<ResponseEntity> postTelemetry(@RequestBody String json, HttpServletRequest request)
throws UnsupportedEncodingException {
DeferredResult<ResponseEntity> responseWriter = new DeferredResult<ResponseEntity>();
if (quotaExceeded(request, responseWriter)) {
return responseWriter;
}
responseWriter.setResult(new ResponseEntity(HttpStatus.ACCEPTED));
Map<Long, List<KvEntry>> telemetryMaps = JsonConverter.convertToTelemetry(new JsonParser().parse(json)).getData();
for (Map.Entry<Long,List<KvEntry>> entry : telemetryMaps.entrySet()) {
System.out.println("key= " + entry.getKey());
for (KvEntry kvEntry: entry.getValue()) {
System.out.println("属性名="+kvEntry.getKey()+ " 属性值="+kvEntry.getValueAsString());
}
}
System.out.println(new String(json.getBytes(), "UTF-8"));
responseWriter.setResult(new ResponseEntity("ok", HttpStatus.ACCEPTED));
return responseWriter;
/**
* 限流操作和物模性解析逻辑操作
*
* if (quotaExceeded(request, responseWriter)) {
* return responseWriter;
* }
*
* Map<Long, List<KvEntry>> telemetryMaps = JsonConverter.convertToTelemetry(new JsonParser().parse(json)).getData();
* for (Map.Entry<Long,List<KvEntry>> entry : telemetryMaps.entrySet()) {
* System.out.println("key= " + entry.getKey());
* for (KvEntry kvEntry: entry.getValue()) {
* System.out.println("属性名="+kvEntry.getKey()+ " 属性值="+kvEntry.getValueAsString());
* }
* }
**/
}

@RequestMapping(value = "/attributes/updates", method = RequestMethod.GET, produces = "application/json")
Expand Down

0 comments on commit 0a35e8b

Please sign in to comment.