Exemplo n.º 1
0
        /// <summary>
        /// Register a callback device change event function to be called when a device change event (e.g. change of name, device added) occurs
        /// </summary>
        /// <param name="deviceEventFunc">Callback function to be called</param>
        /// <param name="obj">Context object that will be echoed back when function is called. Only the object when the first function is registered will be used. Set to null if not used.</param>
        /// <returns>Callback event id</returns>
        public unsafe int tdRegisterDeviceChangeEvent(DeviceChangeEventCallbackFunction deviceChangeEventFunc, Object obj)
        {
            int returnValue = 0;

            if (deviceChangeEventList.Count == 0)
            {
                //first added, register with dll too
                //only the context object of the first event will be registered
                UnmanagedImport.DeviceChangeEventFunctionDelegate deviceChangeEventFunctionDelegate = new UnmanagedImport.DeviceChangeEventFunctionDelegate(deviceChangeEventFunction);

                registeredDeviceChangeEventFunctionId = UnmanagedImport.tdRegisterDeviceChangeEvent(deviceChangeEventFunctionDelegate, (void *)null);
                GC.Collect();
                callbackFunctionReferenceList.Add(registeredDeviceChangeEventFunctionId, deviceChangeEventFunctionDelegate);
            }
            ++lastEventID;
            returnValue = lastEventID;
            DeviceChangeEventFunctionContext deviceChangeEventFuncContext = new DeviceChangeEventFunctionContext();

            deviceChangeEventFuncContext.changeEventCallbackFunc = deviceChangeEventFunc;
            deviceChangeEventFuncContext.context    = obj;
            deviceChangeEventFuncContext.callbackId = returnValue;
            deviceChangeEventList.Add(returnValue, deviceChangeEventFuncContext);

            return(returnValue);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Unregister a callback function. All functions will be unregistered automatically when this objekt is deleted.
        /// </summary>
        /// <param name="eventId">Id of callback even to unregister</param>
        public void unregisterCallback(int eventId)
        {
            deviceEventList.Remove(eventId);
            if (deviceEventList.Count == 0)
            {
                //no more events in list
                UnmanagedImport.tdUnregisterCallback(registeredEventFunctionId);
                callbackFunctionReferenceList.Remove(registeredEventFunctionId);

                /*
                 * if (eventContextHandle.IsAllocated)
                 * {
                 *      eventContextHandle.Free();
                 * }
                 */
            }

            deviceChangeEventList.Remove(eventId);
            if (deviceChangeEventList.Count == 0)
            {
                //no more events in list
                UnmanagedImport.tdUnregisterCallback(registeredDeviceChangeEventFunctionId);
                callbackFunctionReferenceList.Remove(registeredDeviceChangeEventFunctionId);
            }

            rawListenerList.Remove(eventId);
            if (rawListenerList.Count == 0)
            {
                //no more events in list
                UnmanagedImport.tdUnregisterCallback(registeredRawListenerFunctionId);
                callbackFunctionReferenceList.Remove(registeredRawListenerFunctionId);
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// Send a raw command to TellStick. Please read the TellStick protocol definition on how the command should be constructed.
        /// </summary>
        /// <param name="command">The command for TellStick in its native format</param>
        /// <param name="reserved"></param>
        /// <returns>Success or error code</returns>
        public static unsafe int tdSendRawCommand(string command, int reserved)
        {
            char *commandChar = stringToChar(command);
            int   returnValue = UnmanagedImport.tdSendRawCommand(commandChar, reserved);

            Marshal.FreeHGlobal((IntPtr)commandChar);
            return(returnValue);
        }
Exemplo n.º 4
0
        /// <summary>
        ///  Sets a new model for a device. Which model to set depends on the current protocol.
        /// </summary>
        /// <param name="deviceId">Id of device to change</param>
        /// <param name="model">The new model</param>
        /// <returns>Success or error code</returns>
        public static unsafe bool tdSetModel(int deviceId, string model)
        {
            char *modelChar   = stringToChar(model);
            bool  returnValue = UnmanagedImport.tdSetModel(deviceId, modelChar);

            Marshal.FreeHGlobal((IntPtr)modelChar);
            return(returnValue);
        }
Exemplo n.º 5
0
        /// <summary>
        /// Sets a new name for a device.
        /// </summary>
        /// <param name="deviceId">Id of device to change</param>
        /// <param name="name">The new name</param>
        /// <returns>Success or error code</returns>
        public static unsafe bool tdSetName(int deviceId, string name)
        {
            char *nameChar    = stringToChar(name);
            bool  returnValue = UnmanagedImport.tdSetName(deviceId, nameChar);

            Marshal.FreeHGlobal((IntPtr)nameChar);
            return(returnValue);
        }
Exemplo n.º 6
0
        /// <summary>
        /// This changes the current protocol used by a device. After changing the protocol, setting new parameters is required.
        /// </summary>
        /// <param name="deviceId">Id of device to change</param>
        /// <param name="protocol">The new protocol to use</param>
        /// <returns>Success or error code</returns>
        public static unsafe bool tdSetProtocol(int deviceId, string protocol)
        {
            char *protocolChar = stringToChar(protocol);
            bool  returnValue  = UnmanagedImport.tdSetProtocol(deviceId, protocolChar);

            Marshal.FreeHGlobal((IntPtr)protocolChar);
            return(returnValue);
        }
Exemplo n.º 7
0
        /// <summary>
        /// Sets a new protocol specific parameter. Please see the documentation of the protocols before setting any parameter.
        /// </summary>
        /// <param name="deviceId">Id of device to change</param>
        /// <param name="name">The name of the parameter to change</param>
        /// <param name="value">The new value for the parameter</param>
        /// <returns>Success or error code</returns>
        public static unsafe bool tdSetDeviceParameter(int deviceId, string name, string value)
        {
            char *nameChar    = stringToChar(name);
            char *valueChar   = stringToChar(value);
            bool  returnValue = UnmanagedImport.tdSetDeviceParameter(deviceId, nameChar, valueChar);

            Marshal.FreeHGlobal((IntPtr)nameChar);
            Marshal.FreeHGlobal((IntPtr)valueChar);
            return(returnValue);
        }
Exemplo n.º 8
0
        /// <summary>
        /// Convert char* to correctly encoded string, clean up of received char* is optional
        /// </summary>
        /// <param name="input">Char* to convert</param>
        /// <param name="release">Clean up char* or not (don't do that if it is still should be used somewhere)</param>
        /// <returns>Converted string</returns>
        private static unsafe string getString(char *input, bool release)
        {
            string returnString = System.Text.Encoding.UTF8.GetString(System.Text.Encoding.Unicode.GetBytes(new string(input)));

            if (returnString.Contains("\0"))
            {
                returnString = returnString.Substring(0, returnString.IndexOf('\0'));
            }

            if (release)
            {
                UnmanagedImport.tdReleaseString(input);
            }
            GC.Collect();
            return(returnString);
        }
Exemplo n.º 9
0
        /// <summary>
        /// Register a callback listening function to be called when a listening event (e.g. data is received with Tellstick Duo) occurs
        /// </summary>
        /// <param name="listeningFunc">Callback function to be called</param>
        /// <param name="obj">Context object that will be echoed back when function is called. Only the object when the first function is registered will be used. Set to null if not used.</param>
        /// <returns>Callback event id</returns>
        public unsafe int tdRegisterRawDeviceEvent(RawListeningCallbackFunction listeningFunc, Object obj)
        {
            int returnValue = 0;

            if (rawListenerList.Count == 0)
            {
                //first added, register with dll too
                //only the context object of the first event will be registered
                UnmanagedImport.RawListeningDelegate listeningFunctionDelegate = new UnmanagedImport.RawListeningDelegate(rawListeningFunction);

                registeredRawListenerFunctionId = UnmanagedImport.tdRegisterRawDeviceEvent(listeningFunctionDelegate, (void *)null);
                GC.Collect();
                callbackFunctionReferenceList.Add(registeredRawListenerFunctionId, listeningFunctionDelegate);
            }
            ++lastEventID;
            returnValue = lastEventID;
            RawEventFunctionContext rawEventFuncContext = new RawEventFunctionContext();

            rawEventFuncContext.rawCallbackFunc = listeningFunc;
            rawEventFuncContext.context         = obj;
            rawEventFuncContext.callbackId      = returnValue;
            rawListenerList.Add(returnValue, rawEventFuncContext);
            return(returnValue);
        }
Exemplo n.º 10
0
 /// <summary>
 /// Returns the last sent command to a specific device
 /// </summary>
 /// <param name="deviceId">Id of device to query</param>
 /// <param name="methods">The methods supported by the client. See tdMethods() for more information.</param>
 /// <returns>The last sent command as integer, for example TELLSTICK_TURNON or TELLSTICK_TURNOFF</returns>
 public static int tdLastSentCommand(int deviceId, int methods)
 {
     return(UnmanagedImport.tdLastSentCommand(deviceId, methods));
 }
Exemplo n.º 11
0
 /// <summary>
 /// Turns a device off.
 /// Make sure the device supports this by calling tdMethods() before any calls to this function.
 /// </summary>
 /// <param name="deviceId">Id of device to turn off</param>
 /// <returns>Success or error code</returns>
 public static int tdTurnOff(int deviceId)
 {
     return(UnmanagedImport.tdTurnOff(deviceId));
 }
Exemplo n.º 12
0
 /// <summary>
 /// Dims a device.
 /// Make sure the device supports this by calling tdMethods() before any calls to this function.
 /// </summary>
 /// <param name="deviceId">The device id to dim</param>
 /// <param name="level">The level the device should dim to. This value should be 0-255</param>
 /// <returns>Success or error code</returns>
 public static int tdDim(int deviceId, char level)
 {
     return(UnmanagedImport.tdDim(deviceId, level));
 }
Exemplo n.º 13
0
 /// <summary>
 /// Execute a scene action.
 /// Make sure the device supports this by calling tdMethods() before any calls to this function.
 /// </summary>
 /// <param name="deviceId">Id of device to perform the execute action on</param>
 /// <returns>Success or error code</returns>
 public static int tdExecute(int deviceId)
 {
     return(UnmanagedImport.tdExecute(deviceId));
 }
Exemplo n.º 14
0
 /// <summary>
 ///  This function returns the unique id of a device with a specific index.
 ///  To get all the id numbers you should loop over all the devices, use tdGetNumberOfDevices() to get the number of devices
 /// </summary>
 /// <param name="order">Device index</param>
 /// <returns>Id of device at that position</returns>
 public static int tdGetDeviceId(int order)
 {
     return(UnmanagedImport.tdGetDeviceId(order));
 }
Exemplo n.º 15
0
 /// <summary>
 /// Get a protocol specific device parameter
 /// </summary>
 /// <param name="deviceId">The id of the device to query</param>
 /// <param name="name">The name of the parameter to query</param>
 /// <param name="defaultValue">A defaultValue to return if the current parameter hasn't previously been set</param>
 /// <returns>Any protocol specific parameter specified by "name"</returns>
 public static unsafe string tdGetDeviceParameter(int deviceId, string name, string defaultValue)
 {
     return(getString(UnmanagedImport.tdGetDeviceParameter(deviceId, stringToChar(name), stringToChar(defaultValue))));
 }
Exemplo n.º 16
0
 /// <summary>
 /// Returns which type the device is
 /// </summary>
 /// <param name="deviceId">Id of the device</param>
 /// <returns>TELLSTICK_TYPE_DEVICE or TELLSTICK_TYPE_GROUP</returns>
 public static int tdGetDeviceType(int deviceId)
 {
     return(UnmanagedImport.tdGetDeviceType(deviceId));
 }
Exemplo n.º 17
0
 /// <summary>
 /// Removes a device.
 /// </summary>
 /// <param name="deviceId">Id of device to remove</param>
 /// <returns>True on success, false otherwise</returns>
 public static bool tdRemoveDevice(int deviceId)
 {
     return(UnmanagedImport.tdRemoveDevice(deviceId));
 }
Exemplo n.º 18
0
 /// <summary>
 /// Get a human readable string from an error code returned from a function in telldus-core
 /// </summary>
 /// <param name="errorNo">The error code to translate</param>
 /// <returns>A string ready to show to the user.
 /// TELLSTICK_SUCCESS
 /// TELLSTICK_ERROR_NOT_FOUND
 /// TELLSTICK_ERROR_PERMISSION_DENIED
 /// TELLSTICK_ERROR_DEVICE_NOT_FOUND
 /// TELLSTICK_ERROR_METHOD_NOT_SUPPORTED
 /// TELLSTICK_ERROR_COMMUNICATION
 /// TELLSTICK_ERROR_CONNECTING_SERVICE
 /// TELLSTICK_ERROR_UNKNOWN_RESPONSE
 /// TELLSTICK_ERROR_UNKNOWN
 /// </returns>
 public static unsafe string tdGetErrorString(int errorNo)
 {
     return(getString(UnmanagedImport.tdGetErrorString(errorNo)));
 }
Exemplo n.º 19
0
 /// <summary>
 ///   Close the library and clean up the cache it uses.
 ///   This should be called when the library is not supposed to be used anymore.
 ///   Do not use when this has been instantiated, will be closed in destructor then, only on static methods.
 /// </summary>
 public static void tdClose()
 {
     UnmanagedImport.tdClose();
 }
Exemplo n.º 20
0
 /// <summary>
 /// Send "up" command to device.
 /// Make sure the device supports this by calling tdMethods() before any calls to this function.
 /// </summary>
 /// <param name="deviceId">The device id to send the command to</param>
 /// <returns>Success or error code</returns>
 public static int tdUp(int deviceId)
 {
     return(UnmanagedImport.tdUp(deviceId));
 }
Exemplo n.º 21
0
 /// <summary>
 /// Query a device for it’s protocol
 /// </summary>
 /// <param name="deviceId">Id of device to query</param>
 /// <returns>The protocol for a device.</returns>
 public static unsafe string tdGetProtocol(int deviceId)
 {
     return(getString(UnmanagedImport.tdGetProtocol(deviceId)));
 }
Exemplo n.º 22
0
 /// <summary>
 /// This function returns the number of devices configured
 /// </summary>
 /// <returns>The total number of devices configured</returns>
 public static int tdGetNumberOfDevices()
 {
     return(UnmanagedImport.tdGetNumberOfDevices());
 }
Exemplo n.º 23
0
 /// <summary>
 ///  Query a device for which methods it supports. By supplying the methods you support
 ///  the library could remap the methods a device support for better fit the application.
 /// </summary>
 /// <param name="deviceId">Id of device to query</param>
 /// <param name="methodsSupported">Methods that the client application supports</param>
 /// <returns>Supported (both by client application and by device) method-flags OR'ed into an integer
 /// TELLSTICK_TURNON
 /// TELLSTICK_TURNOFF
 /// TELLSTICK_BELL
 /// TELLSTICK_TOGGLE
 /// TELLSTICK_DIM
 /// TELLSTICK_EXECUTE
 /// TELLSTICK_UP
 /// TELLSTICK_DOWN
 /// TELLSTICK_EXECUTE
 /// TELLSTICK_LEARN
 /// </returns>
 public static int tdMethods(int deviceId, int methodsSupported)
 {
     return(UnmanagedImport.tdMethods(deviceId, methodsSupported));
 }
Exemplo n.º 24
0
 /// <summary>
 /// If the last sent command it TELLSTICK_DIM this returns the dimmed value
 /// </summary>
 /// <param name="deviceId">Id of device to query</param>
 /// <returns>The the value as a human readable string, for example "128" for 50%</returns>
 public static unsafe string tdLastSentValue(int deviceId)
 {
     return(getString(UnmanagedImport.tdLastSentValue(deviceId)));
 }
Exemplo n.º 25
0
 /// <summary>
 /// Add a new device to the global database of devices. This function must be called first before
 /// any call to tdSetName(), tdSetProtocol() and similar functions.
 /// </summary>
 /// <returns> The device id for the newly created device. If the creation fails it returnes a
 /// negative value.</returns>
 public static int tdAddDevice()
 {
     return(UnmanagedImport.tdAddDevice());
 }