⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 weblightsserver.java

📁 tini 的weblightserverTINICHENG程序
💻 JAVA
字号:
/*---------------------------------------------------------------------------
 * Copyright (C) 1999,2000,2001 Dallas Semiconductor Corporation, All Rights Reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY,  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL DALLAS SEMICONDUCTOR BE LIABLE FOR ANY CLAIM, DAMAGES
 * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 *
 * Except as contained in this notice, the name of Dallas Semiconductor
 * shall not be used except as stated in the Dallas Semiconductor
 * Branding Policy.
 *---------------------------------------------------------------------------
 */
import java.io.*;
import java.net.*;
import java.util.*;

// 1-Wire imports
import com.dalsemi.onewire.*;
import com.dalsemi.onewire.adapter.*;
import com.dalsemi.onewire.container.*;

// TINI imports
import com.dalsemi.tininet.http.*;   // For the PC, please comment out this line.

/**
 * WebLightsServer is a webserver program that controls light intensities of 
 * dimmable fluorescent ballasts (being controlled through a 1-Wire digital 
 * potentiometer, DS2890, from Dallas Semiconductor http://www.dalsemi.com).  
 * The program requires the client java applet to function as a complete demo.
 *
 * @version    0.00, 15 December 2000
 * @author     SC and BH
 */
public class WebLightsServer extends Thread {

   int k;
   Socket s;

   InputStream in;
   OutputStream out;

   static boolean debug = false;
   static DSPortAdapter smc;
   static OneWireContainer2C owc2C;
   static int[] intensityTable;
   static int[] wiperPositionToIntensity;
   static int n;
   static Vector rom;
   static Vector clients;
   static int[] intensity;
   static ServerSocket ss;
   static Object lock = new Object();

   public WebLightsServer(Socket s) 
   {
      this.s = s;
      synchronized (lock) 
      {
         clients.addElement(this);
      }
      k = -1;
   }

   public static synchronized void main(String[] args) 
   {
      // create startup banner
      System.out.println();
      System.out.println("***************************************");
      System.out.println("*       WebLightsServer V0.00         *");
      System.out.println("*                                     *");
      System.out.println("* Copyright 2000 Dallas Semiconductor *");
      System.out.println("*       http://www.dalsemi.com        *");
      System.out.println("*       http://www.ibutton.com        *");
      System.out.println("*     http://www.ibutton.com/TINI     *");
      System.out.println("***************************************"); 
      System.out.println();

      //////////////////////////////////////
      // Create intensity table for DS2890
      // Intensity values for DS2890 are:
      // full dim = 255 and full bright = 0

      intensityTable = new int[64];    // instantiate intensity table for DS2890s
      // make intensity table
      for (int i = 0; i < 62; i++) intensityTable[i] = 255 - i;
      intensityTable[62] = 96;         // intensity levels are not necessarily linear.
      intensityTable[63] = 0;

      /////////////////////////////////////////////////////////////
      // Create DS2890 wiper-position-to-intensity table for DS2890
      // (a.k.a. the inverse of the IntensityTable above).

      wiperPositionToIntensity = new int[256];  // instantiate inverse of IntensityTable for DS2890
      // make inverse intensity table
      for (int i = 0; i < 96; i++) wiperPositionToIntensity[i] = 63;
      for (int i = 96; i < 194; i++) wiperPositionToIntensity[i] = 62;
      for (int i = 194; i < 256; i ++) wiperPositionToIntensity[i] = 255 - i;

      try 
      {
         File f = new File("WebLights.properties");
         if (f.exists()) 
         {
            // Get the 1-Wire net addresses from WebLights.properties
            FileInputStream fis = new FileInputStream(f);
            BufferedReader br = new BufferedReader(new InputStreamReader(fis));
            rom = new Vector(10, 10);
            smc = OneWireAccessProvider.getDefaultAdapter();
            String id = null;
            String s = null;
            int endIndex = 0;
            int beginIndex = 0;
            while ((s = br.readLine()) != null) 
            {
               beginIndex = (s.indexOf("=") - 16);
               endIndex = s.indexOf("=");
               if (endIndex > 15)
               {
                  id = s.substring(beginIndex, endIndex);
                  id = id.trim();                  
                  owc2C = new OneWireContainer2C(smc, id);
                  rom.addElement(owc2C);
               }
            }
            br.close();
            n = rom.size();
            intensity = new int[n];
            // Read the intensity of all lights and store them in the intensity array.
            for (int i = 0; i < n; i++)
            {
               int wiperPosition = 0;
               owc2C = (OneWireContainer2C) rom.elementAt(i);
               wiperPosition = owc2C.getWiperPosition();
               intensity[i] = wiperPositionToIntensity[wiperPosition];
            }
            clients = new Vector(10, 10);

            ///////////////// For PC, please comment out this code block //////////////////////
            // Initialize and start the TINI-specific webserver thread
            final HTTPServer webserver = new HTTPServer(HTTPServer.DEFAULT_HTTP_PORT);
            webserver.setIndexPage("WebLightsAWT.html");
            webserver.setHTTPRoot("/");
            (new Thread() {
               public void run() {
                  while (true) webserver.serviceRequests();
               }
            }).start();
            ////////////////////////////////      end of block      ////////////////////////////


            // Initialize echo server socket and respond to requests
            ss = new ServerSocket(7);
            while (true) (new WebLightsServer(ss.accept())).start();

         } else System.out.println("No WebLights.properties file, shutting server down.");
      }
      catch (Exception e) 
      {
         if (debug) System.out.println(e);
         try { if (ss != null) ss.close(); }
         catch (IOException x) { if (debug) System.out.println(x); }
      }
   }

   public void run() 
   {
      try 
      {
         in = s.getInputStream();
         out = s.getOutputStream();
         synchronized (lock) {
            for (int i = 0; i < n; i++) 
            {
               out.write(64 + i);
               out.write(intensity[i]);
            }
         }
         int c = 0;
         while ((c = in.read()) != -1) 
         {
            if (c < 64) 
            {
               synchronized (lock) 
               {
                  if (c != intensity[k]) 
                  {
                     if (owc2C != null) owc2C.setWiperPosition(intensityTable[c]);
                     intensity[k] = c;
                     for (int i = 0; i < clients.size(); i++) 
                     {
                        WebLightsServer st = (WebLightsServer) clients.elementAt(i);
                        if (st != this) 
                        {
                           st.out.write(64 + k);
                           int temp1 = 64 + k;
                           st.out.write(c);
                        }    
                     }
                  }
               }
            } 
            else 
            {
               c -= 64;
               if (k != c) 
               {
                  if (c < n) 
                  {
                     owc2C = (OneWireContainer2C) rom.elementAt(c);
                  } 
                  else 
                  {
                     owc2C = null;
                  }
                  k = c;
               }   
            }  
         }
         synchronized (lock) 
         {
            clients.removeElement(this);
         }
      }
      catch (Exception e) 
      {
         if (debug) System.out.println(e + ".  Recovering gracefully...");
      }
      finally 
      {
         try 
         {
            if (out != null) out.close();
            if (in != null) in.close();
            s.close();
            synchronized (lock) 
            {
               clients.removeElement(this);
            }
         }
         catch (Exception e) { if (debug) System.out.println(e); }
      }
   }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -