public interface SettingsProvider
package test.com.zebra.sdk.settings.examples;
import java.text.SimpleDateFormat;
import java.util.*;
import com.zebra.sdk.comm.*;
import com.zebra.sdk.common.card.exceptions.ZebraCardException;
import com.zebra.sdk.common.card.printer.*;
import com.zebra.sdk.common.card.printer.discovery.ZebraCardPrinterFilter;
import com.zebra.sdk.printer.discovery.*;
import com.zebra.sdk.settings.SettingsException;
import com.zebra.sdk.zmotif.settings.ZebraCardSettingNamesZmotif;
public class SettingsProviderExample {
public static void main(String[] args) {
Connection connection = null;
ZebraCardPrinter zebraCardPrinter = null;
try {
for (DiscoveredUsbPrinter usbPrinter : UsbDiscoverer.getZebraUsbPrinters(new ZebraCardPrinterFilter())) {
connection = usbPrinter.getConnection();
connection.open();
zebraCardPrinter = ZebraCardPrinterFactory.getInstance(connection);
displaySettings(zebraCardPrinter);
setPrinterClock(zebraCardPrinter);
cleanUpQuietly(connection, zebraCardPrinter);
}
} catch (Exception e) {
System.out.println("Error retrieving settings: " + e.getMessage());
} finally {
cleanUpQuietly(connection, zebraCardPrinter);
}
}
private static void displaySettings(ZebraCardPrinter zebraCardPrinter) throws SettingsException {
if (zebraCardPrinter != null) {
System.out.println("Available Settings for myDevice:");
Set<String> availableSettings = zebraCardPrinter.getAvailableSettings();
for (String setting : availableSettings) {
System.out.println(setting + ": Range = (" + zebraCardPrinter.getSettingRange(setting) + ")");
}
System.out.println("\nCurrent Setting Values for myDevice:");
Map<String, String> allSettingValues = zebraCardPrinter.getAllSettingValues();
for (String settingName : allSettingValues.keySet()) {
System.out.println(settingName + ":" + allSettingValues.get(settingName));
}
}
}
private static void setPrinterClock(ZebraCardPrinter zebraCardPrinter) {
try {
if (zebraCardPrinter != null) {
Set<String> availableSettings = zebraCardPrinter.getAvailableSettings();
if (availableSettings.contains(ZebraCardSettingNamesZmotif.CLOCK_MONTH)) {
Map<String, String> clockSettings = new HashMap<String, String>();
CurrentTime time = getCurrentTime();
SimpleDateFormat dateFormat = new SimpleDateFormat("M-dd-yyyy HH:mm");
clockSettings.put(ZebraCardSettingNamesZmotif.CLOCK_MONTH, Byte.toString(time.month));
clockSettings.put(ZebraCardSettingNamesZmotif.CLOCK_DAY, Byte.toString(time.day));
clockSettings.put(ZebraCardSettingNamesZmotif.CLOCK_YEAR, Integer.toString(time.year));
clockSettings.put(ZebraCardSettingNamesZmotif.CLOCK_HOUR, Byte.toString(time.hour));
clockSettings.put(ZebraCardSettingNamesZmotif.CLOCK_MINUTE, Byte.toString(time.minute));
Date date = dateFormat.parse(formatTime(clockSettings));
System.out.println("\nSetting the clock to: " + dateFormat.format(date));
zebraCardPrinter.setSettings(clockSettings);
List<String> clockSettingsNames = Arrays.asList(ZebraCardSettingNamesZmotif.CLOCK_MONTH, ZebraCardSettingNamesZmotif.CLOCK_DAY, ZebraCardSettingNamesZmotif.CLOCK_YEAR, ZebraCardSettingNamesZmotif.CLOCK_HOUR, ZebraCardSettingNamesZmotif.CLOCK_MINUTE);
Map<String, String> clockValues = zebraCardPrinter.getSettingsValues(clockSettingsNames);
date = dateFormat.parse(formatTime(clockValues));
System.out.println("New clock value: " + dateFormat.format(date));
} else {
System.out.println("Clock settings not available.");
}
}
} catch (Exception e) {
System.out.println("Error setting the clock: " + e.getLocalizedMessage());
}
}
private static CurrentTime getCurrentTime() {
CurrentTime time = new CurrentTime();
Calendar c = Calendar.getInstance();
time.month = (byte) (c.get(Calendar.MONTH) + 1);
time.day = (byte) c.get(Calendar.DATE);
time.year = c.get(Calendar.YEAR);
time.hour = (byte) c.get(Calendar.HOUR_OF_DAY);
time.minute = (byte) c.get(Calendar.MINUTE);
return time;
}
private static String formatTime(Map<String, String> clockSettings) {
return String.format(Locale.US, "%s-%s-%s %s:%02d", clockSettings.get(ZebraCardSettingNamesZmotif.CLOCK_MONTH), clockSettings.get(ZebraCardSettingNamesZmotif.CLOCK_DAY), clockSettings.get(ZebraCardSettingNamesZmotif.CLOCK_YEAR),
clockSettings.get(ZebraCardSettingNamesZmotif.CLOCK_HOUR), Integer.parseInt(clockSettings.get(ZebraCardSettingNamesZmotif.CLOCK_MINUTE)));
}
private static void cleanUpQuietly(Connection connection, ZebraCardPrinter genericPrinter) {
try {
if (genericPrinter != null) {
genericPrinter.destroy();
genericPrinter = null;
}
} catch (ZebraCardException e) {
e.printStackTrace();
}
if (connection != null) {
try {
connection.close();
connection = null;
} catch (ConnectionException e) {
e.printStackTrace();
}
}
}
private static class CurrentTime {
public byte month = 0;
public byte day = 0;
public byte hour = 0;
public byte minute = 0;
public int year = 0;
}
}
Modifier and Type | Method and Description |
---|---|
java.util.Map<String,Setting> |
getAllSettings()
Retrieve all settings and their attributes.
|
java.util.Map<String,String> |
getAllSettingValues()
Retrieves all of the device's setting values.
|
java.util.Set<String> |
getAvailableSettings()
Retrieve all of the setting identifiers for a device.
|
String |
getSettingRange(String settingId)
Retrieves the allowable range for a setting.
|
java.util.Map<String,String> |
getSettingsValues(java.util.List<String> listOfSettings)
Retrieves the device's setting values for a list of setting IDs.
|
String |
getSettingType(String settingId)
Returns the data type of the setting.
|
String |
getSettingValue(String settingId)
Retrieves the device's setting value for a setting id.
|
boolean |
isSettingReadOnly(String settingId)
Returns true if the setting is read only.
|
boolean |
isSettingValid(String settingId,
String value)
Returns true if value is valid for the given setting.
|
boolean |
isSettingWriteOnly(String settingId)
Returns true if the setting is write only.
|
java.util.Map<String,String> |
processSettingsViaMap(java.util.Map<String,String> settingValuePairs)
Change or retrieve printer settings.
|
void |
setSetting(String settingId,
String value)
Sets the setting to the given value.
|
void |
setSettings(java.util.Map<String,String> settingValuePairs)
Set more than one setting.
|
java.util.Set<String> getAvailableSettings() throws SettingsException
SettingsException
- if the settings could not be loadedgetSettingValue(String)
String getSettingValue(String settingId) throws SettingsException, ConnectionException, ZebraIllegalArgumentException
settingId
- setting id.SettingsException
- if the setting could not be retrieved.ZebraIllegalArgumentException
- if there was an error parsing the response from the printer.ConnectionException
- if there is an error communicating with the printer.java.util.Map<String,String> getSettingsValues(java.util.List<String> listOfSettings) throws SettingsException, ConnectionException, ZebraIllegalArgumentException
listOfSettings
- list of setting IDs.SettingsException
- if the settings could not be retrieved.ZebraIllegalArgumentException
- if there was an error parsing the response from the printer.ConnectionException
- if there is an error communicating with the printer.java.util.Map<String,Setting> getAllSettings() throws SettingsException
SettingsException
- if the settings could not be retrievedjava.util.Map<String,String> getAllSettingValues() throws SettingsException
SettingsException
- if the settings could not be loadedgetAllSettingValues()
void setSetting(String settingId, String value) throws SettingsException
settingId
- setting id.value
- The setting's valueSettingsException
- if the setting is read only, does not exist, or if the setting could not be setvoid setSettings(java.util.Map<String,String> settingValuePairs) throws SettingsException
settingValuePairs
- Map a setting ID to the new value for the setting.SettingsException
- if the settings cannot be sent to the device.String getSettingRange(String settingId) throws SettingsException
settingId
- setting id.SettingsException
- if the setting does not existboolean isSettingValid(String settingId, String value) throws SettingsException
settingId
- setting id.value
- the setting's valueSettingsException
- if the setting does not existboolean isSettingReadOnly(String settingId) throws SettingsException
settingId
- the setting idSettingsException
- if the setting does not existboolean isSettingWriteOnly(String settingId) throws SettingsException
settingId
- the setting idSettingsException
- if the setting does not existString getSettingType(String settingId) throws SettingsException
settingId
- the setting idSettingsException
- if the setting does not existjava.util.Map<String,String> processSettingsViaMap(java.util.Map<String,String> settingValuePairs) throws SettingsException, ConnectionException
Note: Due to the setting verification and validation, additional time and data traffic will be needed for each
processSettingsViaMap
method call. It is recommended to bundle all settings into one map and one method call to
reduce this overhead.
settingValuePairs
- The settings to change or retrieveSettingsException
- if a setting is malformed, or one or more settings could not be set.ConnectionException
- if there is an error communicating with the printer.
© 2016 ZIH Corp. All Rights Reserved.