-Поиск по дневнику

Поиск сообщений в rss_thedaily_wtf

 -Подписка по e-mail

 

 -Постоянные читатели

 -Статистика

Статистика LiveInternet.ru: показано количество хитов и посетителей
Создан: 06.04.2008
Записей:
Комментариев:
Написано: 0


CodeSOD: Once You Eliminate the Impossible…

Вторник, 21 Апреля 2015 г. 14:00 + в цитатник

&Whatever remains, no matter how improbable, must be XML.

William Hogarth - Absurd perspectives.png

Developers have many weaknesses, among them this: they dont like to say that something cant be done. Thats why when Glenn Ms client, TelCo, asked if their request was really impossible, instead of apologizing and vigorously nodding his head, Glenn said, Well, technically&

And thats how he ended up writing this.


    
        
            
                
                    
                        
                            
(?:0x)?([0-9a-fA-F][0-9a-fA-F])\s*([0-9a-fA-F][0-9a-fA-F])\s*([0-9a-fA-F][0-9a-fA-F])\s*([0-9a-fA-F][0-9a-fA-F]) .1.3.6.1.4.1.2011.2.217.1.4.1.1.6 %4$s%3$s%2$s%1$s 8388607 8388608
1
1 (?:0x)?([0-9a-fA-F][0-9a-fA-F])\s*([0-9a-fA-F][0-9a-fA-F])\s*([0-9a-fA-F][0-9a-fA-F])\s*([0-9a-fA-F][0-9a-fA-F]) .1.3.6.1.4.1.2011.2.217.1.4.1.1.6 %4$s%3$s%2$s%1$s 23 255 127
(?:0x)?([0-9a-fA-F][0-9a-fA-F])\s*([0-9a-fA-F][0-9a-fA-F])\s*([0-9a-fA-F][0-9a-fA-F])\s*([0-9a-fA-F][0-9a-fA-F]) .1.3.6.1.4.1.2011.2.217.1.4.1.1.6 %4$s%3$s%2$s%1$s 31 1 1 -1 1

Thats an XML DDF, a Data-Definition File for the systems-monitoring application Glenn supports. See, the system can read data from any device that supports the Modbus/TCP or SNMP protocols, as long as it has a DDF file that defines the data points and how theyre to be displayed. The DDF schema includes the arithmetic, boolean, regex, and conditional operators you see above, to let the system know how the data should be cleaned up.

Logic via XML? Thats a WTF all by itself, sure. But TRWTF is what that snippet above is actually doing. TelCo was monitoring a device that spat out temperature values (simple enough), but it presented them as an eight-character string representing the hexadecimal value of an IEEE754 32-bit floating-point number. The task that Glenn probably should have said was impossible was to cast that bizarre value back into its numeric equivalent. DDF, for all its awesome expressive power, lacked a typecast operator.

Thats why Glenn wrote the DDF shown above, which does this:

  • Since the byte order was flipped, use to put them back in an order we can work with
  • Use to convert from octetString to uint_32
  • Use and to isolate the sign bit, exponent and mantissa
  • Convert the sign bit to +/1
  • Unbias the exponent, then 1 by that many
  • Divide the mantissa by 2^23
  • Multiply the last three items together to get the final result. Since and other operators only accept 2 operands, these need to be nested

Next time, Glenn plans to stick to his guns, because he just heard that TelCo will be modifying their device to present temperature using integers.

[Advertisement] Manage IT infrastructure as code across all environments with Puppet. Puppet Enterprise now offers more control and insight, with role-based access control, activity logging and all-new Puppet Apps. Start your free trial today!

http://thedailywtf.com/articles/once-you-eliminate-the-impossible-

Метки:  

 

Добавить комментарий:
Текст комментария: смайлики

Проверка орфографии: (найти ошибки)

Прикрепить картинку:

 Переводить URL в ссылку
 Подписаться на комментарии
 Подписать картинку