Здесь показаны различия между двумя версиями данной страницы.
Both sides previous revision Предыдущая версия Следущая версия | Предыдущая версия | ||
ru:sidebar:javaapi:codesamples:update_datafalags [18/12/2013 13:01] 127.0.0.1 внешнее изменение |
ru:sidebar:javaapi:codesamples:update_datafalags [06/10/2015 14:00] (текущий) kopa |
||
---|---|---|---|
Строка 3: | Строка 3: | ||
Управление элементами и событиями системы подразумевает под собой отбор типов событий, которые нужно получать, получение событий и их дальнейшую обработку. | Управление элементами и событиями системы подразумевает под собой отбор типов событий, которые нужно получать, получение событий и их дальнейшую обработку. | ||
- | //:!: Внимание!// Для запуска данного примера на Wialon Kit следует заменить **<nowiki>http://hst-api.wialon.com</nowiki>** на **<nowiki>http://kit-api.wialon.com</nowiki>**, а для входа в систему использовать логин и пароль своей учетной записи или логин и пароль демо-пользователя: //**kitdemo kitdemo**//. | + | //:!: Внимание!// Для запуска данного примера на Wialon Kit следует заменить **<nowiki>http://hst-api.wialon.com</nowiki>** на **<nowiki>http://kit-api.wialon.com</nowiki>**. |
<code java> | <code java> | ||
Строка 22: | Строка 22: | ||
public class EventsManagementExample implements Runnable { | public class EventsManagementExample implements Runnable { | ||
- | private Session session; | + | private Session session; |
- | // Login to server | + | // Login to server |
- | private void login(){ | + | private void login(){ |
- | // initialize Wialon session | + | // initialize Wialon session |
- | session.initSession("http://hst-api.wialon.com"); | + | session.initSession("http://hst-api.wialon.com"); |
- | // trying login | + | // trying login |
- | session.login("wialon_test", "test", new ResponseHandler() { | + | session.loginToken("2fe8024e0ab91aa6c8ed82717b71bddcECDC362358DF7D90986F5173D405CD0D42DE7B38", new ResponseHandler() { |
- | @Override | + | @Override |
- | public void onSuccess(String response) { | + | public void onSuccess(String response) { |
- | super.onSuccess(response); | + | super.onSuccess(response); |
- | // login succeed | + | // login succeed |
- | System.out.println(String.format("Logged successfully. User name is %s", session.getCurrUser().getName())); | + | System.out.println(String.format("Logged successfully. User name is %s", session.getCurrUser().getName())); |
- | //call update data flags | + | //call update data flags |
- | updateDataFlags(); | + | updateDataFlags(); |
- | } | + | } |
- | @Override | + | @Override |
- | public void onFailure(int errorCode, Throwable throwableError) { | + | public void onFailure(int errorCode, Throwable throwableError) { |
- | super.onFailure(errorCode, throwableError); | + | super.onFailure(errorCode, throwableError); |
- | // login failed, print error | + | // login failed, print error |
- | System.out.println(Errors.getErrorText(errorCode)); | + | System.out.println(Errors.getErrorText(errorCode)); |
- | } | + | } |
- | }); | + | }); |
- | } | + | } |
- | private void updateDataFlags(){ | + | private void updateDataFlags(){ |
- | //Create new update specification | + | //Create new update specification |
- | UpdateSpec updateSpec=new UpdateSpec(); | + | UpdateSpec updateSpec=new UpdateSpec(); |
- | //Set update mode to add specified flags | + | //Set update mode to add specified flags |
- | updateSpec.setMode(1); | + | updateSpec.setMode(1); |
- | //Set update type by type | + | //Set update type by type |
- | updateSpec.setType("type"); | + | updateSpec.setType("type"); |
- | //Set update data to units | + | //Set update data to units |
- | updateSpec.setData(Item.ItemType.avl_unit); | + | updateSpec.setData(Item.ItemType.avl_unit); |
- | //Set update data flags | + | //Set update data flags |
- | updateSpec.setFlags(Item.dataFlag.base.getValue()| Unit.dataFlag.lastMessage.getValue()); | + | updateSpec.setFlags(Item.dataFlag.base.getValue()| Unit.dataFlag.lastMessage.getValue()); |
- | //Send update by created update specification | + | //Send update by created update specification |
- | session.updateDataFlags(new UpdateSpec[]{updateSpec}, new ResponseHandler() { | + | session.updateDataFlags(new UpdateSpec[]{updateSpec}, new ResponseHandler() { |
- | @Override | + | @Override |
- | public void onSuccess(String response) { | + | public void onSuccess(String response) { |
- | super.onSuccess(response); | + | super.onSuccess(response); |
- | // Update succeed | + | // Update succeed |
- | System.out.println("Update data flags is successful"); | + | System.out.println("Update data flags is successful"); |
- | bindEventsHandler(); | + | bindEventsHandler(); |
- | } | + | } |
- | @Override | + | @Override |
- | public void onFailure(int errorCode, Throwable throwableError) { | + | public void onFailure(int errorCode, Throwable throwableError) { |
- | super.onFailure(errorCode, throwableError); | + | super.onFailure(errorCode, throwableError); |
- | // update data flags failed, print error | + | // update data flags failed, print error |
- | System.out.println(Errors.getErrorText(errorCode)); | + | System.out.println(Errors.getErrorText(errorCode)); |
- | logout(); | + | logout(); |
- | } | + | } |
- | }); | + | }); |
- | } | + | } |
- | private void bindEventsHandler(){ | + | private void bindEventsHandler(){ |
- | //Get items from session | + | //Get items from session |
- | Collection<Item> sessionItems=session.getItems(); | + | Collection<Item> sessionItems=session.getItems(); |
- | //Create events handler | + | //Create events handler |
- | EventsHandlerExample eventsHandlerExample=new EventsHandlerExample(); | + | EventsHandlerExample eventsHandlerExample=new EventsHandlerExample(); |
- | //Add handler to session | + | //Add handler to session |
- | session.addListener(eventsHandlerExample, EventProvider.events.All); | + | session.addListener(eventsHandlerExample, EventProvider.events.All); |
- | if (sessionItems!=null && sessionItems.size()>0) { | + | if (sessionItems!=null && sessionItems.size()>0) { |
- | System.out.println(String.format("%d items added to Session\r\nStart binding event listeners to items", sessionItems.size())); | + | System.out.println(String.format("%d items added to Session\r\nStart binding event listeners to items", sessionItems.size())); |
- | //Add handler to items | + | //Add handler to items |
- | for (Item item : sessionItems) | + | for (Item item : sessionItems) |
- | item.addListener(eventsHandlerExample, EventProvider.events.All); | + | item.addListener(eventsHandlerExample, EventProvider.events.All); |
- | System.out.println("Event listeners successfully bound to items\r\nPress enter to logout and exit"); | + | System.out.println("Event listeners successfully bound to items\r\nPress enter to logout and exit"); |
- | } | + | } |
- | //Wait for press enter key | + | //Wait for press enter key |
- | try { | + | try { |
- | System.in.read(); | + | System.in.read(); |
- | logout(); | + | logout(); |
- | } catch (IOException e) { | + | } catch (IOException e) { |
- | e.printStackTrace(); | + | e.printStackTrace(); |
- | } | + | } |
- | } | + | } |
- | // Logout | + | // Logout |
- | private void logout(){ | + | private void logout(){ |
- | session.logout(new ResponseHandler() { | + | session.logout(new ResponseHandler() { |
- | @Override | + | @Override |
- | public void onSuccess(String response) { | + | public void onSuccess(String response) { |
- | super.onSuccess(response); | + | super.onSuccess(response); |
- | // logout succeed | + | // logout succeed |
- | System.out.println("Logout successfully"); | + | System.out.println("Logout successfully"); |
- | System.exit(0); | + | System.exit(0); |
- | } | + | } |
- | @Override | + | @Override |
- | public void onFailure(int errorCode, Throwable throwableError) { | + | public void onFailure(int errorCode, Throwable throwableError) { |
- | super.onFailure(errorCode, throwableError); | + | super.onFailure(errorCode, throwableError); |
- | // logout failed, print error | + | // logout failed, print error |
- | System.out.println(Errors.getErrorText(errorCode)); | + | System.out.println(Errors.getErrorText(errorCode)); |
- | System.exit(0); | + | System.exit(0); |
- | } | + | } |
- | }); | + | }); |
- | } | + | } |
- | //EventHandler witch listen bound event types | + | //EventHandler witch listen bound event types |
- | private class EventsHandlerExample implements EventHandler{ | + | private class EventsHandlerExample implements EventHandler{ |
- | @Override | + | @Override |
- | public void onEvent(Enum event, Object object, Object oldData, Object newData) { | + | public void onEvent(Enum event, Object object, Object oldData, Object newData) { |
- | String objectValue= object==null ? "null" : object.toString(); | + | String objectValue= object==null ? "null" : object.toString(); |
- | String oldDataValue= oldData==null ? "null" : oldData.toString(); | + | String oldDataValue= oldData==null ? "null" : oldData.toString(); |
- | String newDataValue= newData==null ? "null" : newData.toString(); | + | String newDataValue= newData==null ? "null" : newData.toString(); |
- | System.out.println(String.format("Fired event with type %s, object is %s, oldData is %s, newData is %s", event, objectValue, oldDataValue, newDataValue)); | + | System.out.println(String.format("Fired event with type %s, object is %s, oldData is %s, newData is %s", event, objectValue, oldDataValue, newDataValue)); |
- | } | + | } |
- | } | + | } |
- | @Override | + | @Override |
- | public void run() { | + | public void run() { |
- | // get instance of current Session | + | // get instance of current Session |
- | session=Session.getInstance(); | + | session=Session.getInstance(); |
- | login(); | + | login(); |
- | } | + | } |
- | public static void main(String[] args){ | + | public static void main(String[] args){ |
- | new Thread(new EventsManagementExample()).start(); | + | new Thread(new EventsManagementExample()).start(); |
- | } | + | } |
} | } | ||
</code> | </code> |
|