假期实训项目:智能家居环境检测系统day3(数据库+java新手入门项目,上午)

今天主要完成的是采集模块网络模块

day1主要是配置环境,day2主要是搭结构,day3也就是今天正式加入整块代码。

一,采集模块

  1. 思路

分析得到的类写Enviroment,(代码在day2)

将采集的过程抽象成接口【gather.java】

a.实现接口的步骤:将分析的程序1得到的数据(一份日志文件),在程序2(客户端)中使用IO流读取配置文件的内容(文件输入流,缓冲输入流)

b.用【split】分割字符串,得到我们想要的信息

c.将数据取出封装成一个对象

d.将多个对象装在集合中返回出去即可。

package com.briup.env.client;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.lang.reflect.Array;
import java.sql.Timestamp;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;

import org.junit.Test;
import org.omg.CosNaming.NamingContextExtPackage.AddressHelper;

import com.briup.env.common.entity.Environment;
import com.briup.env.common.interfaces.Gather;

public class Gatherlmpl implements Gather{
 //定义一个数字和环境名称的关系的聚合
 private static Map<String, String> map;
 static{
  map = new HashMap<>();
  map.put("16", "温湿度");
  map.put("256", "光照强度");
  map.put("1280", "CO2浓度");
  
 }
 
 
 @Override
 public Collection<Environment> gather() {
  //提前定义一个集合,用来做返回
  Collection<Environment>coll=new LinkedList<>();
  
  //使用适当的流读取日志文件
  //声明流
  BufferedReader br =null;
  FileReader fr = null;
  try {
   //创建流
   fr = new FileReader(new File("Src/main/resources/radwtmp"));
   br = new BufferedReader(fr);
   //使用流
   String s=null;
   while ((s = br.readLine()) != null) {
    String[] value = s.split("[|]");
    String address = value[3];//定义传感器类型的变量
    String data = value[6];//定义环境数值的变量
    //System.out.println(Arrays.toString(value));
    //测试输出分割
    //新建一个Enviroment对象,将value对号入座
    Environment environment = new Environment();
   
    //给enviroment属性赋值
    //不需要处理的数据,直接博征格式正确放入即可
    environment.setSrcId(value[0]);
    environment.setDesId(value[1]);
    environment.setDevId(value[2]);
    environment.setAddress(address );//该值可以确定是哪个传感器的数据
    environment.setCount(Integer.parseInt(value[4]));
    environment.setCmd(value[5]);
    environment.setStatus(Integer.parseInt(value[7]));
    environment.setTime(new Timestamp(Long.parseLong(value[8])));
    
    //name和data数据需要做处理
    if ("16".equals(address)) {
     //温湿度
     //16的数据需要拆成两个Environment对象
     environment.setName("温度");
     //截取数据
int v1 = Integer.parseInt(data.substring(0,4),16);
     float f1 = (float)(((float)v1*0.00268127)-46.85);
     environment.setData(f1);
     
     //得在这里重新创建一个Environment对象
     Environment env = new Environment();
     env.setSrcId(value[0]);
     env.setDesId(value[1]);
     env.setDevId(value[2]);
     env.setAddress(address);//该值可以确定是哪个传感器的数据
     env.setCount(Integer.parseInt(value[4]));
     env.setCmd(value[5]);
     env.setStatus(Integer.parseInt(value[7]));
     env.setTime(new Timestamp(Long.parseLong(value[8])));
     env.setName("湿度");
     int v2 = Integer.parseInt(data.substring(4,8),16);
     float f2 = (float)(((float)v2*0.00190735)-6);
     env.setData(f2);
     
     coll.add(env);
     
     
    
    }else{
     //光照强度和CO2浓度的名字直接从map中获取
     environment.setName(map.get(address));
     int v = Integer.parseInt(data.substring(0,4),16);
     environment.setData(v);
    }
    
    
    environment.setName("");//这里没有办法从文件数据中获得,需要判断
    environment.setData(0);//这里需要计算
    
    //每循环一次,至少添加一次
    coll.add(environment);
   }
   
  } catch(FileNotFoundException e){
   e.printStackTrace();
  }
  
  catch (Exception e) {
   // TODO: handle exception
   e.printStackTrace();
  }finally {
   
   //流的关闭
   
  }
  
  //在控制台上输出日志文件的内容
  return coll;
 }
 

 @Test
 public void test(){
  //new Gatherlmpl().gather();
  Collection<Environment>coll = new Gatherlmpl().gather();
  coll.forEach(System.out::println);
  System.out.println("总的数据清单的条数");
  System.out.println(coll.size());
 } 
 
}
    

测试结果

maven生命周期,大阶段小阶段

二,网络模块

1.Clientlmpl.java

package com.briup.env.client;

import java.io.IOException;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.Collection;

import org.junit.Test;

import com.briup.env.common.entity.Environment;
import com.briup.env.common.interfaces.Client;

public class Clientlmpl implements Client {
    @Override
    public void send(Collection<Environment> coll) {
        // 定义网络模块的客户端
        Socket socket = null;
        String ip = "127.0.0.1";
        int port = 8888;
        // 对象输出流
        ObjectOutputStream oos = null;//流的初始化需要处理异常
        try {
            System.out.println("【客户端正在访问<"+ip+":"+port+">服务】");
            socket = new Socket(ip,port);
            
            oos = new ObjectOutputStream(socket.getOutputStream());
            oos.writeObject(coll);
            oos.flush();
            oos.close();
            System.out.println("【数据清单发送成功!】");
            
        } catch (IOException e) { 
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally {
            if(socket != null)
                try {
                    socket.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
        }
    }
    
    @Test
    public void test() {
        new Clientlmpl().send(null);
    }

}

测试结果

2.serverlements.java

package com.briup.env.server;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Collection;
import java.util.LinkedList;

import org.junit.Test;

import com.briup.env.common.entity.Environment;
import com.briup.env.common.interfaces.Server;

public class Serverlmpl implements Server{
    @Override
    public Collection<Environment> receive() {
        // 定义一个返回的数据
        Collection<Environment> coll = null;
        // 完成TCP/IP服务器的编写
        ServerSocket serverSocket = null;
        Socket socket = null;
        int port = 8888;
        // 对象输入流
        ObjectInputStream ois = null;
        
        try {
            serverSocket = new ServerSocket(port);
            // 开启监听
            System.out.println("【服务已经启动,正在监听"+port+"端口】");
            socket = serverSocket.accept();
            System.out.println(socket);
            
            ois = new ObjectInputStream(socket.getInputStream());
            Object obj = ois.readObject();
            coll = new LinkedList<>();
            // 比较安全的强转方式,强转是需要类型检测到
            //instanceof左边是引用,右边是类型,判断左侧类型是否属于右边类或子类,若是返回true,不是返回false
            if(obj instanceof Collection<?>) {
                Collection<?> collection = (Collection<?>)obj;
                for(Object o : collection) {
                    if(o instanceof Environment) {
                        Environment e = (Environment)o;
                        coll.add(e);
                    }
                }
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            if(socket != null)
                try {
                    socket.close();
                } catch (IOException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
            if(serverSocket != null)
                try {
                    serverSocket.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
        }
        return coll;
    }
    
    @Test
    public void test() {
        Collection<Environment> coll = new Serverlmpl().receive();
        coll.forEach(System.out::println);
        System.out.println("服务器端接收的数据共:"+coll.size()+"条");
    }
}

测试结果

3.新建一个main函数入口,把数据传输的过程进行连接。

package com.briup.env.client;
/**
 * 客户端整体启动入口
 */

import java.util.Collection;

import com.briup.env.common.entity.Environment;
import com.briup.env.common.interfaces.Client;
import com.briup.env.common.interfaces.Gather;

public class ClientMain {
    public static void main(String[] args) {
        // 创建采集对象
        Gather gather = new Gatherlmpl();
        // 采集数据清单
        Collection<Environment> coll = gather.gather();
        // 创建客户端对象
        Client client = new Clientlmpl();
        // 发送数据清单到服务器
        client.send(coll);
    }
}

连接后运行结果

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值