ZWCSTR get_vendor_string(ZWay zway, int dev) {
    ZWCSTR str_val;
    zdata_acquire_lock(ZDataRoot(zway));
    zdata_get_string(zway_find_device_data(zway, dev, "vendorString"), &str_val);
    zdata_release_lock(ZDataRoot(zway));
    return str_val;
}
variant_t*   command_class_eval_basic(const char* method, device_record_t* record, va_list args)
{
    variant_t* ret_val = NULL;

    if(strcmp(method, "Get") == 0)
    {
        zway_cc_basic_get(zway, record->nodeId, record->instanceId, NULL, NULL, NULL);
        zdata_acquire_lock(ZDataRoot(zway));
        ZDataHolder dh = zway_find_device_instance_data(zway, record->nodeId, record->instanceId, "level");
        int int_val;
        zdata_get_integer(dh, &int_val);
        ret_val = variant_create_int32(DT_INT32, int_val);
        zdata_release_lock(ZDataRoot(zway));
    }
    else if(strcmp(method, "Set") == 0)
    {
        variant_t* arg1 = va_arg(args, variant_t*);
        zway_cc_basic_set(zway, record->nodeId, record->instanceId, variant_get_byte(arg1), NULL, NULL, NULL);
    }
variant_t*  command_class_read_data(device_record_t* record, const char* path)
{
    variant_t* ret_val = NULL;
    zdata_acquire_lock(ZDataRoot(zway));
    ZDataHolder dh = zway_find_device_instance_cc_data(zway, record->nodeId, record->instanceId, record->commandId, path);

    ZWDataType type;
    zdata_get_type(dh, &type);

    switch(type)
    {
    case Integer:
        {
            int int_val;
            zdata_get_integer(dh, &int_val);
            ret_val = variant_create_int32(DT_INT32, int_val);
        }
        break;
    case String:
        {
            const char* string_val;
            zdata_get_string(dh, &string_val);
            ret_val = variant_create_string(strdup(string_val));
        }
        break;
    case Boolean:
        {
            ZWBOOL bool_val;
            zdata_get_boolean(dh, &bool_val);
            ret_val = variant_create_bool((bool)bool_val);
        }
        break;
    default:
        break;

    }
    zdata_release_lock(ZDataRoot(zway));
    return ret_val;
}
    void appendHandlers() {
        char cmd, cc_cmd, holder_root;
        ZWBYTE dev, inst, cc, cc_val;
        char data_path[256];
        char cmd_buffer[256];

        ZWBOOL was_idle = FALSE;

        ZWBOOL basic_level_attached = FALSE;

        int running = TRUE;
        if (!zway_is_running(zway)) {
            running = FALSE;
            break;
        }

        while (true) {
            if (!zway_is_idle(zway)) {
                sleep_ms(10);
                break;
            }
        }

        ZDataHolder basic_level_holder_power, basic_level_holder_meter;
        ZWDevicesList list = zway_devices_list(zway);
        if (list != NULL) {
            int i = 0;
            while (list[i]) {
                zdata_acquire_lock(ZDataRoot(zway));
                if (list[i] > 1) {
                    zway_log(zway, Debug, ZSTR("Search data holder for device %i"), list[i]);
                    basic_level_holder_meter = zway_find_device_instance_cc_data(zway, list[i], 0, 0x31, "4.val");
                    if (basic_level_holder_meter) {
                        basic_level_attached = (zdata_add_callback(basic_level_holder_meter,
                                                                   (ZDataChangeCallback) handle_watt_value, FALSE,
                                                                   NULL) == NoError);
                        if (basic_level_attached) {
                            zway_log(zway, Critical,
                                     ZSTR("Basic.data.level holder handler attached to device %i instance 0"),
                                     list[i]);
                        } else {
                            zway_log(zway, Critical, ZSTR("Could not attache handler to device %i"), list[i]);
                        }
                    }
                    basic_level_holder_power = zway_find_device_instance_cc_data(zway, list[i], 0, 0x32, "0.val");
                    if (basic_level_holder_power) {
                        basic_level_attached = (zdata_add_callback(basic_level_holder_power,
                                                                   (ZDataChangeCallback) handle_power_value, FALSE,
                                                                   NULL) == NoError);
                        if (basic_level_attached) {
                            zway_log(zway, Critical,
                                     ZSTR("Basic.data.level holder handler attached to device %i instance 0"),
                                     list[i]);
                        } else {
                            zway_log(zway, Critical, ZSTR("Could not attache handler to device %i"), list[i]);
                        }
                    }
                    ZDataHolder dataHolder = zway_find_device_instance_cc_data(zway, list[i], 0, 0x25, "level");
                    if (dataHolder) {
                        zdata_add_callback(dataHolder, (ZDataChangeCallback) dataChangeCallback, TRUE, NULL);
                    }
                }
                zdata_release_lock(ZDataRoot(zway));
                i++;
            }
            basic_level_attached = TRUE;
        }
    }
Example #5
0
int do_work(ZWay zway) {
	print_help();

	char cmd, cc_cmd, holder_root;
	ZWBYTE dev, inst, cc, cc_val, nconv;
	char data_path[256];
	char cmd_buffer[256];

	ZWBOOL was_idle = FALSE;

	ZWBOOL basic_level_attached = FALSE;

	int skip = 0;
	int running = TRUE;

	int level = 0;

	/*    while(1){
	 digitalWrite(LED1,1);
	 delay(500);
	 digitalWrite(LED1,0);
	 delay(500);
	 }*/
	while (running) {
		/*    	printf("\n\n\n");
		 zdata_acquire_lock(ZDataRoot(zway));
		 level = get_data(zway, zway_find_device_instance_cc_data(zway, '2', '0', 32, 'level'));
		 zdata_release_lock(ZDataRoot(zway));*/

		/*
		 zdata_acquire_lock(ZDataRoot(zway));
		 level = get_data(zway, zway_find_device_data(zway, '1', 'level'));
		 zdata_release_lock(ZDataRoot(zway));
		 */

		printf("data :::::::    %d", int_val2);

		//zdata_add_callback(zway_find_device_instance_cc_data(zway,1,0,32,"mylevel"),[ZDataChangeCallback] callback, FALSE,[void*]arg);

		printf("\nLOG,while1\n");

		if (!zway_is_running(zway)) //this is almost can not be execute
				{
			running = FALSE;
			break;
		}

		printf("\nLOG,while2\n");
		if (!zway_is_idle(zway)) //maybe wait for
				{
			printf("\nLOG, sleep\n");
			sleep_ms(10);
			continue;
		}
		skip = 0;

		/*      printf("\nLOG,while3\n");
		 if (!basic_level_attached)
		 {
		 printf("\nLOG,basic level start\n");

		 ZDataHolder basic_level_holder;
		 zdata_acquire_lock(ZDataRoot(zway));

		 basic_level_holder = zway_find_device_instance_cc_data(zway, 8, 0, 0x20, "mylevel");
		 //   basic_level_holder = zway_find_device_instance_cc_data(zway, 8, 0, 0x26, "level");
		 if (basic_level_holder)
		 {
		 printf("\nLOG,basic level holder\n");

		 basic_level_attached = (zdata_add_callback(basic_level_holder, (ZDataChangeCallback) print_basic_holder, FALSE, NULL) == NoError);
		 if (basic_level_attached){

		 zway_log(zway, Debug, ZSTR("Basic.data.mylevel holder handler attached to device 8 instance 0"));
		 printf("\nLOG,basic level attatched\n");
		 }

		 }
		 zdata_release_lock(ZDataRoot(zway));

		 }*/

		printf("\nLOG,while4\n");
		if (!basic_level_attached) {
			printf("\nLOG,basic level start\n");

			ZDataHolder basic_level_holder;
			zdata_acquire_lock(ZDataRoot(zway));

			//   basic_level_holder = zway_find_device_instance_cc_data(zway, 8, 0, 0x20, "mylevel");
			basic_level_holder = zway_find_device_instance_cc_data(zway, 2, 0,
					32, "level");
			if (basic_level_holder) {
				printf("\nLOG,basic level holder\n");

				basic_level_attached = (zdata_add_callback(basic_level_holder,
						(ZDataChangeCallback) print_basic_holder, FALSE, NULL)
						== NoError);
				if (basic_level_attached) {

					zway_log(zway, Debug,
							ZSTR(
									"Basic.data.mylevel holder handler attached to device 8 instance 0"));
					printf("\nLOG,basic level attatched\n");

					pthread_create(&pthread, NULL, observer_function, NULL);

					pthread_detach(pthread);
				}
			}
			zdata_release_lock(ZDataRoot(zway));

		}

		printf("> ");
		fgets(cmd_buffer, 255, stdin);
		was_idle = FALSE;

		nconv = sscanf(cmd_buffer, "%c %*s", &cmd);
		printf("nconv : %hhd", &nconv);
		if (nconv > 0) {
			switch (cmd) {
			case 'h':
				print_help();
				break;

			case 'd':
				nconv = sscanf(cmd_buffer, "%c %c", &cmd, &holder_root);
				if (nconv > 1) {
					switch (holder_root) {
					case 'r':
						nconv = sscanf(cmd_buffer, "%c %c %s", &cmd,
								&holder_root, data_path);
						if (nconv >= 2) {
							if (nconv == 2) {
								data_path[0] = '.';
								data_path[1] = '\0';
							}
							zdata_acquire_lock(ZDataRoot(zway));
							dump_data(zway,
									zway_find_controller_data(zway, data_path));
							zdata_release_lock(ZDataRoot(zway));
						}
						break;

					case 'd':
						nconv = sscanf(cmd_buffer, "%c %c %hhd %s", &cmd,
								&holder_root, &dev, data_path);
						if (nconv >= 3) {
							if (nconv == 3) {
								data_path[0] = '.';
								data_path[1] = '\0';
							}
							zdata_acquire_lock(ZDataRoot(zway));
							dump_data(zway,
									zway_find_device_data(zway, dev,
											data_path));
							zdata_release_lock(ZDataRoot(zway));
						}
						break;
					case 'i':
						nconv = sscanf(cmd_buffer, "%c %c %hhd %hhd %s", &cmd,
								&holder_root, &dev, &inst, data_path);
						if (nconv >= 4) {
							if (nconv == 4) {
								data_path[0] = '.';
								data_path[1] = '\0';
							}
							zdata_acquire_lock(ZDataRoot(zway));
							dump_data(zway,
									zway_find_device_instance_data(zway, dev,
											inst, data_path));
							zdata_release_lock(ZDataRoot(zway));
						}
						break;
					case 'c':
						nconv = sscanf(cmd_buffer, "%c %c %hhd %hhd %hhd %s",
								&cmd, &holder_root, &dev, &inst, &cc,
								data_path);
						if (nconv >= 5) {
							if (nconv == 5) {
								data_path[0] = '.';
								data_path[1] = '\0';
							}
							zdata_acquire_lock(ZDataRoot(zway));
							dump_data(zway,
									zway_find_device_instance_cc_data(zway, dev,
											inst, cc, data_path));
							zdata_release_lock(ZDataRoot(zway));
						}
						break;
					}
				}
				break;

			case 's':
				nconv = sscanf(cmd_buffer, "%c %hhd %hhd %hhd %c %hhd", &cmd,
						&dev, &inst, &cc, &cc_cmd, &cc_val);

				printf("nconv : %hhd \n", &nconv);

				/*

				 if (nconv == 6 && cmd == 's' && cc == 0x20 && cc_cmd == 's')
				 {
				 printf("nconv2 : %hhd \n",&nconv);
				 printf("if gogo111111111111111111111111111111111111111111111111111111111111111111\n");
				 zdata_acquire_lock(ZDataRoot(zway));
				 zway_cc_switch_multilevel_set(zway, dev, inst, cc_val, 0x01 ,NULL ,NULL,0);
				 zdata_release_lock(ZDataRoot(zway));
				 }
				 else if (nconv == 5 && cmd == 's' && cc == 0x20 && cc_cmd == 'g')
				 {
				 printf("elseif gogo\n");
				 zdata_acquire_lock(ZDataRoot(zway));
				 zway_cc_switch_multilevel_get(zway, dev, inst,NULL,NULL,0);
				 zdata_release_lock(ZDataRoot(zway));
				 }

				 */

				if (nconv == 6 && cmd == 's' && cc == 0x20 && cc_cmd == 's') {
					printf("nconv2 : %hhd \n", &nconv);
					printf(
							"if gogo111111111111111111111111111111111111111111111111111111111111111111\n");
					zdata_acquire_lock(ZDataRoot(zway));
					zway_cc_basic_set(zway, dev, inst, cc_val, NULL, NULL,
							NULL);
					zdata_release_lock(ZDataRoot(zway));

				} else if (nconv == 5 && cmd == 's' && cc == 0x20
						&& cc_cmd == 'g') {
					printf("elseif gogo\n");
					zdata_acquire_lock(ZDataRoot(zway));
					zway_cc_basic_get(zway, dev, inst, NULL, NULL, NULL);
					zdata_release_lock(ZDataRoot(zway));
				} else if (nconv == 6 && cmd == 's' && cc == 0x26
						&& cc_cmd == 's') {
					printf("nconv2 : %hhd \n", &nconv);
					printf(
							"if gogo333333333333333333333333333333333333333333333333333333333\n");
					zdata_acquire_lock(ZDataRoot(zway));
					zway_cc_switch_multilevel_set(zway, dev, inst, cc_val, 0x01,
							NULL, NULL, 0);
					zdata_release_lock(ZDataRoot(zway));
				}

				break;

			case 'n':
				nconv = sscanf(cmd_buffer, "%c %hhd", &cmd, &dev);

				if (nconv == 2)
					zway_fc_request_node_information(zway, dev, NULL, NULL,
							NULL);
				break;

			case 'm':
				test_memory(zway);
				break;

			case 'a':
				zway_fc_add_node_to_network(zway, TRUE, TRUE, NULL, NULL, NULL);
				break;
			case 'A':
				zway_fc_add_node_to_network(zway, FALSE, TRUE, NULL, NULL,
						NULL);
				break;

			case 'e':
				zway_fc_remove_node_from_network(zway, TRUE, TRUE, NULL, NULL,
						NULL);
				break;

			case 'x':
				running = FALSE;
				break;

			case 'S':
				test_save(zway);
				break;

			case 'R':
				test_restore(zway);
				break;
			case 'q':
				//	digitalWrite(LED1, 1);
				break;
			case 'w':
				//	digitalWrite(LED1, 0);
				break;

			case 'l':
				nconv = sscanf(cmd_buffer, "%c %hhd %hhd", &cmd, &dev, &inst);
				switch (nconv) {
				case 1: {
					ZWDevicesList list = zway_devices_list(zway);
					if (list != NULL) {
						int i = 0;
						printf("Devices list: ");
						while (list[i]) {
							printf("%i ", list[i]);
							i++;
						}
						zway_devices_list_free(list);
						printf("\n");
					} else
						printf("Error happened requesting devices list\n");
				}
					break;

				case 2: {
					ZWInstancesList list = zway_instances_list(zway, dev);
					if (list != NULL) {
						int i = 0;
						printf("Instances list for device %i: ", dev);
						while (list[i]) {
							printf("%i ", list[i]);
							i++;
						}
						zway_instances_list_free(list);
						printf("\n");
					} else
						printf("Error happened requesting instances list\n");
				}
					break;

				case 3: {
					ZWCommandClassesList list = zway_command_classes_list(zway,
							dev, inst);
					if (list != NULL) {
						int i = 0;
						printf(
								"Command Classes list for device %i instance %i: ",
								dev, inst);
						while (list[i]) {
							printf("%02x ", list[i]);
							i++;
						}
						zway_command_classes_list_free(list);
						printf("\n");
					} else
						printf(
								"Error happened requesting command classes list\n");
				}
					break;
				}
				break;
			}
		}
	}

	return 0;
}