Ejemplo n.º 1
0
 // Device constructor. Automatically call the YAPI functin to reindex device
 YDevice(YGenericHub hub, WPEntry wpRec, HashMap<String, ArrayList<YPEntry>> ypRecs)
     throws YAPI_Exception {
   // private attributes
   _hub = hub;
   _wpRec = wpRec;
   _cache_expiration = 0;
   _cache_json = "";
   _moduleYPEntry = new YPEntry(wpRec.getSerialNumber(), "module", YPEntry.BaseClass.Function);
   _moduleYPEntry.setLogicalName(wpRec.getLogicalName());
   _ypRecs = new HashMap<Integer, YPEntry>();
   Set<String> keySet = ypRecs.keySet();
   for (String categ : keySet) {
     for (YPEntry rec : ypRecs.get(categ)) {
       if (rec.getSerial().equals(wpRec.getSerialNumber())) {
         int funydx = rec.getIndex();
         _ypRecs.put(funydx, rec);
       }
     }
   }
 }
Ejemplo n.º 2
0
  // Reload a device API (store in cache), and update YAPI function lists accordingly
  // Intended to be called within UpdateDeviceList only
  public int refresh() throws YAPI_Exception {
    String result = this.requestAPI();
    JSONObject loadval;
    Boolean reindex = false;
    try {
      loadval = new JSONObject(result);

      _cache_expiration = YAPI.GetTickCount() + YAPI.DefaultCacheValidity;
      _cache_json = result;

      // parse module and refresh names if needed
      Iterator<?> keys = loadval.keys();
      while (keys.hasNext()) {
        String key = (String) keys.next();
        if (key.equals("module")) {
          JSONObject module = loadval.getJSONObject("module");
          if (!_wpRec.getLogicalName().equals(module.getString("logicalName"))) {
            _wpRec.setLogicalName(module.getString("logicalName"));
            _moduleYPEntry.setLogicalName(_wpRec.getLogicalName());
            reindex = true;
          }
          _wpRec.setBeacon(module.getInt("beacon"));
        } else if (!key.equals("services")) {
          JSONObject func = loadval.getJSONObject(key);
          String name;
          if (func.has("logicalName")) {
            name = func.getString("logicalName");
          } else {
            name = _wpRec.getLogicalName();
          }
          if (func.has("advertisedValue")) {
            String pubval = func.getString("advertisedValue");
            SafeYAPI()._yHash.setFunctionValue(_wpRec.getSerialNumber(), pubval);
          }
          for (int f = 0; f < _ypRecs.size(); f++) {
            if (_ypRecs.get(f).getFuncId().equals(key)) {
              if (!_ypRecs.get(f).getLogicalName().equals(name)) {
                _ypRecs.get(f).setLogicalName(name);
                reindex = true;
              }
              break;
            }
          }
        }
      }
    } catch (JSONException e) {
      throw new YAPI_Exception(YAPI.IO_ERROR, "Request failed, could not parse API result");
    }

    if (reindex) {
      SafeYAPI()._yHash.reindexDevice(this);
    }
    return YAPI.SUCCESS;
  }