Setting the HTTP charset parameter

本文介绍W3C组织定义的HTTP字符集标准,该标准对于确保国际化的Web内容正确传输至关重要。
[url]http://www.w3.org/International/O-HTTP-charset[/url]
使用的早期版本给出更正后的代码:/* * Copyright 2010 Srikanth Reddy Lingala * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package net.lingala.zip4j; import net.lingala.zip4j.exception.ZipException; import net.lingala.zip4j.headers.HeaderReader; import net.lingala.zip4j.headers.HeaderUtil; import net.lingala.zip4j.headers.HeaderWriter; import net.lingala.zip4j.io.inputstream.NumberedSplitRandomAccessFile; import net.lingala.zip4j.io.inputstream.ZipInputStream; import net.lingala.zip4j.model.FileHeader; import net.lingala.zip4j.model.UnzipParameters; import net.lingala.zip4j.model.Zip4jConfig; import net.lingala.zip4j.model.ZipModel; import net.lingala.zip4j.model.ZipParameters; import net.lingala.zip4j.model.enums.RandomAccessFileMode; import net.lingala.zip4j.progress.ProgressMonitor; import net.lingala.zip4j.tasks.AddFilesToZipTask; import net.lingala.zip4j.tasks.AddFilesToZipTask.AddFilesToZipTaskParameters; import net.lingala.zip4j.tasks.AddFolderToZipTask; import net.lingala.zip4j.tasks.AddFolderToZipTask.AddFolderToZipTaskParameters; import net.lingala.zip4j.tasks.AddStreamToZipTask; import net.lingala.zip4j.tasks.AddStreamToZipTask.AddStreamToZipTaskParameters; import net.lingala.zip4j.tasks.AsyncZipTask; import net.lingala.zip4j.tasks.ExtractAllFilesTask; import net.lingala.zip4j.tasks.ExtractAllFilesTask.ExtractAllFilesTaskParameters; import net.lingala.zip4j.tasks.ExtractFileTask; import net.lingala.zip4j.tasks.ExtractFileTask.ExtractFileTaskParameters; import net.lingala.zip4j.tasks.MergeSplitZipFileTask; import net.lingala.zip4j.tasks.MergeSplitZipFileTask.MergeSplitZipFileTaskParameters; import net.lingala.zip4j.tasks.RemoveFilesFromZipTask; import net.lingala.zip4j.tasks.RemoveFilesFromZipTask.RemoveFilesFromZipTaskParameters; import net.lingala.zip4j.tasks.RenameFilesTask; import net.lingala.zip4j.tasks.RenameFilesTask.RenameFilesTaskParameters; import net.lingala.zip4j.tasks.SetCommentTask; import net.lingala.zip4j.tasks.SetCommentTask.SetCommentTaskTaskParameters; import net.lingala.zip4j.util.FileUtils; import net.lingala.zip4j.util.InternalZipConstants; import net.lingala.zip4j.util.RawIO; import net.lingala.zip4j.util.Zip4jUtil; import java.io.Closeable; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.io.RandomAccessFile; import java.nio.charset.Charset; import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.Map; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.ThreadFactory; import static net.lingala.zip4j.util.FileUtils.isNumberedSplitFile; import static net.lingala.zip4j.util.InternalZipConstants.CHARSET_UTF_8; import static net.lingala.zip4j.util.InternalZipConstants.MIN_BUFF_SIZE; import static net.lingala.zip4j.util.UnzipUtil.createZipInputStream; import static net.lingala.zip4j.util.Zip4jUtil.isStringNotNullAndNotEmpty; /** * Base class to handle zip files. Some of the operations supported * in this class are:<br> * <ul> * <li>Create Zip File</li> * <li>Add files to zip file</li> * <li>Add folder to zip file</li> * <li>Extract files from zip files</li> * <li>Remove files from zip file</li> * </ul> */ public class ZipFile implements Closeable { private File zipFile; private ZipModel zipModel; private boolean isEncrypted; private ProgressMonitor progressMonitor; private boolean runInThread; private char[] password; private HeaderWriter headerWriter = new HeaderWriter(); private Charset charset = null; private ThreadFactory threadFactory; private ExecutorService executorService; private int bufferSize = InternalZipConstants.BUFF_SIZE; private List<InputStream> openInputStreams = new ArrayList<>(); private boolean useUtf8CharsetForPasswords = InternalZipConstants.USE_UTF8_FOR_PASSWORD_ENCODING_DECODING; /** * Creates a new ZipFile instance with the zip file at the location specified in zipFile. * This constructor does not yet create a zip file if it does not exist. Creation happens when adding files * to this ZipFile instance * @param zipFile */ public ZipFile(String zipFile) { this(new File(zipFile), null); } /** * Creates a new ZipFile instance with the zip file at the location specified in zipFile. * Input password will be used for any zip operations like adding files or extracting files. * This constructor does not yet create a zip file if it does not exist. Creation happens when adding files * to this ZipFile instance * @param zipFile */ public ZipFile(String zipFile, char[] password) { this(new File(zipFile), password); } /** * Creates a new Zip File Object with the input file. * If the zip file does not exist, it is not created at this point. * * @param zipFile file reference to the zip file * @throws IllegalArgumentException when zip file parameter is null */ public ZipFile(File zipFile) { this(zipFile, null); } /** * Creates a new Zip File Object with the input file. * If the zip file does not exist, it is not created at this point. * * @param zipFile file reference to the zip file * @param password password to use for the zip file * @throws IllegalArgumentException when zip file parameter is null */ public ZipFile(File zipFile, char[] password) { if (zipFile == null) { throw new IllegalArgumentException("input zip file parameter is null"); } this.zipFile = zipFile; this.password = password; this.runInThread = false; this.progressMonitor = new ProgressMonitor(); } /** * Creates a zip file and adds the list of source file(s) to the zip file. If the zip file * exists then this method throws an exception. Parameters such as compression type, etc * can be set in the input parameters. While the method addFile/addFiles also creates the * zip file if it does not exist, the main functionality of this method is to create a split * zip file. To create a split zip file, set the splitArchive parameter to true with a valid * splitLength. Split Length has to be more than 65536 bytes * * @param filesToAdd - File to be added to the zip file * @param parameters - zip parameters for this file list * @param splitArchive - if archive has to be split or not * @param splitLength - if archive has to be split, then length in bytes at which it has to be split * @throws ZipException */ public void createSplitZipFile(List<File> filesToAdd, ZipParameters parameters, boolean splitArchive, long splitLength) throws ZipException { if (zipFile.exists()) { throw new ZipException("zip file: " + zipFile + " already exists. To add files to existing zip file use addFile method"); } if (filesToAdd == null || filesToAdd.size() == 0) { throw new ZipException("input file List is null, cannot create zip file"); } createNewZipModel(); zipModel.setSplitArchive(splitArchive); zipModel.setSplitLength(splitLength); new AddFilesToZipTask(zipModel, password, headerWriter, buildAsyncParameters()).execute( new AddFilesToZipTaskParameters(filesToAdd, parameters, buildConfig())); } /** * Creates a zip file and adds the files/folders from the specified folder to the zip file. * This method does the same functionality as in addFolder method except that this method * can also create split zip files when adding a folder. To create a split zip file, set the * splitArchive parameter to true and specify the splitLength. Split length has to be more than * or equal to 65536 bytes. Note that this method throws an exception if the zip file already * exists. * * @param folderToAdd * @param parameters * @param splitArchive * @param splitLength * @throws ZipException */ public void createSplitZipFileFromFolder(File folderToAdd, ZipParameters parameters, boolean splitArchive, long splitLength) throws ZipException { if (folderToAdd == null) { throw new ZipException("folderToAdd is null, cannot create zip file from folder"); } if (parameters == null) { throw new ZipException("input parameters are null, cannot create zip file from folder"); } if (zipFile.exists()) { throw new ZipException("zip file: " + zipFile + " already exists. To add files to existing zip file use addFolder method"); } createNewZipModel(); zipModel.setSplitArchive(splitArchive); if (splitArchive) { zipModel.setSplitLength(splitLength); } addFolder(folderToAdd, parameters, false); } /** * Adds input source file to the zip file with default zip parameters. If zip file does not exist, * this method creates a new zip file. * * @param fileToAdd - File with path to be added to the zip file * @throws ZipException */ public void addFile(String fileToAdd) throws ZipException { addFile(fileToAdd, new ZipParameters()); } /** * Adds input source file to the zip file with provided zip parameters. If zip file does not exist, * this method creates a new zip file. * * @param fileToAdd - File with path to be added to the zip file * @param zipParameters - parameters for the entry to be added to zip * @throws ZipException */ public void addFile(String fileToAdd, ZipParameters zipParameters) throws ZipException { if (!isStringNotNullAndNotEmpty(fileToAdd)) { throw new ZipException("file to add is null or empty"); } addFiles(Collections.singletonList(new File(fileToAdd)), zipParameters); } /** * Adds input source file to the zip file with default zip parameters. If zip file does not exist, * this method creates a new zip file. * * @param fileToAdd - File to be added to the zip file * @throws ZipException */ public void addFile(File fileToAdd) throws ZipException { addFiles(Collections.singletonList(fileToAdd), new ZipParameters()); } /** * Adds input source file to the zip file. If zip file does not exist, * this method creates a new zip file. Parameters such as compression type, etc * can be set in the input parameters. * * @param fileToAdd - File to be added to the zip file * @param parameters - zip parameters for this file * @throws ZipException */ public void addFile(File fileToAdd, ZipParameters parameters) throws ZipException { addFiles(Collections.singletonList(fileToAdd), parameters); } /** * Adds the list of input files to the zip file with default zip parameters. If zip file does not exist, * this method creates a new zip file. * * @param filesToAdd * @throws ZipException */ public void addFiles(List<File> filesToAdd) throws ZipException { addFiles(filesToAdd, new ZipParameters()); } /** * Adds the list of input files to the zip file. If zip file does not exist, then * this method creates a new zip file. Parameters such as compression type, etc * can be set in the input parameters. * * @param filesToAdd * @param parameters * @throws ZipException */ public void addFiles(List<File> filesToAdd, ZipParameters parameters) throws ZipException { if (filesToAdd == null || filesToAdd.size() == 0) { throw new ZipException("input file List is null or empty"); } if (parameters == null) { throw new ZipException("input parameters are null"); } readZipInfo(); if (zipModel == null) { throw new ZipException("internal error: zip model is null"); } if (zipFile.exists() && zipModel.isSplitArchive()) { throw new ZipException("Zip file already exists. Zip file format does not allow updating split/spanned files"); } new AddFilesToZipTask(zipModel, password, headerWriter, buildAsyncParameters()).execute( new AddFilesToZipTaskParameters(filesToAdd, parameters, buildConfig())); } /** * Adds the folder in the given file object to the zip file with default zip parameters. If zip file does not exist, * then a new zip file is created. If input folder is invalid then an exception * is thrown. * * @param folderToAdd * @throws ZipException */ public void addFolder(File folderToAdd) throws ZipException { addFolder(folderToAdd, new ZipParameters()); } /** * Adds the folder in the given file object to the zip file. If zip file does not exist, * then a new zip file is created. If input folder is invalid then an exception * is thrown. Zip parameters for the files in the folder to be added can be set in * the input parameters * * @param folderToAdd * @param zipParameters * @throws ZipException */ public void addFolder(File folderToAdd, ZipParameters zipParameters) throws ZipException { if (folderToAdd == null) { throw new ZipException("input path is null, cannot add folder to zip file"); } if (!folderToAdd.exists()) { throw new ZipException("folder does not exist"); } if (!folderToAdd.isDirectory()) { throw new ZipException("input folder is not a directory"); } if (!folderToAdd.canRead()) { throw new ZipException("cannot read input folder"); } if (zipParameters == null) { throw new ZipException("input parameters are null, cannot add folder to zip file"); } addFolder(folderToAdd, zipParameters, true); } /** * Internal method to add a folder to the zip file. * * @param folderToAdd * @param zipParameters * @param checkSplitArchive * @throws ZipException */ private void addFolder(File folderToAdd, ZipParameters zipParameters, boolean checkSplitArchive) throws ZipException { readZipInfo(); if (zipModel == null) { throw new ZipException("internal error: zip model is null"); } if (checkSplitArchive) { if (zipModel.isSplitArchive()) { throw new ZipException("This is a split archive. Zip file format does not allow updating split/spanned files"); } } new AddFolderToZipTask(zipModel, password, headerWriter, buildAsyncParameters()).execute( new AddFolderToZipTaskParameters(folderToAdd, zipParameters, buildConfig())); } /** * Creates a new entry in the zip file and adds the content of the input stream to the * zip file. ZipParameters.isSourceExternalStream and ZipParameters.fileNameInZip have to be * set before in the input parameters. If the file name ends with / or \, this method treats the * content as a directory. Setting the flag ProgressMonitor.setRunInThread to true will have * no effect for this method and hence this method cannot be used to add content to zip in * thread mode * * @param inputStream * @param parameters * @throws ZipException */ public void addStream(InputStream inputStream, ZipParameters parameters) throws ZipException { if (inputStream == null) { throw new ZipException("inputstream is null, cannot add file to zip"); } if (parameters == null) { throw new ZipException("zip parameters are null"); } this.setRunInThread(false); readZipInfo(); if (zipModel == null) { throw new ZipException("internal error: zip model is null"); } if (zipFile.exists() && zipModel.isSplitArchive()) { throw new ZipException("Zip file already exists. Zip file format does not allow updating split/spanned files"); } new AddStreamToZipTask(zipModel, password, headerWriter, buildAsyncParameters()).execute( new AddStreamToZipTaskParameters(inputStream, parameters, buildConfig())); } /** * Extracts all the files in the given zip file to the input destination path. * If zip file does not exist or destination path is invalid then an * exception is thrown. * * @param destinationPath path to which the entries of the zip are to be extracted * @throws ZipException when an issue occurs during extraction */ public void extractAll(String destinationPath) throws ZipException { extractAll(destinationPath, new UnzipParameters()); } /** * Extracts all entries in the zip file to the destination path considering the options defined in * UnzipParameters * * @param destinationPath path to which the entries of the zip are to be extracted * @param unzipParameters parameters to be considered during extraction * @throws ZipException when an issue occurs during extraction */ public void extractAll(String destinationPath, UnzipParameters unzipParameters) throws ZipException { if (!isStringNotNullAndNotEmpty(destinationPath)) { throw new ZipException("output path is null or invalid"); } if (!Zip4jUtil.createDirectoryIfNotExists(new File(destinationPath))) { throw new ZipException("invalid output path"); } if (zipModel == null) { readZipInfo(); } // Throw an exception if zipModel is still null if (zipModel == null) { throw new ZipException("Internal error occurred when extracting zip file"); } new ExtractAllFilesTask(zipModel, password, unzipParameters, buildAsyncParameters()).execute( new ExtractAllFilesTaskParameters(destinationPath, buildConfig())); } /** * Extracts a specific file from the zip file to the destination path. * If destination path is invalid, then this method throws an exception. * <br><br> * If fileHeader is a directory, this method extracts all files under this directory * * @param fileHeader file header corresponding to the entry which has to be extracted * @param destinationPath path to which the entries of the zip are to be extracted * @throws ZipException when an issue occurs during extraction */ public void extractFile(FileHeader fileHeader, String destinationPath) throws ZipException { extractFile(fileHeader, destinationPath, null, new UnzipParameters()); } /** * Extracts a specific file from the zip file to the destination path. * If destination path is invalid, then this method throws an exception. * <br><br> * If fileHeader is a directory, this method extracts all files under this directory * * @param fileHeader file header corresponding to the entry which has to be extracted * @param destinationPath path to which the entries of the zip are to be extracted * @param unzipParameters any parameters that have to be considered during extraction * @throws ZipException when an issue occurs during extraction */ public void extractFile(FileHeader fileHeader, String destinationPath, UnzipParameters unzipParameters) throws ZipException { extractFile(fileHeader, destinationPath, null, unzipParameters); } /** * Extracts a specific file from the zip file to the destination path. * If destination path is invalid, then this method throws an exception. * <br><br> * If newFileName is not null or empty, newly created file name will be replaced by * the value in newFileName. If this value is null, then the file name will be the * value in FileHeader.getFileName. If file being extract is a directory, the directory name * will be replaced with the newFileName * <br><br> * If fileHeader is a directory, this method extracts all files under this directory. * <br/><br/> * Any parameters that have to be considered during extraction can be passed in through unzipParameters * * @param fileHeader file header corresponding to the entry which has to be extracted * @param destinationPath path to which the entries of the zip are to be extracted * @param newFileName if not null, this will be the name given to the file upon extraction * @param unzipParameters any parameters that have to be considered during extraction * @throws ZipException when an issue occurs during extraction */ public void extractFile(FileHeader fileHeader, String destinationPath, String newFileName, UnzipParameters unzipParameters) throws ZipException { if (fileHeader == null) { throw new ZipException("input file header is null, cannot extract file"); } extractFile(fileHeader.getFileName(), destinationPath, newFileName, unzipParameters); } /** * Extracts a specific file from the zip file to the destination path. * This method first finds the necessary file header from the input file name. * <br><br> * File name is relative file name in the zip file. For example if a zip file contains * a file "a.txt", then to extract this file, input file name has to be "a.txt". Another * example is if there is a file "b.txt" in a folder "abc" in the zip file, then the * input file name has to be abc/b.txt * <br><br> * If fileHeader is a directory, this method extracts all files under this directory. * <br><br> * Throws an exception of type {@link ZipException.Type#FILE_NOT_FOUND} if file header could not be found for the given file name. * Throws an exception if the destination path is invalid. * * @param fileName name of the entry which has to be extracted * @param destinationPath path to which the entries of the zip are to be extracted * @throws ZipException when an issue occurs during extraction */ public void extractFile(String fileName, String destinationPath) throws ZipException { extractFile(fileName, destinationPath, null, new UnzipParameters()); } /** * Extracts a specific file from the zip file to the destination path. * This method first finds the necessary file header from the input file name. * <br><br> * File name is relative file name in the zip file. For example if a zip file contains * a file "a.txt", then to extract this file, input file name has to be "a.txt". Another * example is if there is a file "b.txt" in a folder "abc" in the zip file, then the * input file name has to be abc/b.txt * <br><br> * If fileHeader is a directory, this method extracts all files under this directory. * <br><br> * Any parameters that have to be considered during extraction can be passed in through unzipParameters * <br/><br/> * Throws an exception of type {@link ZipException.Type#FILE_NOT_FOUND} if file header could not be found for the given file name. * Throws an exception if the destination path is invalid. * * @param fileName name of the entry which has to be extracted * @param destinationPath path to which the entries of the zip are to be extracted * @param unzipParameters any parameters that have to be considered during extraction * @throws ZipException when an issue occurs during extraction */ public void extractFile(String fileName, String destinationPath, UnzipParameters unzipParameters) throws ZipException { extractFile(fileName, destinationPath, null, unzipParameters); } /** * Extracts a specific file from the zip file to the destination path. * This method first finds the necessary file header from the input file name. * <br><br> * File name is relative file name in the zip file. For example if a zip file contains * a file "a.txt", then to extract this file, input file name has to be "a.txt". Another * example is if there is a file "b.txt" in a folder "abc" in the zip file, then the * input file name has to be abc/b.txt * <br><br> * If newFileName is not null or empty, newly created file name will be replaced by * the value in newFileName. If this value is null, then the file name will be the * value in FileHeader.getFileName. If file being extract is a directory, the directory name * will be replaced with the newFileName * <br><br> * If fileHeader is a directory, this method extracts all files under this directory. * <br><br> * Throws an exception of type {@link ZipException.Type#FILE_NOT_FOUND} if file header could not be found for the given file name. * Throws an exception if the destination path is invalid. * * @param fileName name of the entry which has to be extracted * @param destinationPath path to which the entries of the zip are to be extracted * @param newFileName if not null, this will be the name given to the file upon extraction * @throws ZipException when an issue occurs during extraction */ public void extractFile(String fileName, String destinationPath, String newFileName) throws ZipException { extractFile(fileName, destinationPath, newFileName, new UnzipParameters()); } /** * Extracts a specific file from the zip file to the destination path. * If destination path is invalid, then this method throws an exception. * <br><br> * If newFileName is not null or empty, newly created file name will be replaced by * the value in newFileName. If this value is null, then the file name will be the * value in FileHeader.getFileName. If file being extract is a directory, the directory name * will be replaced with the newFileName * <br><br> * If fileHeader is a directory, this method extracts all files under this directory. * * @param fileHeader file header corresponding to the entry which has to be extracted * @param destinationPath path to which the entries of the zip are to be extracted * @param newFileName if not null, this will be the name given to the file upon extraction * @throws ZipException when an issue occurs during extraction */ public void extractFile(FileHeader fileHeader, String destinationPath, String newFileName) throws ZipException { extractFile(fileHeader, destinationPath, newFileName, new UnzipParameters()); } /** * Extracts a specific file from the zip file to the destination path. * This method first finds the necessary file header from the input file name. * <br/><br/> * File name is relative file name in the zip file. For example if a zip file contains * a file "a.txt", then to extract this file, input file name has to be "a.txt". Another * example is if there is a file "b.txt" in a folder "abc" in the zip file, then the * input file name has to be abc/b.txt * <br/><br/> * If newFileName is not null or empty, newly created file name will be replaced by * the value in newFileName. If this value is null, then the file name will be the * value in FileHeader.getFileName. If file being extract is a directory, the directory name * will be replaced with the newFileName * <br/><br/> * If fileHeader is a directory, this method extracts all files under this directory. * <br/><br/> * Any parameters that have to be considered during extraction can be passed in through unzipParameters * <br/><br/> * Throws an exception of type {@link ZipException.Type#FILE_NOT_FOUND} if file header could not be found for the * given file name. * Throws an exception if the destination path is invalid. * * @param fileName name of the entry which has to be extracted * @param destinationPath path to which the entries of the zip are to be extracted * @param newFileName if not null, this will be the name given to the file upon extraction * @param unzipParameters any parameters that have to be considered during extraction * @throws ZipException when an issue occurs during extraction */ public void extractFile(String fileName, String destinationPath, String newFileName, UnzipParameters unzipParameters) throws ZipException { if (!isStringNotNullAndNotEmpty(fileName)) { throw new ZipException("file to extract is null or empty, cannot extract file"); } if (!isStringNotNullAndNotEmpty(destinationPath)) { throw new ZipException("destination path is empty or null, cannot extract file"); } if (unzipParameters == null) { unzipParameters = new UnzipParameters(); } readZipInfo(); new ExtractFileTask(zipModel, password, unzipParameters, buildAsyncParameters()).execute( new ExtractFileTaskParameters(destinationPath, fileName, newFileName, buildConfig())); } /** * Returns the list of file headers in the zip file. Returns an empty list if the zip file does not exist. * * @return list of file headers * @throws ZipException */ public List<FileHeader> getFileHeaders() throws ZipException { readZipInfo(); if (zipModel == null || zipModel.getCentralDirectory() == null) { return Collections.emptyList(); } return zipModel.getCentralDirectory().getFileHeaders(); } /** * Returns FileHeader if a file header with the given fileHeader * string exists in the zip model: If not returns null * * @param fileName * @return FileHeader * @throws ZipException */ public FileHeader getFileHeader(String fileName) throws ZipException { if (!isStringNotNullAndNotEmpty(fileName)) { throw new ZipException("input file name is emtpy or null, cannot get FileHeader"); } readZipInfo(); if (zipModel == null || zipModel.getCentralDirectory() == null) { return null; } return HeaderUtil.getFileHeader(zipModel, fileName); } /** * Checks to see if the zip file is encrypted * * @return true if encrypted, false if not * @throws ZipException */ public boolean isEncrypted() throws ZipException { if (zipModel == null) { readZipInfo(); if (zipModel == null) { throw new ZipException("Zip Model is null"); } } if (zipModel.getCentralDirectory() == null || zipModel.getCentralDirectory().getFileHeaders() == null) { throw new ZipException("invalid zip file"); } for (FileHeader fileHeader : zipModel.getCentralDirectory().getFileHeaders()) { if (fileHeader != null) { if (fileHeader.isEncrypted()) { isEncrypted = true; break; } } } return isEncrypted; } /** * Checks if the zip file is a split archive * * @return true if split archive, false if not * @throws ZipException */ public boolean isSplitArchive() throws ZipException { if (zipModel == null) { readZipInfo(); if (zipModel == null) { throw new ZipException("Zip Model is null"); } } return zipModel.isSplitArchive(); } /** * Removes the file provided in the input file header from the zip file. * * If zip file is a split zip file, then this method throws an exception as * zip specification does not allow for updating split zip archives. * * If this file header is a directory, all files and directories * under this directory will be removed as well. * * @param fileHeader * @throws ZipException */ public void removeFile(FileHeader fileHeader) throws ZipException { if (fileHeader == null) { throw new ZipException("input file header is null, cannot remove file"); } removeFile(fileHeader.getFileName()); } /** * Removes the file provided in the input parameters from the zip file. * This method first finds the file header and then removes the file. * * If file does not exist, then this method throws an exception. * * If zip file is a split zip file, then this method throws an exception as * zip specification does not allow for updating split zip archives. * * If the entry representing this file name is a directory, all files and directories * under this directory will be removed as well. * * @param fileName * @throws ZipException */ public void removeFile(String fileName) throws ZipException { if (!isStringNotNullAndNotEmpty(fileName)) { throw new ZipException("file name is empty or null, cannot remove file"); } removeFiles(Collections.singletonList(fileName)); } /** * Removes all files from the zip file that match the names in the input list. * * If any of the file is a directory, all the files and directories under this directory * will be removed as well * * If zip file is a split zip file, then this method throws an exception as * zip specification does not allow for updating split zip archives. * * @param fileNames * @throws ZipException */ public void removeFiles(List<String> fileNames) throws ZipException { if (fileNames == null) { throw new ZipException("fileNames list is null"); } if (fileNames.isEmpty()) { return; } if (zipModel == null) { readZipInfo(); } if (zipModel.isSplitArchive()) { throw new ZipException("Zip file format does not allow updating split/spanned files"); } new RemoveFilesFromZipTask(zipModel, headerWriter, buildAsyncParameters()).execute( new RemoveFilesFromZipTaskParameters(fileNames, buildConfig())); } /** * Renames file name of the entry represented by file header. If the file name in the input file header does not * match any entry in the zip file, the zip file will not be modified. * * If the file header is a folder in the zip file, all sub-files and sub-folders in the zip file will also be renamed. * * Zip file format does not allow modifying a split zip file. Therefore if the zip file being dealt with is a split * zip file, this method throws an exception * * @param fileHeader file header to be changed * @param newFileName the file name that has to be changed to * @throws ZipException if fileHeader is null or newFileName is null or empty or if the zip file is a split file */ public void renameFile(FileHeader fileHeader, String newFileName) throws ZipException { if (fileHeader == null) { throw new ZipException("File header is null"); } renameFile(fileHeader.getFileName(), newFileName); } /** * Renames file name of the entry represented by input fileNameToRename. If there is no entry in the zip file matching * the file name as in fileNameToRename, the zip file will not be modified. * * If the entry with fileNameToRename is a folder in the zip file, all sub-files and sub-folders in the zip file will * also be renamed. For a folder, the fileNameToRename has to end with zip file separator "/". For example, if a * folder name "some-folder-name" has to be modified to "new-folder-name", then value of fileNameToRename should be * "some-folder-name/". If newFileName does not end with a separator, zip4j will add a separator. * * Zip file format does not allow modifying a split zip file. Therefore if the zip file being dealt with is a split * zip file, this method throws an exception * * @param fileNameToRename file name in the zip that has to be renamed * @param newFileName the file name that has to be changed to * @throws ZipException if fileNameToRename is empty or newFileName is empty or if the zip file is a split file */ public void renameFile(String fileNameToRename, String newFileName) throws ZipException { if (!Zip4jUtil.isStringNotNullAndNotEmpty(fileNameToRename)) { throw new ZipException("file name to be changed is null or empty"); } if (!Zip4jUtil.isStringNotNullAndNotEmpty(newFileName)) { throw new ZipException("newFileName is null or empty"); } renameFiles(Collections.singletonMap(fileNameToRename, newFileName)); } /** * Renames all the entries in the zip file that match the keys in the map to their corresponding values in the map. If * there are no entries matching any of the keys from the map, the zip file is not modified. * * If any of the entry in the map represents a folder, all files and folders will be renamed so that their parent * represents the renamed folder. * * Zip file format does not allow modifying a split zip file. Therefore if the zip file being dealt with is a split * zip file, this method throws an exception * * @param fileNamesMap map of file names that have to be changed with values in the map being the name to be changed to * @throws ZipException if map is null or if the zip file is a split file */ public void renameFiles(Map<String, String> fileNamesMap) throws ZipException { if (fileNamesMap == null) { throw new ZipException("fileNamesMap is null"); } if (fileNamesMap.size() == 0) { return; } readZipInfo(); if (zipModel.isSplitArchive()) { throw new ZipException("Zip file format does not allow updating split/spanned files"); } AsyncZipTask.AsyncTaskParameters asyncTaskParameters = buildAsyncParameters(); new RenameFilesTask(zipModel, headerWriter, new RawIO(), asyncTaskParameters).execute( new RenameFilesTaskParameters(fileNamesMap, buildConfig())); } /** * Merges split zip files into a single zip file without the need to extract the * files in the archive * * @param outputZipFile * @throws ZipException */ public void mergeSplitFiles(File outputZipFile) throws ZipException { if (outputZipFile == null) { throw new ZipException("outputZipFile is null, cannot merge split files"); } if (outputZipFile.exists()) { throw new ZipException("output Zip File already exists"); } readZipInfo(); if (this.zipModel == null) { throw new ZipException("zip model is null, corrupt zip file?"); } new MergeSplitZipFileTask(zipModel, buildAsyncParameters()).execute( new MergeSplitZipFileTaskParameters(outputZipFile, buildConfig())); } /** * Sets comment for the Zip file * * @param comment * @throws ZipException */ public void setComment(String comment) throws ZipException { if (comment == null) { throw new ZipException("input comment is null, cannot update zip file"); } if (!zipFile.exists()) { throw new ZipException("zip file does not exist, cannot set comment for zip file"); } readZipInfo(); if (zipModel == null) { throw new ZipException("zipModel is null, cannot update zip file"); } if (zipModel.getEndOfCentralDirectoryRecord() == null) { throw new ZipException("end of central directory is null, cannot set comment"); } new SetCommentTask(zipModel, buildAsyncParameters()).execute( new SetCommentTaskTaskParameters(comment, buildConfig())); } /** * Returns the comment set for the Zip file * * @return String * @throws ZipException */ public String getComment() throws ZipException { if (!zipFile.exists()) { throw new ZipException("zip file does not exist, cannot read comment"); } readZipInfo(); if (zipModel == null) { throw new ZipException("zip model is null, cannot read comment"); } if (zipModel.getEndOfCentralDirectoryRecord() == null) { throw new ZipException("end of central directory record is null, cannot read comment"); } return zipModel.getEndOfCentralDirectoryRecord().getComment(); } /** * Returns an input stream for reading the contents of the Zip file corresponding * to the input FileHeader. Throws an exception if the FileHeader does not exist * in the ZipFile * * @param fileHeader * @return ZipInputStream * @throws ZipException */ public ZipInputStream getInputStream(FileHeader fileHeader) throws IOException { if (fileHeader == null) { throw new ZipException("FileHeader is null, cannot get InputStream"); } readZipInfo(); if (zipModel == null) { throw new ZipException("zip model is null, cannot get inputstream"); } ZipInputStream zipInputStream = createZipInputStream(zipModel, fileHeader, password); openInputStreams.add(zipInputStream); return zipInputStream; } /** * Checks to see if the input zip file is a valid zip file. This method * will try to read zip headers. If headers are read successfully, this * method returns true else false. * * Since v2.7.0: if the zip file is a split zip file, this method also checks to see if * all the split files of the zip exists. * * @return boolean - true if a valid zip file, i.e, zip4j is able to read the * zip headers, and in case of a split zip file, all split files of the zip exists; false otherwise * * @since 1.2.3 */ public boolean isValidZipFile() { if (!zipFile.exists()) { return false; } try { readZipInfo(); if (zipModel.isSplitArchive() && !verifyAllSplitFilesOfZipExists(getSplitZipFiles())) { return false; } return true; } catch (Exception e) { return false; } } /** * Returns the full file path+names of all split zip files * in an ArrayList. For example: If a split zip file(abc.zip) has a 10 split parts * this method returns an array list with path + "abc.z01", path + "abc.z02", etc. * Returns null if the zip file does not exist * * @return List of Split zip Files * @throws ZipException */ public List<File> getSplitZipFiles() throws ZipException { readZipInfo(); return FileUtils.getSplitZipFiles(zipModel); } /** * Closes any open streams that were open by an instance of this class. * * @throws IOException when the underlying input stream throws an exception when trying to close it */ @Override public void close() throws IOException { for (InputStream inputStream : openInputStreams) { inputStream.close(); } openInputStreams.clear(); } /** * Sets a password to be used for the zip file. Will override if a password supplied via ZipFile constructor * @param password - char array of the password to be used */ public void setPassword(char[] password) { this.password = password; } /** * Returns the size of the buffer used to read streams * * @return size of the buffer used to read streams */ public int getBufferSize() { return bufferSize; } /** * Sets the size of buffer that should be used when reading streams. This size cannot be less than the value defined * in InternalZipConstants.MIN_BUFF_SIZE * * @param bufferSize size of the buffer that should be used when reading streams * @throws IllegalArgumentException if bufferSize is less than value configured in InternalZipConstants.MIN_BUFF_SIZE */ public void setBufferSize(int bufferSize) { if (bufferSize < MIN_BUFF_SIZE) { throw new IllegalArgumentException("Buffer size cannot be less than " + MIN_BUFF_SIZE + " bytes"); } this.bufferSize = bufferSize; } /** * Reads the zip header information for this zip file. If the zip file * does not exist, it creates an empty zip model.<br><br> * <b>Note:</b> This method does not read local file header information * * @throws ZipException */ private void readZipInfo() throws ZipException { if (zipModel != null) { return; } if (!zipFile.exists()) { createNewZipModel(); return; } if (!zipFile.canRead()) { throw new ZipException("no read access for the input zip file"); } try (RandomAccessFile randomAccessFile = initializeRandomAccessFileForHeaderReading()) { HeaderReader headerReader = new HeaderReader(); zipModel = headerReader.readAllHeaders(randomAccessFile, buildConfig()); zipModel.setZipFile(zipFile); } catch (ZipException e) { throw e; } catch (IOException e) { throw new ZipException(e); } } private void createNewZipModel() { zipModel = new ZipModel(); zipModel.setZipFile(zipFile); } private RandomAccessFile initializeRandomAccessFileForHeaderReading() throws IOException { if (isNumberedSplitFile(zipFile)) { File[] allSplitFiles = FileUtils.getAllSortedNumberedSplitFiles(zipFile); NumberedSplitRandomAccessFile numberedSplitRandomAccessFile = new NumberedSplitRandomAccessFile(zipFile, RandomAccessFileMode.READ.getValue(), allSplitFiles); numberedSplitRandomAccessFile.openLastSplitFileForReading(); return numberedSplitRandomAccessFile; } return new RandomAccessFile(zipFile, RandomAccessFileMode.READ.getValue()); } private AsyncZipTask.AsyncTaskParameters buildAsyncParameters() { if (runInThread) { if (threadFactory == null) { threadFactory = Executors.defaultThreadFactory(); } executorService = Executors.newSingleThreadExecutor(threadFactory); } return new AsyncZipTask.AsyncTaskParameters(executorService, runInThread, progressMonitor); } private boolean verifyAllSplitFilesOfZipExists(List<File> allSplitFiles) { for (File splitFile : allSplitFiles) { if (!splitFile.exists()) { return false; } } return true; } public ProgressMonitor getProgressMonitor() { return progressMonitor; } public boolean isRunInThread() { return runInThread; } public void setRunInThread(boolean runInThread) { this.runInThread = runInThread; } public File getFile() { return zipFile; } /** * Returns user defined charset that was set by setCharset() method. If no charset was explicitly defined * (by calling setCharset()), this method returns the default charset which zip4j uses, which is utf-8. * * @return user-defined charset or utf-8 if no charset explicitly set */ public Charset getCharset() { if (charset == null) { return CHARSET_UTF_8; } return charset; } /** * Sets the charset to be used for encoding file names and comments * * @param charset charset to use to encode file names and comments * @throws IllegalArgumentException if charset is null */ public void setCharset(Charset charset) throws IllegalArgumentException { if(charset == null) { throw new IllegalArgumentException("charset cannot be null"); } this.charset = charset; } public void setThreadFactory(ThreadFactory threadFactory) { this.threadFactory = threadFactory; } public ExecutorService getExecutorService() { return executorService; } @Override public String toString() { return zipFile.toString(); } private Zip4jConfig buildConfig() { return new Zip4jConfig(charset, bufferSize, useUtf8CharsetForPasswords); } public boolean isUseUtf8CharsetForPasswords() { return useUtf8CharsetForPasswords; } public void setUseUtf8CharsetForPasswords(boolean useUtf8CharsetForPasswords) { this.useUtf8CharsetForPasswords = useUtf8CharsetForPasswords; } } 只修改addstream 和 文件大小为3gb
08-21
3.5 Edit the Script 1. Launch a text or code editor to create a new JavaScript file. 2. Review the script one function at a time. There are four functions that must be implemented in the script to support solicited ethernet communications. • onProfileLoad: Retrieves driver metadata • onValidateTag: Verifies the address and data type created in the configuration or any dynamic tags created in an OPC client are valid for the end device connected • onTagsRequest: Builds a packet of bytes to transmit to the device across the wire. • onData: Interprets the response from the device and updates tag values or indicates if the read or write operation was successful based on the data in the response. Note: onTagsRequest and onData can do much more then described in this example. These functions can be used to communicate with many kinds of protocols. For more information view the Profile Library Plugin Help documentation. 3. Build out the script one function at a time, use the following information to edit the script. Required function: onProfileLoad The onProfileLoad function is the first of these functions called by the driver. It retrieves driver metadata, identifying the interface between the script and the driver by specifying the version of Universal Device Driver with which it was created as well as the mode. For more information on the mode please view the Profile Library plug-in help. Note: The only supported version is 2.0. Any other value is rejected, leading to failure of all subsequent functions. Any exception thrown out of any of the “framework” functions is caught and results in failure of the current operation. An exception thrown out of: • onProfileLoad causes all subsequent operations to fail until corrected • onValidateTag causes the tag address to be treated as “invalid” • onTagsRequest causes the read or write operation on the current tag to fail • onData causes the read or write operation on the current tag to fail Below is the entire onProfileLoad function: function onProfileLoad() { return { version: “2.0”, mode: “Client” }; } Required function: onValidateTag The onValidateTag script function is to validate the address syntax of a tag and the data type, which is central to communicating with a device. In the case of a Modbus device, this function ensures that an address is a holding register in the supported range. If desired, add logic to this function to modify various tag fields, such as providing a valid default data type,r modifying an address format to enforce consistency among tag addresses, or assigning a bulkId to group specific tags together. For the onValidateTag function in this Modbus example, review the sections: // Validate the address is a holding register in the supported range let tagAddress = info.tag.address; try { let numericAddress = parseInt(tagAddress, 10); if (numericAddress < MINREGISTERADDRESS || numericAddress > MAXREGISTERADDRESS || isNaN(numericAddress)) { info.tag.valid = false; return info.tag; } // If grouping tags into bulks, assign bulkId now. // Otherwise, the next bulkId is assigned by default. let bulkId = Math.floor((numericAddress - MINREGISTERADDRESS)/BULKREGISTERCOUNT); info.tag.bulkId = bulkId; log(`Modbus Ethernet onValidateTag: Bulk register count ${BULKREGISTERCOUNT}, address ${tagAddress}, bulkId ${info.tag.bulkId}`, VERBOSE_LOGGING); info.tag.valid = true; return info.tag; } catch (e) { // Use log to provide helpful information that can assist with error resolution log(`Unexpected error (onValidateTag): ${e.message}`, VERBOSE_LOGGING); info.tag.valid = false; return info.tag; } The code above offers a look at the JavaScript object info that the driver provides to the script writer. This object is meant to hold data to be exchanged between the script and the driver. It checks the address received from the driver (info.tag.address) and verifies it is in the expected range for a Modbus holding register as defined by constants MINREGISTERADDRESS, MAXREGISTERADDRESS. If it’s not in that range, fail the tag being added by setting the valid field of the tag to false: info.tag.valid = false. The script also defines the bulkId field for each tag. The register in the address along with the BULKREGISTERCOUNT constant facilitates assigning the bulkId that allows blocking together consecutive registers. Once the tags are blocked together, the Universal Device driver will then provide them in the tags object passed to the onTagsRequest and onData functions. // Provide a valid default data type based on register // Note: "Default" is an invalid data type let validDataTypes = {"3": "Word", "4": "Word"} if (info.tag.dataType === "Default") { let registerChar = info.tag.address.charAt(0); info.tag.dataType = validDataTypes[registerChar]; } /* * The regular expression to compare address to. * ^4 starts with '4' * 0* find zero or more occurrences of '0' * 1$ ends with '1' */ let addressRegex = /^40*1$/; // Correct a "semi-correct" tag address (e.g. 401 or 400001 --> 40001) with regex if (addressRegex.test(info.tag.address)) { info.tag.address = "40001"; } The above code provides examples of logic to modify various tag fields. The first code block resets the data type if Default is initially selected. While Default is a Kepware server data type, it is an invalid return value for a tag data type (i.e., info.tag.dataType). As such, provide an appropriate and valid data type based on the register if the data type is set as Default. The second code block uses a regex to recognize semi-correct addresses and modify them accordingly. In the above implementation, this logic adjusts tag addresses with too few or too many zeros; for example, ‘401’ or ‘400001` is changed to ‘40001’. Below is the entire onValidateTag function: function onValidateTag(info) { // Provide a valid default data type based on register // Note: "Default" is an invalid data type let validDataTypes = {"3": "Long", "4": "DWord"} if (info.tag.dataType === "Default") { let registerChar = info.tag.address.charAt(0); info.tag.dataType = validDataTypes[registerChar]; } /* * The regular expression to compare address to. * ^4 starts with '4' * 0* find zero or more occurrences of '0' * 1$ ends with '1' */ let addressRegex = /^40*1$/; // Correct a "semi-correct" tag address (e.g. 401 or 400001 --> 40001) with regex if (addressRegex.test(info.tag.address)) { info.tag.address = "40001"; } // Validate the address is a holding register in the supported range let tagAddress = info.tag.address; try {let numericAddress = parseInt(tagAddress, 10); if (numericAddress < MINREGISTERADDRESS || numericAddress > MAXREGISTERADDRESS || isNaN(numericAddress)) { info.tag.valid = false; return info.tag; } // If grouping tags into bulks, assign bulkId now. // Otherwise, the next bulkId is assigned by default. let bulkId = Math.floor((numericAddress - MINREGISTERADDRESS)/BULKREGISTERCOUNT); info.tag.bulkId = bulkId; log(`Modbus Ethernet onValidateTag: Bulk register count ${BULKREGISTERCOUNT}, address ${tagAddress}, bulkId ${info.tag.bulkId}`, VERBOSE_LOGGING); info.tag.valid = true; return info.tag; } catch (e) { // Use log to provide helpful information that can assist with error resolution log(`Unexpected error (onValidateTag): ${e.message}`, VERBOSE_LOGGING); info.tag.valid = false; return info.tag; } } Required function: onTagsRequest The onTagsRequest script function builds a packet of bytes that is sent to the target Modbus device. In the example implementation, the onTagsRequest function makes use of two helper functions to build action-specific packet: BuildReadMessage and BuildWriteMessage: function onTagsRequest(info) { let action = "Fail"; if (info.type === "Read") { let readData = BuildReadMessage(info.tags); // Evaluate if the data was successfully built if (readData.length === 12) { action = "Receive"; } return { action: action, data: readData }; } else if (info.type === "Write") { SENTWRITEDATA = BuildWriteMessage(info.tags); // Evaluate if the data was successfully built if (SENTWRITEDATA.length === 12) { action = "Receive"; } return { action: action, data: SENTWRITEDATA }; } } Unlike the onTagsRequest function, these helper functions are not required; they help make the script more manageable. Let’s dive into these helper functions now. Helper Function: BuildReadMessage This function builds into the packet the function code for a Modbus read to ensure that the read is on the appropriate address(es). Most of the Modbus-specific pieces of this snippet are documented in code comments with the important parts called out. The Modbus protocol supports blocking / bulk read and write functionality. The Universal Device Driver supports blocking tags for reads but does not support blocking tags for writes. The tags parameter is an array containing at least one tag element. If, in onValidateTag, the script assigned the same bulkId to more than one tag, then those tags sharing a bulkId are included in the array when the request type is Read. function BuildReadMessage (tags) { // This should never happen, but it's best practice if (tags.length === 0) { throw "No tags were requested for read request."; } // Sort the Modbus registers low to high let registers = []; for(let i=0; i<tags.length; i++) { registers[i] = parseInt(tags[i].address, 10); } registers.sort (sortNumber); // Find the lowest register, and the number of registers required to read the whole block let first = registers[0]; let count = registers[registers.length - 1] - first + 1; // Get the zero-based register index to make the request first -= 40001; The code above checks the tags component of the JavaScript object info (i.e. info.tags). This component holds an array of tags. Each tag has an address used to build a request packet for a read. The beginning of this section of code ensures that the driver has given a tag to build a request packet. If the length of the tags array is zero, it exits the function because there's no reason for the driver to build a request packet if no tag – and in turn, no address – is provided. // Update the transaction ID in the stateful transaction object if (TXID === undefined) { TXID = 0; } else { TXID++; } JavaScript is not a strongly typed language, making it possible to modify a variable's type or composition at runtime. This is something to take advantage of within the BuildReadMessage function. The above code snippet updates the value of a global variable TXID, which represents a transaction ID exchanged between the script and the driver. Use this global variable to keep track of the number of times it is building packets to transmit to the device. It's important to keep track of this because the transaction ID is a necessary part of the Modbus protocol, as seen in the next step. TXID is stateful between transactions because it is shared between the script and driver and maintains state across transactions. Every time this function is called, the transaction ID value maintains the state it was the last time it was changed at runtime. // Build the Modbus Ethernet data let data = // ----Transaction ID------|-Protocol--|---Length--|Server|-Fxn-| [hiByte(TXID), loByte(TXID), 0x00, 0x00, 0x00, 0x06, 0x00, 0x03, ------Starting Address-------|-------Register count--------| hiByte(first), loByte(first), hiByte(count), loByte(count)] The above shows the packet being constructed. It is an array of bytes to be sent to the Modbus device. The code comments the different parts of the packet that are defined in the Modbus protocol; for instance, the TXID described earlier is used in the protocol as the top two bytes. Note: Only bytes are currently supported for the data array. Below is the entire BuildReadMessage function: function BuildReadMessage (tags) { // This should never happen, but it's best practice if (tags.length === 0) { throw "No tags were requested for read request."; } // Sort the Modbus registers low to high let registers = []; for(let i=0; i<tags.length; i++) { registers[i] = parseInt(tags[i].address, 10); } registers.sort (sortNumber); // Find the lowest register, and the number of registers required to read the whole block let first = registers[0]; let count = registers[registers.length - 1] - first + 1; // Get the zero-based register index to make the request first -= 40001; www.ptc.com 11 ©2021-2023 PTC, Inc. All Rights Reserved. // Initialize or update the transaction ID in the stateful transaction object if (TXID === undefined) { TXID = 0; } else { TXID++; } // Build the Modbus Ethernet data let data = // ----Transaction ID------|-Protocol--|---Length--|Server|-Fxn-|------Starting Address--- - [hiByte(TXID), loByte(TXID), 0x00, 0x00, 0x00, 0x06, 0x00, 0x03, hiByte(first), ---|-------Register count--------| loByte(first), hiByte(count), loByte(count)] return data; } Helper Function: BuildWriteMessage The BuildWriteMessage function is similar to the BuildReadMessage function in that it assists with building an array of bytes to send the device. However, this function facilitates writing a value to, rather than reading a value from, a Modbus device. Note: Not all devices support writes. If the target device does support writes, the BuildWriteMessage function – in conjunction with the ParseWriteMessage function – provides an example of how to implement this functionality. // This should never happen but it's best practice if (tags.length === 0) { throw "No tags were requested for write request."; } // Sort the Modbus registers low to high let register = parseInt(tags[0].address, 10); register -= 40001; // Get the value to write which is located in the first // element in the tags[n].value object let value = parseInt(tags[0].value); The code above assigns the integer value of the tag address to the variable register. Additionally, is assigns the value of the first tag value to the variable value since KEPServerEX only allows single writes. // Build the Modbus Ethernet data let data = // ----Transaction ID-----|-Protocol--|---Length--|Server|-Fxn-| www.ptc.com 12 ©2021-2023 PTC, Inc. All Rights Reserved. [ hiByte(TXID), loByte(TXID), 0x00, 0x00, 0x00, 0x06, 0x00, 0x06, --------Starting Address----------|-------value to write--------| hiByte(register), loByte(register), hiByte(value), loByte(value) ]; return data; The above shows how to build up a write packet, which is very similar to building a read packet within the BuildReadMessage function. Required function: onData The onData script function parses the array of bytes received from a Modbus device. In the example implementation, as was the case with the onTagsRequest function, the onData function uses two helper functions to parse responses from a Modbus device: ParseReadMessage and ParseWriteMessage: function onData(info) { let action = ACTIONFAILURE; if (info.type === "Read") { let tags = ParseReadMessage(info.tags, info.data); // Evaluate if the data was successfully parsed from the packet if (tags[0].value != null || tags[0].quality != null) { action = ACTIONCOMPLETE; } return { action: action, tags: tags }; } else if (info.type === "Write") { action = ParseWriteMessage(info.data); return { action: action, tags: info.tags }; } } Helper Function: ParseReadMessage This function's purpose is to parse an incoming packet into a tag value to update the respective tag in the server. The incoming packet is passed to the script via the JavaScript object information as the returned byte array is contained in its data component (i.e. info.data). The function determines what information is important based on the protocol specification and extracts the value for the tag/address. This value is assigned to the value field of the tag (e.g. info.tags[0].value) and then returned from the function, which is how the tag is updated in the server. function ParseReadMessage(tags, data) { // This should never happen but it's best practice if (tags.length === 0) { throw "No tags were requested for read request."; } log(`Modbus Ethernet ParseReadMessage: data ${JSON.stringify(data)}`, VERBOSE_LOGGING); // Convert the string addresses to integers (eg 40001) let registers = []; for(let i=0; i < tags.length; i++) { registers[i] = parseInt(tags[i].address, 10); } // Find the lowest numbered register - this is the starting address let startingAddress = Array.min (registers); // MBE Response values start here: let offset = 9; // Enough bytes? if (data.length < offset + 2 * registers.length) { // Iterate the registers and set the quality of each tag to bad for (let i = 0; i < registers.length; ++i) { // Log message only once for this response if (i === 0) { if (data.length === offset){ log(`Modbus Ethernet ParseReadMessage: Device returned an error code ${data[7]}, ${data[8]}`); } else { log(`Modbus Ethernet ParseReadMessage: Invalid response from device`); } } tags[i].quality = "Bad"; } } The code above performs error checking and gathering some information about the transaction. If the number of bytes in the response is not the number of bytes expected, then the script sets the quality of each tag to Bad. If the response appears to include an error code from the device, then the script provides that information in the message passed to the log function. Otherwise, the script logs a message indicating an invalid response from the device. The result is an updated tags component of the JavaScript object info to be shared with the driver and ultimately used to update the tag qualities in the server. // Iterate the registers and lookup the response value for each for (let i = 0; i < registers.length; ++i) { // Calculate the index of this register's value in the response buffer let index = registers[i] - startingAddress; // Extract it from the response buffer www.ptc.com 14 ©2021-2023 PTC, Inc. All Rights Reserved. let hi = data[2*index + offset]; let lo = data[2*index + offset + 1]; tags[i].value = (wordFromBytes (hi, lo)); } return tags; The code above extracts the value returned from the device and assigns it to the appropriate tag to be used to update the tag value in the server. The result is an updated tags component of the JavaScript object info to be shared with the driver and ultimately used to update the tag values in the server. Below is the entire ParseReadMessage function. function ParseReadMessage(tags, data) { // This should never happen but it's best practice if (tags.length === 0) { throw "No tags were requested for read request."; } log(`Modbus Ethernet ParseReadMessage: data ${JSON.stringify(data)}`, VERBOSE_LOGGING); // Convert the string addresses to integers (eg 40001) let registers = []; for(let i=0; i < tags.length; i++) { registers[i] = parseInt(tags[i].address, 10); } // Find the lowest numbered register - this is the starting address let startingAddress = Array.min (registers); // MBE Response values start here: let offset = 9; // Enough bytes? if (data.length < offset + 2 * registers.length) { // Iterate the registers and set the quality of each tag to bad for (let i = 0; i < registers.length; ++i) { // Log message only once for this response if (i === 0) { if (data.length === offset){ log(`Modbus Ethernet ParseReadMessage: Device returned an error code ${data[7]}, ${data[8]}`); } else { log(`Modbus Ethernet ParseReadMessage: Invalid response from device`); } } tags[i].quality = "Bad"; } www.ptc.com 15 ©2021-2023 PTC, Inc. All Rights Reserved. } else { // Iterate the registers and lookup the response value for each. // Assigning the quality of the tag is optional. If undefined, Good is assumed. for (let i = 0; i < registers.length; ++i) { // Calc the index of this register's value in the response buffer let index = registers[i] - startingAddress; // Extract the value from the response buffer let hi = data[2*index + offset]; let lo = data[2*index + offset + 1]; tags[i].value = (wordFromBytes (hi, lo)); } } return tags; } Helper Function: ParseWriteMessage The purpose of the ParseWriteMessage function is to determine if the write was successful. Most devices respond that the request was received and executed. In the case of Modbus, the response echoes the request, which makes it possible to compare the returned message with the sent message that was saved in the global variable SENTWRITEDATA. Below is the entire ParseWriteMessage function: function ParseWriteMessage(data) { // Modbus echoes a write request so if the data sent // does not match the data received, then the write fails SENTWRITEDATA.forEach((e1) => data.forEach((e2) => { if (e1 !== e2) { return "Fail"; } })); return "Complete"; } 把3.5以上各个代码段,整理成一段完整的代码,这段代码必须有合理性,可直接复制出来能运行的代码。
最新发布
09-30
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值