仰邦LED-bx_y08二次开发的java版流程梳理记录
首次开发的项目是一个停车场主要显示剩余多少车辆 遇到很多难题;主要还要搞清楚二次开发LED的发送流程,在这记录下
//初始化API环境 仅一次
Y2Env.initial("log.properties");
//链接固定的LEDIP
String url="http://172.111.30.11";
Y2Screen screen = new Y2Screen(url);
//登陆
if(!screen.login("guest", "guest")){System.out.println("登入失败!");
//登录成功 初始化资源
screen.turnOff();//关机
screen.turnOn(); //开机
screen.syncTime();//校时
screen.changeVolume(60); //音量调至60
screen.clearPlayResources();//清除播放资源(删除播放节目)
// 建立动态节目
DynamicPlayFile file = new DynamicPlayFile();//建立区域
DynamicArea darea = file.createArea(0,0,80,32,1);//top ,left ,width.height ,节目编码;节目最终的显示位置大小
DynamicArea darea2 = file.createArea(0,40,80,30,2);
//它的动态展示效果 二次开发包里的 PDF 有介绍 animationType(0) 0代表快速打印
darea.addText("剩余车位").fgColor(Color.red).stayTime(200).animationSpeed(10).rowHeight(height).animationType(0).setFont(new Y2Font() .size(fontSize));
darea2.addText("数量"+5).fgColor(Color.green).setFont(new Y2Font().size(24));
System.out.println(i);
boolean write = dyn.write(file);
System.out.println("是否成功"+ write);
screen.logout();//退出
关于项目测试时用的代码 的逻辑实现
中文 乱码问题 自己转的utf-8 失败告终 :没在去测试别的
二次开发包里 C#示例代码里 是转化成的 base64 ( java里应该也是这 没有测试)
上面与图片只是很简陋测试代码
如有错误,想法请告知
部署tomcat后 现场返回的日志 只有重启服务才会登陆一次 中间无写入失败再去登陆的情况 ,隔上两三天 LED 黑屏 ,只能强制每天登陆两次,具体的状况没搞清楚? -_-?
当时写完 没有优化过的 凑活着看吧`
package com.example.carboen.main;
import onbon.y2.Y2DynamicManager;
import onbon.y2.Y2Env;
import onbon.y2.Y2Exception;
import onbon.y2.Y2Screen;
import onbon.y2.common.AlignmentType;
import onbon.y2.common.Y2Font;
import onbon.y2.play.DynamicArea;
import onbon.y2.play.DynamicPlayFile;
import onbon.y2.play.ProgramPlayFile;
import onbon.y2.play.TextArea;
import org.apache.http.impl.client.HttpClients;
import org.apache.juli.logging.Log;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.support.PropertiesLoaderUtils;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.stereotype.Component;
import java.awt.*;
import java.io.*;
import java.net.URL;
import java.net.URLConnection;
import java.text.SimpleDateFormat;
import java.util.*;
@Component
public class CarInfo implements ApplicationRunner {
// public Y2Screen screen;
// public Y2Screen screen2;
public static int oldInt = -1;
public static int newInt = -1;
static SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH");
static Y2Screen screen;
static Y2Screen screen2;
// 于节目中新增一个动态区
Date date;
SimpleDateFormat ss = new SimpleDateFormat("ss");
SimpleDateFormat mm = new SimpleDateFormat("mm");
static String ip1 = "";
static String ip2 = "";
static Logger logger =LoggerFactory.getLogger(CarInfo.class);
// public static Map<String,String> forObject;
@Override
public void run(ApplicationArguments args) throws Exception {
Properties properties = new Properties();
properties = PropertiesLoaderUtils.loadProperties(new ClassPathResource("db.properties"));
logger.info("正在链接--稍等");
System.out.println("正在链接--稍等");
//初始化API环境 仅一次
Y2Env.initial("log.properties");
//连上特定的屏幕并执行一些命令
//建立一个新的屏幕
try {
Properties properties1 = new Properties();
properties1 = PropertiesLoaderUtils.loadProperties(new ClassPathResource("db.properties"));
ip1 = properties.getProperty("ip1");
ip2 = properties.getProperty("ip2");
}catch (Exception e){
logger.info( e.getMessage());
System.out.println(e.getMessage());
}
screen = startInfo(ip1);
screen2 = startInfo(ip2);
timerStart();
}
public Y2Screen startInfo(String ip) {
try {
Y2Screen screen3 = new Y2Screen("http://" + ip);
//登入
if (!screen3.login("guest", "guest")) {
logger.info("登入失败!"+ip);
System.out.println("登入败!"+ip);
}else {
logger.info("登入成功!"+ip);
System.out.println("登入成功!"+ip);
}
//403968491
// screen3.turnOff();//关机
boolean b = screen3.turnOn();//开机
screen3.syncTime();//校时
screen3.changeVolume(60); //音量调至60
screen3.clearPlayResources();//清除播放资源(删除播放节目)
// textArea(screen,0);
// timerStart();
return screen3;
} catch (Exception e) {
logger.info(e.getMessage());
System.out.println(e.getMessage());
return null;
}
}
public void timerStart() {
try {
Properties properties = new Properties();
properties = PropertiesLoaderUtils.loadProperties(new ClassPathResource("db.properties"));
int timeStart1 = Integer.parseInt(properties.getProperty("timeStart"));
int timeOut = Integer.parseInt(properties.getProperty("timeOut"));
String interfaseIP = properties.getProperty("interfaseIP");
String intervalTime = properties.getProperty("intervalTime");
long period = Long.parseLong(intervalTime);
// ip1 = properties.getProperty("ip1");
// ip2 = properties.getProperty("ip2");
Timer timer = new Timer();
// Integer oldMes = OldStopCarInfo.getTotalRemainNum();
logger.warn("开启 定时循环");
timer.schedule(new TimerTask() {
//网络 接口 待定
@Override
public void run() {
logger.warn("定时循环开始--"+oldInt+"--"+newInt);
//上次的 记录数据 = 新数据 下面作比较
oldInt = newInt;
ClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory(HttpClients.createDefault());
//post 取数据
String s = sendPost("http://" + interfaseIP, null);
//取不到 一直取
while (s==null){
s= sendPost("http://" + interfaseIP, null);
try {
Thread.sleep(5000);
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
net.sf.json.JSONObject jsonObject = net.sf.json.JSONObject.fromObject(s);
// StopCarInfo stopCarInfo = (StopCarInfo) net.sf.json.JSONObject.toBean(jsonObject, StopCarInfo.class);
newInt = jsonObject.getInt("totalRemainNum");
logger.warn("http接口参数分析"+newInt+" "+oldInt);
System.out.println("http接口参数分析"+newInt+" "+oldInt);
//解析的数据 传给 newint
int old1= oldInt;
int new1=newInt;
// System.out.println(anInt);
date = new Date();
String format = simpleDateFormat.format(date);
int dateInt = Integer.parseInt(format);
String formatss = ss.format(date);
String formatmm = mm.format(date);
if(dateInt==5){
if (formatmm.equals("15")){
if(formatss.equals("01")||formatss.equals("02")||formatss.equals("03")||formatss.equals("04")||formatss.equals("05")||formatss.equals("06")||formatss.equals("07")||formatss.equals("08"))
screen = startInfo(ip1);
screen2 = startInfo(ip2);
}
}
//强制每天登陆2次
if(dateInt==16) {
if (formatmm.equals("30")) {
if (formatss.equals("01") || formatss.equals("02") || formatss.equals("03") || formatss.equals("04") || formatss.equals("05") || formatss.equals("06") || formatss.equals("07") || formatss.equals("08"))
screen = startInfo(ip1);
screen2 = startInfo(ip2);
}
}
//时间 设定 关机
if (dateInt >= timeStart1 && dateInt < timeOut) {
try {
// System.out.println("黑屏时间已到");
screen.clearPlayResources();
screen2.clearPlayResources();
logger.warn("时间 设定 清屏");
System.out.println("时间 设定 清屏");
Long aLong = (1000 * 60 * 25L);
try {
Thread.sleep(aLong);
} catch (Exception e) {
System.out.println(e.getMessage());
}
} catch (Y2Exception e) {
e.printStackTrace();
};
//参数判断 一致不作为
}
else if (old1 == new1) {
logger.warn(oldInt+"相等"+newInt+"参数判断一致不写入LED");
System.out.println(oldInt+"相等"+newInt+"参数判断一致不写入LED");
// 参数不一致 写入数据
}
else if(old1 != new1) {
//执行 节目 分类 发送 把 newMes字符 传递
//写入是否成功 不成功 重新登入
logger.warn(oldInt+"不相等"+newInt);
System.out.println(oldInt+"不相等"+newInt);
try{
screen.clearPlayResources();
screen2.clearPlayResources();
}catch (Exception e){
logger.info(e.getMessage());
System.out.println(e.getMessage());
}
Boolean he = he(screen, newInt);
Boolean he1 = he(screen2, newInt);
if (!he){
logger.warn("写入失败"+ip1+"登陆");
System.out.println("写入失败"+ip1+"登陆");
screen = startInfo(ip1);
}else {
logger.warn("写入成功"+ip1);
System.out.println("写入成功"+ip1);
}
if (!he1){
System.out.println("写入失败"+ip2+"登陆");
logger.info("写入失败"+ip2+"登陆");
screen2 = startInfo(ip2);
}else {
logger.warn("写入成功"+ip2);
System.out.println("写入成功"+ip2);
}
}else {
logger.info(oldInt+"---"+newInt);
}
// stopCarInfo = OldStopCarInfo;
}
}, 8000, period);//延迟8秒启动,每?秒执行一次;0
} catch (Exception e) {
System.out.println(e.getMessage());
// timerStart();
}
}
public Boolean he(Y2Screen screen, int textArea1) {
logger.warn("写入动态区域");
System.out.println("写入动态区域");
try {
Properties properties1 = new Properties();
properties1 = PropertiesLoaderUtils.loadProperties(new ClassPathResource("db.properties"));
int x = Integer.parseInt(properties1.getProperty("x"));
int y = Integer.parseInt(properties1.getProperty("y"));
int width = Integer.parseInt(properties1.getProperty("width"));
int height = Integer.parseInt(properties1.getProperty("height"));
int fontSize = Integer.parseInt(properties1.getProperty("fontsize"));
int rowheight = Integer.parseInt(properties1.getProperty("rowHeight"));
int x2 = Integer.parseInt(properties1.getProperty("x2"));
int y2 = Integer.parseInt(properties1.getProperty("y2"));
int width2 = Integer.parseInt(properties1.getProperty("width2"));
int height2 = Integer.parseInt(properties1.getProperty("height2"));
int fontSize2 = Integer.parseInt(properties1.getProperty("fontsize2"));
String row1 = properties1.getProperty("row1");
String row2 = properties1.getProperty("row2");
String texarea = "";
if (10 > textArea1) {
texarea = "00" + textArea1;
} else if (100 > textArea1) {
texarea = "0" + textArea1;
} else {
texarea = "" + textArea1;
}
//字体行数
int rwo = Integer.parseInt(properties1.getProperty("row"));
Y2DynamicManager dyn = screen.dynamic();
DynamicPlayFile file = new DynamicPlayFile();
DynamicArea darea = file.createArea(x,y,width,height,1);
DynamicArea darea2 = file.createArea(x2,y2,width2,height2,2);
darea.addText("剩余车位").fgColor(Color.red).stayTime(200).animationSpeed(10).rowHeight(height).animationType(0)
.setFont(new Y2Font()
.size(fontSize));
darea2.addText(texarea).fgColor(Color.green).stayTime(200).animationSpeed(10).animationType(0)
.rowHeight(height2)
.setFont(new Y2Font()
.size(fontSize2));
// 将动态区上传
boolean write = dyn.write(file);
return write;
} catch (Exception e) {
logger.warn("写入动态区域出错"+e.getMessage());
System.out.println("写入动态区域出错"+e.getMessage());
}
return false;
}
public static String sendPost(String url, String param) {
logger.warn("接口取参");
PrintWriter out = null;
BufferedReader in = null;
String result = "";
try {
URL realUrl = new URL(url);
// 打开和URL之间的连接
URLConnection conn = realUrl.openConnection();
// 设置通用的请求属性
conn.setRequestProperty("accept", "*/*");
conn.setRequestProperty("connection", "Keep-Alive");
conn.setRequestProperty("user-agent",
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
// 发送POST请求必须设置如下两行
conn.setDoOutput(true);
conn.setDoInput(true);
// 获取URLConnection对象对应的输出流
out = new PrintWriter(conn.getOutputStream());
// 发送请求参数
out.print(param);
// flush输出流的缓冲
out.flush();
// 定义BufferedReader输入流来读取URL的响应
in = new BufferedReader(
new InputStreamReader(conn.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
result += line;
}
} catch (Exception e) {
logger.info("发送 POST 请求出现异常!" + e);
System.out.println("发送 POST 请求出现异常!" + e);
return null;
}
//使用finally块来关闭输出流、输入流
finally {
try {
if (out != null) {
out.close();
}
if (in != null) {
in.close();
}
} catch (IOException ex) {
System.out.println(ex.getMessage());
}
}
return result;
}
}
`