00001 #include "rwil.h"
00002 #include "procrwil.h"
00003 #include "X10Device.h"
00004 #include "X10Recv.h"
00005
00006 #include <stdio.h>
00007 using namespace rwil;
00008
00009
00010 static void rwil_callback(const list<Message>& messages);
00011
00012
00014 rwil_handle rwil_init()
00015 {
00016 RWIL *ptr = new RWIL;
00017 rwil_handle handle;
00018 ptr->Start();
00019 ptr->AddDevice(new X10Recv("rwil_x10_listener", ptr->output));
00020 handle = ptr;
00021 ptr->output.SetAddCallback(rwil_callback);
00022 return handle;
00023 }
00024
00026 rwil_device rwil_create(rwil_handle rwilhandle, char house, int num)
00027 {
00028 char buf[20];
00029 int val;
00030 RWIL *rwil = (RWIL*)rwilhandle;
00031 rwil_device device;
00032 val = (house << 16) + num;
00033 sprintf(buf, "%d", val);
00034 X10Device *dev = new X10Device(buf, rwil->output, house, num);
00035 rwil->AddDevice(dev);
00036 device.handle = rwilhandle;
00037 device.dev_handle = val;
00038 return device;
00039 }
00040
00042 void rwil_destroy(rwil_device handle)
00043 {
00044 RWIL *rwil = (RWIL*)handle.handle;
00045 char buf[20];
00046 sprintf(buf, "%d", handle.dev_handle);
00047 rwil->RemoveDevice(buf);
00048 }
00049
00051 void rwil_on(rwil_device handle, time_t when)
00052 {
00053 rwil_setbright(handle, 100, when);
00054 }
00056 void rwil_off(rwil_device handle, time_t when)
00057 {
00058 rwil_setbright(handle, 0, when);
00059 }
00060
00062 void rwil_setbright(rwil_device handle, int brightness, time_t when)
00063 {
00064 RWIL *rwil = (RWIL*)handle.handle;
00065 char buf[20];
00066 sprintf(buf, "%d", handle.dev_handle);
00067
00068 Message msg(buf, "dim", when);
00069 msg.SetParam("percent", IntToString(brightness));
00070 msg.SetParam("rwilhandle", IntToString((int)rwil));
00071 rwil->input.Add(msg);
00072 }
00073
00075 void rwil_interpolatebright(rwil_device handle, int startbrightness, int endbrightness, time_t startwhen, time_t endwhen, int steps)
00076 {
00077 RWIL *rwil = (RWIL*)handle.handle;
00078 char buf[20];
00079
00080 time_t timedif = endwhen - startwhen;
00081 int brightdif = endbrightness - startbrightness;
00082
00083 sprintf(buf, "%d", handle.dev_handle);
00084
00085 int count = 0;
00086 for(count = 0; count < steps; count++)
00087 {
00088 Message msg(buf, "dim", startwhen + timedif * count / steps);
00089 msg.SetParam("percent", IntToString(startbrightness + brightdif * count / steps));
00090
00091
00092 if((count + 1)== steps)msg.SetParam("rwilhandle", IntToString((int)rwil));
00093 rwil->input.Add(msg);
00094 }
00095 }
00096
00098 int rwil_getbright(rwil_device handle)
00099 {
00100 RWIL *rwil = (RWIL*)handle.handle;
00101 char buf[20];
00102 sprintf(buf, "%d", handle.dev_handle);
00103
00104 X10Device* dev = (X10Device *)rwil->GetDevice(buf);
00105 return StringToInt(dev->GetStatus());
00106 }
00107
00109 int rwil_ison(rwil_device handle)
00110 {
00111 if(rwil_getbright(handle)) return 1;
00112 return 0;
00113 }
00114 static rwil_success_callback_t success_call = NULL;
00115 static rwil_failure_callback_t failure_call = NULL;
00116 static rwil_x10_listener_t listener_call = NULL;
00117
00118
00120 void rwil_success_callback(rwil_success_callback_t callback)
00121 {
00122 success_call = callback;
00123 }
00124
00126 void rwil_failure_callback(rwil_failure_callback_t callback)
00127 {
00128 failure_call = callback;
00129 }
00130
00131 void rwil_set_x10_listener(rwil_x10_listener_t callback)
00132 {
00133 listener_call = callback;
00134 }
00135
00137 void rwil_shutdown(rwil_handle rwilhandle)
00138 {
00139 RWIL *rwil = (RWIL*)rwilhandle;
00140 rwil->Kill();
00141 rwil->Join();
00142 delete rwil;
00143 }
00144
00145 static void rwil_callback(const list<Message>& messages)
00146 {
00147 list<Message>::const_iterator iter;
00148
00149 iter = messages.begin();
00150 rwil_device dev;
00151 while(iter != messages.end())
00152 {
00153 std::string str = iter->GetParam("rwilhandle");
00154 if(str=="")
00155 {
00156 if(iter->GetDevice().compare("rwil_x10_listener") == 0)
00157 {
00158 if(listener_call != 0)
00159 {
00160 listener_call(iter->GetParam("house")[0], StringToInt(iter->GetParam("number")), iter->GetCommand().c_str());
00161 }
00162 }
00163 iter++;
00164 continue;
00165 }
00166 dev.handle = (void *)StringToInt(str);
00167 dev.dev_handle = StringToInt(iter->GetDevice());
00168 str = iter->GetParam("message");
00169 if(str == "success")
00170 {
00171 if(success_call)
00172 {
00173 success_call(dev);
00174 }
00175 }
00176 else
00177 {
00178 if(failure_call)
00179 {
00180 failure_call(dev, str.c_str());
00181 }
00182 }
00183 iter++;
00184 }
00185 }