アルファ版。新版→https://osdn.jp/users/tacticsrealize/pf/ChlorophyllUploader/wiki/FrontPage
修訂. | 82b375367456635f711775be0d76dcafef88ca32 |
---|---|
大小 | 3,606 bytes |
時間 | 2015-07-08 20:15:01 |
作者 | |
Log Message | 新ファイル形式に対応しArduinoとの連携 |
package ants.chlorofilsender;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import com.opencsv.CSVParser;
import com.opencsv.CSVReader;
public class PacketChlorofilSender
{
public LocalDateTime time;
public Channel[] channels = new Channel[6];
public Channel average = new Channel();
public double height;
public PacketChlorofilSender()
{
for (int i = 0; i < channels.length; i++) {
channels[i] = new Channel();
}
}
public PacketChlorofilSender(
LocalDateTime time,
double ch1p, double ch1s,
double ch2p, double ch2s,
double ch3p, double ch3s,
double ch4p, double ch4s,
double ch5p, double ch5s,
double ch6p, double ch6s,
double averageP, double averageS,
double height)
{
this();
this.time = time;
channels[0].set(ch1p, ch1s);
channels[1].set(ch2p, ch2s);
channels[2].set(ch3p, ch3s);
channels[3].set(ch4p, ch4s);
channels[4].set(ch5p, ch5s);
channels[5].set(ch6p, ch6s);
average.set(averageP, averageS);
this.height = height;
}
public static ArrayList<PacketChlorofilSender> parse(LocalDateTime time, File file) throws IOException, PacketChlorofilSenderExcception
{
return parse(time, new FileInputStream(file));
}
public static ArrayList<PacketChlorofilSender> parse(LocalDateTime time, InputStream in) throws IOException, PacketChlorofilSenderExcception
{
try (CSVReader csvReader =
new CSVReader(new InputStreamReader(in))) {
ArrayList<PacketChlorofilSender> packets = new ArrayList<>();
for (String[] csvLine : csvReader) {
packets.add(parse(time, csvLine));
}
return packets;
}
}
public static PacketChlorofilSender parse(LocalDateTime time, String csv) throws IOException, PacketChlorofilSenderExcception
{
CSVParser parser = new CSVParser();
return parse(time, parser.parseLineMulti(csv));
}
public static class PacketChlorofilSenderExcception extends Exception
{
public PacketChlorofilSenderExcception(String string)
{
super(string);
}
public PacketChlorofilSenderExcception(NumberFormatException e)
{
super(e);
}
}
public static PacketChlorofilSender parse(LocalDateTime time, String[] csvLine) throws PacketChlorofilSenderExcception
{
// CSV生カットデータがくる
// カットデータの長さチェック
if (csvLine.length != 15) {
throw new PacketChlorofilSenderExcception("illegal size of columns: "
+ csvLine.length
+ " != 15");
}
// カットデータの整形と型変換
List<Double> doubles;
try {
doubles = Stream.of(csvLine)
.map(String::trim)
.map(Double::parseDouble)
.collect(Collectors.toList());
} catch (NumberFormatException e) {
throw new PacketChlorofilSenderExcception(e);
}
// build
return new PacketChlorofilSender(
time,
doubles.get(0),
doubles.get(1),
doubles.get(2),
doubles.get(3),
doubles.get(4),
doubles.get(5),
doubles.get(6),
doubles.get(7),
doubles.get(8),
doubles.get(9),
doubles.get(10),
doubles.get(11),
doubles.get(12),
doubles.get(13),
doubles.get(14));
}
public static class Channel
{
public double p;
public double s;
public void set(double p, double s)
{
this.p = p;
this.s = s;
}
}
}